public class MathArrays extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
MathArrays.Function
Real-valued function that operate on an array or a part of it.
|
static class |
MathArrays.OrderDirection
Specification of ordering direction.
|
static class |
MathArrays.Position
Specification for indicating that some operation applies
before or after a given index.
|
Modifier and Type | Method and 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 void |
checkEqualLength(double[] a,
double[] b)
Check that both arrays have the same length.
|
static boolean |
checkEqualLength(double[] a,
double[] b,
boolean abort)
Check that both arrays have the same length.
|
static void |
checkEqualLength(int[] a,
int[] b)
Check that both arrays have the same length.
|
static boolean |
checkEqualLength(int[] a,
int[] b,
boolean abort)
Check that both arrays have the same length.
|
static void |
checkNonNegative(long[] in)
Check that all entries of the input array are >= 0.
|
static void |
checkNonNegative(long[][] in)
Check all entries of the input array are >= 0.
|
static void |
checkNotNaN(double[] in)
Check that no entry of the input array is
NaN . |
static void |
checkOrder(double[] val)
Check that the given array is sorted in strictly increasing order.
|
static void |
checkOrder(double[] val,
MathArrays.OrderDirection dir,
boolean strict)
Check that the given array is sorted.
|
static boolean |
checkOrder(double[] val,
MathArrays.OrderDirection dir,
boolean strict,
boolean abort)
Check that the given array is sorted.
|
static void |
checkPositive(double[] in)
Check that all entries of the input array are strictly positive.
|
static void |
checkRectangular(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 double[] |
copyOf(double[] source)
Creates a copy of the
source array. |
static double[] |
copyOf(double[] source,
int len)
Creates a copy of the
source array. |
static int[] |
copyOf(int[] source)
Creates a copy of the
source array. |
static int[] |
copyOf(int[] source,
int len)
Creates a copy of the
source array. |
static double[] |
copyOfRange(double[] source,
int from,
int to)
Creates a copy of the
source array. |
static double |
cosAngle(double[] v1,
double[] v2)
Calculates the cosine of the angle between two vectors.
|
static double |
distance(double[] p1,
double[] p2)
Calculates the L2 (Euclidean) distance between two points.
|
static double |
distance(int[] p1,
int[] p2)
Calculates the L2 (Euclidean) distance between two points.
|
static double |
distance1(double[] p1,
double[] p2)
Calculates the L1 (sum of abs) distance between two points.
|
static int |
distance1(int[] p1,
int[] p2)
Calculates the L1 (sum of abs) distance between two points.
|
static double |
distanceInf(double[] p1,
double[] p2)
Calculates the L∞ (max of abs) distance between two points.
|
static int |
distanceInf(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 boolean |
equals(double[] x,
double[] 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) . |
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 by
Precision.equals(float,float) . |
static boolean |
equalsIncludingNaN(double[] x,
double[] y)
Returns
true iff both arguments are null or have same
dimensions and all their elements are equal as defined by
this method . |
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 by
this method . |
static boolean |
isMonotonic(double[] val,
MathArrays.OrderDirection dir,
boolean strict)
Check that an array is monotonically increasing or decreasing.
|
static <T extends Comparable<? super T>> |
isMonotonic(T[] val,
MathArrays.OrderDirection dir,
boolean strict)
Check that an array is monotonically increasing or decreasing.
|
static double |
linearCombination(double[] a,
double[] b)
Compute a linear combination accurately.
|
static double |
linearCombination(double a1,
double b1,
double a2,
double b2)
Compute a linear combination accurately.
|
static double |
linearCombination(double a1,
double b1,
double a2,
double b2,
double a3,
double b3)
Compute a linear combination accurately.
|
static double |
linearCombination(double a1,
double b1,
double a2,
double b2,
double a3,
double b3,
double a4,
double b4)
Compute a linear combination accurately.
|
static int[] |
natural(int n)
Returns an array representing the natural number
n . |
static double[] |
normalizeArray(double[] values,
double normalizedSum)
Normalizes an array to make it sum to a specified value.
|
static double |
safeNorm(double[] v)
Returns the Cartesian norm (2-norm), handling both overflow and underflow.
|
static double[] |
scale(double val,
double[] arr)
Create a copy of an array scaled by a value.
|
static void |
scaleInPlace(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 of
size integers starting at start ,
skipping stride numbers. |
static void |
shuffle(int[] list)
Shuffle the entries of the given array.
|
static void |
shuffle(int[] list,
int start,
MathArrays.Position pos)
Shuffle the entries of the given array.
|
static void |
shuffle(int[] list,
int start,
MathArrays.Position pos,
RandomGenerator rng)
Shuffle the entries of the given array, using the
Fisher–Yates algorithm.
|
static void |
shuffle(int[] list,
RandomGenerator rng)
Shuffle the entries of the given array.
|
static void |
sortInPlace(double[] x,
double[]... yList)
Sort an array in ascending order in place and perform the same reordering
of entries on other arrays.
|
static void |
sortInPlace(double[] x,
MathArrays.OrderDirection dir,
double[]... yList)
Sort an array in place and perform the same reordering of entries on
other arrays.
|
static double[] |
unique(double[] data)
Returns an array consisting of the unique values in
data . |
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.
|
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.
|
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.
|
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.
|
public static double[] scale(double val, double[] arr)
arr
- Array to scale.val
- Scalar.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).
arr
- Array to scaleval
- Scalarpublic static double[] ebeAdd(double[] a, double[] b) throws DimensionMismatchException
a
- First term of the addition.b
- Second term of the addition.r
where r[i] = a[i] + b[i]
.DimensionMismatchException
- if the array lengths differ.public static double[] ebeSubtract(double[] a, double[] b) throws DimensionMismatchException
a
- First term.b
- Element to be subtracted.r
where r[i] = a[i] - b[i]
.DimensionMismatchException
- if the array lengths differ.public static double[] ebeMultiply(double[] a, double[] b) throws DimensionMismatchException
a
- First factor of the multiplication.b
- Second factor of the multiplication.r
where r[i] = a[i] * b[i]
.DimensionMismatchException
- if the array lengths differ.public static double[] ebeDivide(double[] a, double[] b) throws DimensionMismatchException
a
- Numerator of the division.b
- Denominator of the division.r
where r[i] = a[i] / b[i]
.DimensionMismatchException
- if the array lengths differ.public static double distance1(double[] p1, double[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static int distance1(int[] p1, int[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static double distance(double[] p1, double[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static double cosAngle(double[] v1, double[] v2)
v1
- Cartesian coordinates of the first vector.v2
- Cartesian coordinates of the second vector.public static double distance(int[] p1, int[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static double distanceInf(double[] p1, double[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static int distanceInf(int[] p1, int[] p2) throws DimensionMismatchException
p1
- the first pointp2
- the second pointDimensionMismatchException
- if the array lengths differ.public static <T extends Comparable<? super T>> boolean isMonotonic(T[] val, MathArrays.OrderDirection dir, boolean strict)
T
- the type of the elements in the specified arrayval
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.true
if sorted, false
otherwise.public static boolean isMonotonic(double[] val, MathArrays.OrderDirection dir, boolean strict)
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.true
if sorted, false
otherwise.public static boolean checkEqualLength(double[] a, double[] b, boolean abort)
a
- Array.b
- Array.abort
- Whether to throw an exception if the check fails.true
if the arrays have the same length.DimensionMismatchException
- if the lengths differ and
abort
is true
.public static void checkEqualLength(double[] a, double[] b)
a
- Array.b
- Array.DimensionMismatchException
- if the lengths differ.public static boolean checkEqualLength(int[] a, int[] b, boolean abort)
a
- Array.b
- Array.abort
- Whether to throw an exception if the check fails.true
if the arrays have the same length.DimensionMismatchException
- if the lengths differ and
abort
is true
.public static void checkEqualLength(int[] a, int[] b)
a
- Array.b
- Array.DimensionMismatchException
- if the lengths differ.public static boolean checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort) throws NonMonotonicSequenceException
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.abort
- Whether to throw an exception if the check fails.true
if the array is sorted.NonMonotonicSequenceException
- if the array is not sorted
and abort
is true
.public static void checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict) throws NonMonotonicSequenceException
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.NonMonotonicSequenceException
- if the array is not sorted.public static void checkOrder(double[] val) throws NonMonotonicSequenceException
val
- Values.NonMonotonicSequenceException
- if the array is not sorted.public static void checkRectangular(long[][] in) throws NullArgumentException, DimensionMismatchException
in
- array to be testedNullArgumentException
- if input array is nullDimensionMismatchException
- if input array is not rectangularpublic static void checkPositive(double[] in) throws NotStrictlyPositiveException
in
- Array to be testedNotStrictlyPositiveException
- if any entries of the array are not
strictly positive.public static void checkNotNaN(double[] in) throws NotANumberException
NaN
.in
- Array to be tested.NotANumberException
- if an entry is NaN
.public static void checkNonNegative(long[] in) throws NotPositiveException
in
- Array to be testedNotPositiveException
- if any array entries are less than 0.public static void checkNonNegative(long[][] in) throws NotPositiveException
in
- Array to be testedNotPositiveException
- if any array entries are less than 0.public static double safeNorm(double[] v)
Minpack Copyright Notice (1999) University of Chicago. All rights reserved |
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
|
v
- Vector of doubles.public static void sortInPlace(double[] x, double[]... yList) throws DimensionMismatchException, NullArgumentException
x = [3, 1, 2], y = [1, 2, 3]
and z = [0, 5, 7]
, then
sortInPlace(x, y, z)
will update x
to [1, 2, 3]
,
y
to [2, 3, 1]
and z
to [5, 7, 0]
.x
- Array to be sorted and used as a pattern for permutation
of the other arrays.yList
- Set of arrays whose permutations of entries will follow
those performed on x
.DimensionMismatchException
- if any y
is not the same
size as x
.NullArgumentException
- if x
or any y
is null.public static void sortInPlace(double[] x, MathArrays.OrderDirection dir, double[]... yList) throws NullArgumentException, DimensionMismatchException
sortInPlace
method, but
allows the order of the sort to be provided in the dir
parameter.x
- Array to be sorted and used as a pattern for permutation
of the other arrays.dir
- Order direction.yList
- Set of arrays whose permutations of entries will follow
those performed on x
.DimensionMismatchException
- if any y
is not the same
size as x
.NullArgumentException
- if x
or any y
is nullpublic static int[] copyOf(int[] source)
source
array.source
- Array to be copied.public static double[] copyOf(double[] source)
source
array.source
- Array to be copied.public static int[] copyOf(int[] source, int len)
source
array.source
- Array to be copied.len
- Number of entries to copy. If smaller then the source
length, the copy will be truncated, if larger it will padded with
zeroes.public static double[] copyOf(double[] source, int len)
source
array.source
- Array to be copied.len
- Number of entries to copy. If smaller then the source
length, the copy will be truncated, if larger it will padded with
zeroes.public static double[] copyOfRange(double[] source, int from, int to)
source
array.source
- Array to be copied.from
- Initial index of the range to be copied, inclusive.to
- Final index of the range to be copied, exclusive. (This index may lie outside the array.)public static double linearCombination(double[] a, double[] b) throws DimensionMismatchException
ai bi
to high accuracy.
It does so by using specific multiplication and addition algorithms to
preserve accuracy and reduce cancellation effects.
a
- Factors.b
- Factors.Σi ai bi
.DimensionMismatchException
- if arrays dimensions don't matchpublic static double linearCombination(double a1, double b1, double a2, double b2)
This method computes a1×b1 + a2×b2 to high accuracy. It does so by using specific multiplication and addition algorithms to preserve accuracy and reduce cancellation effects. It is based on the 2005 paper Accurate Sum and Dot Product by Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput.
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second termlinearCombination(double, double, double, double, double, double)
,
linearCombination(double, double, double, double, double, double, double, double)
public static double linearCombination(double a1, double b1, double a2, double b2, double a3, double b3)
This method computes a1×b1 + a2×b2 + a3×b3 to high accuracy. It does so by using specific multiplication and addition algorithms to preserve accuracy and reduce cancellation effects. It is based on the 2005 paper Accurate Sum and Dot Product by Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput.
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third termlinearCombination(double, double, double, double)
,
linearCombination(double, double, double, double, double, double, double, double)
public static double linearCombination(double a1, double b1, double a2, double b2, double a3, double b3, double a4, double b4)
This method computes a1×b1 + a2×b2 + a3×b3 + a4×b4 to high accuracy. It does so by using specific multiplication and addition algorithms to preserve accuracy and reduce cancellation effects. It is based on the 2005 paper Accurate Sum and Dot Product by Takeshi Ogita, Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput.
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second terma3
- first factor of the third termb3
- second factor of the third terma4
- first factor of the third termb4
- second factor of the third termlinearCombination(double, double, double, double)
,
linearCombination(double, double, double, double, double, double)
public static boolean equals(float[] x, float[] y)
Precision.equals(float,float)
.x
- first arrayy
- second arraypublic static boolean equalsIncludingNaN(float[] x, float[] y)
this method
.x
- first arrayy
- second arraypublic static boolean equals(double[] x, double[] y)
true
iff both arguments are null
or have same
dimensions and all their elements are equal as defined by
Precision.equals(double,double)
.x
- First array.y
- Second array.true
if the values are both null
or have same
dimension and equal elements.public static boolean equalsIncludingNaN(double[] x, double[] y)
true
iff both arguments are null
or have same
dimensions and all their elements are equal as defined by
this method
.x
- First array.y
- Second array.true
if the values are both null
or have same
dimension and equal elements.public static double[] normalizeArray(double[] values, double normalizedSum) throws MathIllegalArgumentException, MathArithmeticException
x |-> 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 normalizedSum
is 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.
values
- Input array to be normalizednormalizedSum
- Target sum for the normalized arrayMathArithmeticException
- if the input array contains infinite
elements or sums to zero.MathIllegalArgumentException
- if the target sum is infinite or NaN
.public static <T> T[] buildArray(Field<T> field, int length)
Arrays are filled with field.getZero()
T
- the type of the field elementsfield
- field to which array elements belonglength
- of the arraypublic static <T> T[][] buildArray(Field<T> field, int rows, int columns)
Arrays are filled with field.getZero()
T
- the type of the field elementsfield
- 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 way new Field[rows][]
works)public static double[] convolve(double[] x, double[] h) throws NullArgumentException, NoDataException
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.
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.x
and h
.
This array's length will be x.length + h.length - 1
.NullArgumentException
- if either x
or h
is null
.NoDataException
- if either x
or h
is empty.public static void shuffle(int[] list, int start, MathArrays.Position pos)
start
and pos
parameters select which portion
of the array is randomized and which is left untouched.list
- Array whose entries will be shuffled (in-place).start
- Index at which shuffling begins.pos
- Shuffling is performed for index positions between
start
and either the end (if MathArrays.Position.TAIL
)
or the beginning (if MathArrays.Position.HEAD
) of the array.shuffle(int[],int,Position,RandomGenerator)
public static void shuffle(int[] list, int start, MathArrays.Position pos, RandomGenerator rng)
start
and pos
parameters select which portion
of the array is randomized and which is left untouched.list
- Array whose entries will be shuffled (in-place).start
- Index at which shuffling begins.pos
- Shuffling is performed for index positions between
start
and either the end (if MathArrays.Position.TAIL
)
or the beginning (if MathArrays.Position.HEAD
) of the array.rng
- Random number generator.public static void shuffle(int[] list, RandomGenerator rng)
list
- Array whose entries will be shuffled (in-place).rng
- Random number generator.shuffle(int[],int,Position,RandomGenerator)
public static void shuffle(int[] list)
list
- Array whose entries will be shuffled (in-place).shuffle(int[],int,Position,RandomGenerator)
public static int[] natural(int n)
n
.n
- Natural number.n
-1.
If n == 0
, the returned array is empty.public static int[] sequence(int size, int start, int stride)
size
integers starting at start
,
skipping stride
numbers.size
- Natural number.start
- Natural number.stride
- Natural number.start, start + stride, ..., start + (size - 1) * stride
.
If size == 0
, the returned array is empty.public static boolean verifyValues(double[] values, int begin, int length) throws MathIllegalArgumentException
true
iff the parameters designate a subarray of
positive lengthMathIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0.
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeMathIllegalArgumentException
- if the indices are invalid or the array is nullpublic static boolean verifyValues(double[] values, int begin, int length, boolean allowEmpty) throws MathIllegalArgumentException
true
iff the parameters designate a subarray of
non-negative lengthIllegalArgumentException
if the array is null or
or the indices are invalidfalse
length
is 0 unless allowEmpty
is true
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeallowEmpty
- if true
then zero length arrays are allowedMathIllegalArgumentException
- if the indices are invalid or the array is nullpublic static boolean verifyValues(double[] values, double[] weights, int begin, int length) throws MathIllegalArgumentException
true
iff the parameters designate a subarray of
positive length and the weights array contains legitimate values.IllegalArgumentException
if any of the following are true:
false
length
is 0.
values
- the input arrayweights
- the weights arraybegin
- index of the first array element to includelength
- the number of elements to includeMathIllegalArgumentException
- if the indices are invalid or the array is nullpublic static boolean verifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty) throws MathIllegalArgumentException
true
iff the parameters designate a subarray of
non-negative length and the weights array contains legitimate values.MathIllegalArgumentException
if any of the following are true:
false
length
is 0 unless allowEmpty
is true
.
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
- if true
than allow zero length arrays to pass.true
if the parameters are valid.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.public static double[] concatenate(double[]... x)
x
- list of double[] arrays to concatenateNullPointerException
- if any of the arrays are nullpublic static double[] unique(double[] data)
data
.
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 in data
,
Double.NaN
will be the first element of the output array,
even if the array also contains Double.POSITIVE_INFINITY
.data
- array to scanNullPointerException
- if data is nullCopyright © 2003–2016 The Apache Software Foundation. All rights reserved.