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

public Class MathArrays

extends Object
Class Inheritance
Imports
java.lang.reflect.Array, java.util.ArrayList, .Arrays, .Collections, .Comparator, .Iterator, .List, .TreeSet, org.apache.commons.math3.Field, org.apache.commons.math3.random.RandomGenerator, .Well19937c, org.apache.commons.math3.distribution.UniformIntegerDistribution, org.apache.commons.math3.exception.DimensionMismatchException, .MathArithmeticException, .MathIllegalArgumentException, .MathInternalError, .NoDataException, .NonMonotonicSequenceException, .NotPositiveException, .NotStrictlyPositiveException, .NullArgumentException, .NumberIsTooLargeException, .NotANumberException, org.apache.commons.math3.exception.util.LocalizedFormats

Arrays utilities.
Since
3.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
MathArrays.Function

Real-valued function that operate on an array or a part of it.

public static enum
MathArrays.OrderDirection

Specification of ordering direction.

private static class
MathArrays.PairDoubleInteger

A helper data structure holding a double and an integer value.

public static enum
MathArrays.Position

Specification for indicating that some operation applies before or after a given index.

Constructor Summary

AccessConstructor and Description
private
MathArrays()

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the field elements
T
>
T[]

Returns:

a new array
buildArray
(final Field<T>
field to which array elements belong
field
,
final int
of the array
length
)

Build an array of elements.

public static <
the type of the field elements
T
>
T[][]

Returns:

a new array
buildArray
(final Field<T>
field to which array elements belong
field
,
final int
number of rows in the array
rows
,
final int
number of columns (may be negative to build partial arrays in the same way new Field[rows][] works)
columns
)

Build a double dimension array of elements.

public static boolean

Returns:

true if the arrays have the same length.
checkEqualLength
(double[]
Array.
a
,
double[]
Array.
b
,
boolean
Whether to throw an exception if the check fails.
abort
)

Check that both arrays have the same length.

public static void
checkEqualLength(double[]
Array.
a
,
double[]
Array.
b
)

Check that both arrays have the same length.

public static boolean

Returns:

true if the arrays have the same length.
checkEqualLength
(int[]
Array.
a
,
int[]
Array.
b
,
boolean
Whether to throw an exception if the check fails.
abort
)

Check that both arrays have the same length.

public static void
checkEqualLength(int[]
Array.
a
,
int[]
Array.
b
)

Check that both arrays have the same length.

public static void
checkNonNegative(final long[]
Array to be tested
in
)

Check that all entries of the input array are >= 0.

public static void
checkNonNegative(final long[][]
Array to be tested
in
)

Check all entries of the input array are >= 0.

public static void
checkNotNaN(final double[]
Array to be tested.
in
)

Check that no entry of the input array is NaN.

public static boolean

Returns:

true if the array is sorted.
checkOrder
(double[]
Values.
val
,
MathArrays.OrderDirection
Ordering direction.
dir
,
boolean
Whether the order should be strict.
strict
,
boolean
Whether to throw an exception if the check fails.
abort
)

Check that the given array is sorted.

public static void
checkOrder(double[]
Values.
val
,
MathArrays.OrderDirection
Ordering direction.
dir
,
boolean
Whether the order should be strict.
strict
)

Check that the given array is sorted.

public static void
checkOrder(double[]
Values.
val
)

Check that the given array is sorted in strictly increasing order.

public static void
checkPositive(final double[]
Array to be tested
in
)

Check that all entries of the input array are strictly positive.

public static void
checkRectangular(final long[][]
array to be tested
in
)

Throws DimensionMismatchException if the input array is not rectangular.

public static double[]

Returns:

a new array consisting of the entries of the argument arrays
concatenate
(double[]...
list of double[] arrays to concatenate
x
)

Concatenates a sequence of arrays.

public static double[]

Returns:

the convolution of x and h. This array's length will be x.length + h.length - 1.
convolve
(double[]
First sequence. Typically, this sequence will represent an input signal to a system.
x
,
double[]
Second sequence. Typically, this sequence will represent the impulse response of the system.
h
)

Calculates the convolution between two sequences.

public static int[]

Returns:

the copied array.
copyOf
(int[]
Array to be copied.
source
)

Creates a copy of the source array.

public static double[]

Returns:

the copied array.
copyOf
(double[]
Array to be copied.
source
)

Creates a copy of the source array.

public static int[]

Returns:

the copied array.
copyOf
(int[]
Array to be copied.
source
,
int
Number of entries to copy. If smaller then the source length, the copy will be truncated, if larger it will padded with zeroes.
len
)

Creates a copy of the source array.

public static double[]

Returns:

