Class InternalUtils
java.lang.Object
org.apache.commons.rng.sampling.distribution.InternalUtils
Functions used by some of the samplers.
This class is not part of the public API, as it would be
better to group these utilities in a dedicated component.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classClass for computing the natural logarithm of the factorial ofn. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe first array index with a non-zero log factorial.private static final doubleThe multiplier to convert the least significant 53-bits of alongto adouble.private static final double[]All long-representable factorials, precomputed as the natural logarithm using Matlab R2023a VPA: log(vpa(x)). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static doublelogFactorial(int n) (package private) static doublemakeDouble(long v) Creates adoublein the interval[0, 1)from alongvalue.(package private) static doublemakeNonZeroDouble(long v) Creates adoublein the interval(0, 1]from alongvalue.(package private) static NormalizedGaussianSamplerCreate a new instance of the given sampler usingSharedStateSampler.withUniformRandomProvider(UniformRandomProvider).(package private) static doublerequireFinite(double x, String name) Checks the valuexis finite.(package private) static doublerequirePositive(double x, String name) Checks the valuex >= 0.(package private) static doublerequirePositiveFinite(double x, String name) Checks the valuex >= 0and is finite.(package private) static doublerequireRange(double min, double max, double x, String name) Checks the value is within the range:min <= x < max.(package private) static doublerequireRangeClosed(double min, double max, double x, String name) Checks the value is within the closed range:min <= x <= max.(package private) static doublerequireStrictlyPositive(double x, String name) Checks the valuex > 0.(package private) static doublerequireStrictlyPositiveFinite(double x, String name) Checks the valuex > 0and is finite.(package private) static doublevalidateProbabilities(double[] probabilities) Validate the probabilities sum to a finite positive number.
-
Field Details
-
LOG_FACTORIALS
private static final double[] LOG_FACTORIALSAll long-representable factorials, precomputed as the natural logarithm using Matlab R2023a VPA: log(vpa(x)).Note: This table could be any length. Previously this stored the long value of n!, not log(n!). Using the previous length maintains behaviour.
-
BEGIN_LOG_FACTORIALS
private static final int BEGIN_LOG_FACTORIALSThe first array index with a non-zero log factorial.- See Also:
-
DOUBLE_MULTIPLIER
private static final double DOUBLE_MULTIPLIERThe multiplier to convert the least significant 53-bits of alongto adouble. Taken from org.apache.commons.rng.core.util.NumberFactory.- See Also:
-
-
Constructor Details
-
InternalUtils
private InternalUtils()Utility class.
-
-
Method Details
-
logFactorial
static double logFactorial(int n) - Parameters:
n- Argument.- Returns:
n!- Throws:
IndexOutOfBoundsException- if the result is too large to be represented by along(i.e. ifn > 20), ornis negative.
-
validateProbabilities
static double validateProbabilities(double[] probabilities) Validate the probabilities sum to a finite positive number.- Parameters:
probabilities- the probabilities- Returns:
- the sum
- Throws:
IllegalArgumentException- ifprobabilitiesis null or empty, a probability is negative, infinite orNaN, or the sum of all probabilities is not strictly positive.
-
requireFinite
Checks the valuexis finite.- Parameters:
x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifxis non-finite
-
requirePositiveFinite
Checks the valuex >= 0and is finite. Note: This method allowsx == -0.0.- Parameters:
x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx < 0or is non-finite
-
requireStrictlyPositiveFinite
Checks the valuex > 0and is finite.- Parameters:
x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx <= 0or is non-finite
-
requirePositive
Checks the valuex >= 0. Note: This method allowsx == -0.0.- Parameters:
x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx < 0
-
requireStrictlyPositive
Checks the valuex > 0.- Parameters:
x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx <= 0
-
requireRange
Checks the value is within the range:min <= x < max.- Parameters:
min- Minimum (inclusive).max- Maximum (exclusive).x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx < min || x >= max.
-
requireRangeClosed
Checks the value is within the closed range:min <= x <= max.- Parameters:
min- Minimum (inclusive).max- Maximum (inclusive).x- Value.name- Name of the value.- Returns:
- x
- Throws:
IllegalArgumentException- ifx < min || x > max.
-
newNormalizedGaussianSampler
static NormalizedGaussianSampler newNormalizedGaussianSampler(NormalizedGaussianSampler sampler, UniformRandomProvider rng) Create a new instance of the given sampler usingSharedStateSampler.withUniformRandomProvider(UniformRandomProvider).- Parameters:
sampler- Source sampler.rng- Generator of uniformly distributed random numbers.- Returns:
- the new sampler
- Throws:
UnsupportedOperationException- if the underlying sampler is not aSharedStateSampleror does not return aNormalizedGaussianSamplerwhen sharing state.
-
makeDouble
static double makeDouble(long v) Creates adoublein the interval[0, 1)from alongvalue.- Parameters:
v- Number.- Returns:
- a
doublevalue in the interval[0, 1).
-
makeNonZeroDouble
static double makeNonZeroDouble(long v) Creates adoublein the interval(0, 1]from alongvalue.- Parameters:
v- Number.- Returns:
- a
doublevalue in the interval(0, 1].
-