Class LinearAlgebra


  • public class LinearAlgebra
    extends Object
    This class implements some methods from linear algebra (e.g. solution of a linear equation, PCA). It is basically a functional wrapper using either the Apache commons math or JBlas
    Version:
    1.6
    Author:
    Christian Fries
    • Constructor Summary

      Constructors 
      Constructor Description
      LinearAlgebra()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static double[][] diag​(double[] vector)
      Generates a diagonal matrix with the input vector on its diagonal
      double[][] exp​(double[][] matrix)
      Calculate the "matrix exponential" (expm).
      org.apache.commons.math3.linear.RealMatrix exp​(org.apache.commons.math3.linear.RealMatrix matrix)
      Calculate the "matrix exponential" (expm).
      static double[][] factorReduction​(double[][] correlationMatrix, int numberOfFactors)
      Returns a correlation matrix which has rank < n and for which the first n factors agree with the factors of correlationMatrix.
      static double[][] factorReductionUsingCommonsMath​(double[][] correlationMatrix, int numberOfFactors)
      Returns a correlation matrix which has rank < n and for which the first n factors agree with the factors of correlationMatrix.
      static double[][] getFactorMatrix​(double[][] correlationMatrix, int numberOfFactors)
      Returns the matrix of the n Eigenvectors corresponding to the first n largest Eigenvalues of a correlation matrix.
      static double[][] invert​(double[][] matrix)
      Returns the inverse of a given matrix.
      static double[][] multMatrices​(double[][] left, double[][] right)
      Multiplication of two matrices.
      static double[][] pseudoInverse​(double[][] matrix)
      Pseudo-Inverse of a matrix calculated in the least square sense.
      static double[] solveLinearEquation​(double[][] matrixA, double[] b)
      Find a solution of the linear equation A x = b where A is an n x m - matrix given as double[n][m] b is an m - vector given as double[m], x is an n - vector given as double[n],
      static double[] solveLinearEquationLeastSquare​(double[][] matrix, double[] vector)
      Find a solution of the linear equation A x = b in the least square sense where A is an n x m - matrix given as double[n][m] b is an m - vector given as double[m], x is an n - vector given as double[n],
      static double[][] solveLinearEquationLeastSquare​(double[][] matrix, double[][] rhs)
      Find a solution of the linear equation A X = B in the least square sense where A is an n x m - matrix given as double[n][m] B is an m x k - matrix given as double[m][k], X is an n x k - matrix given as double[n][k],
      static double[] solveLinearEquationSVD​(double[][] matrixA, double[] b)
      Find a solution of the linear equation A x = b where A is an n x m - matrix given as double[n][m] b is an m - vector given as double[m], x is an n - vector given as double[n],
      static double[] solveLinearEquationSymmetric​(double[][] matrix, double[] vector)
      Find a solution of the linear equation A x = b where A is an symmetric n x n - matrix given as double[n][n] b is an n - vector given as double[n], x is an n - vector given as double[n],
      static double[] solveLinearEquationTikonov​(double[][] matrixA, double[] b, double lambda)
      Find a solution of the linear equation A x = b where A is an n x m - matrix given as double[n][m] b is an m - vector given as double[m], x is an n - vector given as double[n], using a standard Tikhonov regularization, i.e., we solve in the least square sense A* x = b* where A* = (A^T, lambda I)^T and b* = (b^T , 0)^T.
      static double[] solveLinearEquationTikonov​(double[][] matrixA, double[] b, double lambda0, double lambda1, double lambda2)
      Find a solution of the linear equation A x = b where A is an n x m - matrix given as double[n][m] b is an m - vector given as double[m], x is an n - vector given as double[n], using a Tikhonov regularization, i.e., we solve in the least square sense A* x = b* where A* = (A^T, lambda0 I, lambda1 S, lambda2 C)^T and b* = (b^T , 0 , 0 , 0)^T.
      static double[][] transpose​(double[][] matrix)
      Transpose a matrix
    • Constructor Detail

      • LinearAlgebra

        public LinearAlgebra()
    • Method Detail

      • solveLinearEquationTikonov

        public static double[] solveLinearEquationTikonov​(double[][] matrixA,
                                                          double[] b,
                                                          double lambda)
        Find a solution of the linear equation A x = b where
        • A is an n x m - matrix given as double[n][m]
        • b is an m - vector given as double[m],
        • x is an n - vector given as double[n],
        using a standard Tikhonov regularization, i.e., we solve in the least square sense A* x = b* where A* = (A^T, lambda I)^T and b* = (b^T , 0)^T.
        Parameters:
        matrixA - The matrix A (left hand side of the linear equation).
        b - The vector (right hand of the linear equation).
        lambda - The parameter lambda of the Tikhonov regularization. Lambda effectively measures which small numbers are considered zero.
        Returns:
        A solution x to A x = b.
      • solveLinearEquationTikonov

        public static double[] solveLinearEquationTikonov​(double[][] matrixA,
                                                          double[] b,
                                                          double lambda0,
                                                          double lambda1,
                                                          double lambda2)
        Find a solution of the linear equation A x = b where
        • A is an n x m - matrix given as double[n][m]
        • b is an m - vector given as double[m],
        • x is an n - vector given as double[n],
        using a Tikhonov regularization, i.e., we solve in the least square sense A* x = b* where A* = (A^T, lambda0 I, lambda1 S, lambda2 C)^T and b* = (b^T , 0 , 0 , 0)^T. The matrix I is the identity matrix, effectively reducing the level of the solution vector. The matrix S is the first order central finite difference matrix with -lambda1 on the element [i][i-1] and +lambda1 on the element [i][i+1] The matrix C is the second order central finite difference matrix with -0.5 lambda2 on the element [i][i-1] and [i][i+1] and lambda2 on the element [i][i].
        Parameters:
        matrixA - The matrix A (left hand side of the linear equation).
        b - The vector (right hand of the linear equation).
        lambda0 - The parameter lambda0 of the Tikhonov regularization. Reduces the norm of the solution vector.
        lambda1 - The parameter lambda1 of the Tikhonov regularization. Reduces the slope of the solution vector.
        lambda2 - The parameter lambda1 of the Tikhonov regularization. Reduces the curvature of the solution vector.
        Returns:
        The solution x of the equation A* x = b*
      • solveLinearEquation

        public static double[] solveLinearEquation​(double[][] matrixA,
                                                   double[] b)
        Find a solution of the linear equation A x = b where
        • A is an n x m - matrix given as double[n][m]
        • b is an m - vector given as double[m],
        • x is an n - vector given as double[n],
        Parameters:
        matrixA - The matrix A (left hand side of the linear equation).
        b - The vector (right hand of the linear equation).
        Returns:
        A solution x to A x = b.
      • solveLinearEquationSVD

        public static double[] solveLinearEquationSVD​(double[][] matrixA,
                                                      double[] b)
        Find a solution of the linear equation A x = b where
        • A is an n x m - matrix given as double[n][m]
        • b is an m - vector given as double[m],
        • x is an n - vector given as double[n],
        Parameters:
        matrixA - The matrix A (left hand side of the linear equation).
        b - The vector (right hand of the linear equation).
        Returns:
        A solution x to A x = b.
      • invert

        public static double[][] invert​(double[][] matrix)
        Returns the inverse of a given matrix.
        Parameters:
        matrix - A matrix given as double[n][n].
        Returns:
        The inverse of the given matrix.
      • solveLinearEquationSymmetric

        public static double[] solveLinearEquationSymmetric​(double[][] matrix,
                                                            double[] vector)
        Find a solution of the linear equation A x = b where
        • A is an symmetric n x n - matrix given as double[n][n]
        • b is an n - vector given as double[n],
        • x is an n - vector given as double[n],
        Parameters:
        matrix - The matrix A (left hand side of the linear equation).
        vector - The vector b (right hand of the linear equation).
        Returns:
        A solution x to A x = b.
      • solveLinearEquationLeastSquare

        public static double[] solveLinearEquationLeastSquare​(double[][] matrix,
                                                              double[] vector)
        Find a solution of the linear equation A x = b in the least square sense where
        • A is an n x m - matrix given as double[n][m]
        • b is an m - vector given as double[m],
        • x is an n - vector given as double[n],
        Parameters:
        matrix - The matrix A (left hand side of the linear equation).
        vector - The vector b (right hand of the linear equation).
        Returns:
        A solution x to A x = b.
      • solveLinearEquationLeastSquare

        public static double[][] solveLinearEquationLeastSquare​(double[][] matrix,
                                                                double[][] rhs)
        Find a solution of the linear equation A X = B in the least square sense where
        • A is an n x m - matrix given as double[n][m]
        • B is an m x k - matrix given as double[m][k],
        • X is an n x k - matrix given as double[n][k],
        Parameters:
        matrix - The matrix A (left hand side of the linear equation).
        rhs - The matrix B (right hand of the linear equation).
        Returns:
        A solution X to A X = B.
      • getFactorMatrix

        public static double[][] getFactorMatrix​(double[][] correlationMatrix,
                                                 int numberOfFactors)
        Returns the matrix of the n Eigenvectors corresponding to the first n largest Eigenvalues of a correlation matrix. These Eigenvectors can also be interpreted as "principal components" (i.e., the method implements the PCA).
        Parameters:
        correlationMatrix - The given correlation matrix.
        numberOfFactors - The requested number of factors (eigenvectors).
        Returns:
        Matrix of n Eigenvectors (columns) (matrix is given as double[n][numberOfFactors], where n is the number of rows of the correlationMatrix.
      • factorReduction

        public static double[][] factorReduction​(double[][] correlationMatrix,
                                                 int numberOfFactors)
        Returns a correlation matrix which has rank < n and for which the first n factors agree with the factors of correlationMatrix.
        Parameters:
        correlationMatrix - The given correlation matrix.
        numberOfFactors - The requested number of factors (Eigenvectors).
        Returns:
        Factor reduced correlation matrix.
      • factorReductionUsingCommonsMath

        public static double[][] factorReductionUsingCommonsMath​(double[][] correlationMatrix,
                                                                 int numberOfFactors)
        Returns a correlation matrix which has rank < n and for which the first n factors agree with the factors of correlationMatrix.
        Parameters:
        correlationMatrix - The given correlation matrix.
        numberOfFactors - The requested number of factors (Eigenvectors).
        Returns:
        Factor reduced correlation matrix.
      • exp

        public double[][] exp​(double[][] matrix)
        Calculate the "matrix exponential" (expm). Note: The function currently requires jblas. If jblas is not availabe on your system, an exception will be thrown. A future version of this function may implement a fall back.
        Parameters:
        matrix - The given matrix.
        Returns:
        The exp(matrix).
      • exp

        public org.apache.commons.math3.linear.RealMatrix exp​(org.apache.commons.math3.linear.RealMatrix matrix)
        Calculate the "matrix exponential" (expm). Note: The function currently requires jblas. If jblas is not availabe on your system, an exception will be thrown. A future version of this function may implement a fall back.
        Parameters:
        matrix - The given matrix.
        Returns:
        The exp(matrix).
      • transpose

        public static double[][] transpose​(double[][] matrix)
        Transpose a matrix
        Parameters:
        matrix - The given matrix.
        Returns:
        The transposed matrix.
      • pseudoInverse

        public static double[][] pseudoInverse​(double[][] matrix)
        Pseudo-Inverse of a matrix calculated in the least square sense.
        Parameters:
        matrix - The given matrix A.
        Returns:
        pseudoInverse The pseudo-inverse matrix P, such that A*P*A = A and P*A*P = P
      • diag

        public static double[][] diag​(double[] vector)
        Generates a diagonal matrix with the input vector on its diagonal
        Parameters:
        vector - The given matrix A.
        Returns:
        diagonalMatrix The matrix with the vectors entries on its diagonal
      • multMatrices

        public static double[][] multMatrices​(double[][] left,
                                              double[][] right)
        Multiplication of two matrices.
        Parameters:
        left - The matrix A.
        right - The matrix B
        Returns:
        product The matrix product of A*B (if suitable)