Top Description Fields Constructors Methods
org.apache.commons.math3.util

public final Class MathUtils

extends Object
Class Inheritance
Imports
java.util.Arrays, org.apache.commons.math3.RealFieldElement, org.apache.commons.math3.exception.MathArithmeticException, .NotFiniteNumberException, .NullArgumentException, org.apache.commons.math3.exception.util.Localizable, .LocalizedFormats

Miscellaneous utility functions.
See Also
ArithmeticUtils, Precision, MathArrays

Field Summary

Modifier and TypeField and Description
public static final double
PI_SQUARED

\(\pi^2\)

public static final double
TWO_PI

\(2\pi\)

Constructor Summary

AccessConstructor and Description
private
MathUtils()

Class contains only static methods.

Method Summary

Modifier and TypeMethod and Description
public static void
checkFinite(final double
Argument.
x
)

Check that the argument is a real number.

public static void
checkFinite(final double[]
Arguments.
val
)

Check that all the elements are real numbers.

public static void
checkNotNull(Object
Object to be checked.
o
,
Localizable
Message pattern.
pattern
,
Object...
Arguments to replace the placeholders in pattern.
args
)

Checks that an object is not null.

public static void
checkNotNull(Object
Object to be checked.
o
)

Checks that an object is not null.

public static byte

Returns:

a value with magnitude equal to magnitude and with the same sign as the sign argument.
copySign
(byte
Magnitude of the returned value.
magnitude
,
byte
Sign of the returned value.
sign
)

Returns the first argument with the sign of the second argument.

public static short

Returns:

a value with magnitude equal to magnitude and with the same sign as the sign argument.
copySign
(short
Magnitude of the returned value.
magnitude
,
short
Sign of the returned value.
sign
)

Returns the first argument with the sign of the second argument.

public static int

Returns:

a value with magnitude equal to magnitude and with the same sign as the sign argument.
copySign
(int
Magnitude of the returned value.
magnitude
,
int
Sign of the returned value.
sign
)

Returns the first argument with the sign of the second argument.

public static long

Returns:

a value with magnitude equal to magnitude and with the same sign as the sign argument.
copySign
(long
Magnitude of the returned value.
magnitude
,
long
Sign of the returned value.
sign
)

Returns the first argument with the sign of the second argument.

public static boolean

Returns:

new Double(x).equals(new Double(y))
equals
(double
Value
x
,
double
Value
y
)

Returns true if the values are equal according to semantics of Double#equals(Object).

public static int

Returns:

the hash code
hash
(double
the value to be hashed
value
)

Returns an integer hash code representing the given double value.

public static int

Returns:

the hash code
hash
(double[]
the value to be hashed (may be null)
value
)

Returns an integer hash code representing the given double array.

public static <
the type of the field elements
T extends RealFieldElement<T>
>
T

Returns:

max(a1, e2)
max
(final T
first element
e1
,
final T
second element
e2
)

Find the maximum of two field elements.

public static <
the type of the field elements
T extends RealFieldElement<T>
>
T

Returns:

min(a1, e2)
min
(final T
first element
e1
,
final T
second element
e2
)

Find the minimum of two field elements.

public static double

Returns:

a-2kπ with integer k and center-π <= a-2kπ <= center+π
normalizeAngle
(double
angle to normalize
a
,
double
center of the desired 2π interval for the result
center
)

Normalize an angle in a 2π wide interval around a center value.

public static double

Returns:

the value, within the interval [0 |period|), that corresponds to a.
reduce
(double
Value to reduce.
a
,
double
Period.
period
,
double
Value that will be mapped to 0.
offset
)

Reduce |a - offset| to the primary interval [0, |period|).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PI_SQUAREDback to summary
public static final double PI_SQUARED

\(\pi^2\)

Since
3.4
TWO_PIback to summary
public static final double TWO_PI

\(2\pi\)

Since
2.1

Constructor Detail

MathUtilsback to summary
private MathUtils()

Class contains only static methods.

Method Detail

checkFiniteback to summary
public static void checkFinite(final double x) throws NotFiniteNumberException

Check that the argument is a real number.

Parameters
x:double

Argument.

Exceptions
NotFiniteNumberException:
if x is not a finite real number.
checkFiniteback to summary
public static void checkFinite(final double[] val) throws NotFiniteNumberException

Check that all the elements are real numbers.

Parameters
val:double[]

Arguments.

Exceptions
NotFiniteNumberException:
if any values of the array is not a finite real number.
checkNotNullback to summary
public static void checkNotNull(Object o, Localizable pattern, Object... args) throws NullArgumentException

Checks that an object is not null.

Parameters
o:Object

Object to be checked.

pattern:Localizable

Message pattern.

args:Object[]

Arguments to replace the placeholders in pattern.

Exceptions
NullArgumentException:
if o is null.
checkNotNullback to summary
public static void checkNotNull(Object o) throws NullArgumentException

Checks that an object is not null.

Parameters
o:Object

Object to be checked.

Exceptions
NullArgumentException:
if o is null.
copySignback to summary
public static byte copySign(byte magnitude, byte sign) throws MathArithmeticException

Returns the first argument with the sign of the second argument.

Parameters
magnitude:byte

Magnitude of the returned value.

sign:byte

Sign of the returned value.

Returns:byte

a value with magnitude equal to magnitude and with the same sign as the sign argument.

Exceptions
MathArithmeticException:
if magnitude == Byte.MIN_VALUE and sign >= 0.
copySignback to summary
public static short copySign(short magnitude, short sign) throws MathArithmeticException

Returns the first argument with the sign of the second argument.

Parameters
magnitude:short

Magnitude of the returned value.

sign:short

Sign of the returned value.

Returns:short

a value with magnitude equal to magnitude and with the same sign as the sign argument.

Exceptions
MathArithmeticException:
if magnitude == Short.MIN_VALUE and sign >= 0.
copySignback to summary
public static int copySign(int magnitude, int sign) throws MathArithmeticException

Returns the first argument with the sign of the second argument.

Parameters
magnitude:int

Magnitude of the returned value.

sign:int

Sign of the returned value.

Returns:int

a value with magnitude equal to magnitude and with the same sign as the sign argument.

Exceptions
MathArithmeticException:
if magnitude == Integer.MIN_VALUE and sign >= 0.
copySignback to summary
public static long copySign(long magnitude, long sign) throws MathArithmeticException

Returns the first argument with the sign of the second argument.

Parameters
magnitude:long

Magnitude of the returned value.

sign:long

Sign of the returned value.

Returns:long

a value with magnitude equal to magnitude and with the same sign as the sign argument.

Exceptions
MathArithmeticException:
if magnitude == Long.MIN_VALUE and sign >= 0.
equalsback to summary
public static boolean equals(double x, double y)

Returns true if the values are equal according to semantics of Double#equals(Object).

Parameters
x:double

Value

y:double

Value

Returns:boolean

new Double(x).equals(new Double(y))

hashback to summary
public static int hash(double value)

Returns an integer hash code representing the given double value.

Parameters
value:double

the value to be hashed

Returns:int

the hash code

hashback to summary
public static int hash(double[] value)

Returns an integer hash code representing the given double array.

Parameters
value:double[]

the value to be hashed (may be null)

Returns:int

the hash code

Since
1.2
maxback to summary
public static <T extends RealFieldElement<T>> T max(final T e1, final T e2)

Find the maximum of two field elements.

Parameters
<T>
the type of the field elements
e1:T

first element

e2:T

second element

Returns:T

max(a1, e2)

Since
3.6
minback to summary
public static <T extends RealFieldElement<T>> T min(final T e1, final T e2)

Find the minimum of two field elements.

Parameters
<T>
the type of the field elements
e1:T

first element

e2:T

second element

Returns:T

min(a1, e2)

Since
3.6
normalizeAngleback to summary
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:double

angle to normalize

center:double

center of the desired 2π interval for the result

Returns:double

a-2kπ with integer k and center-π <= a-2kπ <= center+π

Since
1.2
reduceback to summary
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 NaN or infinite, the result is NaN.

Parameters
a:double

Value to reduce.

period:double

Period.

offset:double

Value that will be mapped to 0.

Returns:double

the value, within the interval [0 |period|), that corresponds to a.