org.knime.base.util.math
Class MathUtils

java.lang.Object
  extended by org.knime.base.util.math.MathUtils

public final class MathUtils
extends Object

Implements basic mathematical functions.

Author:
Christoph Sieb, University of Konstanz

Method Summary
static double[][] add(double[][] matrix1, double[][] matrix2)
          Adds two matrices.
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 relativ 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 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 two matrices.
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[][] 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

add

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

multiply

public static double[] multiply(double[][] matrix,
                                double[] vector)
                         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:
matrix - the matrix on the left side
vector - the vector on the right side
Returns:
the result matrix
Throws:
IllegalArgumentException - if the matrices are not compatible

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 can not 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

denormalizeMatrix

public static double[][] denormalizeMatrix(double[][] y,
                                           double[] standardDev,
                                           double[] mean)
Denormalizes the matrix relativ 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

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[] 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.

addArrays

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)

addMatrix

public static void addMatrix(double[][] firstMatrix,
                             double[][] secondMatrix)
Adds the second matrix to the first one.

Parameters:
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)


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.