Package jline.util

Class Matrix

java.lang.Object
jline.util.Matrix
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ClassSwitchMatrix

public class Matrix extends Object implements Serializable
A sparse matrix data structure supporting linear algebra functions similar to those available in MATLAB.
See Also:
  • Field Details

    • data

      public org.ejml.data.DMatrixSparseCSC data
  • Constructor Details

    • Matrix

      public Matrix(int numRows, int numCols, int arrayLength)
    • Matrix

      public Matrix(int numRows, int numCols)
    • Matrix

      public Matrix(Matrix matrix)
    • Matrix

      public Matrix(org.ejml.data.DMatrixSparseCSC matrix)
    • Matrix

      public Matrix(double[][] arrays)
    • Matrix

      public Matrix(int[] array)
    • Matrix

      public Matrix(double[] array)
    • Matrix

      public Matrix(List<Double> array)
    • Matrix

      public Matrix(ArrayList<List<Double>> arrays)
    • Matrix

      public Matrix(org.ejml.simple.SimpleMatrix matrix)
    • Matrix

      public Matrix(String matrixString)
      Parse matrix from string in MATLAB or Python formats MATLAB format assumes commas between row elements and semicolon between rows
      Parameters:
      matrixString - input string, e.g., "[10,4;5,9]" or "[[10,4],[4,9]]"
  • Method Details

    • oneMinusMatrix

      public static Matrix oneMinusMatrix(Matrix matrix)
    • allbut

      public static Matrix allbut(Matrix y, int k)
      Returns all elements in a matrix except the first ones
      Parameters:
      y - - a matrix
      k - - a position of an element in the matrix
      Returns:
      - A row vector with all elements of y but ypos
    • matchrow

      public static int matchrow(Matrix matrix, Matrix row)
      Returns the position of the given row in the corresponding matrix
      Parameters:
      matrix - - the matrix to be searched
      row - - the row
      Returns:
      - Position of the given row in the matrix, or -1 otherwise
    • weaklyConnect

      public static Set<Set<Integer>> weaklyConnect(Matrix param, Set<Integer> colsToIgnore)
      Weakly-connected components of a sub-matrix.
      Parameters:
      param - Input matrix
      colsToIgnore - Indexes to be ignored
      Returns:
      Weighted connected components
    • oner

      public static Matrix oner(Matrix N, Integer s)
      Decrease by one an element of an integer vector.
      Parameters:
      N - integer vector
      s - dimension to decrease
      Returns:
      Decreased vector
    • oner

      public static Matrix oner(Matrix N, List<Integer> r)
      Decrease by one an element of an integer vector.
      Parameters:
      N - integer vector
      r - dimension to decrease
      Returns:
      Decreased vector
    • factln

      public static Matrix factln(Matrix n)
      Computes log(x!) of all elements x of the input matrix
      Parameters:
      n - input matrix
      Returns:
      Decreased vector
    • logsumexp

      public static double logsumexp(Matrix x)
      Computes log(sum_i exp(x_i)) of all elements x_i of the input matrix
      Parameters:
      x - input matrix
      Returns:
      log-sum-exp value
    • cartesian

      public static Matrix cartesian(Matrix matrixA, Matrix matrixB)
      Cartesian product of two matrices. It replicates elements of the first input matrix and pairs them with each row of the second input matrix.
      Parameters:
      matrixA - first input matrix
      matrixB - second input matrix
    • intersect

      public static List<Double> intersect(Matrix matrixA, Matrix matrixB)
      Interesection of two matrices. Return rows common to both
      Parameters:
      matrixA - first input matrix
      matrixB - second input matrix
    • uniqueRowIndexes

      public static UniqueRowResult uniqueRowIndexes(Matrix m)
    • uniqueRows

      public static UniqueRowResult uniqueRows(Matrix m)
    • extract

      public static void extract(Matrix src, int srcX0, int srcX1, int srcY0, int srcY1, Matrix dst, int dstY0, int dstX0)
    • extract

      public static Matrix extract(Matrix src, int srcX0, int srcX1, int srcY0, int srcY1)
    • extractRows

      public static Matrix extractRows(Matrix A, int row0, int row1)
    • extractRows

      public static Matrix extractRows(Matrix A, int row0, int row1, Matrix out)
    • extractColumns

      public static Matrix extractColumns(Matrix A, int col0, int col1)
    • extractColumns

      public static Matrix extractColumns(Matrix A, int col0, int col1, Matrix out)
    • extractColumn

      public static Matrix extractColumn(Matrix A, int column, Matrix out)
    • extractDiag

      public static void extractDiag(Matrix A, Matrix outputB)
    • concatColumns

      public static Matrix concatColumns(Matrix left, Matrix right, Matrix out)
    • concatRows

      public static Matrix concatRows(Matrix top, Matrix bottom, Matrix out)
    • diagMatrix

      public static Matrix diagMatrix(Matrix A, double[] values, int offset, int length)
    • diag

      public static Matrix diag(double... values)
    • solve

      public static boolean solve(Matrix a, Matrix b, Matrix x)
    • eye

      public static Matrix eye(int length)
    • ones

      public static Matrix ones(int rows, int cols)
    • zeros

      public static Matrix zeros(int rows, int cols)
    • pow

      public static Matrix pow(Matrix a, int b)
    • first_norm

      public static double first_norm(Matrix a)
    • negative

      public static Matrix negative(Matrix a)
    • scale_mult

      public static Matrix scale_mult(Matrix a, double n)
    • inf_norm

      public static double inf_norm(Matrix a)
    • cellsum

      public static Matrix cellsum(Map<Integer,Matrix> cell_array)
    • createLike

      public static Matrix createLike(Matrix B)
    • lyap

      public static Matrix lyap(Matrix A, Matrix B, Matrix C, Matrix D)
    • sylv

      public static Matrix sylv(Matrix A, Matrix B, Matrix C)
    • matrix_add_vector

      public static Matrix matrix_add_vector(Matrix matrix, Matrix vector)
    • col_vector_add_row_vector

      public static Matrix col_vector_add_row_vector(Matrix col_vector, Matrix row_vector)
    • columnMatrixToDoubleArray

      public static double[] columnMatrixToDoubleArray(Matrix columnMatrix)
    • findIndexWithZeroSum

      public static List<Integer> findIndexWithZeroSum(Matrix matrix, boolean isRow)
      Parameters:
      matrix - , the matrix which is going to check
      isRow - , true if check through row, otherwise false
      Returns:
      a list of integer show the index of row/col with zero-sum
    • maxAbsDiff

      public static double maxAbsDiff(Matrix a, Matrix b)
    • singleton

      public static Matrix singleton(double value)
    • expm

      public static Matrix expm(Matrix m)
    • inv

      public static Matrix inv(Matrix m)
    • findRows

      public static List<Integer> findRows(Matrix matrix, Matrix row)
    • elementMinNonZero

      public static double elementMinNonZero(Matrix matrix)
    • compare

      public static boolean compare(Matrix A, Matrix B, String op)
    • sumCumprod

      public static double sumCumprod(Matrix matrix)
    • logSum

      public static double logSum(Matrix matrix)
    • cell2mat

      public static Matrix cell2mat(Map<Integer,Matrix> cellArray)
    • getColIndexSum

      public static int getColIndexSum(Map<Integer,Matrix> cellArray)
    • exp

      public Matrix exp()
    • log

      public Matrix log()
    • fact

      public Matrix fact()
    • factln

      public Matrix factln()
    • reverse

      public Matrix reverse()
      For a vector, return a matrix with reversed index positions. For example, for a vector v, this would be v(end:-1:1);
    • reverseRows

      public Matrix reverseRows()
    • sort

      public Matrix sort()
      Sort the elements of the matrix
    • sortEq

      public Matrix sortEq()
      Sort the elements of the matrix
    • sqrt

      public Matrix sqrt()
    • square

      public Matrix square()
    • getNumCols

      public int getNumCols()
      Get the total number of columns
    • getNumElements

      public int getNumElements()
      Get the total number of elements
    • getNumRows

      public int getNumRows()
      Get the total number of rows
    • getNonZeroRows

      public int[] getNonZeroRows()
      Get number of rows with at least a non-zero
    • getNonZeroCols

      public int[] getNonZeroCols()
    • getNonZeroValues

      public double[] getNonZeroValues()
      Get array of non-zeros
    • getNonZeroLength

      public int getNonZeroLength()
      Get number of non-zeros
    • reshape

      public void reshape(int numRows, int numCols)
      Reshape matrix to the given number of rows and columns
    • getColMax

      public int getColMax(int col)
      Return position of column maximum
    • getRowMax

      public int getRowMax(int row)
      Return position of row maximum
    • getColIndexes

      public int[] getColIndexes()
      Get indexes of columns that carry data
    • setTo

      public void setTo(Matrix m)
      Copy values of the input matrix
    • growMaxColumns

      public void growMaxColumns(int newmax, boolean preserve)
      Grow the maximum number of columns
    • growMaxLength

      public void growMaxLength(int newmax, boolean preserve)
      Grow the maximum number of elements
    • shrinkNumCols

      public void shrinkNumCols(int newmax)
      Reduce the maximum number of columns
    • shrinkNumRows

      public void shrinkNumRows(int newmax)
      Reduce the maximum number of rows
    • isAssigned

      public boolean isAssigned(int row, int col)
      Check if sparse matrix element is assigned
    • unsafe_set

      public void unsafe_set(int row, int col, double val)
      Fast version of set that does not check bounds
    • printNonZero

      public void printNonZero()
      Print non-zero values
    • reshape

      public void reshape(int numRows, int numCols, int arrayLength)
      Reshape number of rows, columns and the number of non-zeros
    • inv

      public Matrix inv()
    • toDMatrixSparseCSC

      public org.ejml.data.DMatrixSparseCSC toDMatrixSparseCSC()
    • toDMatrixSparseCSC

      public org.ejml.data.DMatrixSparseCSC toDMatrixSparseCSC(Matrix matrix)
    • expandMatrix

      public void expandMatrix(int rows, int cols, int nz_length)
    • isDiag

      public boolean isDiag()
    • clone

      public Matrix clone()
      Overrides:
      clone in class Object
    • hasFinite

      public boolean hasFinite()
    • hasInfinite

      public boolean hasInfinite()
    • hasNaN

      public boolean hasNaN()
    • value

      public double value()
    • get

      public double get(int i, int j)
    • get

      public double get(int idx)
    • set

      public void set(int idx, double val)
    • set

      public void set(int row, int col, double val)
    • set

      public void set(int row, int col, int val)
    • cumsumViaRow

      public Matrix cumsumViaRow()
    • cumsumViaCol

      public Matrix cumsumViaCol()
    • sumRows

      public double sumRows(int row)
    • sumRows

      public Matrix sumRows()
    • sumCols

      public double sumCols(int col)
    • sumCols

      public Matrix sumCols()
    • sumAbsCols

      public double sumAbsCols(int col)
    • sumAbsRows

      public double sumAbsRows(int row)
    • repmat

      public Matrix repmat(int rows, int cols)
    • find

      public Matrix find()
    • replace

      public void replace(double delete, double replacement)
    • findNumber

      public Matrix findNumber(double number)
    • findZero

      public Matrix findZero()
    • findNonNegative

      public Matrix findNonNegative()
    • uniqueInRow

      public Matrix uniqueInRow(int rowIdx)
    • uniqueNonZerosInRow

      public Matrix uniqueNonZerosInRow(int rowIdx)
    • uniqueNonNegativeInRow

      public Matrix uniqueNonNegativeInRow(int rowIdx)
    • uniqueInCol

      public Matrix uniqueInCol(int colIdx)
    • uniqueNonZerosInCol

      public Matrix uniqueNonZerosInCol(int colIdx)
    • uniqueNonNegativeInCol

      public Matrix uniqueNonNegativeInCol(int colIdx)
    • count

      public int count(double val)
    • countEachRow

      public Matrix countEachRow(double val)
    • length

      public int length()
    • absEq

      public void absEq()
    • removeNegative

      public void removeNegative()
    • removeInfinity

      public void removeInfinity()
    • isFinite

      public boolean isFinite()
    • removeNaN

      public void removeNaN()
    • isEmpty

      public boolean isEmpty()
    • apply

      public void apply(double source, double target, String op)
    • elementIncrease

      public Matrix elementIncrease(double val)
    • meanCol

      public Matrix meanCol()
    • meanRow

      public Matrix meanRow()
    • elementPower

      public Matrix elementPower(double t)
    • fromArray2D

      public Matrix fromArray2D(int[][] matrix)
    • zero

      public void zero()
    • fromArray2D

      public Matrix fromArray2D(double[][] matrix)
    • fill

      public Matrix fill(double val)
    • transpose

      public Matrix transpose()
    • sub

      public Matrix sub(double x)
    • sub

      public Matrix sub(Matrix matrix)
    • sub

      public Matrix sub(double alpha, Matrix matrix)
    • add

      public Matrix add(Matrix matrix)
    • add

      public Matrix add(double alpha, Matrix matrix)
    • add

      public void add(double alpha)
    • subEq

      public void subEq(double x)
    • subEq

      public void subEq(Matrix matrix)
    • subEq

      public void subEq(double alpha, Matrix matrix)
    • addEq

      public void addEq(Matrix matrix)
    • addEq

      public void addEq(double alpha, Matrix matrix)
    • div

      public Matrix div(Matrix den)
    • divEq

      public void divEq(Matrix den)
    • divideEq

      public void divideEq(double scalar)
    • divide

      public void divide(double scalar, Matrix outputB, boolean flag)
    • divideRows

      public void divideRows(double[] diag, int offset)
    • multEq

      public void multEq(Matrix B)
    • mult

      public Matrix mult(Matrix B)
    • mult

      public Matrix mult(Matrix B, Matrix out)
    • elementSum

      public double elementSum()
    • elementMin

      public double elementMin()
    • elementMax

      public double elementMax()
    • hadamard

      public Matrix hadamard(Matrix B)
    • elementMult

      public Matrix elementMult(Matrix B)
    • elementMult

      public Matrix elementMult(Matrix B, Matrix output)
    • elementDiv

      public Matrix elementDiv(Matrix B)
      Performs element-wise division
      Parameters:
      B - - the other matrix
      Returns:
      - A ./ B
    • elementMultWithVector

      public Matrix elementMultWithVector(Matrix B)
      Performs element-wise multiplication Note that B is a row vector, and the result is A_{ij} = \sum_{j=1}^n A_{ij} * B(i)
      Parameters:
      B - - the other row vector
      Returns:
      A_i * B
    • removeZeros

      public void removeZeros(double val)
    • changeSign

      public void changeSign()
    • toIntArray1D

      public int[] toIntArray1D()
    • toArray1D

      public double[] toArray1D()
    • toList1D

      public List<Double> toList1D()
    • toArray2D

      public double[][] toArray2D()
    • toDouble

      public Double toDouble()
    • toDoubleList

      public List<List<Double>> toDoubleList()
    • sumSubMatrix

      public double sumSubMatrix(int startRow, int endRow, int startCol, int endCol)
    • sumSubMatrix

      public double sumSubMatrix(int[] rowIndexes, int[] colIndexes)
    • sumSubMatrix

      public double sumSubMatrix(boolean[] rowIndexes, boolean[] colIndexes)
    • sumRows

      public Matrix sumRows(int startCol, int endCol)
    • sumCols

      public Matrix sumCols(int startRow, int endRow)
    • ones

      public void ones()
    • krons

      public Matrix krons(Matrix other)
    • createBlockDiagonal

      public Matrix createBlockDiagonal(Matrix matrix2)
    • elementMaxAbs

      public double elementMaxAbs()
    • scale

      public Matrix scale(double scalar)
    • scaleEq

      public void scaleEq(double scalar)
    • scaleEq

      public void scaleEq(double scalar, Matrix output)
    • ceil

      public Matrix ceil()
      Computes the matrix resulted from ceiling every member of the current matrix
      Returns:
      - the matrix obtained by ceiling the current matrix
    • ceilEq

      public Matrix ceilEq()
      Computes the matrix resulted from ceiling every member of the current matrix
      Returns:
      - the matrix obtained by ceiling the current matrix
    • colon

      public Matrix colon()
    • columnMajorOrder

      public Matrix columnMajorOrder()
      Equivalent to the colon operator in MATLAB: (:).
      Returns:
      - the Matrix in a column-major order, flattened as a column vector.
    • getRow

      public Matrix getRow(int i)
    • getColumn

      public Matrix getColumn(int j)
    • setColumn

      public Matrix setColumn(int j, Matrix col)
    • setRow

      public Matrix setRow(int j, Matrix row)
    • setColumns

      public Matrix setColumns(int j0, int j1, Matrix cols)
    • setRows

      public Matrix setRows(int i0, int i1, Matrix rows)
    • isEqualTo

      public boolean isEqualTo(Matrix m)
      Checks if two matrices are equal
      Parameters:
      m - - the other matrix
      Returns:
      - true if the current matrix and the other matrix are equal, false otherwise
    • isEqualToTol

      public boolean isEqualToTol(Matrix m, double tol)
      Checks if two matrices are equal within a tolerance
      Parameters:
      m - - the other matrix
      tol - - the tolerance
      Returns:
      - true if the current matrix and the other matrix are equal, false otherwise
    • any

      public boolean any()
      Checks if the current matrix has a non-zero element
      Returns:
      - true if there is a non-zero element in the matrix, false otherwise
    • elementMult

      public double elementMult()
      Computes the product of the elements of a row/column vector
      Returns:
      - the product of all the elements of the given vector
    • norm

      public double norm()
      Computes the Euclidean norm of a matrix
      Returns:
      - the Euclidean norm of the given matrix
    • insert_sub_matrix

      public void insert_sub_matrix(int start_row, int start_col, int end_row, int end_col, Matrix matrix_to_be_inserted)
    • eigenvalue

      public Matrix eigenvalue()
    • eigenvector

      public Matrix eigenvector()
    • safe_mult

      public Matrix safe_mult(Matrix B)
    • rank

      public int rank()
    • element_power

      public Matrix element_power(double a)
    • det

      public double det()
    • remove

      public void remove(int row, int col)
    • removeInfinite

      public void removeInfinite()
    • element_divide

      public Matrix element_divide(Matrix b)
    • hasDuplicates

      public boolean hasDuplicates()
    • right_matrix_divide

      public Matrix right_matrix_divide(Matrix b)
    • left_matrix_divide

      public Matrix left_matrix_divide(Matrix b)
    • qr

      public Map<String,Matrix> qr()
    • schur

      public Map<String,Matrix> schur(String method, Integer it_)
    • schur

      public Map<String,Matrix> schur()
    • isDiag_withintol

      public boolean isDiag_withintol()
    • compatible_sizes_add

      public Matrix compatible_sizes_add(Matrix b)
    • row_increase

      public void row_increase(int row, double a)
    • col_increase

      public void col_increase(int col, double a)
    • kron

      public Matrix kron(Matrix b)
    • print

      public void print()
    • prettyPrint

      public void prettyPrint()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getRowElements

      public Matrix getRowElements(int row)
      Parameters:
      row -
      Returns:
      new matrix include all elements from the given row index
    • removeRows

      public void removeRows(Collection<Integer> rows)
      Removes the specified rows from the given matrix. If possible, use a HashSet for the collection of rows to improve performance.
      Parameters:
      rows - - the indices of the rows to be removed
    • removeCols

      public void removeCols(Collection<Integer> cols)
      Removes the specified columns from the given matrix. If possible, use a HashSet for the collection of columns to improve performance.
      Parameters:
      cols - - the indices of the columns to be removed
    • concatCols

      public Matrix concatCols(Matrix other)
      Concatenates the columns of two matrices
      Parameters:
      other - - the other matrix which will be concatenated to the current one
      Returns:
      - a new matrix containing the columns of the two matrices, concatenated
    • powerSumRows

      public double powerSumRows(int row, double alpha)
      Parameters:
      row -
      alpha -
      Returns:
      sum_{j=0}^n |A{ij}|^alpha
    • powerSumCols

      public double powerSumCols(int col, double alpha)
      Parameters:
      col -
      alpha -
      Returns:
      sum_{j=0}^n |A{ji}|^alpha
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • randMatrix

      public void randMatrix(int length)
    • getRowsFrom

      public Matrix getRowsFrom(int row)
    • expandMatrixToSquare

      public void expandMatrixToSquare()
    • mulByMinusOne

      public void mulByMinusOne()
    • getSlice

      public Matrix getSlice(int rowStart, int rowEnd, int colStart, int colEnd)
    • getSlice

      public Matrix getSlice(boolean[] rowFlags, boolean[] colFlags)
    • setSlice

      public Matrix setSlice(int rowStart, int rowEnd, int colStart, int colEnd, Matrix newSlice)
    • setSliceEq

      public void setSliceEq(int rowStart, int rowEnd, int colStart, int colEnd, Matrix newSlice)
    • expm

      public Matrix expm()
      Adapted from jblas and IHMC Original documentation:

      Calculate matrix exponential of a square matrix.

      A scaled Pade approximation algorithm from Golub and Van Loan "Matrix Computations", algorithm 11.3.1 and 11.2.

      Returns:
      matrix exponential of the matrix
    • reciprocal

      public Matrix reciprocal()
    • findNonZeroRowsInColumn

      public List<Integer> findNonZeroRowsInColumn(int column)
      Parameters:
      column -
      Returns:
      List of row index where the value at the given column is not zero
    • allEqualToOne

      public boolean allEqualToOne()
    • getSubMatrix

      public Matrix getSubMatrix(Matrix rows, Matrix cols)
    • setToNaN

      public void setToNaN()
    • setNaNToZero

      public void setNaNToZero()
    • readFromFile

      public static Matrix readFromFile(String fileName)
    • compareMatrix

      public boolean compareMatrix(Matrix matrix)