Package org.apache.commons.numbers.core
Enum Norm
- All Implemented Interfaces:
Serializable
,Comparable<Norm>
Norm functions.
The implementations provide increased numerical accuracy. Algorithms primary source is the 2005 paper Accurate Sum and Dot Product by Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interface
Function of array argument.private static interface
Function of 3 arguments.private static interface
Function of 2 arguments. -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionAlias forL2
.Manhattan norm (sum of the absolute values of the arguments).Maximum norm (maximum of the absolute values of the arguments).Alias forL1
.Alias forLINF
. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Norm.Array
Function of array argument.private static final int
Threshold for the difference between the exponents of two Euclidean 2D input values where the larger value dominates the calculation.private static final double
Threshold for scaling large numbers.private static final double
Threshold for scaling up a single value bySCALE_UP
without risking overflow when the value is squared.private static final double
Value used to scale down large numbers.private static final double
Value used to scale up small numbers.private static final double
Threshold for scaling small numbers.private final Norm.Three
Function of 3 arguments.private final Norm.Two
Function of 2 arguments. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
private
Norm
(Norm.Two two, Norm.Three three, Norm.Array array) -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
ensureNonEmpty
(double[] a) private static double
euclidean
(double[] v) Computes the Euclidean norm.private static double
euclidean
(double x, double y) Computes the Euclidean norm.private static double
euclidean
(double x, double y, double z) Computes the Euclidean norm.private static double
euclideanNormSpecial
(double[] v, int start) Special cases of non-finite input.private static double
manhattan
(double[] v) Computes the Manhattan norm.private static double
manhattan
(double x, double y) Computes the Manhattan norm.private static double
manhattan
(double x, double y, double z) Computes the Manhattan norm.private static double
maximum
(double[] v) Computes the maximum norm.private static double
maximum
(double x, double y) Computes the maximum norm.private static double
maximum
(double x, double y, double z) Computes the maximum norm.final double
of
(double[] v) Computes the norm.final double
of
(double x, double y) Computes the norm.final double
of
(double x, double y, double z) Computes the norm.static Norm
Returns the enum constant of this type with the specified name.static Norm[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
Enum Constant Details
-
L1
Manhattan norm (sum of the absolute values of the arguments). -
MANHATTAN
Alias forL1
. -
L2
-
EUCLIDEAN
Alias forL2
. -
LINF
Maximum norm (maximum of the absolute values of the arguments). -
MAXIMUM
Alias forLINF
.
-
-
Field Details
-
SMALL_THRESH
private static final double SMALL_THRESHThreshold for scaling small numbers. This value is chosen such that doubles set to this value can be squared without underflow. Values less than this must be scaled up.- See Also:
-
LARGE_THRESH
private static final double LARGE_THRESHThreshold for scaling large numbers. This value is chosen such that 2^31 doubles set to this value can be squared and added without overflow. Values greater than this must be scaled down.- See Also:
-
SAFE_SCALE_UP_THRESH
private static final double SAFE_SCALE_UP_THRESHThreshold for scaling up a single value bySCALE_UP
without risking overflow when the value is squared.- See Also:
-
SCALE_DOWN
private static final double SCALE_DOWNValue used to scale down large numbers.- See Also:
-
SCALE_UP
private static final double SCALE_UPValue used to scale up small numbers.- See Also:
-
EXP_DIFF_THRESHOLD_2D
private static final int EXP_DIFF_THRESHOLD_2DThreshold for the difference between the exponents of two Euclidean 2D input values where the larger value dominates the calculation.- See Also:
-
two
Function of 2 arguments. -
three
Function of 3 arguments. -
array
Function of array argument.
-
-
Constructor Details
-
Norm
- Parameters:
two
- Function of 2 arguments.three
- Function of 3 arguments.array
- Function of array argument.
-
Norm
- Parameters:
alias
- Alternative name.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
of
public final double of(double x, double y) Computes the norm.Special cases:
- If either value is
Double.NaN
, then the result isDouble.NaN
. - If either value is infinite and the other value is not
Double.NaN
, then the result isDouble.POSITIVE_INFINITY
.
- Parameters:
x
- Argument.y
- Argument.- Returns:
- the norm.
- If either value is
-
of
public final double of(double x, double y, double z) Computes the norm.Special cases:
- If any value is
Double.NaN
, then the result isDouble.NaN
. - If any value is infinite and no value is not
Double.NaN
, then the result isDouble.POSITIVE_INFINITY
.
- Parameters:
x
- Argument.y
- Argument.z
- Argument.- Returns:
- the norm.
- If any value is
-
of
public final double of(double[] v) Computes the norm.Special cases:
- If any value is
Double.NaN
, then the result isDouble.NaN
. - If any value is infinite and no value is not
Double.NaN
, then the result isDouble.POSITIVE_INFINITY
.
- Parameters:
v
- Argument.- Returns:
- the norm.
- Throws:
IllegalArgumentException
- if the array is empty.
- If any value is
-
manhattan
private static double manhattan(double x, double y) Computes the Manhattan norm.- Parameters:
x
- first input valuey
- second input value- Returns:
- \(|x| + |y|\).
- See Also:
-
manhattan
private static double manhattan(double x, double y, double z) Computes the Manhattan norm.- Parameters:
x
- first input valuey
- second input valuez
- third input value- Returns:
- \(|x| + |y| + |z|\)
- See Also:
-
manhattan
private static double manhattan(double[] v) Computes the Manhattan norm.- Parameters:
v
- input values- Returns:
- \(|v_0| + ... + |v_i|\)
- See Also:
-
euclidean
private static double euclidean(double x, double y) Computes the Euclidean norm. This implementation handles possible overflow or underflow.Comparison with Math.hypot() While not guaranteed to return the same result, this method provides similar error bounds as
Math.hypot(double, double)
(and may run faster on some JVM).- Parameters:
x
- first inputy
- second input- Returns:
- \(\sqrt{x^2 + y^2}\).
- See Also:
-
euclidean
private static double euclidean(double x, double y, double z) Computes the Euclidean norm. This implementation handles possible overflow or underflow.- Parameters:
x
- first inputy
- second inputz
- third input- Returns:
- \(\sqrt{x^2 + y^2 + z^2}\)
- See Also:
-
euclidean
private static double euclidean(double[] v) Computes the Euclidean norm. This implementation handles possible overflow or underflow.- Parameters:
v
- input values- Returns:
- \(\sqrt{v_0^2 + ... + v_{n-1}^2}\).
- See Also:
-
euclideanNormSpecial
private static double euclideanNormSpecial(double[] v, int start) Special cases of non-finite input.- Parameters:
v
- input vectorstart
- index to start examining the input vector from- Returns:
- Euclidean norm special value
-
maximum
private static double maximum(double x, double y) Computes the maximum norm.- Parameters:
x
- first inputy
- second input- Returns:
- \(\max{(|x|, |y|)}\).
- See Also:
-
maximum
private static double maximum(double x, double y, double z) Computes the maximum norm.- Parameters:
x
- first inputy
- second inputz
- third input- Returns:
- \(\max{(|x|, |y|, |z|)}\).
- See Also:
-
maximum
private static double maximum(double[] v) Computes the maximum norm.- Parameters:
v
- input values- Returns:
- \(\max{(|v_0|, \ldots, |v_{n-1}|)}\)
- See Also:
-
ensureNonEmpty
private static void ensureNonEmpty(double[] a) - Parameters:
a
- Array.- Throws:
IllegalArgumentException
- for zero-size array.
-