Class MathArrays
- java.lang.Object
-
- org.hipparchus.util.MathArrays
-
public class MathArrays extends Object
Arrays utilities.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MathArrays.Function
Real-valued function that operates 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.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends FieldElement<T>>
T[]buildArray(Field<T> field, int length)
Build an array of elements.static <T extends FieldElement<T>>
T[][]buildArray(Field<T> field, int rows, int columns)
Build a double dimension array of elements.static <T extends FieldElement<T>>
T[][][]buildArray(Field<T> field, int l1, int l2, int l3)
Build a triple 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 <T extends CalculusFieldElement<T>>
voidcheckEqualLength(T[] a, T[] b)
Check that both arrays have the same length.static <T extends CalculusFieldElement<T>>
booleancheckEqualLength(T[] a, T[] 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 isNaN
.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 <T extends CalculusFieldElement<T>>
voidcheckOrder(T[] val)
Check that the given array is sorted in strictly increasing order.static <T extends CalculusFieldElement<T>>
voidcheckOrder(T[] val, MathArrays.OrderDirection dir, boolean strict)
Check that the given array is sorted.static <T extends CalculusFieldElement<T>>
booleancheckOrder(T[] 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 MathIllegalArgumentException 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
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(byte[] x, byte[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.static boolean
equals(double[] x, double[] y)
Returnstrue
iff both arguments arenull
or have same dimensions and all their elements are equal as defined byPrecision.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 byPrecision.equals(float,float)
.static boolean
equals(int[] x, int[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.static boolean
equals(long[] x, long[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.static boolean
equals(short[] x, short[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.static boolean
equalsIncludingNaN(double[] x, double[] y)
Returnstrue
iff both arguments arenull
or have same dimensions and all their elements are equal as defined bythis 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 bythis 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>>
booleanisMonotonic(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 numbern
.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 ofsize
integers starting atstart
, skippingstride
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 indata
.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.
-
-
-
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.
-
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
-
ebeAdd
public static double[] ebeAdd(double[] a, double[] b) throws MathIllegalArgumentException
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
r
wherer[i] = a[i] + b[i]
. - Throws:
MathIllegalArgumentException
- if the array lengths differ.
-
ebeSubtract
public static double[] ebeSubtract(double[] a, double[] b) throws MathIllegalArgumentException
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
r
wherer[i] = a[i] - b[i]
. - Throws:
MathIllegalArgumentException
- if the array lengths differ.
-
ebeMultiply
public static double[] ebeMultiply(double[] a, double[] b) throws MathIllegalArgumentException
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
r
wherer[i] = a[i] * b[i]
. - Throws:
MathIllegalArgumentException
- if the array lengths differ.
-
ebeDivide
public static double[] ebeDivide(double[] a, double[] b) throws MathIllegalArgumentException
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
r
wherer[i] = a[i] / b[i]
. - Throws:
MathIllegalArgumentException
- if the array lengths differ.
-
distance1
public static double distance1(double[] p1, double[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- if the array lengths differ.
-
distance1
public static int distance1(int[] p1, int[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- if the array lengths differ.
-
distance
public static double distance(double[] p1, double[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- if the array lengths differ.
-
cosAngle
public static double cosAngle(double[] v1, double[] v2)
Calculates the cosine of the angle between two vectors.- Parameters:
v1
- Cartesian coordinates of the first vector.v2
- Cartesian coordinates of the second vector.- Returns:
- the cosine of the angle between the vectors.
-
distance
public static double distance(int[] p1, int[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- if the array lengths differ.
-
distanceInf
public static double distanceInf(double[] p1, double[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- if the array lengths differ.
-
distanceInf
public static int distanceInf(int[] p1, int[] p2) throws MathIllegalArgumentException
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:
MathIllegalArgumentException
- 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:
true
if sorted,false
otherwise.
-
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:
true
if sorted,false
otherwise.
-
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:
true
if the arrays have the same length.- Throws:
MathIllegalArgumentException
- if the lengths differ andabort
istrue
.
-
checkEqualLength
public static void checkEqualLength(double[] a, double[] b)
Check that both arrays have the same length.- Parameters:
a
- Array.b
- Array.- Throws:
MathIllegalArgumentException
- if the lengths differ.
-
checkEqualLength
public static <T extends CalculusFieldElement<T>> boolean checkEqualLength(T[] a, T[] b, boolean abort)
Check that both arrays have the same length.- Type Parameters:
T
- the type of the field elements- Parameters:
a
- Array.b
- Array.abort
- Whether to throw an exception if the check fails.- Returns:
true
if the arrays have the same length.- Throws:
MathIllegalArgumentException
- if the lengths differ andabort
istrue
.- Since:
- 1.5
-
checkEqualLength
public static <T extends CalculusFieldElement<T>> void checkEqualLength(T[] a, T[] b)
Check that both arrays have the same length.- Type Parameters:
T
- the type of the field elements- Parameters:
a
- Array.b
- Array.- Throws:
MathIllegalArgumentException
- if the lengths differ.- Since:
- 1.5
-
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:
true
if the arrays have the same length.- Throws:
MathIllegalArgumentException
- if the lengths differ andabort
istrue
.
-
checkEqualLength
public static void checkEqualLength(int[] a, int[] b)
Check that both arrays have the same length.- Parameters:
a
- Array.b
- Array.- Throws:
MathIllegalArgumentException
- if the lengths differ.
-
checkOrder
public static boolean checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort) throws MathIllegalArgumentException
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:
true
if the array is sorted.- Throws:
MathIllegalArgumentException
- if the array is not sorted andabort
istrue
.
-
checkOrder
public static void checkOrder(double[] val, MathArrays.OrderDirection dir, boolean strict) throws MathIllegalArgumentException
Check that the given array is sorted.- Parameters:
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.- Throws:
MathIllegalArgumentException
- if the array is not sorted.
-
checkOrder
public static void checkOrder(double[] val) throws MathIllegalArgumentException
Check that the given array is sorted in strictly increasing order.- Parameters:
val
- Values.- Throws:
MathIllegalArgumentException
- if the array is not sorted.
-
checkOrder
public static <T extends CalculusFieldElement<T>> boolean checkOrder(T[] val, MathArrays.OrderDirection dir, boolean strict, boolean abort) throws MathIllegalArgumentException
Check that the given array is sorted.- Type Parameters:
T
- the type of the field elements- Parameters:
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.abort
- Whether to throw an exception if the check fails.- Returns:
true
if the array is sorted.- Throws:
MathIllegalArgumentException
- if the array is not sorted andabort
istrue
.- Since:
- 1.5
-
checkOrder
public static <T extends CalculusFieldElement<T>> void checkOrder(T[] val, MathArrays.OrderDirection dir, boolean strict) throws MathIllegalArgumentException
Check that the given array is sorted.- Type Parameters:
T
- the type of the field elements- Parameters:
val
- Values.dir
- Ordering direction.strict
- Whether the order should be strict.- Throws:
MathIllegalArgumentException
- if the array is not sorted.- Since:
- 1.5
-
checkOrder
public static <T extends CalculusFieldElement<T>> void checkOrder(T[] val) throws MathIllegalArgumentException
Check that the given array is sorted in strictly increasing order.- Type Parameters:
T
- the type of the field elements- Parameters:
val
- Values.- Throws:
MathIllegalArgumentException
- if the array is not sorted.- Since:
- 1.5
-
checkRectangular
public static void checkRectangular(long[][] in) throws MathIllegalArgumentException, NullArgumentException
Throws MathIllegalArgumentException if the input array is not rectangular.- Parameters:
in
- array to be tested- Throws:
NullArgumentException
- if input array is nullMathIllegalArgumentException
- if input array is not rectangular
-
checkPositive
public static void checkPositive(double[] in) throws MathIllegalArgumentException
Check that all entries of the input array are strictly positive.- Parameters:
in
- Array to be tested- Throws:
MathIllegalArgumentException
- if any entries of the array are not strictly positive.
-
checkNotNaN
public static void checkNotNaN(double[] in) throws MathIllegalArgumentException
Check that no entry of the input array isNaN
.- Parameters:
in
- Array to be tested.- Throws:
MathIllegalArgumentException
- if an entry isNaN
.
-
checkNonNegative
public static void checkNonNegative(long[] in) throws MathIllegalArgumentException
Check that all entries of the input array are >= 0.- Parameters:
in
- Array to be tested- Throws:
MathIllegalArgumentException
- if any array entries are less than 0.
-
checkNonNegative
public static void checkNonNegative(long[][] in) throws MathIllegalArgumentException
Check all entries of the input array are >= 0.- Parameters:
in
- Array to be tested- Throws:
MathIllegalArgumentException
- if any array entries are less than 0.
-
safeNorm
public static double safeNorm(double[] v)
Returns the Cartesian norm (2-norm), handling both overflow and underflow. Translation of the minpack enorm subroutine.The redistribution policy for MINPACK is available here, for convenience, it is reproduced below.
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:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:
This product includes software developed by the University of Chicago, as Operator of Argonne National Laboratory.
Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. - WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL BE CORRECTED.
- LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGES.
- Parameters:
v
- Vector of doubles.- Returns:
- the 2-norm of the vector.
-
sortInPlace
public static void sortInPlace(double[] x, double[]... yList) throws MathIllegalArgumentException, NullArgumentException
Sort an array in ascending order in place and perform the same reordering of entries on other arrays. For example, ifx = [3, 1, 2], y = [1, 2, 3]
andz = [0, 5, 7]
, thensortInPlace(x, y, z)
will updatex
to[1, 2, 3]
,y
to[2, 3, 1]
andz
to[5, 7, 0]
.- Parameters:
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 onx
.- Throws:
MathIllegalArgumentException
- if anyy
is not the same size asx
.NullArgumentException
- ifx
or anyy
is null.
-
sortInPlace
public static void sortInPlace(double[] x, MathArrays.OrderDirection dir, double[]... yList) throws MathIllegalArgumentException, NullArgumentException
Sort an array in place and perform the same reordering of entries on other arrays. This method works the same as the othersortInPlace
method, but allows the order of the sort to be provided in thedir
parameter.- Parameters:
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 onx
.- Throws:
MathIllegalArgumentException
- if anyy
is not the same size asx
.NullArgumentException
- ifx
or anyy
is null
-
linearCombination
public static double linearCombination(double[] a, double[] b) throws MathIllegalArgumentException
Compute a linear combination accurately. This method computes the sum of the productsai bi
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.- Parameters:
a
- Factors.b
- Factors.- Returns:
Σi ai bi
.- Throws:
MathIllegalArgumentException
- if arrays dimensions don't match
-
linearCombination
public static double linearCombination(double a1, double b1, double a2, double b2)
Compute a linear combination accurately.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.
- Parameters:
a1
- first factor of the first termb1
- second factor of the first terma2
- first factor of the second termb2
- second factor of the second term- Returns:
- a1×b1 + a2×b2
- See Also:
linearCombination(double, double, double, double, double, double)
,linearCombination(double, double, double, double, double, double, double, double)
-
linearCombination
public static double linearCombination(double a1, double b1, double a2, double b2, double a3, double b3)
Compute a linear combination accurately.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.
- Parameters:
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 term- Returns:
- a1×b1 + a2×b2 + a3×b3
- See Also:
linearCombination(double, double, double, double)
,linearCombination(double, double, double, double, double, double, double, double)
-
linearCombination
public static double linearCombination(double a1, double b1, double a2, double b2, double a3, double b3, double a4, double b4)
Compute a linear combination accurately.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.
- Parameters:
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 term- Returns:
- a1×b1 + a2×b2 + a3×b3 + a4×b4
- See Also:
linearCombination(double, double, double, double)
,linearCombination(double, double, double, double, double, double)
-
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
-
equals
public static boolean equals(double[] x, double[] y)
Returnstrue
iff both arguments arenull
or have same dimensions and all their elements are equal as defined byPrecision.equals(double,double)
.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
equalsIncludingNaN
public static boolean equalsIncludingNaN(double[] x, double[] y)
Returnstrue
iff both arguments arenull
or have same dimensions and all their elements are equal as defined bythis method
.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
equals
public static boolean equals(long[] x, long[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
equals
public static boolean equals(int[] x, int[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
equals
public static boolean equals(byte[] x, byte[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
equals
public static boolean equals(short[] x, short[] y)
Returnstrue
if both arguments arenull
or have same dimensions and all their elements are equals.- Parameters:
x
- First array.y
- Second array.- Returns:
true
if the values are bothnull
or have same dimension and equal elements.
-
normalizeArray
public static double[] normalizeArray(double[] values, double normalizedSum) throws MathIllegalArgumentException, MathRuntimeException
Normalizes an array to make it sum to a specified value. Returns the result of the transformationx ↦ x * normalizedSum / sum
applied 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. The input array is unchanged by this method.
- Parameters:
values
- Input array to be normalizednormalizedSum
- Target sum for the normalized array- Returns:
- the normalized array
- Throws:
MathRuntimeException
- if the input array contains infinite elements or sums to zeroMathIllegalArgumentException
- if the target sum is infinite orNaN
-
buildArray
public static <T extends FieldElement<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
-
buildArray
public static <T extends FieldElement<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
-
buildArray
public static <T extends FieldElement<T>> T[][][] buildArray(Field<T> field, int l1, int l2, int l3)
Build a triple 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 belongl1
- number of elements along first dimensionl2
- number of elements along second dimensionl3
- number of elements along third dimension (may be negative to build partial arrays in the same waynew Field[l1][l2][]
works)- Returns:
- a new array
- Since:
- 1.4
-
convolve
public static double[] convolve(double[] x, double[] h) throws MathIllegalArgumentException, NullArgumentException
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
x
andh
. This array's length will bex.length + h.length - 1
. - Throws:
NullArgumentException
- if eitherx
orh
isnull
.MathIllegalArgumentException
- if eitherx
orh
is empty.
-
shuffle
public static void shuffle(int[] list, int start, MathArrays.Position pos)
Shuffle the entries of the given array. Thestart
andpos
parameters select which portion of the array is randomized and which is left untouched.- Parameters:
list
- Array whose entries will be shuffled (in-place).start
- Index at which shuffling begins.pos
- Shuffling is performed for index positions betweenstart
and either the end (ifMathArrays.Position.TAIL
) or the beginning (ifMathArrays.Position.HEAD
) of the array.- See Also:
shuffle(int[],int,Position,RandomGenerator)
-
shuffle
public static void shuffle(int[] list, int start, MathArrays.Position pos, RandomGenerator rng)
Shuffle the entries of the given array, using the Fisher–Yates algorithm. Thestart
andpos
parameters select which portion of the array is randomized and which is left untouched.- Parameters:
list
- Array whose entries will be shuffled (in-place).start
- Index at which shuffling begins.pos
- Shuffling is performed for index positions betweenstart
and either the end (ifMathArrays.Position.TAIL
) or the beginning (ifMathArrays.Position.HEAD
) of the array.rng
- Random number generator.
-
shuffle
public static void shuffle(int[] list, RandomGenerator rng)
Shuffle the entries of the given array.- Parameters:
list
- Array whose entries will be shuffled (in-place).rng
- Random number generator.- See Also:
shuffle(int[],int,Position,RandomGenerator)
-
shuffle
public static void shuffle(int[] list)
Shuffle the entries of the given array.- Parameters:
list
- Array whose entries will be shuffled (in-place).- See Also:
shuffle(int[],int,Position,RandomGenerator)
-
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 ofsize
integers starting atstart
, skippingstride
numbers.- 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.
-
verifyValues
public static boolean verifyValues(double[] values, int begin, int length) throws MathIllegalArgumentException
This method is used to verify that the input parameters designate a subarray of positive length.- returns
true
iff the parameters designate a subarray of positive length - throws
MathIllegalArgumentException
if the array is null or or the indices are invalid - returns
false
if the array is non-null, butlength
is 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
- returns
-
verifyValues
public static boolean verifyValues(double[] values, int begin, int length, boolean allowEmpty) throws MathIllegalArgumentException
This method is used to verify that the input parameters designate a subarray of positive length.- returns
true
iff the parameters designate a subarray of non-negative length - throws
IllegalArgumentException
if the array is null or or the indices are invalid - returns
false
if the array is non-null, butlength
is 0 unlessallowEmpty
istrue
- Parameters:
values
- the input arraybegin
- index of the first array element to includelength
- the number of elements to includeallowEmpty
- iftrue
then zero length arrays are allowed- Returns:
- true if the parameters are valid
- Throws:
MathIllegalArgumentException
- if the indices are invalid or the array is null
- returns
-
verifyValues
public static boolean verifyValues(double[] values, double[] weights, int begin, int length) throws MathIllegalArgumentException
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
true
iff the parameters designate a subarray of positive length and the weights array contains legitimate values. - throws
IllegalArgumentException
if 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 start and length arguments do not determine a valid array
- returns
false
if the array is non-null, butlength
is 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
- returns
-
verifyValues
public static boolean verifyValues(double[] values, double[] weights, int begin, int length, boolean allowEmpty) throws MathIllegalArgumentException
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
true
iff the parameters designate a subarray of non-negative length and the weights array contains legitimate values. - throws
MathIllegalArgumentException
if 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 start and length arguments do not determine a valid array
- returns
false
if the array is non-null, butlength
is 0 unlessallowEmpty
istrue
- 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
- iftrue
than allow zero length arrays to pass.- Returns:
true
if 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.
- 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
-
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.NaN
will 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
-
-