Package com.irurueta.algebra
Class ArrayUtils
java.lang.Object
com.irurueta.algebra.ArrayUtils
Class containing utility methods for common operations with arrays of values.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic double
angle
(double[] firstOperand, double[] secondOperand) Computes the angle between two vectors.static double
dotProduct
(double[] firstOperand, double[] secondOperand) Computes the dot product of provided arrays as the sum of the product of the elements of both arrays.static double
dotProduct
(double[] firstOperand, double[] secondOperand, Matrix jacobianFirst, Matrix jacobianSecond) Computes the dot product of provided arrays as the sum of the product of the elements of both arrays.static Complex
dotProduct
(Complex[] firstOperand, Complex[] secondOperand) Computes the dot product of provided arrays as the sum of the product of the elements of both arraysprivate static void
internalMultiplyByScalar
(double[] inputArray, double scalar, double[] result) Internal method that multiplied by scalar provided input array without comparing length of input array and result array.private static void
internalMultiplyByScalar
(Complex[] inputArray, double scalar, Complex[] result) Internal method that multiplied by scalar provided input array without comparing length of input array and result array.private static void
internalSubtract
(double[] firstOperand, double[] secondOperand, double[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand).private static void
internalSubtract
(Complex[] firstOperand, Complex[] secondOperand, Complex[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand).private static void
internalSum
(double[] firstOperand, double[] secondOperand, double[] result) Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand).private static void
internalSum
(Complex[] firstOperand, Complex[] secondOperand, Complex[] result) Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand).static double
max
(double[] v) Finds the maximum value into provided array.static double
max
(double[] v, int[] pos) Finds the maximum value into provided array.static double
min
(double[] v) Finds the minimum value into provided array.static double
min
(double[] v, int[] pos) Finds the minimum value into provided array.static void
minMax
(double[] v, double[] result, int[] pos) Finds the minimum and maximum values into provided array and finds their positions.static void
multiplyByScalar
(double[] inputArray, double scalar, double[] result) Multiplies values in provided input array by provided scalar value and stores the result in provided result array.static void
multiplyByScalar
(Complex[] inputArray, double scalar, Complex[] result) Multiplies values in provided input array by provided scalar value and stores the result in provided result array.static double[]
multiplyByScalarAndReturnNew
(double[] inputArray, double scalar) Multiplies values in provided array by provided scalar value and returns the result in a new array.static Complex[]
multiplyByScalarAndReturnNew
(Complex[] inputArray, double scalar) Multiplies values in provided array by provided scalar value and returns the result in a new array.static void
normalize
(double[] v) Normalizes provided array and updates its values.static void
normalize
(double[] v, double[] result) Normalizes provided array.static void
Normalizes provided array and computes corresponding jacobian.static void
Normalizes provided array, updates its values and computes corresponding jacobian.static double[]
normalizeAndReturnNew
(double[] v) Normalizes provided array.static double[]
normalizeAndReturnNew
(double[] v, Matrix jacobian) Normalizes provided array and computes corresponding jacobian.static void
reverse
(double[] v) Reverses provided array.static void
reverse
(double[] v, double[] result) Reverses the order of elements in the array.static void
Reverses provided array.static void
Reverses the order of elements in the array.static double[]
reverseAndReturnNew
(double[] v) Returns a new array containing provided array having its elements in reversed order.static Complex[]
Returns a new array containing provided array having its elements in reversed order.static void
sqrt
(double[] v) Updates provided array by setting on each element its squared root.static void
sqrt
(double[] v, double[] result) Computes the squared root of each element of provided array and sets the result into provided result array.static double[]
sqrtAndReturnNew
(double[] v) Computes the squared root of each element of provided array and returns the result as a new array.static void
subtract
(double[] firstOperand, double[] secondOperand, double[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand).static void
Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand).static double[]
subtractAndReturnNew
(double[] firstOperand, double[] secondOperand) Subtracts provided operands and returns the result as a new array instance.static Complex[]
subtractAndReturnNew
(Complex[] firstOperand, Complex[] secondOperand) Subtracts provided operands and returns the result as a new array instance.static void
sum
(double[] firstOperand, double[] secondOperand, double[] result) Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand).static void
Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand).static double[]
sumAndReturnNew
(double[] firstOperand, double[] secondOperand) Sums provided operands and returns the result as a new array instance.static Complex[]
sumAndReturnNew
(Complex[] firstOperand, Complex[] secondOperand) Sums provided operands and returns the result as a new array instance.
-
Constructor Details
-
ArrayUtils
private ArrayUtils()Constructor.
-
-
Method Details
-
internalMultiplyByScalar
private static void internalMultiplyByScalar(double[] inputArray, double scalar, double[] result) Internal method that multiplied by scalar provided input array without comparing length of input array and result array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.result
- Array where result is stored.- See Also:
-
multiplyByScalar
public static void multiplyByScalar(double[] inputArray, double scalar, double[] result) Multiplies values in provided input array by provided scalar value and stores the result in provided result array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.result
- Array where result is stored.- Throws:
IllegalArgumentException
- Thrown if inputArray length and result array length are not equal.
-
multiplyByScalarAndReturnNew
public static double[] multiplyByScalarAndReturnNew(double[] inputArray, double scalar) Multiplies values in provided array by provided scalar value and returns the result in a new array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.- Returns:
- Result obtained after multiplying input array by provided scalar value.
-
internalSum
private static void internalSum(double[] firstOperand, double[] secondOperand, double[] result) Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand). Summation is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length. This method does not check array lengths.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of summation.
-
sum
public static void sum(double[] firstOperand, double[] secondOperand, double[] result) Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand). Summation is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of summation.- Throws:
IllegalArgumentException
- Raised if not all arrays have the same length.
-
sumAndReturnNew
public static double[] sumAndReturnNew(double[] firstOperand, double[] secondOperand) Sums provided operands and returns the result as a new array instance. Summation is done in an element by element basis.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.- Returns:
- Sum of first and second operands.
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length.
-
internalSubtract
private static void internalSubtract(double[] firstOperand, double[] secondOperand, double[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand). Subtraction is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length. This method does not check array lengths.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of subtraction.
-
subtract
public static void subtract(double[] firstOperand, double[] secondOperand, double[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand). Subtraction is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of subtraction.- Throws:
IllegalArgumentException
- Raised if not all arrays have the same length.
-
subtractAndReturnNew
public static double[] subtractAndReturnNew(double[] firstOperand, double[] secondOperand) Subtracts provided operands and returns the result as a new array instance. Subtraction is done in an element by element basis.- Parameters:
firstOperand
- First operandsecondOperand
- Second operand- Returns:
- Subtraction of first and second operands
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length
-
dotProduct
public static double dotProduct(double[] firstOperand, double[] secondOperand) Computes the dot product of provided arrays as the sum of the product of the elements of both arrays.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.- Returns:
- Dot product.
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length.
-
dotProduct
public static double dotProduct(double[] firstOperand, double[] secondOperand, Matrix jacobianFirst, Matrix jacobianSecond) Computes the dot product of provided arrays as the sum of the product of the elements of both arrays.- Parameters:
firstOperand
- first operand.secondOperand
- second operand.jacobianFirst
- matrix where jacobian of first operand will be stored. Must be a column matrix having the same number of rows as the first operand length.jacobianSecond
- matrix where jacobian of second operand will be stored. Must be a column matrix having the same number of rows as the second operand length.- Returns:
- dot product.
- Throws:
IllegalArgumentException
- if first and second operands don't have the same length or if jacobian matrices are not column vectors having the same length as their respective operands.
-
angle
public static double angle(double[] firstOperand, double[] secondOperand) Computes the angle between two vectors. The angle is defined between 0 and PI.- Parameters:
firstOperand
- first operand.secondOperand
- second operand.- Returns:
- angle between arrays.
- Throws:
IllegalArgumentException
- if first and second operands don't have the same length.
-
internalMultiplyByScalar
Internal method that multiplied by scalar provided input array without comparing length of input array and result array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.result
- Array where result is stored.- See Also:
-
multiplyByScalar
Multiplies values in provided input array by provided scalar value and stores the result in provided result array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.result
- Array where result is stored.- Throws:
IllegalArgumentException
- Thrown if inputArray length and result array length are not equal.
-
multiplyByScalarAndReturnNew
Multiplies values in provided array by provided scalar value and returns the result in a new array.- Parameters:
inputArray
- Array to be multiplied.scalar
- Scalar used for multiplication.- Returns:
- Result obtained after multiplying input array by provided scalar value.
-
internalSum
Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand). Summation is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length. This method does not check array lengths.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of summation.
-
sum
Sums provided operands arrays and stores the result in provided result array (i.e. result = firstOperand + secondOperand). Summation is done in an element by element basis. Provided array result must be initialized. All arrays must have the same length.- Parameters:
firstOperand
- First operand.secondOperand
- Second operand.result
- Result of summation.- Throws:
IllegalArgumentException
- Raised if not all arrays have the same length.
-
sumAndReturnNew
Sums provided operands and returns the result as a new array instance. Summation is done in an element by element basis- Parameters:
firstOperand
- First operandsecondOperand
- Second operand- Returns:
- Sum of first and second operands
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length
-
internalSubtract
private static void internalSubtract(Complex[] firstOperand, Complex[] secondOperand, Complex[] result) Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand). Subtraction is done in an element by element basis Provided array result must be initialized. All arrays must have the same length This method does not check array lengths- Parameters:
firstOperand
- First operandsecondOperand
- Second operandresult
- Result of subtraction
-
subtract
Subtracts provided operands arrays and stores the result in provided result array (i.e. result = firstOperand - secondOperand). Subtraction is done in an element by element basis Provided array result must be initialized. All arrays must have the same length- Parameters:
firstOperand
- First operandsecondOperand
- Second operandresult
- Result of subtraction- Throws:
IllegalArgumentException
- Raised if not all arrays have the same length
-
subtractAndReturnNew
Subtracts provided operands and returns the result as a new array instance. Subtraction is done in an element by element basis- Parameters:
firstOperand
- First operandsecondOperand
- Second operand- Returns:
- Subtraction of first and second operands
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length
-
dotProduct
Computes the dot product of provided arrays as the sum of the product of the elements of both arrays- Parameters:
firstOperand
- First operandsecondOperand
- Second operand- Returns:
- Dot product
- Throws:
IllegalArgumentException
- Raised if first and second operands arrays don't have the same length
-
normalize
Normalizes provided array and computes corresponding jacobian.- Parameters:
v
- array to be normalized.result
- array where result of normalized array will be stored.jacobian
- matrix where jacobian will be stored.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length or if provided jacobian is not NxN, where N is length of arrays.
-
normalizeAndReturnNew
Normalizes provided array and computes corresponding jacobian.- Parameters:
v
- array to be normalized.jacobian
- matrix where jacobian will be stored.- Returns:
- a new array instance containing normalized array.
- Throws:
IllegalArgumentException
- if provided jacobian is not NxN, where N is length of arrays.
-
normalize
Normalizes provided array, updates its values and computes corresponding jacobian.- Parameters:
v
- array to be normalized and updated.jacobian
- matrix where jacobian will be stored.- Throws:
IllegalArgumentException
- if provided jacobian is not NxN, where N is length of arrays.
-
normalize
public static void normalize(double[] v, double[] result) Normalizes provided array.- Parameters:
v
- array to be normalized.result
- array where result of normalized array will be stored.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
normalizeAndReturnNew
public static double[] normalizeAndReturnNew(double[] v) Normalizes provided array.- Parameters:
v
- array to be normalized.- Returns:
- a new array instance containing normalized array.
-
normalize
public static void normalize(double[] v) Normalizes provided array and updates its values.- Parameters:
v
- array to be normalized and updated.
-
reverse
public static void reverse(double[] v, double[] result) Reverses the order of elements in the array.- Parameters:
v
- array to be reversed.result
- instance where results will be stored.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
reverse
public static void reverse(double[] v) Reverses provided array. This method updates provided array to contain its reversed values.- Parameters:
v
- array to be reversed.
-
reverseAndReturnNew
public static double[] reverseAndReturnNew(double[] v) Returns a new array containing provided array having its elements in reversed order.- Parameters:
v
- array to be reversed.- Returns:
- a new instance containing reversed array.
-
reverse
Reverses the order of elements in the array.- Parameters:
v
- array to be reversed.result
- instance where results will be stored.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
reverse
Reverses provided array. This method updates provided array to contain its reversed values.- Parameters:
v
- array to be reversed.
-
reverseAndReturnNew
Returns a new array containing provided array having its elements in reversed order.- Parameters:
v
- array to be reversed.- Returns:
- a new instance containing reversed array.
-
sqrt
public static void sqrt(double[] v, double[] result) Computes the squared root of each element of provided array and sets the result into provided result array.- Parameters:
v
- input array to compute the squared root of each element.result
- array where results will be stored.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
sqrtAndReturnNew
public static double[] sqrtAndReturnNew(double[] v) Computes the squared root of each element of provided array and returns the result as a new array.- Parameters:
v
- input array to compute the squared root of each element.- Returns:
- a new array containing the squared root of each element of input array.
-
sqrt
public static void sqrt(double[] v) Updates provided array by setting on each element its squared root.- Parameters:
v
- input array to be updated with its squared root elements.
-
min
public static double min(double[] v, int[] pos) Finds the minimum value into provided array.- Parameters:
v
- array where minimum must be found.pos
- position where minimum was found. Position will be stored at position zero of the array, if provided.- Returns:
- minimum value.
-
min
public static double min(double[] v) Finds the minimum value into provided array.- Parameters:
v
- array where minimum must be found.- Returns:
- minimum value.
-
max
public static double max(double[] v, int[] pos) Finds the maximum value into provided array.- Parameters:
v
- array where maximum must be found.pos
- position where maximum was found. Position will be stored at position zero of the array, if provided.- Returns:
- maximum value.
-
max
public static double max(double[] v) Finds the maximum value into provided array.- Parameters:
v
- array where maximum must be found.- Returns:
- maximum value.
-
minMax
public static void minMax(double[] v, double[] result, int[] pos) Finds the minimum and maximum values into provided array and finds their positions.- Parameters:
v
- array where minimum and maximum must be found.result
- array of length 2 containing found minimum and maximum values at positions 0 and 1 respectively.pos
- array of length 2 containing positions where minimum and maximum where found in v array. Position 0 will contain minimum position, position 1 will contain maximum position.- Throws:
IllegalArgumentException
- if provided result or pos array don't have length 2.
-