Class MathArrays
- java.lang.Object
-
- org.apache.commons.math4.legacy.core.MathArrays
-
public final class MathArrays extends Object
Arrays utilities.- Since:
- 3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMathArrays.FunctionReal-valued function that operate on an array or a part of it.static classMathArrays.OrderDirectionSpecification of ordering direction.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]buildArray(Field<T> field, int length)Build an array of elements.static <T> T[][]buildArray(Field<T> field, int rows, int columns)Build a double dimension array of elements.static voidcheckEqualLength(double[] a, double[] b)Check that both arrays have the same length.static booleancheckEqualLength(double[] a, double[] b, boolean abort)Check that both arrays have the same length.static voidcheckEqualLength(int[] a, int[] b)Check that both arrays have the same length.static booleancheckEqualLength(int[] a, int[] b, boolean abort)Check that both arrays have the same length.static voidcheckNonNegative(long[] in)Check that all entries of the input array are >= 0.static voidcheckNonNegative(long[][] in)Check all entries of the input array are >= 0.static voidcheckNotNaN(double[] in)Check that no entry of the input array isNaN.static voidcheckOrder(double[] val)Check that the given array is sorted in strictly increasing order.static voidcheckOrder(double[] val, MathArrays.OrderDirection dir, boolean strict)Check that the given array is sorted.static booleancheckOrder(double[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort)Check that the given array is sorted.static voidcheckPositive(double[] in)Check that all entries of the input array are strictly positive.static voidcheckRectangular(long[][] in)Throws DimensionMismatchException if the input array is not rectangular.static double[]concatenate(double[]... x)Concatenates a sequence of arrays.static double[]convolve(double[] x, double[] h)Calculates the convolution between two sequences.static doubledistance(double[] p1, double[] p2)Calculates the L2 (Euclidean) distance between two points.static doubledistance(int[] p1, int[] p2)Calculates the L2 (Euclidean) distance between two points.static doubledistance1(double[] p1, double[] p2)Calculates the L1 (sum of abs) distance between two points.static intdistance1(int[] p1, int[] p2)Calculates the L1 (sum of abs) distance between two points.static doubledistanceInf(double[] p1, double[] p2)Calculates the L∞ (max of abs) distance between two points.static intdistanceInf(int[] p1, int[] p2)Calculates the L∞ (max of abs) distance between two points.static double[]ebeAdd(double[] a, double[] b)Creates an array whose contents will be the element-by-element addition of the arguments.static double[]ebeDivide(double[] a, double[] b)Creates an array whose contents will be the element-by-element division of the first argument by the second.static double[]ebeMultiply(double[] a, double[] b)Creates an array whose contents will be the element-by-element multiplication of the arguments.static double[]ebeSubtract(double[] a, double[] b)Creates an array whose contents will be the element-by-element subtraction of the second argument from the first.static booleanequals(double[] x, double[] y)Returnstrueiff both arguments arenullor have same dimensions and all their elements are equal as defined byPrecision.equals(double,double).static booleanequals(float[] x, float[] y)Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined byPrecision.equals(float,float).static booleanequalsIncludingNaN(double[] x, double[] y)Returnstrueiff both arguments arenullor have same dimensions and all their elements are equal as defined bythis method.static booleanequalsIncludingNaN(float[] x, float[] y)Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined bythis method.static booleanisMonotonic(double[] val, MathArrays.OrderDirection dir, boolean strict)Check that an array is monotonically increasing or decreasing.static <T extends Comparable<? super T>>
booleanisMonotonic(T[] val, MathArrays.OrderDirection dir, boolean strict)Check that an array is monotonically increasing or decreasing.static int[]natural(int n)Returns an array representing the natural numbern.static double[]normalizeArray(double[] values, double normalizedSum)Normalizes an array to make it sum to a specified value.static double[]scale(double val, double[] arr)Create a copy of an array scaled by a value.static voidscaleInPlace(double val, double[] arr)Multiply each element of an array by a value.static int[]sequence(int size, int start, int stride)Returns an array ofsizeintegers starting atstart, skippingstridenumbers.static double[]unique(double[] data)Returns an array consisting of the unique values indata.static booleanverifyValues(double[] values, double[] weights, int begin, int length)This method is used to verify that the begin and length parameters designate a subarray of positive length and the weights are all non-negative, non-NaN, finite, and not all zero.static booleanverifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty)This method is used to verify that the begin and length parameters designate a subarray of positive length and the weights are all non-negative, non-NaN, finite, and not all zero.static booleanverifyValues(double[] values, int begin, int length)This method is used to verify that the input parameters designate a subarray of positive length.static booleanverifyValues(double[] values, int begin, int length, boolean allowEmpty)This method is used to verify that the input parameters designate a subarray of positive length.
-
-
-
Method Detail
-
scale
public static double[] scale(double val, double[] arr)
Create a copy of an array scaled by a value.- Parameters:
arr- Array to scale.val- Scalar.- Returns:
- scaled copy of array with each entry multiplied by val.
- Since:
- 3.2
-
scaleInPlace
public static void scaleInPlace(double val, double[] arr)
Multiply each element of an array by a value.
The array is modified in place (no copy is created).
- Parameters:
arr- Array to scaleval- Scalar- Since:
- 3.2
-
ebeAdd
public static double[] ebeAdd(double[] a, double[] b)
Creates an array whose contents will be the element-by-element addition of the arguments.- Parameters:
a- First term of the addition.b- Second term of the addition.- Returns:
- a new array
rwherer[i] = a[i] + b[i]. - Throws:
DimensionMismatchException- if the array lengths differ.- Since:
- 3.1
-
ebeSubtract
public static double[] ebeSubtract(double[] a, double[] b)
Creates an array whose contents will be the element-by-element subtraction of the second argument from the first.- Parameters:
a- First term.b- Element to be subtracted.- Returns:
- a new array
rwherer[i] = a[i] - b[i]. - Throws:
DimensionMismatchException- if the array lengths differ.- Since:
- 3.1
-
ebeMultiply
public static double[] ebeMultiply(double[] a, double[] b)
Creates an array whose contents will be the element-by-element multiplication of the arguments.- Parameters:
a- First factor of the multiplication.b- Second factor of the multiplication.- Returns:
- a new array
rwherer[i] = a[i] * b[i]. - Throws:
DimensionMismatchException- if the array lengths differ.- Since:
- 3.1
-
ebeDivide
public static double[] ebeDivide(double[] a, double[] b)
Creates an array whose contents will be the element-by-element division of the first argument by the second.- Parameters:
a- Numerator of the division.b- Denominator of the division.- Returns:
- a new array
rwherer[i] = a[i] / b[i]. - Throws:
DimensionMismatchException- if the array lengths differ.- Since:
- 3.1
-
distance1
public static double distance1(double[] p1, double[] p2)
Calculates the L1 (sum of abs) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L1 distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
distance1
public static int distance1(int[] p1, int[] p2)
Calculates the L1 (sum of abs) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L1 distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
distance
public static double distance(double[] p1, double[] p2)
Calculates the L2 (Euclidean) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L2 distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
distance
public static double distance(int[] p1, int[] p2)
Calculates the L2 (Euclidean) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L2 distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
distanceInf
public static double distanceInf(double[] p1, double[] p2)
Calculates the L∞ (max of abs) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L∞ distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
distanceInf
public static int distanceInf(int[] p1, int[] p2)
Calculates the L∞ (max of abs) distance between two points.- Parameters:
p1- the first pointp2- the second point- Returns:
- the L∞ distance between the two points
- Throws:
DimensionMismatchException- if the array lengths differ.
-
isMonotonic
public static <T extends Comparable<? super T>> boolean isMonotonic(T[] val, MathArrays.OrderDirection dir, boolean strict)
Check that an array is monotonically increasing or decreasing.- Type Parameters:
T- the type of the elements in the specified array- Parameters:
val- Values.dir- Ordering direction.strict- Whether the order should be strict.- Returns:
trueif sorted,falseotherwise.
-
isMonotonic
public static boolean isMonotonic(double[] val, MathArrays.OrderDirection dir, boolean strict)
Check that an array is monotonically increasing or decreasing.- Parameters:
val- Values.dir- Ordering direction.strict- Whether the order should be strict.- Returns:
trueif sorted,falseotherwise.
-
checkEqualLength
public static boolean checkEqualLength(double[] a, double[] b, boolean abort)
Check that both arrays have the same length.- Parameters:
a- Array.b- Array.abort- Whether to throw an exception if the check fails.- Returns:
trueif the arrays have the same length.- Throws:
DimensionMismatchException- if the lengths differ andabortistrue.- Since:
- 3.6
-
checkEqualLength
public static void checkEqualLength(double[] a, double[] b)
Check that both arrays have the same length.- Parameters:
a- Array.b- Array.- Throws:
DimensionMismatchException- if the lengths differ.- Since:
- 3.6
-
checkEqualLength
public static boolean checkEqualLength(int[] a, int[] b, boolean abort)
Check that both arrays have the same length.- Parameters:
a- Array.b- Array.abort- Whether to throw an exception if the check fails.- Returns:
trueif the arrays have the same length.- Throws:
DimensionMismatchException- if the lengths differ andabortistrue.- Since:
- 3.6
-
checkEqualLength
public static void checkEqualLength(int[] a, int[] b)
Check that both arrays have the same length.- Parameters:
a- Array.b- Array.- Throws:
DimensionMismatchException- if the lengths differ.- Since:
- 3.6
-
checkOrder
public static boolean checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort)
Check that the given array is sorted.- Parameters:
val- Values.dir- Ordering direction.strict- Whether the order should be strict.abort- Whether to throw an exception if the check fails.- Returns:
trueif the array is sorted.- Throws:
NonMonotonicSequenceException- if the array is not sorted andabortistrue.
-
checkOrder
public static void checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict)
Check that the given array is sorted.- Parameters:
val- Values.dir- Ordering direction.strict- Whether the order should be strict.- Throws:
NonMonotonicSequenceException- if the array is not sorted.- Since:
- 2.2
-
checkOrder
public static void checkOrder(double[] val)
Check that the given array is sorted in strictly increasing order.- Parameters:
val- Values.- Throws:
NonMonotonicSequenceException- if the array is not sorted.- Since:
- 2.2
-
checkRectangular
public static void checkRectangular(long[][] in)
Throws DimensionMismatchException if the input array is not rectangular.- Parameters:
in- array to be tested- Throws:
NullArgumentException- if input array is nullDimensionMismatchException- if input array is not rectangular- Since:
- 3.1
-
checkPositive
public static void checkPositive(double[] in)
Check that all entries of the input array are strictly positive.- Parameters:
in- Array to be tested- Throws:
NotStrictlyPositiveException- if any entries of the array are not strictly positive.- Since:
- 3.1
-
checkNotNaN
public static void checkNotNaN(double[] in)
Check that no entry of the input array isNaN.- Parameters:
in- Array to be tested.- Throws:
NotANumberException- if an entry isNaN.- Since:
- 3.4
-
checkNonNegative
public static void checkNonNegative(long[] in)
Check that all entries of the input array are >= 0.- Parameters:
in- Array to be tested- Throws:
NotPositiveException- if any array entries are less than 0.- Since:
- 3.1
-
checkNonNegative
public static void checkNonNegative(long[][] in)
Check all entries of the input array are >= 0.- Parameters:
in- Array to be tested- Throws:
NotPositiveException- if any array entries are less than 0.- Since:
- 3.1
-
equals
public static boolean equals(float[] x, float[] y)
Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined byPrecision.equals(float,float).- Parameters:
x- first arrayy- second array- Returns:
- true if the values are both null or have same dimension and equal elements.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(float[] x, float[] y)
Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined bythis method.- Parameters:
x- first arrayy- second array- Returns:
- true if the values are both null or have same dimension and equal elements
- Since:
- 2.2
-
equals
public static boolean equals(double[] x, double[] y)
Returnstrueiff both arguments arenullor have same dimensions and all their elements are equal as defined byPrecision.equals(double,double).- Parameters:
x- First array.y- Second array.- Returns:
trueif the values are bothnullor have same dimension and equal elements.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(double[] x, double[] y)
Returnstrueiff both arguments arenullor have same dimensions and all their elements are equal as defined bythis method.- Parameters:
x- First array.y- Second array.- Returns:
trueif the values are bothnullor have same dimension and equal elements.- Since:
- 2.2
-
normalizeArray
public static double[] normalizeArray(double[] values, double normalizedSum)
Normalizes an array to make it sum to a specified value. Returns the result of the transformationx |-> x * normalizedSum / sumapplied to each non-NaN element x of the input array, where sum is the sum of the non-NaN entries in the input array.Throws IllegalArgumentException if
normalizedSumis infinite or NaN and ArithmeticException if the input array contains any infinite elements or sums to 0.Ignores (i.e., copies unchanged to the output array) NaNs in the input array.
- Parameters:
values- Input array to be normalizednormalizedSum- Target sum for the normalized array- Returns:
- the normalized array.
- Throws:
MathArithmeticException- if the input array contains infinite elements or sums to zero.MathIllegalArgumentException- if the target sum is infinite orNaN.- Since:
- 2.1
-
buildArray
public static <T> T[] buildArray(Field<T> field, int length)
Build an array of elements.Arrays are filled with field.getZero()
- Type Parameters:
T- the type of the field elements- Parameters:
field- field to which array elements belonglength- of the array- Returns:
- a new array
- Since:
- 3.2
-
buildArray
public static <T> T[][] buildArray(Field<T> field, int rows, int columns)
Build a double dimension array of elements.Arrays are filled with field.getZero()
- Type Parameters:
T- the type of the field elements- Parameters:
field- field to which array elements belongrows- number of rows in the arraycolumns- number of columns (may be negative to build partial arrays in the same waynew Field[rows][]works)- Returns:
- a new array
- Since:
- 3.2
-
convolve
public static double[] convolve(double[] x, double[] h)
Calculates the convolution between two sequences.The solution is obtained via straightforward computation of the convolution sum (and not via FFT). Whenever the computation needs an element that would be located at an index outside the input arrays, the value is assumed to be zero.
- Parameters:
x- First sequence. Typically, this sequence will represent an input signal to a system.h- Second sequence. Typically, this sequence will represent the impulse response of the system.- Returns:
- the convolution of
xandh. This array's length will bex.length + h.length - 1. - Throws:
NullArgumentException- if eitherxorhisnull.NoDataException- if eitherxorhis empty.- Since:
- 3.3
-
natural
public static int[] natural(int n)
Returns an array representing the natural numbern.- Parameters:
n- Natural number.- Returns:
- an array whose entries are the numbers 0, 1, ...,
n-1. Ifn == 0, the returned array is empty.
-
sequence
public static int[] sequence(int size, int start, int stride)
Returns an array ofsizeintegers starting atstart, skippingstridenumbers.- Parameters:
size- Natural number.start- Natural number.stride- Natural number.- Returns:
- an array whose entries are the numbers
start, start + stride, ..., start + (size - 1) * stride. Ifsize == 0, the returned array is empty. - Since:
- 3.4
-
verifyValues
public static boolean verifyValues(double[] values, int begin, int length)
This method is used to verify that the input parameters designate a subarray of positive length.- returns
trueiff the parameters designate a subarray of positive length - throws
MathIllegalArgumentExceptionif the array is null or or the indices are invalid - returns
falseif the array is non-null, butlengthis 0.
- Parameters:
values- the input arraybegin- index of the first array element to includelength- the number of elements to include- Returns:
- true if the parameters are valid and designate a subarray of positive length
- Throws:
MathIllegalArgumentException- if the indices are invalid or the array is null- Since:
- 3.3
- returns
-
verifyValues
public static boolean verifyValues(double[] values, int begin, int length, boolean allowEmpty)
This method is used to verify that the input parameters designate a subarray of positive length.- returns
trueiff the parameters designate a subarray of non-negative length - throws
IllegalArgumentExceptionif the array is null or or the indices are invalid - returns
falseif the array is non-null, butlengthis 0 unlessallowEmptyistrue
- Parameters:
values- the input arraybegin- index of the first array element to includelength- the number of elements to includeallowEmpty- iftruethen zero length arrays are allowed- Returns:
- true if the parameters are valid
- Throws:
MathIllegalArgumentException- if the indices are invalid or the array is null- Since:
- 3.3
- returns
-
verifyValues
public static boolean verifyValues(double[] values, double[] weights, int begin, int length)
This method is used to verify that the begin and length parameters designate a subarray of positive length and the weights are all non-negative, non-NaN, finite, and not all zero.- returns
trueiff the parameters designate a subarray of positive length and the weights array contains legitimate values. - throws
IllegalArgumentExceptionif any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
- the start and length arguments do not determine a valid array
- returns
falseif the array is non-null, butlengthis 0.
- Parameters:
values- the input arrayweights- the weights arraybegin- index of the first array element to includelength- the number of elements to include- Returns:
- true if the parameters are valid and designate a subarray of positive length
- Throws:
MathIllegalArgumentException- if the indices are invalid or the array is null- Since:
- 3.3
- returns
-
verifyValues
public static boolean verifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty)
This method is used to verify that the begin and length parameters designate a subarray of positive length and the weights are all non-negative, non-NaN, finite, and not all zero.- returns
trueiff the parameters designate a subarray of non-negative length and the weights array contains legitimate values. - throws
MathIllegalArgumentExceptionif any of the following are true:- the values array is null
- the weights array is null
- the weights array does not have the same length as the values array
- the weights array contains one or more infinite values
- the weights array contains one or more NaN values
- the weights array contains negative values
- the weights array does not contain at least one non-zero value (applies when length is non zero)
- the start and length arguments do not determine a valid array
- returns
falseif the array is non-null, butlengthis 0 unlessallowEmptyistrue.
- Parameters:
values- the input array.weights- the weights array.begin- index of the first array element to include.length- the number of elements to include.allowEmpty- iftruethan allow zero length arrays to pass.- Returns:
trueif the parameters are valid.- Throws:
NullArgumentException- if either of the arrays are nullMathIllegalArgumentException- if the array indices are not valid, the weights array contains NaN, infinite or negative elements, or there are no positive weights.- Since:
- 3.3
- returns
-
concatenate
public static double[] concatenate(double[]... x)
Concatenates a sequence of arrays. The return array consists of the entries of the input arrays concatenated in the order they appear in the argument list. Null arrays cause NullPointerExceptions; zero length arrays are allowed (contributing nothing to the output array).- Parameters:
x- list of double[] arrays to concatenate- Returns:
- a new array consisting of the entries of the argument arrays
- Throws:
NullPointerException- if any of the arrays are null- Since:
- 3.6
-
unique
public static double[] unique(double[] data)
Returns an array consisting of the unique values indata. The return array is sorted in descending order. Empty arrays are allowed, but null arrays result in NullPointerException. Infinities are allowed. NaN values are allowed with maximum sort order - i.e., if there are NaN values indata,Double.NaNwill be the first element of the output array, even if the array also containsDouble.POSITIVE_INFINITY.- Parameters:
data- array to scan- Returns:
- descending list of values included in the input array
- Throws:
NullPointerException- if data is null- Since:
- 3.6
-
-