Class Matrix


  • public class Matrix
    extends java.lang.Object
    • Field Detail

      • TINY16

        public static final double TINY16
      • OUT_SEPARATOR_COL

        public static java.lang.String OUT_SEPARATOR_COL
      • OUT_SEPARATOR_ROW

        public static java.lang.String OUT_SEPARATOR_ROW
    • Constructor Detail

      • Matrix

        public Matrix()
      • Matrix

        public Matrix​(int rows,
                      int cols)
      • Matrix

        public Matrix​(Matrix ma)
      • Matrix

        public Matrix​(boolean row,
                      double[] arr)
        Parameters:
        row - if true the matrix has one row. If false the matrix has one column.
        arr -
      • Matrix

        public Matrix​(boolean row,
                      byte[] arr)
      • Matrix

        public Matrix​(boolean row,
                      int[] dArray)
      • Matrix

        public Matrix​(double[][] arrArr)
        Deep copy
        Parameters:
        arrArr -
      • Matrix

        public Matrix​(double[][] arrArr,
                      boolean flat)
      • Matrix

        public Matrix​(float[][] arrArr)
      • Matrix

        public Matrix​(int[][] arrArr)
      • Matrix

        public Matrix​(byte[][] arrArr)
      • Matrix

        public Matrix​(java.util.List<DoubleVec> vecDoubleVec)
        Parameters:
        vecDoubleVec - Vector with DoubleVec
      • Matrix

        public Matrix​(boolean bRow,
                      java.util.List<java.lang.Double> liDoubles)
    • Method Detail

      • add

        public Matrix add​(double v)
      • addRow

        public void addRow​(double[] arr)
        Parameters:
        arr - adds a row at the end of the matrix.
      • addRow

        public void addRow​(int[] arr)
      • add2Row

        public void add2Row​(int row,
                            Matrix ma2,
                            int row2)
        Parameters:
        row - where the data are added on
        ma2 - matrix from where the data are taken
        row2 - data row
      • addToElement

        public void addToElement​(int i,
                                 int j,
                                 double value)
      • add2CompleteCol

        public Matrix add2CompleteCol​(Matrix maRow)
        The value in the col from the input matrix is added to all values in the corresponding col in the matrix.
        Parameters:
        maRow - matrix with one row.
      • assignCol

        public void assignCol​(int iCol,
                              Matrix ma)
      • assignCol

        public void assignCol​(int iCol,
                              Matrix ma,
                              int iColMa)
      • assignRow

        public void assignRow​(int iRow,
                              java.util.Vector<java.lang.Double> vecRow)
      • assignRow

        public void assignRow​(int iRow,
                              double[] arr)
      • assignRow

        public void assignRow​(int iRow,
                              int[] arr)
      • assignRow

        public void assignRow​(int iRow,
                              DoubleVec dv)
      • containsRow

        public boolean containsRow​(DoubleVec dv)
      • copy

        public void copy​(Matrix maSource)
        Copies a matrix ma into this, the pointer to ma1 is not changed.
        Parameters:
        maSource -
      • copy

        public void copy​(int offsetRows,
                         Matrix maSource)
        This (target matrix) must have appropriate dimensions.
        Parameters:
        offsetRows -
        maSource -
      • copyColumn

        public void copyColumn​(Matrix maSource,
                               int colSource,
                               int colDestination)
      • get

        public final double get​(int row,
                                int col)
      • getAbs

        public Matrix getAbs()
      • getArray

        public final double[][] getArray()
      • getArrayAsInt

        public int[][] getArrayAsInt()
                              throws java.lang.NumberFormatException
        Throws:
        java.lang.NumberFormatException
      • getArrayCopy

        public double[][] getArrayCopy()
      • getCenteredMatrix

        public Matrix getCenteredMatrix()
        Returns:
        column wise centered matrix.
      • getCenteredMatrix

        public Matrix getCenteredMatrix​(Matrix maMean)
      • getCol

        public Matrix getCol​(int col)
      • removeCol

        public Matrix removeCol​(int col2Remove)
      • getColAsDouble

        public double[] getColAsDouble​(int col)
      • getColAsList

        public java.util.List<java.lang.Double> getColAsList​(int col)
      • getColAsFloat

        public float[] getColAsFloat​(int iCol)
      • getColAsInt

        public int[] getColAsInt​(int iCol)
      • getColumns

        public Matrix getColumns​(java.util.Vector<java.lang.Integer> vecIndices)
        Deprecated.
        use getColumns(List vecIndices) instead
        Parameters:
        vecIndices -
        Returns:
      • getColumns

        public Matrix getColumns​(java.util.List<java.lang.Integer> liIndex)
      • getColumns

        public Matrix getColumns​(int[] arrIndex)
      • getColDim

        public int getColDim()
      • cols

        public int cols()
      • getDeleteColsZeroVar

        public Matrix getDeleteColsZeroVar​(java.util.List<java.lang.Integer> vecIndices)
        Deletes columns with zero variance.
        Parameters:
        vecIndices - the integer vector is filled with the indices from the input matrix which are taken for the new matrix.
        Returns:
        input matrix reduced by all cols with zero variance.
      • covariance

        public Matrix covariance()
        Generate a covariance matrix, each column contains values of a pulling.
        Returns:
        An n-by-n matrix.
      • correlation

        public Matrix correlation()
        Generate a correlation matrix, each column contains values of a pulling.
        Returns:
        An n-by-n matrix.
      • getDiagonal

        public Matrix getDiagonal()
        Matrix diagonal extraction.
        Returns:
        An d*1 Matrix of diagonal elements, d = min(m,n).
      • getLinedCol

        public Matrix getLinedCol()
      • getList

        public java.util.List<java.lang.Double> getList()
      • getMatrix

        public Matrix getMatrix​(int i0,
                                int i1,
                                int j0,
                                int j1)
      • getMatrix

        public Matrix getMatrix​(int[] r,
                                int[] c)
        Get a submatrix.
        Parameters:
        r - Array of row indices.
        c - Array of column indices.
        Returns:
        A(r(:),c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getMatrix

        public Matrix getMatrix​(int i0,
                                int i1,
                                int[] c)
        Get a submatrix.
        Parameters:
        i0 - Initial row index
        i1 - Final row index
        c - Array of column indices.
        Returns:
        A(i0:i1,c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getMatrix

        public Matrix getMatrix​(int[] r,
                                int j0,
                                int j1)
        Get a submatrix.
        Parameters:
        r - Array of row indices.
        j0 - Initial column index
        j1 - Final column index
        Returns:
        A(r(:),j0:j1)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getNumElements

        public int getNumElements()
      • getNumElementsLarger

        public int getNumElementsLarger​(double val2Compare)
      • getNumElementsEqual

        public int getNumElementsEqual​(double val2Compare)
      • log

        public Matrix log()
        Log function from the class Math .
        Returns:
        matrix with the natural logarithm (base e) of a double value.
      • diagonalize

        public Matrix diagonalize()
      • devide

        public Matrix devide​(double dDivisor)
      • devideDivisorBigger

        public Matrix devideDivisorBigger​(Matrix maDivisor)
      • devideCols

        public Matrix devideCols​(Matrix maDivisor)
      • devideRow

        public void devideRow​(int row,
                              double denominator)
      • getEigenvector

        public static final void getEigenvector​(Matrix A,
                                                int n,
                                                Matrix D,
                                                Matrix E)
        Householder reduction according num rec 11.2. Followed from an Eigen value decomposition with the calculation of the Eigen vectors according num rec 11.3.
        Parameters:
        A - intercept input matrix
        n - number of considered eigen values
        D -
        E -
      • getMinRow

        public double getMinRow​(int row)
        Parameters:
        row - row
        Returns:
        smallest value in specified row.
      • getMinRowIndex

        public int getMinRowIndex​(int row)
        Parameters:
        row - row
        Returns:
        the column index of the smallest value in the row.
      • getMinRowIndexRND

        public int getMinRowIndexRND​(int row)
        03.10.04 MvK Shuffles the indices before searching the minimum. This randomizes the search order.
        Parameters:
        row - row
        Returns:
        the column index of the smallest value in the row.
      • getID

        public int getID()
      • getEuclideanDistanceFastRows

        public double getEuclideanDistanceFastRows​(int iRow,
                                                   Matrix A,
                                                   int iRowA)
        Skipping the sqrt.
        Parameters:
        iRow -
        A -
        iRowA -
        Returns:
      • getEuclideanDistanceFastRows

        public double getEuclideanDistanceFastRows​(int row1,
                                                   int row2)
        Skipping the sqrt.
        Parameters:
        row1 -
        row2 -
        Returns:
      • getMaximumValue

        public double getMaximumValue()
      • getMean

        public double getMean()
      • getMeanCols

        public Matrix getMeanCols()
      • getMeanCols

        public Matrix getMeanCols​(int[] rowIndex)
      • getMeanCol

        public double getMeanCol​(int col)
      • getMeanRow

        public double getMeanRow​(int row)
      • getMeanRows

        public Matrix getMeanRows()
      • getMeanCol

        public double getMeanCol​(int iColIndex,
                                 int[] rowIndex)
      • getMedian

        public double getMedian​(int row)
      • getMedian

        public double getMedian()
      • getMedianCols

        public Matrix getMedianCols()
      • getMaxMin

        public Matrix getMaxMin()
        max and min vals for all cols
        Returns:
        matrix with two rows.
      • getMax

        public double getMax()
        Returns:
      • getMax

        public double getMax​(int col)
        get max value for that col.
        Parameters:
        col -
        Returns:
      • getMaxIndex

        public java.awt.Point getMaxIndex()
        Returns:
        Point(row,col) for the largest value.
      • getMaxRow

        public double getMaxRow​(int row)
      • getMaxRowIndexRND

        public int getMaxRowIndexRND​(int row)
        Shuffles the indices before searching the minimum.
        Parameters:
        row - row
        Returns:
        the column index of the largest value in the row.
      • getMaxRowIndex

        public int getMaxRowIndex​(int col)
        Parameters:
        col -
        Returns:
        the row index of the largest value in the col.
      • getMin

        public double getMin()
      • getMinPos

        public ScorePoint getMinPos()
        Returns:
        ValPos
      • getMinRows

        public Matrix getMinRows()
        Returns:
        matrix with minimum value from each row.
      • getMinRowsPosCol

        public Matrix getMinRowsPosCol()
        Returns:
        matrix dimension n,2. First row contains column position of min val, second col the min value.
      • getMin

        public double getMin​(int col)
      • getMinColIndex

        public int getMinColIndex​(int row)
        Parameters:
        row -
        Returns:
        column index for the field with the lowest value for the given row.
      • getColIndexContainingMaxVal

        public int getColIndexContainingMaxVal​(int row)
        Parameters:
        row -
        Returns:
        column index for the field with the largest value for the given row.
      • getColIndexContainingMaxVal

        public ScorePoint getColIndexContainingMaxVal​(int rowEnd,
                                                      int colEnd)
        Parameters:
        rowEnd - the values in the matrix will be considered until this row (exclusively).
        colEnd - the values in the matrix will be considered until this col (exclusively).
        Returns:
      • getNormalizedMatrix

        public Matrix getNormalizedMatrix()
      • getNormalizedMatrix

        public Matrix getNormalizedMatrix​(Matrix maStandardDeviation)
      • getUpperTriangle

        public double[] getUpperTriangle()
        For a quadratic matrix only.
        Returns:
      • getPythag

        public static double getPythag​(double a,
                                       double b)
        pythag computes sqrt(a^2 + b^2) without destructive underflow or overflow.
        Parameters:
        a - length a
        b - length b
        Returns:
        double
      • getPythag2

        public static double getPythag2​(double a,
                                        double b)
      • areRowsEqual

        public boolean areRowsEqual​(int row1,
                                    int row2)
      • equal

        public boolean equal​(Matrix ma)
      • equal

        public boolean equal​(Matrix ma,
                             double dLimit)
      • equalDimension

        public boolean equalDimension​(Matrix ma)
        Checks two matrices for equal dimensions.
        Parameters:
        ma - matrix to compare with.
        Returns:
        true if the number of getColDim and the number of getRowDim are corresponding.
      • hasOnlyFinite

        public boolean hasOnlyFinite()
      • multiplyValByVal

        public Matrix multiplyValByVal​(Matrix ma)
        Value by value multiplication Matrices must have the same dimensions.
        Parameters:
        ma -
        Returns:
      • multCols

        public Matrix multCols​(Matrix maMuiltiplicant)
      • multiply

        public Matrix multiply​(double dScalar)
      • multiply

        public double multiply​(int row1,
                               Matrix ma2,
                               int row2)
      • multiply

        public final Matrix multiply​(boolean transA,
                                     boolean transB,
                                     Matrix ma)
        Multiplication of two matrices. The algorithm checks for the correct dimensions of the matrices. If the dimensions are not corresponding a runtime exception is thrown. The left matrix is the instantiated object. Algorithm taken from Sedgewick.
        Parameters:
        transA - if true the left matrix is transposed before the multiplication
        transB - if true the right matrix is transposed before the multiplication
        ma - right matrix
        Returns:
        matrix with dimension getRowDim(), ma.getColDim().
      • getParsed

        public static Matrix getParsed​(java.util.Vector<java.lang.String> vecStringMatrix)
                                throws java.lang.Exception
        Parses an vector with Strings and converts it to a Matrix.
        Parameters:
        vecStringMatrix - vector with strings.
        Returns:
        Matrix
        Throws:
        java.lang.Exception
      • pow

        public Matrix pow​(double exp)
      • det

        public double det()
        Matrix determinant
        Returns:
        determinant
      • resize

        public void resize​(int rowsNew,
                           int colsNew)
        Resizes the matrix, the old data are written to the new matrix, if possible.
        Parameters:
        rowsNew - new number of getRowDim.
        colsNew - new number of getColDim.
      • getRow

        public double[] getRow​(int row)
        Flat copy.
        Parameters:
        row -
        Returns:
      • getRowCopy

        public double[] getRowCopy​(int row)
      • getRowAsList

        public java.util.List<java.lang.Double> getRowAsList​(int row)
      • getRowAsFloat

        public float[] getRowAsFloat​(int row)
      • getRowDim

        public int getRowDim()
      • rows

        public int rows()
      • getSorted

        public Matrix getSorted()
        Sorts the row of the matrix according the compareTo(...) function in DoubleVec
        Returns:
        sorted matrix
      • getSQRT

        public Matrix getSQRT()
      • getSquaredSum

        public double getSquaredSum()
      • getStandardDeviationCols

        public Matrix getStandardDeviationCols()
      • getStandardDeviation

        public double getStandardDeviation()
      • getVariance

        public double getVariance()
      • getCoefficientVariation

        public double getCoefficientVariation()
        Coefficient of variation (CV) is a normalized measure of dispersion of a probability distribution. It is defined as the ratio of the standard deviation sigma to the mean mu.
        Returns:
      • getVarianceCol

        public double getVarianceCol​(int col)
      • getVarianceCentered

        public double getVarianceCentered()
      • getVarianceCols

        public Matrix getVarianceCols()
        Returns:
        row with variance
      • increase

        public void increase​(int row,
                             int col,
                             double v)
      • set

        public final void set​(int row,
                              int col,
                              double v)
      • set

        public void set​(Matrix ma)
      • set

        public void set​(double[][] arr)
      • setFlat

        public void setFlat​(double[][] arr)
      • set

        public void set​(double v)
      • setID

        public void setID​(int id)
      • setCol

        public void setCol​(int col,
                           double v)
      • setCol

        public void setCol​(int col,
                           double[] arr)
      • setRow

        public void setRow​(int iRow,
                           double v)
      • setRow

        public void setRow​(int iRow,
                           double[] arr)
        Deep copy
        Parameters:
        iRow -
        arr -
      • setRow

        public void setRow​(int iRow,
                           int[] arr)
      • setSeparatorCol

        public static void setSeparatorCol​(java.lang.String s)
      • setSeparatorRow

        public static void setSeparatorRow​(java.lang.String s)
      • shuffleRows

        public void shuffleRows()
      • swapRows

        public void swapRows​(int a,
                             int b)
      • sortRows

        public void sortRows​(int col)
      • getStandardized

        public Matrix getStandardized()
        Get the standard scores, also known as z-scores.
        Returns:
        matrux with standardized values.
      • getSubMatrix

        public Matrix getSubMatrix​(int indexRowStart,
                                   int indexRowEnd,
                                   int indexColStart,
                                   int indexColEnd)
        Parameters:
        indexRowStart - start row included
        indexRowEnd - end row included
        indexColStart - start col included
        indexColEnd - end col included
        Returns:
        matrix
      • getSubMatrix

        public Matrix getSubMatrix​(java.util.List<java.lang.Integer> liIndexRow)
      • getSum

        public double getSum()
      • getSumUpperTriangle

        public double getSumUpperTriangle()
        Matrix has to be quadratic.
        Returns:
      • getSumCols

        public Matrix getSumCols()
      • getSumRows

        public Matrix getSumRows()
      • getSumCol

        public double getSumCol​(int col)
      • getSumRow

        public double getSumRow​(int row)
      • getSumSquared

        public double getSumSquared()
      • getNext4NeighboursTorus

        public double[] getNext4NeighboursTorus​(int row,
                                                int col)
      • getNext8NeighboursTorus

        public double[] getNext8NeighboursTorus​(int row,
                                                int col)
      • row2Matrix

        public Matrix row2Matrix​(int row,
                                 int len)
      • getTorus

        public double getTorus​(int row,
                               int col)
      • getTrace

        public double getTrace()
        Matrix trace.
        Returns:
        sum of the diagonal elements.
      • getTranspose

        public Matrix getTranspose()
      • getFlipped

        public Matrix getFlipped()
        Opposite direction to transpose.
      • toRow

        public Matrix toRow()
      • toArray

        public double[] toArray()
        Returns:
        deep copy.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toStringBinary

        public java.lang.String toStringBinary()
      • toString

        public java.lang.String toString​(int digits)
      • toString

        public java.lang.String toString​(int rowEnd,
                                         int colEnd,
                                         int digits)
        Parameters:
        rowEnd - exclusive
        colEnd - exclusive
        digits -
        Returns:
      • toStringRow

        public java.lang.String toStringRow​(int row,
                                            int iDigits)
      • toStringRowNumber

        public java.lang.String toStringRowNumber​(int iDigits,
                                                  java.lang.String sSeparatorCol)
      • subtract

        public Matrix subtract​(double val)
      • subtractFromCols

        public Matrix subtractFromCols​(Matrix maDivisor)
      • Sign

        public static double Sign​(double a,
                                  double b)
      • getRND

        public static Matrix getRND​(int rows,
                                    int cols)
      • write

        public void write​(java.lang.String sFile,
                          boolean apppend)
      • write

        public void write​(java.lang.String sFile)
      • write

        public void write​(java.io.File fiMa)
      • write

        public void write​(java.io.File fiMa,
                          boolean apppend,
                          int digits)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String sFiMa,
                          boolean apppend,
                          int digits)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • write

        public void write​(java.io.File fiMa,
                          java.text.DecimalFormat nf,
                          boolean apppend)
      • write

        public void write​(java.io.OutputStream os)
      • writeAsLineBase64Encoded

        public java.lang.String writeAsLineBase64Encoded()
      • write

        public void write​(java.io.OutputStream os,
                          java.text.NumberFormat nf)
      • toStringWithColTags

        public java.lang.String toStringWithColTags​(java.util.List<java.lang.String> liColTags,
                                                    java.text.DecimalFormat nf,
                                                    java.lang.String separator)
      • toStringWithRowTags

        public java.lang.String toStringWithRowTags​(java.util.List<java.lang.String> liRowTags,
                                                    java.text.DecimalFormat nf,
                                                    java.lang.String separator)
      • writeSerialized

        public void writeSerialized​(java.io.File fiOut)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readSerialized

        public static Matrix readSerialized​(java.io.File fiIn)
                                     throws java.io.FileNotFoundException,
                                            java.io.IOException,
                                            java.lang.ClassNotFoundException
        Throws:
        java.io.FileNotFoundException
        java.io.IOException
        java.lang.ClassNotFoundException
      • format

        public static java.text.DecimalFormat format​(int digits)
      • write

        public void write​(java.lang.String sFile,
                          boolean bApppend,
                          int digits,
                          int totalWidth)
      • format

        public static java.lang.String format​(double val,
                                              java.text.DecimalFormat nf,
                                              int totalWidth)