FMUTIL  0.1
Fortran Miscellaneous UTILities
search.f90
Go to the documentation of this file.
1 !##############################################################################
2 ! ________ _____ ______________
3 ! / ____/ |/ / / / /_ __/ _/ /
4 ! / /_ / /|_/ / / / / / / / // /
5 ! / __/ / / / / /_/ / / / _/ // /___
6 ! /_/ /_/ /_/\____/ /_/ /___/_____/
7 !
8 ! Copyright 2020 Bharat Mahajan
9 !
10 ! Licensed under the Apache License, Version 2.0 (the "License");
11 ! you may not use this file except in compliance with the License.
12 ! You may obtain a copy of the License at
13 !
14 ! http://www.apache.org/licenses/LICENSE-2.0
15 !
16 ! Unless required by applicable law or agreed to in writing, software
17 ! distributed under the License is distributed on an "AS IS" BASIS,
18 ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 ! See the License for the specific language governing permissions and
20 ! limitations under the License.
21 !
26 !
27 !##############################################################################
28 
29 
31 module search
32 
33  use fmutilbase
34 
35  implicit none
36 
38  integer, parameter :: max_iterations = 50
39 
41  abstract interface
42  pure function func(x) result(y)
43  import :: wp
44  implicit none
45  real(wp), intent(in) :: x
46  real(wp) :: y
47  end function func
48  end interface
49 
50  contains
51 
52 
55  pure function binarysearch(SortedX, SortedDataArray)
56 
57  !import :: WP
58 
59  implicit none
60 
61  real(wp), dimension(1:), intent(in) :: sortedx
62  real(wp), dimension(:), intent(in) :: sorteddataarray
63  integer, dimension(size(SortedX)) :: binarysearch
64 
65  integer :: lb, ub, a, b, m, n, ctr, xloc
66  real(wp) :: x
67 
68  ! initialize
69  lb = 1
70  ub = size(sorteddataarray)
71  m = lb
72 
73  ! for each element
74  do ctr = 1, size(sortedx)
75 
76  ! element to search for
77  x = sortedx(ctr)
78 
79  ! current interval. Note we assume that the array of the elements
80  ! to search for is also a sorted array.
81  a = m
82  b = ub
83 
84  ! loop for finding the element
85  do
86  ! size of the current interval
87  n = b - a + 1
88 
89  ! mid-point
90  m = floor((n+1)/2.0) + (a-1)
91 
92  if (x < sorteddataarray(m)) then
93  ! search the lower half
94  b = m
95  cycle
96  elseif (x > sorteddataarray(m) .AND. (m+1) /= b) then
97  ! search the upper half
98  a = m
99  cycle
100  else
101  ! We either have found the exact match
102  ! Or x lies in between Data(m) and Data(m+1)
103  xloc = m
104  exit
105  end if
106  end do
107 
108  ! save the location of the current element
109  binarysearch(ctr) = xloc
110  end do
111 
112  end function binarysearch
113 
114 
115 
116 end module search
fmutilbase::wp
integer, parameter, public wp
Definition: fmutil_base.F90:41
search::max_iterations
integer, parameter max_iterations
Max iterations for the root-finding.
Definition: search.f90:38
search
Search Module.
Definition: search.f90:31
search::binarysearch
pure integer function, dimension(size(sortedx)) binarysearch(SortedX, SortedDataArray)
Performs binary search on a provided sorted array.
Definition: search.f90:56
fmutilbase
FMUTIL Base Module.
Definition: fmutil_base.F90:31
search::func
Interface of the function whose root needs to be computed.
Definition: search.f90:42