Package jline.util
Class Matrix
java.lang.Object
jline.util.Matrix
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ClassSwitchMatrix
A sparse matrix data structure supporting linear algebra functions similar to those available in MATLAB.
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionMatrix
(double[] array) Matrix
(double[][] arrays) Matrix
(int[] array) Matrix
(int numRows, int numCols) Matrix
(int numRows, int numCols, int arrayLength) Parse matrix from string in MATLAB or Python formats MATLAB format assumes commas between row elements and semicolon between rowsMatrix
(org.ejml.data.DMatrixSparseCSC matrix) Matrix
(org.ejml.simple.SimpleMatrix matrix) -
Method Summary
Modifier and TypeMethodDescriptionvoid
absEq()
void
add
(double alpha) void
void
static Matrix
Returns all elements in a matrix except the first onesboolean
boolean
any()
Checks if the current matrix has a non-zero elementvoid
static Matrix
Cartesian product of two matrices.ceil()
Computes the matrix resulted from ceiling every member of the current matrixceilEq()
Computes the matrix resulted from ceiling every member of the current matrixstatic Matrix
static Matrix
void
clone()
void
col_increase
(int col, double a) static Matrix
col_vector_add_row_vector
(Matrix col_vector, Matrix row_vector) colon()
Equivalent to the colon operator in MATLAB: (:).static double[]
columnMatrixToDoubleArray
(Matrix columnMatrix) static boolean
boolean
compareMatrix
(Matrix matrix) concatCols
(Matrix other) Concatenates the columns of two matricesstatic Matrix
concatColumns
(Matrix left, Matrix right, Matrix out) static Matrix
concatRows
(Matrix top, Matrix bottom, Matrix out) int
count
(double val) countEachRow
(double val) createBlockDiagonal
(Matrix matrix2) static Matrix
createLike
(Matrix B) double
det()
static Matrix
diag
(double... values) static Matrix
diagMatrix
(Matrix A, double[] values, int offset, int length) void
void
void
divideEq
(double scalar) void
divideRows
(double[] diag, int offset) element_power
(double a) elementDiv
(Matrix B) Performs element-wise divisionelementIncrease
(double val) double
double
double
static double
elementMinNonZero
(Matrix matrix) double
Computes the product of the elements of a row/column vectorelementMult
(Matrix B, Matrix output) 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)elementPower
(double t) double
boolean
exp()
void
expandMatrix
(int rows, int cols, int nz_length) void
expm()
Adapted from jblas and IHMC Original documentation:static Matrix
static Matrix
static void
static Matrix
extractColumn
(Matrix A, int column, Matrix out) static Matrix
extractColumns
(Matrix A, int col0, int col1) static Matrix
extractColumns
(Matrix A, int col0, int col1, Matrix out) static void
extractDiag
(Matrix A, Matrix outputB) static Matrix
extractRows
(Matrix A, int row0, int row1) static Matrix
extractRows
(Matrix A, int row0, int row1, Matrix out) static Matrix
eye
(int length) fact()
factln()
static Matrix
Computes log(x!) of all elements x of the input matrixfill
(double val) find()
findIndexWithZeroSum
(Matrix matrix, boolean isRow) findNonZeroRowsInColumn
(int column) findNumber
(double number) findZero()
static double
first_norm
(Matrix a) fromArray2D
(double[][] matrix) fromArray2D
(int[][] matrix) double
get
(int idx) double
get
(int i, int j) int[]
Get indexes of columns that carry datastatic int
getColIndexSum
(Map<Integer, Matrix> cellArray) int
getColMax
(int col) Return position of column maximumgetColumn
(int j) int[]
int
Get number of non-zerosint[]
Get number of rows with at least a non-zerodouble[]
Get array of non-zerosint
Get the total number of columnsint
Get the total number of elementsint
Get the total number of rowsgetRow
(int i) getRowElements
(int row) int
getRowMax
(int row) Return position of row maximumgetRowsFrom
(int row) getSlice
(boolean[] rowFlags, boolean[] colFlags) getSlice
(int rowStart, int rowEnd, int colStart, int colEnd) getSubMatrix
(Matrix rows, Matrix cols) void
growMaxColumns
(int newmax, boolean preserve) Grow the maximum number of columnsvoid
growMaxLength
(int newmax, boolean preserve) Grow the maximum number of elementsboolean
boolean
int
hashCode()
boolean
boolean
hasNaN()
static double
void
insert_sub_matrix
(int start_row, int start_col, int end_row, int end_col, Matrix matrix_to_be_inserted) Interesection of two matrices.inv()
static Matrix
boolean
isAssigned
(int row, int col) Check if sparse matrix element is assignedboolean
isDiag()
boolean
boolean
isEmpty()
boolean
Checks if two matrices are equalboolean
isEqualToTol
(Matrix m, double tol) Checks if two matrices are equal within a toleranceboolean
isFinite()
int
length()
log()
static double
static double
Computes log(sum_i exp(x_i)) of all elements x_i of the input matrixstatic Matrix
static int
Returns the position of the given row in the corresponding matrixstatic Matrix
matrix_add_vector
(Matrix matrix, Matrix vector) static double
maxAbsDiff
(Matrix a, Matrix b) meanCol()
meanRow()
void
void
static Matrix
double
norm()
Computes the Euclidean norm of a matrixstatic Matrix
oneMinusMatrix
(Matrix matrix) static Matrix
Decrease by one an element of an integer vector.static Matrix
Decrease by one an element of an integer vector.void
ones()
static Matrix
ones
(int rows, int cols) static Matrix
double
powerSumCols
(int col, double alpha) double
powerSumRows
(int row, double alpha) void
void
print()
void
Print non-zero valuesqr()
void
randMatrix
(int length) int
rank()
static Matrix
readFromFile
(String fileName) void
remove
(int row, int col) void
removeCols
(Collection<Integer> cols) Removes the specified columns from the given matrix.void
void
void
void
void
removeRows
(Collection<Integer> rows) Removes the specified rows from the given matrix.void
removeZeros
(double val) void
replace
(double delete, double replacement) repmat
(int rows, int cols) void
reshape
(int numRows, int numCols) Reshape matrix to the given number of rows and columnsvoid
reshape
(int numRows, int numCols, int arrayLength) Reshape number of rows, columns and the number of non-zerosreverse()
For a vector, return a matrix with reversed index positions.void
row_increase
(int row, double a) scale
(double scalar) static Matrix
scale_mult
(Matrix a, double n) void
scaleEq
(double scalar) void
schur()
void
set
(int idx, double val) void
set
(int row, int col, double val) void
set
(int row, int col, int val) setColumns
(int j0, int j1, Matrix cols) void
void
setSliceEq
(int rowStart, int rowEnd, int colStart, int colEnd, Matrix newSlice) void
Copy values of the input matrixvoid
setToNaN()
void
shrinkNumCols
(int newmax) Reduce the maximum number of columnsvoid
shrinkNumRows
(int newmax) Reduce the maximum number of rowsstatic Matrix
singleton
(double value) static boolean
sort()
Sort the elements of the matrixsortEq()
Sort the elements of the matrixsqrt()
square()
sub
(double x) void
subEq
(double x) void
void
double
sumAbsCols
(int col) double
sumAbsRows
(int row) sumCols()
double
sumCols
(int col) sumCols
(int startRow, int endRow) static double
sumCumprod
(Matrix matrix) sumRows()
double
sumRows
(int row) sumRows
(int startCol, int endCol) double
sumSubMatrix
(boolean[] rowIndexes, boolean[] colIndexes) double
sumSubMatrix
(int[] rowIndexes, int[] colIndexes) double
sumSubMatrix
(int startRow, int endRow, int startCol, int endCol) static Matrix
double[]
double[][]
org.ejml.data.DMatrixSparseCSC
org.ejml.data.DMatrixSparseCSC
toDMatrixSparseCSC
(Matrix matrix) toDouble()
int[]
toList1D()
toString()
uniqueInCol
(int colIdx) uniqueInRow
(int rowIdx) uniqueNonNegativeInCol
(int colIdx) uniqueNonNegativeInRow
(int rowIdx) uniqueNonZerosInCol
(int colIdx) uniqueNonZerosInRow
(int rowIdx) static UniqueRowResult
static UniqueRowResult
uniqueRows
(Matrix m) void
unsafe_set
(int row, int col, double val) Fast version of set that does not check boundsdouble
value()
weaklyConnect
(Matrix param, Set<Integer> colsToIgnore) Weakly-connected components of a sub-matrix.void
zero()
static Matrix
zeros
(int rows, int cols)
-
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
-
Matrix
public Matrix(org.ejml.data.DMatrixSparseCSC matrix) -
Matrix
public Matrix(double[][] arrays) -
Matrix
public Matrix(int[] array) -
Matrix
public Matrix(double[] array) -
Matrix
-
Matrix
-
Matrix
public Matrix(org.ejml.simple.SimpleMatrix matrix) -
Matrix
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
-
allbut
Returns all elements in a matrix except the first ones- Parameters:
y
- - a matrixk
- - a position of an element in the matrix- Returns:
- - A row vector with all elements of y but ypos
-
matchrow
Returns the position of the given row in the corresponding matrix- Parameters:
matrix
- - the matrix to be searchedrow
- - the row- Returns:
- - Position of the given row in the matrix, or -1 otherwise
-
weaklyConnect
Weakly-connected components of a sub-matrix.- Parameters:
param
- Input matrixcolsToIgnore
- Indexes to be ignored- Returns:
- Weighted connected components
-
oner
Decrease by one an element of an integer vector.- Parameters:
N
- integer vectors
- dimension to decrease- Returns:
- Decreased vector
-
oner
Decrease by one an element of an integer vector.- Parameters:
N
- integer vectorr
- dimension to decrease- Returns:
- Decreased vector
-
factln
Computes log(x!) of all elements x of the input matrix- Parameters:
n
- input matrix- Returns:
- Decreased vector
-
logsumexp
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
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 matrixmatrixB
- second input matrix
-
intersect
Interesection of two matrices. Return rows common to both- Parameters:
matrixA
- first input matrixmatrixB
- second input matrix
-
uniqueRowIndexes
-
uniqueRows
-
extract
-
extract
-
extractRows
-
extractRows
-
extractColumns
-
extractColumns
-
extractColumn
-
extractDiag
-
concatColumns
-
concatRows
-
diagMatrix
-
diag
-
solve
-
eye
-
ones
-
zeros
-
pow
-
first_norm
-
negative
-
scale_mult
-
inf_norm
-
cellsum
-
createLike
-
lyap
-
sylv
-
matrix_add_vector
-
col_vector_add_row_vector
-
columnMatrixToDoubleArray
-
findIndexWithZeroSum
- Parameters:
matrix
- , the matrix which is going to checkisRow
- , true if check through row, otherwise false- Returns:
- a list of integer show the index of row/col with zero-sum
-
maxAbsDiff
-
singleton
-
expm
-
inv
-
findRows
-
elementMinNonZero
-
compare
-
sumCumprod
-
logSum
-
cell2mat
-
getColIndexSum
-
exp
-
log
-
fact
-
factln
-
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
-
sort
Sort the elements of the matrix -
sortEq
Sort the elements of the matrix -
sqrt
-
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
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
-
toDMatrixSparseCSC
public org.ejml.data.DMatrixSparseCSC toDMatrixSparseCSC() -
toDMatrixSparseCSC
-
expandMatrix
public void expandMatrix(int rows, int cols, int nz_length) -
isDiag
public boolean isDiag() -
clone
-
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
-
cumsumViaCol
-
sumRows
public double sumRows(int row) -
sumRows
-
sumCols
public double sumCols(int col) -
sumCols
-
sumAbsCols
public double sumAbsCols(int col) -
sumAbsRows
public double sumAbsRows(int row) -
repmat
-
find
-
replace
public void replace(double delete, double replacement) -
findNumber
-
findZero
-
findNonNegative
-
uniqueInRow
-
uniqueNonZerosInRow
-
uniqueNonNegativeInRow
-
uniqueInCol
-
uniqueNonZerosInCol
-
uniqueNonNegativeInCol
-
count
public int count(double val) -
countEachRow
-
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
-
elementIncrease
-
meanCol
-
meanRow
-
elementPower
-
fromArray2D
-
zero
public void zero() -
fromArray2D
-
fill
-
transpose
-
sub
-
sub
-
sub
-
add
-
add
-
add
public void add(double alpha) -
subEq
public void subEq(double x) -
subEq
-
subEq
-
addEq
-
addEq
-
div
-
divEq
-
divideEq
public void divideEq(double scalar) -
divide
-
divideRows
public void divideRows(double[] diag, int offset) -
multEq
-
mult
-
mult
-
elementSum
public double elementSum() -
elementMin
public double elementMin() -
elementMax
public double elementMax() -
hadamard
-
elementMult
-
elementMult
-
elementDiv
Performs element-wise division- Parameters:
B
- - the other matrix- Returns:
- - A ./ B
-
elementMultWithVector
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
-
toArray2D
public double[][] toArray2D() -
toDouble
-
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
-
sumCols
-
ones
public void ones() -
krons
-
createBlockDiagonal
-
elementMaxAbs
public double elementMaxAbs() -
scale
-
scaleEq
public void scaleEq(double scalar) -
scaleEq
-
ceil
Computes the matrix resulted from ceiling every member of the current matrix- Returns:
- - the matrix obtained by ceiling the current matrix
-
ceilEq
Computes the matrix resulted from ceiling every member of the current matrix- Returns:
- - the matrix obtained by ceiling the current matrix
-
colon
-
columnMajorOrder
Equivalent to the colon operator in MATLAB: (:).- Returns:
- - the Matrix in a column-major order, flattened as a column vector.
-
getRow
-
getColumn
-
setColumn
-
setRow
-
setColumns
-
setRows
-
isEqualTo
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
Checks if two matrices are equal within a tolerance- Parameters:
m
- - the other matrixtol
- - 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
-
eigenvector
-
safe_mult
-
rank
public int rank() -
element_power
-
det
public double det() -
remove
public void remove(int row, int col) -
removeInfinite
public void removeInfinite() -
element_divide
-
hasDuplicates
public boolean hasDuplicates() -
right_matrix_divide
-
left_matrix_divide
-
qr
-
schur
-
schur
-
isDiag_withintol
public boolean isDiag_withintol() -
compatible_sizes_add
-
row_increase
public void row_increase(int row, double a) -
col_increase
public void col_increase(int col, double a) -
kron
-
print
public void print() -
prettyPrint
public void prettyPrint() -
toString
-
getRowElements
- Parameters:
row
-- Returns:
- new matrix include all elements from the given row index
-
removeRows
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
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
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
-
hashCode
public int hashCode() -
randMatrix
public void randMatrix(int length) -
getRowsFrom
-
expandMatrixToSquare
public void expandMatrixToSquare() -
mulByMinusOne
public void mulByMinusOne() -
getSlice
-
getSlice
-
setSlice
-
setSliceEq
-
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
-
findNonZeroRowsInColumn
- Parameters:
column
-- Returns:
- List of row index where the value at the given column is not zero
-
allEqualToOne
public boolean allEqualToOne() -
getSubMatrix
-
setToNaN
public void setToNaN() -
setNaNToZero
public void setNaNToZero() -
readFromFile
-
compareMatrix
-