the copied array.
copyOf
(double[]
Array to be copied.
source
,
int
Number of entries to copy. If smaller then the source length, the copy will be truncated, if larger it will padded with zeroes.
len
)

Creates a copy of the source array.

public static double[]

Returns:

the copied array.
copyOfRange
(double[]
Array to be copied.
source
,
int
Initial index of the range to be copied, inclusive.
from
,
int
Final index of the range to be copied, exclusive. (This index may lie outside the array.)
to
)

Creates a copy of the source array.

public static double

Returns:

the cosine of the angle between the vectors.
cosAngle
(double[]
Cartesian coordinates of the first vector.
v1
,
double[]
Cartesian coordinates of the second vector.
v2
)

Calculates the cosine of the angle between two vectors.

public static double

Returns:

the L2 distance between the two points
distance
(double[]
the first point
p1
,
double[]
the second point
p2
)

Calculates the L2 (Euclidean) distance between two points.

public static double

Returns:

the L2 distance between the two points
distance
(int[]
the first point
p1
,
int[]
the second point
p2
)

Calculates the L2 (Euclidean) distance between two points.

public static double

Returns:

the L1 distance between the two points
distance1
(double[]
the first point
p1
,
double[]
the second point
p2
)

Calculates the L1 (sum of abs) distance between two points.

public static int

Returns:

the L1 distance between the two points
distance1
(int[]
the first point
p1
,
int[]
the second point
p2
)

Calculates the L1 (sum of abs) distance between two points.

public static double

Returns:

the L∞ distance between the two points
distanceInf
(double[]
the first point
p1
,
double[]
the second point
p2
)

Calculates the L∞ (max of abs) distance between two points.

public static int

Returns:

the L∞ distance between the two points
distanceInf
(int[]
the first point
p1
,
int[]
the second point
p2
)

Calculates the L∞ (max of abs) distance between two points.

public static double[]

Returns:

a new array r where r[i] = a[i] + b[i].
ebeAdd
(double[]
First term of the addition.
a
,
double[]
Second term of the addition.
b
)

Creates an array whose contents will be the element-by-element addition of the arguments.

public static double[]

Returns:

a new array r where r[i] = a[i] / b[i].
ebeDivide
(double[]
Numerator of the division.
a
,
double[]
Denominator of the division.
b
)

Creates an array whose contents will be the element-by-element division of the first argument by the second.

public static double[]

Returns:

a new array r where r[i] = a[i] * b[i].
ebeMultiply
(double[]
First factor of the multiplication.
a
,
double[]
Second factor of the multiplication.
b
)

Creates an array whose contents will be the element-by-element multiplication of the arguments.

public static double[]

Returns:

a new array r where r[i] = a[i] - b[i].
ebeSubtract
(double[]
First term.
a
,
double[]
Element to be subtracted.
b
)

Creates an array whose contents will be the element-by-element subtraction of the second argument from the first.

public static boolean

Returns:

true if the values are both null or have same dimension and equal elements.
equals
(float[]
first array
x
,
float[]
second array
y
)

Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined by Precision#equals(float, float).

public static boolean

Returns:

true if the values are both null or have same dimension and equal elements.
equals
(double[]
First array.
x
,
double[]
Second array.
y
)

Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined by Precision#equals(double, double).

public static boolean

Returns:

true if the values are both null or have same dimension and equal elements
equalsIncludingNaN
(float[]
first array
x
,
float[]
second array
y
)

Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined by this method.

public static boolean

Returns:

true if the values are both null or have same dimension and equal elements.
equalsIncludingNaN
(double[]
First array.
x
,
double[]
Second array.
y
)

Returns true iff both arguments are null or have same dimensions and all their elements are equal as defined by this method.

public static <
the type of the elements in the specified array
T extends Comparable<? super T>
>
boolean

Returns:

true if sorted, false otherwise.
isMonotonic
(T[]
Values.
val
,
MathArrays.OrderDirection
Ordering direction.
dir
,
boolean
Whether the order should be strict.
strict
)

Check that an array is monotonically increasing or decreasing.

public static boolean

Returns:

true if sorted, false otherwise.
isMonotonic
(double[]
Values.
val
,
MathArrays.OrderDirection
Ordering direction.
dir
,
boolean
Whether the order should be strict.
strict
)

Check that an array is monotonically increasing or decreasing.

public static double

Returns:

Σi ai bi.
linearCombination
(final double[]
Factors.
a
,
final double[]
Factors.
b
)

Compute a linear combination accurately.

public static double

Returns:

a1×b1 + a2×b2
linearCombination
(final double
first factor of the first term
a1
,
final double
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final double
second factor of the second term
b2
)

Compute a linear combination accurately.

public static double

Returns:

a1×b1 + a2×b2 + a3×b3
linearCombination
(final double
first factor of the first term
a1
,
final double
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final double
second factor of the second term
b2
,
final double
first factor of the third term
a3
,
final double
second factor of the third term
b3
)

Compute a linear combination accurately.

public static double

Returns:

a1×b1 + a2×b2 + a3×b3 + a4×b4
linearCombination
(final double
first factor of the first term
a1
,
final double
second factor of the first term
b1
,
final double
first factor of the second term
a2
,
final double
second factor of the second term
b2
,
final double
first factor of the third term
a3
,
final double
second factor of the third term
b3
,
final double
first factor of the third term
a4
,
final double
second factor of the third term
b4
)

Compute a linear combination accurately.

public static int[]

Returns:

an array whose entries are the numbers 0, 1, ..., n-1. If n == 0, the returned array is empty.
natural
(int
Natural number.
n
)

Returns an array representing the natural number n.

public static double[]

Returns:

the normalized array.
normalizeArray
(double[]
Input array to be normalized
values
,
double
Target sum for the normalized array
normalizedSum
)

Normalizes an array to make it sum to a specified value.

public static double

Returns:

the 2-norm of the vector.
safeNorm
(double[]
Vector of doubles.
v
)

Returns the Cartesian norm (2-norm), handling both overflow and underflow.

public static double[]

Returns:

scaled copy of array with each entry multiplied by val.
scale
(double
Scalar.
val
,
final double[]
Array to scale.
arr
)

Create a copy of an array scaled by a value.

public static void
scaleInPlace(double
Scalar
val
,
final double[]
Array to scale
arr
)

Multiply each element of an array by a value.

public static int[]

Returns:

an array whose entries are the numbers start, start + stride, ..., start + (size - 1) * stride. If size == 0, the returned array is empty.
sequence
(int
Natural number.
size
,
int
Natural number.
start
,
int
Natural number.
stride
)

Returns an array of size integers starting at start, skipping stride numbers.

public static void
shuffle(int[]
Array whose entries will be shuffled (in-place).
list
,
int
Index at which shuffling begins.
start
,
MathArrays.Position
Shuffling is performed for index positions between start and either the end (if Position#TAIL) or the beginning (if Position#HEAD) of the array.
pos
)

Shuffle the entries of the given array.

public static void
shuffle(int[]
Array whose entries will be shuffled (in-place).
list
,
int
Index at which shuffling begins.
start
,
MathArrays.Position
Shuffling is performed for index positions between start and either the end (if Position#TAIL) or the beginning (if Position#HEAD) of the array.
pos
,
RandomGenerator
Random number generator.
rng
)

Shuffle the entries of the given array, using the Fisher–Yates algorithm.

public static void
shuffle(int[]
Array whose entries will be shuffled (in-place).
list
,
RandomGenerator
Random number generator.
rng
)

Shuffle the entries of the given array.

public static void
shuffle(int[]
Array whose entries will be shuffled (in-place).
list
)

Shuffle the entries of the given array.

public static void
sortInPlace(double[]
Array to be sorted and used as a pattern for permutation of the other arrays.
x
,
double[]...
Set of arrays whose permutations of entries will follow those performed on x.
yList
)

Sort an array in ascending order in place and perform the same reordering of entries on other arrays.

public static void
sortInPlace(double[]
Array to be sorted and used as a pattern for permutation of the other arrays.
x
,
final MathArrays.OrderDirection
Order direction.
dir
,
double[]...
Set of arrays whose permutations of entries will follow those performed on x.
yList
)

Sort an array in place and perform the same reordering of entries on other arrays.

public static double[]

Returns:

descending list of values included in the input array
unique
(double[]
array to scan
data
)

Returns an array consisting of the unique values in data.

public static boolean

Returns:

true if the parameters are valid and designate a subarray of positive length
verifyValues
(final double[]
the input array
values
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
)

This method is used to verify that the input parameters designate a subarray of positive length.

public static boolean

Returns:

true if the parameters are valid
verifyValues
(final double[]
the input array
values
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
length
,
final boolean
if true then zero length arrays are allowed
allowEmpty
)

This method is used to verify that the input parameters designate a subarray of positive length.

public static boolean

Returns:

true if the parameters are valid and designate a subarray of positive length
verifyValues
(final double[]
the input array
values
,
final double[]
the weights array
weights
,
final int
index of the first array element to include
begin
,
final int
the number of elements to include
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.

public static boolean

Returns:

true if the parameters are valid.
verifyValues
(final double[]
the input array.
values
,
final double[]
the weights array.
weights
,
final int
index of the first array element to include.
begin
,
final int
the number of elements to include.
length
,
final boolean
if true than allow zero length arrays to pass.
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait