public final 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.
|
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 |
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 |
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[] |
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 |
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 int[] |
sequence(int size,
int start,
int stride)
Returns an array of
size integers starting at start ,
skipping stride numbers. |
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.
|
public static double[] ebeAdd(double[] a, double[] b)
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)
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)
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)
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)
p1
- the first pointp2
- the second pointpublic static int distance1(int[] p1, int[] p2)
p1
- the first pointp2
- the second pointpublic static double distance(double[] p1, double[] p2)
p1
- the first pointp2
- the second pointpublic static double distance(int[] p1, int[] p2)
p1
- the first pointp2
- the second pointpublic static double distanceInf(double[] p1, double[] p2)
p1
- the first pointp2
- the second pointpublic static int distanceInf(int[] p1, int[] p2)
p1
- the first pointp2
- the second pointpublic 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 checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort)
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)
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)
val
- Values.NonMonotonicSequenceException
- if the array is not sorted.public static void checkRectangular(long[][] in)
in
- array to be testedNullArgumentException
- if input array is nullDimensionMismatchException
- if input array is not rectangularpublic static void checkPositive(double[] in)
in
- Array to be testedNotStrictlyPositiveException
- if any entries of the array are not
strictly positive.public static void checkNonNegative(long[] in)
in
- Array to be testedNotPositiveException
- if any array entries are less than 0.public static void checkNonNegative(long[][] in)
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)
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)
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 linearCombination(double[] a, double[] b)
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)
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 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.Copyright © 2017 CNES. All rights reserved.