Class MatrixFunctions


  • public class MatrixFunctions
    extends java.lang.Object

    Title: MatrixFunctions

    Description: Matrix operations for which a direct access to the matrix member variables is not necessary

    References http://www.ini.uzh.ch/~fred/java/Matrix.java https://introcs.cs.princeton.edu/java/95linear/Cholesky.java.html

    Copyright: Actelion Ltd., Inc. All Rights Reserved This software is the proprietary information of Actelion Pharmaceuticals, Ltd. Use is subject to license terms.

    Version:
    1.0 20.11.2003 MvK: Start implementation 10.06.2004 MvK read matrix.
    Author:
    Modest von Korff
    • Constructor Detail

      • MatrixFunctions

        public MatrixFunctions()
    • Method Detail

      • convert2Binary

        public static Matrix convert2Binary​(Matrix A,
                                            double thresh)
      • split

        public static Matrix[] split​(Matrix A,
                                     int row)
        Splits the matrix row wise into two matrices.
        Parameters:
        A -
        row -
        Returns:
      • splitCol

        public static Matrix[] splitCol​(Matrix A,
                                        int col)
      • isEqualCol

        public static boolean isEqualCol​(Matrix A,
                                         int colA,
                                         Matrix B,
                                         int colB,
                                         double thresh)
      • equals

        public static boolean equals​(double d1,
                                     double d2)
      • id

        public static double[][] id​(int n)
        Returns a (n x n) identity matrix. (The elements on the diagonal have value 1.0, all others are set to 0.0).
        Parameters:
        n - the size of the matrix
        Returns:
        the identity matrix
      • inv

        public static Matrix inv​(Matrix A)
        Returns the inverse of a matrix. The method uses a LU decomposition with pivot. There is no check on the size (is it a square matrix) or the rank (is it inversible, ie det doesn't equal 0).
        Parameters:
        A -
        Returns:
        A^(-1)
      • isSymmetric

        public static boolean isSymmetric​(Matrix A)
      • isSquare

        public static boolean isSquare​(Matrix A)
      • cholesky

        public static Matrix cholesky​(Matrix A)
        Parameters:
        A -
        Returns:
        Cholesky factor L of psd matrix A = L L^T
      • calculateSimilarityMatrixRowWise

        public static Matrix calculateSimilarityMatrixRowWise​(Matrix X1,
                                                              Matrix X2)
      • calculateMaxSimilarity

        public static double[] calculateMaxSimilarity​(Matrix XTrain,
                                                      Matrix Xtest)
      • calculateMaxSimilarity

        public static IntegerDouble[] calculateMaxSimilarity​(Matrix X,
                                                             double[] arr,
                                                             int nMostSimilar)
      • calculateMaxSimilarity

        public static IntegerDouble calculateMaxSimilarity​(Matrix X,
                                                           double[] arr)
      • upperTriangle

        public static double[] upperTriangle​(Matrix maQuadratic)
        Parameters:
        maQuadratic -
        Returns:
        upper triangle from a quadratic matrix as an array.
      • appendRows

        public static Matrix appendRows​(java.util.List<Matrix> liMatrix)
      • create

        public static Matrix create​(java.util.List<double[]> li)
        List is already used as a constructor for Matrix. So we have to place this method here.
        Parameters:
        li -
        Returns:
      • countFieldsBiggerThan

        public static int countFieldsBiggerThan​(Matrix ma,
                                                int row,
                                                double thresh)
      • countFieldsBiggerThanThreshColWise

        public static int countFieldsBiggerThanThreshColWise​(Matrix ma,
                                                             int col,
                                                             double thresh)
      • countFieldsBiggerThanThreshColWise

        public static Matrix countFieldsBiggerThanThreshColWise​(Matrix ma,
                                                                double thresh)
      • getDistanceMatrixTanimotoInv

        public static Matrix getDistanceMatrixTanimotoInv​(Matrix ma1,
                                                          Matrix ma2)
        Calculates the inverse Tanimoto coefficient from row wise comparison of the two input matrices.
        Parameters:
        ma1 -
        ma2 -
        Returns:
        complete distance matrix calculated between all rows from the two input matrices.
      • getDistanceMatrix

        public static Matrix getDistanceMatrix​(java.util.List<java.awt.Point> li)
      • getDistTanimotoInvReduced

        public static Matrix getDistTanimotoInvReduced​(Matrix ma1,
                                                       Matrix ma2)
      • getDistanceTanimotoInv

        public static double getDistanceTanimotoInv​(Matrix ma1,
                                                    int row1,
                                                    Matrix ma2,
                                                    int row2)
        Parameters:
        ma1 - Matrix
        row1 - row
        ma2 - Matrix
        row2 - row
        Returns:
        maximum distance = 0, minimum distance = 1
      • getSumSquaredDiff

        public static double getSumSquaredDiff​(Matrix ma1,
                                               int row1,
                                               Matrix ma2,
                                               int row2)
      • getTotalSumSquaredDiffRowWise

        public static double getTotalSumSquaredDiffRowWise​(Matrix ma1,
                                                           Matrix ma2)
      • getDistTanimotoInvReduced

        public static double getDistTanimotoInvReduced​(Matrix ma1,
                                                       int row1,
                                                       Matrix ma2,
                                                       int row2)
        Only fields are considered which are not nut 0 in both or in one of the rows we from where the distance is calculated.
        Parameters:
        ma1 - Matrix
        row1 - row
        ma2 - Matrix
        row2 - row
        Returns:
        maximum distance = 0, minimum distance = 1
      • getMooreNeighborhood

        public static java.util.List<java.awt.Point> getMooreNeighborhood​(java.awt.Point p,
                                                                          Matrix ma)
        A square-shaped neighborhood that can be used to define a set of cells surrounding a given point.
        Parameters:
        p -
        ma -
        Returns:
      • getMooreNeighborhood

        public static java.util.List<java.awt.Point> getMooreNeighborhood​(java.awt.Point p,
                                                                          int r,
                                                                          Matrix ma)
      • getRowMinUnique

        public static Matrix getRowMinUnique​(Matrix ma)
        Parameters:
        ma - Matrix with ma.rows and 1 col. Containing the min value from each row.
        Returns:
      • getPoints

        public static java.util.List<java.awt.Point> getPoints​(Matrix ma)
        Parameters:
        ma -
        Returns:
        list with indices Point(row,col) for values>0.
      • getIndicesUniqueMaxRowWise

        public static java.util.List<java.awt.Point> getIndicesUniqueMaxRowWise​(Matrix maIn)
      • getIndicesUniqueMaxColumnWise

        public static java.util.List<java.awt.Point> getIndicesUniqueMaxColumnWise​(Matrix maIn)
      • getScaledByFactor

        public static Matrix getScaledByFactor​(Matrix ma,
                                               double fac)
      • getScaled

        public static Matrix getScaled​(Matrix ma)
        Centers and divides by standard deviation.
        Parameters:
        ma -
        Returns:
      • getKMeanClusters

        public static Matrix getKMeanClusters​(Matrix ma,
                                              int k)
        03.10.04 MvK
        Parameters:
        ma - matrix with objects in rows
        k - number of desired cluster
        Returns:
        Matrix of cluster centers, k rows and cols equal ma.cols.
      • getCorrPearson

        public static final double getCorrPearson​(Matrix A,
                                                  Matrix B)
      • getCorrSpearman

        public static final double getCorrSpearman​(Matrix A,
                                                   Matrix B)
      • getCorrPearson

        public static double getCorrPearson​(Matrix A,
                                            int col1,
                                            int col2)
      • getCovariance

        public static double getCovariance​(Matrix A,
                                           Matrix B)
      • getCovarianceCentered

        public static double getCovarianceCentered​(Matrix A,
                                                   Matrix B)
      • getRandomMatrix

        public static Matrix getRandomMatrix​(int rows,
                                             int cols)
        generates a matrix with double values between 0 (inclusive) and 1 (exclusive).
        Parameters:
        rows - rows
        cols - columns
        Returns:
        matrix
      • rnorm

        public static Matrix rnorm​(int n,
                                   double mean,
                                   double sd)
        Parameters:
        n -
        mean -
        sd -
        Returns:
        random one column matrix with given mean and standard deviation.
      • getNonZeroCols

        public static IntVec getNonZeroCols​(Matrix X)
        Parameters:
        X -
        Returns:
        bit wise.
      • vecvec2Matrix

        public static void vecvec2Matrix​(java.util.Vector<java.util.Vector<java.lang.Double>> vecvec,
                                         Matrix ma)
        Converts a vector of vectors into doubles, each vector results in a row in the matrix. All vectors have to be of equal length or a runtime exception is thrown.
        Parameters:
        vecvec - vector on vectors, has to be converted into doubles
        ma - resulting matrix
      • readCSV

        public static Matrix readCSV​(java.io.File fi)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public static Matrix read​(java.io.File fiMatrix)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public static Matrix read​(java.io.File fiMatrix,
                                  boolean skipFirstLine)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public static Matrix read​(java.lang.String s)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • readAsLineBase64Encoded

        public static Matrix readAsLineBase64Encoded​(java.lang.String s)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • read

        public static Matrix read​(java.io.InputStream is)
      • read

        public static Matrix read​(java.io.InputStream is,
                                  boolean skipFirstLine)
      • writeQuadraticSymmetricMatrixPairwise

        public static void writeQuadraticSymmetricMatrixPairwise​(Matrix ma,
                                                                 java.io.File fiTxt)
                                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • columnIntoDoubleArray

        public static void columnIntoDoubleArray​(Matrix A,
                                                 int col,
                                                 DoubleArray da)
      • createIdentifiedObject

        public static java.util.List<IdentifiedObject<double[]>> createIdentifiedObject​(Matrix A)