Class Searches
java.lang.Object
org.apache.commons.statistics.inference.Searches
Search utility methods.
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Range threshold to use a binary search. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static int
searchAscending
(int a, int b, double x, IntToDoubleFunction value) Conduct a search betweena
inclusive andb
inclusive to find the highest index wherevalue <= x
.(package private) static int
searchDescending
(int a, int b, double x, IntToDoubleFunction value) Conduct a search betweena
inclusive andb
inclusive to find the lowest index wherevalue <= x
.
-
Field Details
-
BINARY_SEARCH
private static final int BINARY_SEARCHRange threshold to use a binary search. The binary search takes O(log(n)) so is used when n is large and a sequential search is slower.- See Also:
-
-
Constructor Details
-
Searches
private Searches()No instances.
-
-
Method Details
-
searchDescending
Conduct a search betweena
inclusive andb
inclusive to find the lowest index wherevalue <= x
. The values must be in descending order. The method is functionally equivalent to:i = b + 1 while (i > a AND value(i - 1) <= x) i = i - 1 return i
The function is only evaluated between the closed interval
[a, b]
. Special cases:- If
value(a) <= x
the returned index isa
. - If
value(b) > x
the returned index isb + 1
.
- Parameters:
a
- Lower limit (inclusive).b
- Upper limit (inclusive).x
- Target value.value
- Function to evaluate the value at an index.- Returns:
- the minimum index where
value(i) <= x
.
- If
-
searchAscending
Conduct a search betweena
inclusive andb
inclusive to find the highest index wherevalue <= x
. The values must be in ascending order. The method is functionally equivalent to:i = a - 1 while (i < b AND value(i + 1) <= x) i = i + 1 return i
The function is only evaluated between the closed interval
[a, b]
. Special cases:- If
value(a) > x
the returned index isa - 1
. - If
value(b) <= x
the returned index isb
.
- Parameters:
a
- Lower limit (inclusive).b
- Upper limit (inclusive).x
- Target value.value
- Function to evaluate the value at an index.- Returns:
- the maximum index where
value(i) <= x
.
- If
-