Class Searches
- java.lang.Object
-
- org.apache.commons.statistics.inference.Searches
-
final class Searches extends java.lang.Object
Search utility methods.- Since:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BINARY_SEARCH
Range threshold to use a binary search.
-
Constructor Summary
Constructors Modifier Constructor Description private
Searches()
No instances.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static int
searchAscending(int a, int b, double x, java.util.function.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, java.util.function.IntToDoubleFunction value)
Conduct a search betweena
inclusive andb
inclusive to find the lowest index wherevalue <= x
.
-
-
-
Field Detail
-
BINARY_SEARCH
private static final int BINARY_SEARCH
Range 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:
- Constant Field Values
-
-
Method Detail
-
searchDescending
static int searchDescending(int a, int b, double x, java.util.function.IntToDoubleFunction value)
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
static int searchAscending(int a, int b, double x, java.util.function.IntToDoubleFunction value)
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
-
-