r8vec_bracket Subroutine

subroutine r8vec_bracket(n, x, xval, left, right)

************80

! R8VEC_BRACKET searches a sorted R8VEC for successive brackets of a value.

Discussion:

An R8VEC is an array of double precision real values.

If the values in the vector are thought of as defining intervals
on the real line, then this routine searches for the interval
nearest to or containing the given value.

Licensing:

This code is distributed under the GNU LGPL license.

Modified:

06 April 1999

Author:

John Burkardt

Parameters:

Input, integer ( kind = 4 ) N, length of input array.

Input, real ( kind = 8 ) X(N), an array sorted into ascending order.

Input, real ( kind = 8 ) XVAL, a value to be bracketed.

Output, integer ( kind = 4 ) LEFT, RIGHT, the results of the search.
Either:
  XVAL < X(1), when LEFT = 1, RIGHT = 2;
  X(N) < XVAL, when LEFT = N-1, RIGHT = N;
or
  X(LEFT) <= XVAL <= X(RIGHT).

Arguments

Type IntentOptional Attributes Name
integer(kind=4) :: n
real(kind=8) :: x(n)
real(kind=8) :: xval
integer(kind=4) :: left
integer(kind=4) :: right

Source Code

subroutine r8vec_bracket ( n, x, xval, left, right )

  !*****************************************************************************80
  !
  !! R8VEC_BRACKET searches a sorted R8VEC for successive brackets of a value.
  !
  !  Discussion:
  !
  !    An R8VEC is an array of double precision real values.
  !
  !    If the values in the vector are thought of as defining intervals
  !    on the real line, then this routine searches for the interval
  !    nearest to or containing the given value.
  !
  !  Licensing:
  !
  !    This code is distributed under the GNU LGPL license. 
  !
  !  Modified:
  !
  !    06 April 1999
  !
  !  Author:
  !
  !    John Burkardt
  !
  !  Parameters:
  !
  !    Input, integer ( kind = 4 ) N, length of input array.
  !
  !    Input, real ( kind = 8 ) X(N), an array sorted into ascending order.
  !
  !    Input, real ( kind = 8 ) XVAL, a value to be bracketed.
  !
  !    Output, integer ( kind = 4 ) LEFT, RIGHT, the results of the search.
  !    Either:
  !      XVAL < X(1), when LEFT = 1, RIGHT = 2;
  !      X(N) < XVAL, when LEFT = N-1, RIGHT = N;
  !    or
  !      X(LEFT) <= XVAL <= X(RIGHT).
  !
  implicit none

  integer ( kind = 4 ) n

  integer ( kind = 4 ) i
  integer ( kind = 4 ) left
  integer ( kind = 4 ) right
  real    ( kind = 8 ) x(n)
  real    ( kind = 8 ) xval

  do i = 2, n - 1

     if ( xval < x(i) ) then
        left = i - 1
        right = i
        return
     end if

  end do

  left = n - 1
  right = n

  return
end subroutine r8vec_bracket