Class MathUtils
- java.lang.Object
-
- org.hipparchus.util.MathUtils
-
public final class MathUtils extends Object
Miscellaneous utility functions.- See Also:
ArithmeticUtils,Precision,MathArrays
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMathUtils.FieldSumAndResidual<T extends FieldElement<T>>Result class fortwoSum(FieldElement, FieldElement)containing the sum and the residual error in the sum.static classMathUtils.SumAndResidualResult class fortwoSum(double, double)containing the sum and the residual error in the sum.
-
Field Summary
Fields Modifier and Type Field Description static doublePI_SQUARED\(\pi^2\)static doubleSEMI_PI\(\pi/2\).static doubleTWO_PI\(2\pi\)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheckDimension(int dimension, int otherDimension)Checks that the given dimensions match.static voidcheckFinite(double x)Check that the argument is a real number.static voidcheckFinite(double[] val)Check that all the elements are real numbers.static voidcheckNotNull(Object o)Checks that an object is not null.static voidcheckNotNull(Object o, Localizable pattern, Object... args)Checks that an object is not null.static voidcheckRangeInclusive(double value, double lo, double hi)Checks that the given value is strictly within the range [lo, hi].static voidcheckRangeInclusive(long value, long lo, long hi)Checks that the given value is strictly within the range [lo, hi].static bytecopySign(byte magnitude, byte sign)Returns the first argument with the sign of the second argument.static intcopySign(int magnitude, int sign)Returns the first argument with the sign of the second argument.static longcopySign(long magnitude, long sign)Returns the first argument with the sign of the second argument.static shortcopySign(short magnitude, short sign)Returns the first argument with the sign of the second argument.static booleanequals(double x, double y)Returnstrueif the values are equal according to semantics ofDouble.equals(Object).static StringgetHipparchusVersion()Get Hipparchus version.static inthash(double value)Returns an integer hash code representing the given double value.static inthash(double[] value)Returns an integer hash code representing the given double array.static <T extends CalculusFieldElement<T>>
Tmax(T e1, T e2)Find the maximum of two field elements.static <T extends CalculusFieldElement<T>>
Tmin(T e1, T e2)Find the minimum of two field elements.static doublenormalizeAngle(double a, double center)Normalize an angle in a 2π wide interval around a center value.static <T extends CalculusFieldElement<T>>
TnormalizeAngle(T a, T center)Normalize an angle in a 2π wide interval around a center value.static doublereduce(double a, double period, double offset)Reduce|a - offset|to the primary interval[0, |period|).static MathUtils.SumAndResidualtwoSum(double a, double b)Sumsaandbusing Møller's 2Sum algorithm.static <T extends FieldElement<T>>
MathUtils.FieldSumAndResidual<T>twoSum(T a, T b)Sumsaandbusing Møller's 2Sum algorithm.
-
-
-
Field Detail
-
TWO_PI
public static final double TWO_PI
\(2\pi\)- See Also:
- Constant Field Values
-
PI_SQUARED
public static final double PI_SQUARED
\(\pi^2\)- See Also:
- Constant Field Values
-
SEMI_PI
public static final double SEMI_PI
\(\pi/2\).- See Also:
- Constant Field Values
-
-
Method Detail
-
hash
public static int hash(double value)
Returns an integer hash code representing the given double value.- Parameters:
value- the value to be hashed- Returns:
- the hash code
-
equals
public static boolean equals(double x, double y)Returnstrueif the values are equal according to semantics ofDouble.equals(Object).- Parameters:
x- Valuey- Value- Returns:
Double.valueOf(x).equals(Double.valueOf(y))
-
hash
public static int hash(double[] value)
Returns an integer hash code representing the given double array.- Parameters:
value- the value to be hashed (may be null)- Returns:
- the hash code
-
normalizeAngle
public static double normalizeAngle(double a, double center)Normalize an angle in a 2π wide interval around a center value.This method has three main uses:
- normalize an angle between 0 and 2π:
a = MathUtils.normalizeAngle(a, FastMath.PI); - normalize an angle between -π and +π
a = MathUtils.normalizeAngle(a, 0.0); - compute the angle between two defining angular positions:
angle = MathUtils.normalizeAngle(end, start) - start;
Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.
- Parameters:
a- angle to normalizecenter- center of the desired 2π interval for the result- Returns:
- a-2kπ with integer k and center-π <= a-2kπ <= center+π
- normalize an angle between 0 and 2π:
-
normalizeAngle
public static <T extends CalculusFieldElement<T>> T normalizeAngle(T a, T center)
Normalize an angle in a 2π wide interval around a center value.This method has three main uses:
- normalize an angle between 0 and 2π:
a = MathUtils.normalizeAngle(a, FastMath.PI); - normalize an angle between -π and +π
a = MathUtils.normalizeAngle(a, zero); - compute the angle between two defining angular positions:
angle = MathUtils.normalizeAngle(end, start).subtract(start);
Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.
- Type Parameters:
T- the type of the field elements- Parameters:
a- angle to normalizecenter- center of the desired 2π interval for the result- Returns:
- a-2kπ with integer k and center-π <= a-2kπ <= center+π
- normalize an angle between 0 and 2π:
-
max
public static <T extends CalculusFieldElement<T>> T max(T e1, T e2)
Find the maximum of two field elements.- Type Parameters:
T- the type of the field elements- Parameters:
e1- first elemente2- second element- Returns:
- max(a1, e2)
-
min
public static <T extends CalculusFieldElement<T>> T min(T e1, T e2)
Find the minimum of two field elements.- Type Parameters:
T- the type of the field elements- Parameters:
e1- first elemente2- second element- Returns:
- min(a1, e2)
-
reduce
public static double reduce(double a, double period, double offset)Reduce
|a - offset|to the primary interval[0, |period|).Specifically, the value returned is
a - |period| * floor((a - offset) / |period|) - offset.If any of the parameters are
NaNor infinite, the result isNaN.- Parameters:
a- Value to reduce.period- Period.offset- Value that will be mapped to0.- Returns:
- the value, within the interval
[0 |period|), that corresponds toa.
-
copySign
public static byte copySign(byte magnitude, byte sign) throws MathRuntimeExceptionReturns the first argument with the sign of the second argument.- Parameters:
magnitude- Magnitude of the returned value.sign- Sign of the returned value.- Returns:
- a value with magnitude equal to
magnitudeand with the same sign as thesignargument. - Throws:
MathRuntimeException- ifmagnitude == Byte.MIN_VALUEandsign >= 0.
-
copySign
public static short copySign(short magnitude, short sign) throws MathRuntimeExceptionReturns the first argument with the sign of the second argument.- Parameters:
magnitude- Magnitude of the returned value.sign- Sign of the returned value.- Returns:
- a value with magnitude equal to
magnitudeand with the same sign as thesignargument. - Throws:
MathRuntimeException- ifmagnitude == Short.MIN_VALUEandsign >= 0.
-
copySign
public static int copySign(int magnitude, int sign) throws MathRuntimeExceptionReturns the first argument with the sign of the second argument.- Parameters:
magnitude- Magnitude of the returned value.sign- Sign of the returned value.- Returns:
- a value with magnitude equal to
magnitudeand with the same sign as thesignargument. - Throws:
MathRuntimeException- ifmagnitude == Integer.MIN_VALUEandsign >= 0.
-
copySign
public static long copySign(long magnitude, long sign) throws MathRuntimeExceptionReturns the first argument with the sign of the second argument.- Parameters:
magnitude- Magnitude of the returned value.sign- Sign of the returned value.- Returns:
- a value with magnitude equal to
magnitudeand with the same sign as thesignargument. - Throws:
MathRuntimeException- ifmagnitude == Long.MIN_VALUEandsign >= 0.
-
checkFinite
public static void checkFinite(double x) throws MathIllegalArgumentExceptionCheck that the argument is a real number.- Parameters:
x- Argument.- Throws:
MathIllegalArgumentException- ifxis not a finite real number.
-
checkFinite
public static void checkFinite(double[] val) throws MathIllegalArgumentExceptionCheck that all the elements are real numbers.- Parameters:
val- Arguments.- Throws:
MathIllegalArgumentException- if any values of the array is not a finite real number.
-
checkNotNull
public static void checkNotNull(Object o, Localizable pattern, Object... args) throws NullArgumentException
Checks that an object is not null.- Parameters:
o- Object to be checked.pattern- Message pattern.args- Arguments to replace the placeholders inpattern.- Throws:
NullArgumentException- ifoisnull.
-
checkNotNull
public static void checkNotNull(Object o) throws NullArgumentException
Checks that an object is not null.- Parameters:
o- Object to be checked.- Throws:
NullArgumentException- ifoisnull.
-
checkRangeInclusive
public static void checkRangeInclusive(long value, long lo, long hi)Checks that the given value is strictly within the range [lo, hi].- Parameters:
value- value to be checked.lo- the lower bound (inclusive).hi- the upper bound (inclusive).- Throws:
MathIllegalArgumentException- ifvalueis strictly outside [lo, hi].
-
checkRangeInclusive
public static void checkRangeInclusive(double value, double lo, double hi)Checks that the given value is strictly within the range [lo, hi].- Parameters:
value- value to be checked.lo- the lower bound (inclusive).hi- the upper bound (inclusive).- Throws:
MathIllegalArgumentException- ifvalueis strictly outside [lo, hi].
-
checkDimension
public static void checkDimension(int dimension, int otherDimension)Checks that the given dimensions match.- Parameters:
dimension- the first dimension.otherDimension- the second dimension.- Throws:
MathIllegalArgumentException- if length != otherLength.
-
getHipparchusVersion
public static String getHipparchusVersion()
Get Hipparchus version.The version is automatically retrieved from a properties file generated at maven compilation time. When using an IDE not configured to use maven, then a default value
"unknown"will be returned.- Returns:
- hipparchus version
- Since:
- 4.0
-
twoSum
public static MathUtils.SumAndResidual twoSum(double a, double b)
Sumsaandbusing Møller's 2Sum algorithm.References:
- Møller, Ole. "Quasi double-precision in floating point addition." BIT 5, 37–50 (1965).
- Shewchuk, Richard J. "Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates." Discrete & Computational Geometry 18, 305–363 (1997).
- https://en.wikipedia.org/wiki/2Sum
- Parameters:
a- first summandb- second summand- Returns:
- sum and residual error in the sum
-
twoSum
public static <T extends FieldElement<T>> MathUtils.FieldSumAndResidual<T> twoSum(T a, T b)
Sumsaandbusing Møller's 2Sum algorithm.References:
- Møller, Ole. "Quasi double-precision in floating point addition." BIT 5, 37–50 (1965).
- Shewchuk, Richard J. "Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates." Discrete & Computational Geometry 18, 305–363 (1997).
- https://en.wikipedia.org/wiki/2Sum
- Type Parameters:
T- field element type- Parameters:
a- first summandb- second summand- Returns:
- sum and residual error in the sum
-
-