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
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
public class CRSMatrix extends RowMajorSparseMatrix
This is a CRS (Compressed Row Storage) matrix class.
-
-
Field Summary
Fields Modifier and Type Field Description private int[]columnIndicesprivate static byteMATRIX_TAGprivate static intMINIMUM_SIZEprivate int[]rowPointersprivate double[]values-
Fields inherited from class org.la4j.matrix.SparseMatrix
cardinality
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private intalign(int cardinality)MatrixblankOfShape(int rows, int columns)Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rowsxcolumns.static CRSMatrixblock(Matrix a, Matrix b, Matrix c, Matrix d)MatrixcopyOfShape(int rows, int columns)Copies this matrix into the new matrix with specified dimensions:rowsandcolumns.static CRSMatrixdiagonal(int size, double diagonal)voideach(MatrixProcedure procedure)Applies givenprocedureto each element of this matrix.voideachInRow(int i, VectorProcedure procedure)Applies givenprocedureto each element of specified row of this matrix.voideachNonZero(MatrixProcedure procedure)Applies givenprocedureto each non-zero element of this matrix.voideachNonZeroInRow(int i, VectorProcedure procedure)Applies the givenprocedureto each non-zero element of the specified row of this matrix.static CRSMatrixfrom1DArray(int rows, int columns, double[] array)Creates a newCRSMatrixfrom the given 1Darraywith compressing (copying) the underlying array.static CRSMatrixfrom2DArray(double[][] array)Creates a newCRSMatrixfrom the given 2Darraywith compressing (copying) the underlying array.static CRSMatrixfromBinary(byte[] array)DecodesCRSMatrixfrom the given bytearray.static CRSMatrixfromCSV(java.lang.String csv)ParsesCRSMatrixfrom the given CSV string.static CRSMatrixfromMatrixMarket(java.lang.String mm)ParsesCRSMatrixfrom the given Matrix Market string.VectorgetColumn(int j)Copies the specified column of this matrix into the vector.doublegetOrElse(int i, int j, double defaultValue)Gets the specified element, or adefaultValueif there is no actual element at (i,j) in this sparse matrix.VectorgetRow(int i)Copies the specified row of this matrix into the vector.private voidgrowUp()static CRSMatrixidentity(int size)Creates an identityCRSMatrixof the givensize.private voidinsert(int k, int i, int j, double value)java.util.Iterator<java.lang.Integer>iteratorOfNonZeroRows()VectorIteratoriteratorOfRow(int i)Returns a vector iterator of the given row {code i}.doublemax()Searches for the maximum value of the elements of this matrix.doublemaxInRow(int i)Searches for the maximum value of specified row in this matrix.doublemin()Searches for the minimum value of the elements of this matrix.doubleminInRow(int i)Searches for the minimum value of specified row in this matrix.booleannonZeroAt(int i, int j)Whether or not the specified element is not zero.VectorIteratornonZeroIteratorOfRow(int i)Returns a non-zero vector iterator of the given rowi.RowMajorMatrixIteratornonZeroRowMajorIterator()Returns a non-zero row-major matrix iterator.static CRSMatrixrandom(int rows, int columns, double density, java.util.Random random)static CRSMatrixrandomSymmetric(int size, double density, java.util.Random random)Creates a random symmetricCRSMatrixof the givensize.private voidremove(int k, int i)RowMajorMatrixIteratorrowMajorIterator()Returns a row-major matrix iterator.private intsearchForColumnIndex(int j, int left, int right)Matrixselect(int[] rowIndices, int[] columnIndices)Returns a CRSMatrix with the selected rows and columns.voidset(int i, int j, double value)Sets the specified element of this matrix to givenvalue.voidsetAll(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.voidupdateAt(int i, int j, MatrixFunction function)Updates the specified element of this matrix by applying givenfunction.static CRSMatrixzero(int rows, int columns)static CRSMatrixzero(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
-
-
-
-
Field Detail
-
MATRIX_TAG
private static final byte MATRIX_TAG
- See Also:
- Constant Field Values
-
MINIMUM_SIZE
private static final int MINIMUM_SIZE
- See Also:
- Constant Field Values
-
values
private double[] values
-
columnIndices
private int[] columnIndices
-
rowPointers
private int[] rowPointers
-
-
Method Detail
-
zero
public static CRSMatrix zero(int rows, int columns)
-
zero
public static CRSMatrix zero(int rows, int columns, int capacity)
-
diagonal
public static CRSMatrix diagonal(int size, double diagonal)
-
identity
public static CRSMatrix identity(int size)
Creates an identityCRSMatrixof the givensize.
-
random
public static CRSMatrix random(int rows, int columns, double density, java.util.Random random)
-
randomSymmetric
public static CRSMatrix randomSymmetric(int size, double density, java.util.Random random)
Creates a random symmetricCRSMatrixof the givensize.
-
from1DArray
public static CRSMatrix from1DArray(int rows, int columns, double[] array)
Creates a newCRSMatrixfrom the given 1Darraywith compressing (copying) the underlying array.
-
from2DArray
public static CRSMatrix from2DArray(double[][] array)
Creates a newCRSMatrixfrom the given 2Darraywith compressing (copying) the underlying array.
-
fromBinary
public static CRSMatrix fromBinary(byte[] array)
DecodesCRSMatrixfrom the given bytearray.- Parameters:
array- the byte array representing a matrix- Returns:
- a decoded matrix
-
fromCSV
public static CRSMatrix fromCSV(java.lang.String csv)
ParsesCRSMatrixfrom the given CSV string.- Parameters:
csv- the CSV string representing a matrix- Returns:
- a parsed matrix
-
fromMatrixMarket
public static CRSMatrix fromMatrixMarket(java.lang.String mm)
ParsesCRSMatrixfrom 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:SparseMatrixGets the specified element, or adefaultValueif there is no actual element at (i,j) in this sparse matrix.- Specified by:
getOrElsein 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:MatrixSets the specified element of this matrix to givenvalue.
-
setAll
public void setAll(double value)
Description copied from class:MatrixSets all elements of this matrix to the givenvalue.
-
getRow
public Vector getRow(int i)
Description copied from class:MatrixCopies the specified row of this matrix into the vector.- Overrides:
getRowin classSparseMatrix- Parameters:
i- the row index- Returns:
- the row represented as vector
-
getColumn
public Vector getColumn(int j)
Description copied from class:MatrixCopies the specified column of this matrix into the vector.- Overrides:
getColumnin classSparseMatrix- Parameters:
j- the column index- Returns:
- the column represented as vector
-
copyOfShape
public Matrix copyOfShape(int rows, int columns)
Description copied from class:MatrixCopies this matrix into the new matrix with specified dimensions:rowsandcolumns.- Specified by:
copyOfShapein 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
public void eachNonZero(MatrixProcedure procedure)
Description copied from class:SparseMatrixApplies givenprocedureto each non-zero element of this matrix.- Overrides:
eachNonZeroin classSparseMatrix- Parameters:
procedure- the matrix procedure
-
each
public void each(MatrixProcedure procedure)
Description copied from class:MatrixApplies givenprocedureto each element of this matrix.
-
eachInRow
public void eachInRow(int i, VectorProcedure procedure)Description copied from class:MatrixApplies givenprocedureto each element of specified row of this matrix.
-
eachNonZeroInRow
public void eachNonZeroInRow(int i, VectorProcedure procedure)Description copied from class:SparseMatrixApplies the givenprocedureto each non-zero element of the specified row of this matrix.- Overrides:
eachNonZeroInRowin classSparseMatrix- Parameters:
i- the row index.procedure- theVectorProcedure.
-
updateAt
public void updateAt(int i, int j, MatrixFunction function)Description copied from class:MatrixUpdates the specified element of this matrix by applying givenfunction.
-
nonZeroAt
public boolean nonZeroAt(int i, int j)Description copied from class:SparseMatrixWhether or not the specified element is not zero.- Specified by:
nonZeroAtin classSparseMatrix- Parameters:
i- element's row indexj- element's column index- Returns:
trueif specified element is not zero,falseotherwise
-
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:MatrixSearches for the maximum value of the elements of this matrix.
-
min
public double min()
Description copied from class:MatrixSearches for the minimum value of the elements of this matrix.
-
maxInRow
public double maxInRow(int i)
Description copied from class:MatrixSearches for the maximum value of specified row in this matrix.
-
minInRow
public double minInRow(int i)
Description copied from class:MatrixSearches for the minimum value of specified row in this matrix.
-
select
public Matrix select(int[] rowIndices, int[] columnIndices)
Returns a CRSMatrix with the selected rows and columns.
-
to
public <T extends Matrix> T to(MatrixFactory<T> factory)
Description copied from class:MatrixConverts this matrix using the givenfactory.
-
blankOfShape
public Matrix blankOfShape(int rows, int columns)
Description copied from class:MatrixCreates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rowsxcolumns.- Specified by:
blankOfShapein classMatrix- Returns:
- blank matrix
-
iteratorOfNonZeroRows
public java.util.Iterator<java.lang.Integer> iteratorOfNonZeroRows()
- Specified by:
iteratorOfNonZeroRowsin classRowMajorSparseMatrix
-
rowMajorIterator
public RowMajorMatrixIterator rowMajorIterator()
Description copied from class:MatrixReturns a row-major matrix iterator.- Overrides:
rowMajorIteratorin classMatrix- Returns:
- a row-major matrix iterator.
-
nonZeroRowMajorIterator
public RowMajorMatrixIterator nonZeroRowMajorIterator()
Description copied from class:SparseMatrixReturns a non-zero row-major matrix iterator.- Overrides:
nonZeroRowMajorIteratorin classSparseMatrix- Returns:
- a non-zero row-major matrix iterator.
-
nonZeroIteratorOfRow
public VectorIterator nonZeroIteratorOfRow(int i)
Description copied from class:SparseMatrixReturns a non-zero vector iterator of the given rowi.- Overrides:
nonZeroIteratorOfRowin classSparseMatrix- Returns:
- a non-zero vector iterator
-
iteratorOfRow
public VectorIterator iteratorOfRow(int i)
Description copied from class:MatrixReturns a vector iterator of the given row {code i}.- Overrides:
iteratorOfRowin classMatrix- Returns:
- a vector iterator
-
-