Package org.la4j.matrix.sparse
Class CRSMatrix
java.lang.Object
org.la4j.Matrix
org.la4j.matrix.SparseMatrix
org.la4j.matrix.RowMajorSparseMatrix
org.la4j.matrix.sparse.CRSMatrix
This is a CRS (Compressed Row Storage) matrix class.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]
private static final byte
private static final int
private int[]
private double[]
Fields inherited from class org.la4j.matrix.SparseMatrix
cardinality
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate int
align
(int cardinality) blankOfShape
(int rows, int columns) Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.static CRSMatrix
copyOfShape
(int rows, int columns) Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.static CRSMatrix
diagonal
(int size, double diagonal) void
each
(MatrixProcedure procedure) Applies givenprocedure
to each element of this matrix.void
eachInRow
(int i, VectorProcedure procedure) Applies givenprocedure
to each element of specified row of this matrix.void
eachNonZero
(MatrixProcedure procedure) Applies givenprocedure
to each non-zero element of this matrix.void
eachNonZeroInRow
(int i, VectorProcedure procedure) Applies the givenprocedure
to each non-zero element of the specified row of this matrix.static CRSMatrix
from1DArray
(int rows, int columns, double[] array) Creates a newCRSMatrix
from the given 1Darray
with compressing (copying) the underlying array.static CRSMatrix
from2DArray
(double[][] array) Creates a newCRSMatrix
from the given 2Darray
with compressing (copying) the underlying array.static CRSMatrix
fromBinary
(byte[] array) DecodesCRSMatrix
from the given bytearray
.static CRSMatrix
ParsesCRSMatrix
from the given CSV string.static CRSMatrix
ParsesCRSMatrix
from the given Matrix Market string.getColumn
(int j) Copies the specified column of this matrix into the vector.double
getOrElse
(int i, int j, double defaultValue) Gets the specified element, or adefaultValue
if there is no actual element at (i
,j
) in this sparse matrix.getRow
(int i) Copies the specified row of this matrix into the vector.private void
growUp()
static CRSMatrix
identity
(int size) Creates an identityCRSMatrix
of the givensize
.private void
insert
(int k, int i, int j, double value) iteratorOfRow
(int i) Returns a vector iterator of the given row {code i}.double
max()
Searches for the maximum value of the elements of this matrix.double
maxInRow
(int i) Searches for the maximum value of specified row in this matrix.double
min()
Searches for the minimum value of the elements of this matrix.double
minInRow
(int i) Searches for the minimum value of specified row in this matrix.boolean
nonZeroAt
(int i, int j) Whether or not the specified element is not zero.nonZeroIteratorOfRow
(int i) Returns a non-zero vector iterator of the given rowi
.Returns a non-zero row-major matrix iterator.static CRSMatrix
static CRSMatrix
randomSymmetric
(int size, double density, Random random) Creates a random symmetricCRSMatrix
of the givensize
.private void
remove
(int k, int i) Returns a row-major matrix iterator.private int
searchForColumnIndex
(int j, int left, int right) select
(int[] rowIndices, int[] columnIndices) Returns a CRSMatrix with the selected rows and columns.void
set
(int i, int j, double value) Sets the specified element of this matrix to givenvalue
.void
setAll
(double value) Sets all elements of this matrix to the givenvalue
.<T extends Matrix>
Tto
(MatrixFactory<T> factory) Converts this matrix using the givenfactory
.byte[]
toBinary()
Encodes this matrix into a byte array.void
updateAt
(int i, int j, MatrixFunction function) Updates the specified element of this matrix by applying givenfunction
.static CRSMatrix
zero
(int rows, int columns) static CRSMatrix
zero
(int rows, int columns, int capacity) Methods inherited from class org.la4j.matrix.RowMajorSparseMatrix
apply, apply, apply, isRowMajor, rotate, transpose
Methods inherited from class org.la4j.matrix.SparseMatrix
add, capacity, cardinality, density, eachNonZeroInColumn, ensureCardinalityIsCorrect, foldNonZero, foldNonZeroInColumn, foldNonZeroInColumns, foldNonZeroInRow, foldNonZeroInRows, get, isColumnMajor, isZeroAt, multiply, nonZeroColumnMajorIterator, nonZeroIterator, nonZeroIteratorOfColumn, toMatrixMarket
Methods inherited from class org.la4j.Matrix
add, blank, blankOfColumns, blankOfRows, columnMajorIterator, columns, constant, copy, copyOfColumns, copyOfRows, determinant, diagonalProduct, divide, eachInColumn, ensureDimensionsAreCorrect, ensureIndexesAreInBounds, equals, equals, euclideanNorm, fail, fold, foldColumn, foldColumns, foldRow, foldRows, hadamardProduct, hashCode, infinityNorm, insert, insert, insert, insert, insertColumn, insertRow, is, is, iterator, iteratorOfColumn, kroneckerProduct, manhattanNorm, maxInColumn, minInColumn, mkString, mkString, mkString, multiply, multiply, multiplyByItsTranspose, non, non, norm, power, product, random, randomSymmetric, rank, removeColumn, removeFirstColumn, removeFirstRow, removeLastColumn, removeLastRow, removeRow, rows, setColumn, setColumn, setRow, setRow, shuffle, slice, sliceBottomRight, sliceTopLeft, subtract, subtract, sum, swapColumns, swapRows, toColumnMajorSparseMatrix, toColumnVector, toCSV, toCSV, toDenseMatrix, toMatrixMarket, toRowMajorSparseMatrix, toRowVector, toSparseMatrix, toString, trace, transform, transformColumn, transformRow, unit, update, updateColumn, updateRow, withDecompositor, withInverter, withSolver
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
MATRIX_TAG
private static final byte MATRIX_TAG- See Also:
-
MINIMUM_SIZE
private static final int MINIMUM_SIZE- See Also:
-
values
private double[] values -
columnIndices
private int[] columnIndices -
rowPointers
private int[] rowPointers
-
-
Constructor Details
-
CRSMatrix
public CRSMatrix() -
CRSMatrix
public CRSMatrix(int rows, int columns) -
CRSMatrix
public CRSMatrix(int rows, int columns, int capacity) -
CRSMatrix
public CRSMatrix(int rows, int columns, int cardinality, double[] values, int[] columnIndices, int[] rowPointers)
-
-
Method Details
-
zero
-
zero
-
diagonal
-
identity
Creates an identityCRSMatrix
of the givensize
. -
random
-
randomSymmetric
Creates a random symmetricCRSMatrix
of the givensize
. -
from1DArray
Creates a newCRSMatrix
from the given 1Darray
with compressing (copying) the underlying array. -
from2DArray
Creates a newCRSMatrix
from the given 2Darray
with compressing (copying) the underlying array. -
block
-
fromBinary
DecodesCRSMatrix
from the given bytearray
.- Parameters:
array
- the byte array representing a matrix- Returns:
- a decoded matrix
-
fromCSV
ParsesCRSMatrix
from the given CSV string.- Parameters:
csv
- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
ParsesCRSMatrix
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed matrix
-
getOrElse
public double getOrElse(int i, int j, double defaultValue) Description copied from class:SparseMatrix
Gets the specified element, or adefaultValue
if there is no actual element at (i
,j
) in this sparse matrix.- Specified by:
getOrElse
in classSparseMatrix
- Parameters:
i
- the element's row indexj
- the element's column indexdefaultValue
- the default value- Returns:
- the element of this vector or a default value
-
set
public void set(int i, int j, double value) Description copied from class:Matrix
Sets the specified element of this matrix to givenvalue
. -
setAll
public void setAll(double value) Description copied from class:Matrix
Sets all elements of this matrix to the givenvalue
. -
getRow
Description copied from class:Matrix
Copies the specified row of this matrix into the vector.- Overrides:
getRow
in classSparseMatrix
- Parameters:
i
- the row index- Returns:
- the row represented as vector
-
getColumn
Description copied from class:Matrix
Copies the specified column of this matrix into the vector.- Overrides:
getColumn
in classSparseMatrix
- Parameters:
j
- the column index- Returns:
- the column represented as vector
-
copyOfShape
Description copied from class:Matrix
Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.- Specified by:
copyOfShape
in classMatrix
- Parameters:
rows
- the number of rows in new matrixcolumns
- the number of columns in new matrix- Returns:
- the copy of this matrix with new size
-
eachNonZero
Description copied from class:SparseMatrix
Applies givenprocedure
to each non-zero element of this matrix.- Overrides:
eachNonZero
in classSparseMatrix
- Parameters:
procedure
- the matrix procedure
-
each
Description copied from class:Matrix
Applies givenprocedure
to each element of this matrix. -
eachInRow
Description copied from class:Matrix
Applies givenprocedure
to each element of specified row of this matrix. -
eachNonZeroInRow
Description copied from class:SparseMatrix
Applies the givenprocedure
to each non-zero element of the specified row of this matrix.- Overrides:
eachNonZeroInRow
in classSparseMatrix
- Parameters:
i
- the row index.procedure
- theVectorProcedure
.
-
updateAt
Description copied from class:Matrix
Updates the specified element of this matrix by applying givenfunction
. -
nonZeroAt
public boolean nonZeroAt(int i, int j) Description copied from class:SparseMatrix
Whether or not the specified element is not zero.- Specified by:
nonZeroAt
in classSparseMatrix
- Parameters:
i
- element's row indexj
- element's column index- Returns:
true
if specified element is not zero,false
otherwise
-
searchForColumnIndex
private int searchForColumnIndex(int j, int left, int right) -
insert
private void insert(int k, int i, int j, double value) -
remove
private void remove(int k, int i) -
growUp
private void growUp() -
align
private int align(int cardinality) -
max
public double max()Description copied from class:Matrix
Searches for the maximum value of the elements of this matrix. -
min
public double min()Description copied from class:Matrix
Searches for the minimum value of the elements of this matrix. -
maxInRow
public double maxInRow(int i) Description copied from class:Matrix
Searches for the maximum value of specified row in this matrix. -
minInRow
public double minInRow(int i) Description copied from class:Matrix
Searches for the minimum value of specified row in this matrix. -
select
Returns a CRSMatrix with the selected rows and columns. -
to
Description copied from class:Matrix
Converts this matrix using the givenfactory
. -
blankOfShape
Description copied from class:Matrix
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.- Specified by:
blankOfShape
in classMatrix
- Returns:
- blank matrix
-
iteratorOfNonZeroRows
- Specified by:
iteratorOfNonZeroRows
in classRowMajorSparseMatrix
-
rowMajorIterator
Description copied from class:Matrix
Returns a row-major matrix iterator.- Overrides:
rowMajorIterator
in classMatrix
- Returns:
- a row-major matrix iterator.
-
nonZeroRowMajorIterator
Description copied from class:SparseMatrix
Returns a non-zero row-major matrix iterator.- Overrides:
nonZeroRowMajorIterator
in classSparseMatrix
- Returns:
- a non-zero row-major matrix iterator.
-
nonZeroIteratorOfRow
Description copied from class:SparseMatrix
Returns a non-zero vector iterator of the given rowi
.- Overrides:
nonZeroIteratorOfRow
in classSparseMatrix
- Returns:
- a non-zero vector iterator
-
iteratorOfRow
Description copied from class:Matrix
Returns a vector iterator of the given row {code i}.- Overrides:
iteratorOfRow
in classMatrix
- Returns:
- a vector iterator
-
toBinary
public byte[] toBinary()Description copied from class:Matrix
Encodes this matrix into a byte array.
-