org.knime.base.util.math

## Class MathUtils

• ```public final class MathUtils
extends Object```
Implements basic mathematical functions.
Author:
Christoph Sieb, University of Konstanz
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static double[][]` ```add(double[][] matrix1, double[][] matrix2)```
`static void` ```addArrays(double[] firstArray, double[] secondArray)```
Adds the second array to the first one.
`static void` ```addMatrix(double[][] firstMatrix, double[][] secondMatrix)```
Adds the second matrix to the first one.
`static double[][]` ```denormalizeMatrix(double[][] y, double[] mean)```
Denormalizes the matrix relativ to the mean of the input data.
`static double[][]` ```denormalizeMatrix(double[][] y, double[] standardDev, double[] mean)```
Denormalizes the matrix relative to the mean of the input data and to the standard deviation.
`static double[]` ```denormalizeVector(double[] vector, double mean)```
Denormalizes the vector relative to the mean of the input data.
`static double[]` ```denormalizeVector(double[] vector, double standardDev, double mean)```
Denormalizes the vector relative to the mean of the input data and to the standard deviation.
`static double` ```dotProduct(double[] vector1, double[] vector2)```
Computes dot product of two vectors.
`static double` ```hypotenuse(double x, double y)```
Calculates sqrt(x^2 + y^2) reducing the risk of over- or underflow.
`static double[][]` `inverse(double[][] aOrig)`
Calculates the inverse matrix of a given matrix.
`static double[]` ```multiply(double[][] matrix, double[] vector)```
Multiplies matrix with a column vector.
`static double[][]` ```multiply(double[][] matrix1, double[][] matrix2)```
Multiplies two matrices.
`static double[][]` `multiplyLeftWithTranspose(double[][] mat)`
Multiplies a matrix with its transposed matrix.
`static double[][]` `normalizeMatrix(double[][] matrix)`
Normalizes the matrix relative to the mean and standard deviation of the input data.
`static double[][]` ```normalizeMatrix(double[][] matrix, double[] mean)```
Normalizes the matrix relative to the mean of the input data.
`static double[][]` ```normalizeMatrix(double[][] matrix, double[] standardDev, double[] mean)```
Normalizes the matrix relative to the mean of the input data and to the standard deviation.
`static double` `spectralNorm(double[][] matrix)`
Computes the spectral norm of the given matrix.
`static double[][]` ```subtract(double[][] matrix1, double[][] matrix2)```
Subtracts two matrices.
`static double[]` ```subtract(double[] vector1, double[] vector2)```
Subtracts one vector from another.
`static double[][]` `transpose(double[][] inputMatrix)`
Transposes the given matrix.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Method Detail

• #### hypotenuse

```public static double hypotenuse(double x,
double y)```
Calculates sqrt(x^2 + y^2) reducing the risk of over- or underflow. The default equation is transformed as follows:
result = sqrt(x^2 + y^2)
result^2 = x^2 + y^2
result^2 = x^2 * (1 + y^2/x^2)
result^2 = x^2 * (1 + (y/x)^2)
result = |x| * sqrt(1 + (y/x)^2)
It is important to perform a case differentiation. The formula is transformed the same way but by extracting y instead of x. The advantage is that the ^2 is performed on a mostly smaller number due to the division.
Parameters:
`x` - the x value
`y` - the y value
Returns:
sqrt(x^2 + y^2)
• #### multiply

```public static double[][] multiply(double[][] matrix1,
double[][] matrix2)
throws IllegalArgumentException```
Multiplies two matrices. Matrix 1 is multiplied from the left to matrix 2. Therefore, result matrix = matrix 1 * matrix 2. The matrices must be compatible, i.e. the number of columns of matrix 1 must equal to the number of rows of matrix 2.
Parameters:
`matrix1` - the matrix on the left side
`matrix2` - the matrix on the right side
Returns:
the result matrix
Throws:
`IllegalArgumentException` - if the matrices are not compatible

```public static double[][] add(double[][] matrix1,
double[][] matrix2)
throws IllegalArgumentException```
Adds two matrices. The matrices must have the same column and row count. The returned matrix object is a new object (no changes to argument arrays are done).
Parameters:
`matrix1` - the matrix on the left side
`matrix2` - the matrix on the right side
Returns:
the result matrix
Throws:
`IllegalArgumentException` - if the matrices are not compatible
• #### subtract

```public static double[][] subtract(double[][] matrix1,
double[][] matrix2)
throws IllegalArgumentException```
Subtracts two matrices. The matrices must have the same column and row count. The returned matrix object is a new object (no changes to argument arrays are done).
Parameters:
`matrix1` - the matrix on the left side
`matrix2` - the matrix on the right side
Returns:
the result matrix
Throws:
`IllegalArgumentException` - if the matrices are not compatible
Since:
2.8
• #### multiply

```public static double[] multiply(double[][] matrix,
double[] vector)
throws IllegalArgumentException```
Multiplies matrix with a column vector. Matrix is multiplied from the left to vector. Therefore, result matrix = matrix * vector. The matrix and the vector must be compatible, i.e. the number of columns of the matrix must equal to the length of the vector.
Parameters:
`matrix` - the matrix on the left side
`vector` - the vector on the right side
Returns:
the result vector
Throws:
`IllegalArgumentException` - if the objects are not compatible
• #### subtract

```public static double[] subtract(double[] vector1,
double[] vector2)```
Subtracts one vector from another.
Parameters:
`vector1` - Vector 1
`vector2` - Vector 2
Returns:
Vector resulting from subtraction
Since:
2.8
• #### dotProduct

```public static double dotProduct(double[] vector1,
double[] vector2)```
Computes dot product of two vectors.
Parameters:
`vector1` - Vector 1
`vector2` - Vector 2
Returns:
Dot product of the two vectors.
Since:
2.8
• #### multiplyLeftWithTranspose

`public static double[][] multiplyLeftWithTranspose(double[][] mat)`
Multiplies a matrix with its transposed matrix. The transposed matrix is multiplied to the left of the original matrix.
Parameters:
`mat` - the matrix
Returns:
the result matrix
• #### transpose

`public static double[][] transpose(double[][] inputMatrix)`
Transposes the given matrix.
Parameters:
`inputMatrix` - the matrix to transposed
Returns:
the transposed matrix where the number of rows and columns is changed according to the given matrix
• #### inverse

`public static double[][] inverse(double[][] aOrig)`
Calculates the inverse matrix of a given matrix. The implementation applies the decomposition according to Gauss-Jordan identifying pivot elements.
Parameters:
`aOrig` - the original matrix
Returns:
the inverse matrix
Throws:
`ArithmeticException` - if the matrix is not a square matrix or the inverse cannot be computed (because of linear dependencies)
`NullPointerException` - if the argument is `null` or contains `null` elements
• #### normalizeMatrix

```public static double[][] normalizeMatrix(double[][] matrix,
double[] standardDev,
double[] mean)```
Normalizes the matrix relative to the mean of the input data and to the standard deviation.
Parameters:
`matrix` - the matrix to normalize
`standardDev` - the standard deviation for all columns used to normalize the matrix
`mean` - the mean for all columns used to normalize the matrix
Returns:
the normalized matrix
• #### normalizeMatrix

```public static double[][] normalizeMatrix(double[][] matrix,
double[] mean)```
Normalizes the matrix relative to the mean of the input data.
Parameters:
`matrix` - the matrix to normalize
`mean` - the mean for all columns used to normalize the matrix
Returns:
the normalized matrix
• #### normalizeMatrix

`public static double[][] normalizeMatrix(double[][] matrix)`
Normalizes the matrix relative to the mean and standard deviation of the input data.
Parameters:
`matrix` - the matrix to normalize
Returns:
the normalized matrix
• #### denormalizeVector

```public static double[] denormalizeVector(double[] vector,
double standardDev,
double mean)```
Denormalizes the vector relative to the mean of the input data and to the standard deviation.
Parameters:
`vector` - the input array to denormalize
`standardDev` - the standard deviation for all columns used to denormalize the matrix
`mean` - the mean for all columns used to denormalize the matrix
Returns:
the denormalized vector
• #### denormalizeVector

```public static double[] denormalizeVector(double[] vector,
double mean)```
Denormalizes the vector relative to the mean of the input data.
Parameters:
`vector` - the input array to denormalize
`mean` - the mean for all columns used to denormalize the matrix
Returns:
the denormalized vector
• #### denormalizeMatrix

```public static double[][] denormalizeMatrix(double[][] y,
double[] standardDev,
double[] mean)```
Denormalizes the matrix relative to the mean of the input data and to the standard deviation.
Parameters:
`y` - the matrix to denormalize
`standardDev` - the standard deviation for all columns used to denormalize the matrix
`mean` - the mean for all columns used to denormalize the matrix
Returns:
the denormalized matrix
• #### denormalizeMatrix

```public static double[][] denormalizeMatrix(double[][] y,
double[] mean)```
Denormalizes the matrix relativ to the mean of the input data.
Parameters:
`y` - the matrix to denormalize
`mean` - the mean for all columns used to denormalize the matrix
Returns:
the denormalized matrix
• #### spectralNorm

`public static double spectralNorm(double[][] matrix)`
Computes the spectral norm of the given matrix. It is defined as the square root of the maximum absolute value of the eigenvalues of the product of the matrix with its transposed form.
Parameters:
`matrix` - the matrix to compute the norm for.
Returns:
the spectral norm of the matrix.

```public static void addArrays(double[] firstArray,
double[] secondArray)```
Adds the second array to the first one.
Parameters:
`firstArray` - the first array will be changed by adding the second one to it
`secondArray` - the second array will be added to the first one (stays unchanged)
```public static void addMatrix(double[][] firstMatrix,
`firstMatrix` - the first matrix will be changed by adding the second one to it
`secondMatrix` - the second matrix will be added to the first one (stays unchanged)