public class CSRSparseMatrix extends Object implements SparseMatrix
(value, col_ind, row_ptr). Three arrays are used to represent a sparse matrix:
value stores all the non-zero entries of the matrix from left to right, top to bottom.
col_ind is the column indices corresponding to the values. row_ptr is the
list of values indices where each row starts. For example:
\[
\begin{bmatrix}
1 & 2 & 0 & 0\\
0 & 3 & 9 & 0\\
0 & 1 & 4 & 0
\end{bmatrix}
\]
Note:value = [ 1 2 3 9 1 4 ] col_ind = [ 0 1 1 2 1 2 ] row_ptr = [ 0 2 4 6 ]
(row_ptr[i] - row_ptr[i - 1]) is the number of non-zero entries in row i.
This format is very inefficient for incremental construction or changes using
set(int, int, double), but efficient for matrix computation.SparseMatrix.Entry, SparseMatrix.ValueArray| Constructor and Description |
|---|
CSRSparseMatrix(CSRSparseMatrix that)
Copy constructor.
|
CSRSparseMatrix(int nRows,
int nCols)
Constructs a sparse matrix in CSR format.
|
CSRSparseMatrix(int nRows,
int nCols,
int[] rowIndices,
int[] columnIndices,
double[] values)
Constructs a sparse matrix in CSR format.
|
CSRSparseMatrix(int nRows,
int nCols,
List<SparseMatrix.Entry> entries)
Constructs a sparse matrix in CSR format by a list of non-zero entries.
|
CSRSparseMatrix(int nRows,
int nCols,
List<SparseMatrix.Entry> entries,
boolean areEntriesSorted)
Constructs a sparse matrix in CSR format by a list of non-zero entries.
|
CSRSparseMatrix(Matrix A)
Constructs a sparse matrix from a matrix.
|
| Modifier and Type | Method and Description |
|---|---|
Matrix |
add(Matrix that)
this + that
|
CSRSparseMatrix |
deepCopy()
The implementation returns an instance created from
this by the copy
constructor of the class, or just this if the instance itself is
immutable. |
boolean |
equals(Object obj) |
double |
get(int i,
int j)
Get the matrix entry at [i,j].
|
SparseVector |
getColumn(int j)
Get the specified column in the matrix as a vector.
|
List<SparseMatrix.Entry> |
getEntryList()
Exports the non-zero values in the matrix as a list of
SparseMatrix.Entrys. |
SparseVector |
getRow(int i)
Get the specified row in the matrix as a vector.
|
SparseMatrix.ValueArray |
getValueArray()
Exports the non-zero values in the matrix as arrays of row/column indices
and values.
|
int |
hashCode() |
Matrix |
minus(Matrix that)
this - that
|
Matrix |
multiply(Matrix that)
this * that
|
Vector |
multiply(Vector v)
Right multiply this matrix, A, by a vector.
|
int |
nCols()
Gets the number of columns.
|
int |
nNonZeros()
Get the number of non-zero entries in the structure.
|
int |
nRows()
Gets the number of rows.
|
CSRSparseMatrix |
ONE()
Get an identity matrix that has the same dimension as this matrix.
|
CSRSparseMatrix |
opposite()
Get the opposite of this matrix.
|
CSRSparseMatrix |
scaled(double c)
Scale this matrix, A, by a constant.
|
void |
set(int row,
int col,
double value)
Set the matrix entry at [i,j] to a value.
|
CSRSparseMatrix |
t()
Get the transpose of this matrix.
|
DenseMatrix |
toDense()
Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix,
DenseMatrix. |
String |
toString() |
CSRSparseMatrix |
ZERO()
Get a zero matrix that has the same dimension as this matrix.
|
public CSRSparseMatrix(int nRows,
int nCols)
nRows - the number of rowsnCols - the number of columnspublic CSRSparseMatrix(int nRows,
int nCols,
int[] rowIndices,
int[] columnIndices,
double[] values)
nRows - the number of rowsnCols - the number of columnsrowIndices - the row indices of the non-zeros valuescolumnIndices - the column indices of the non-zeros valuesvalues - the non-zero valuespublic CSRSparseMatrix(int nRows,
int nCols,
List<SparseMatrix.Entry> entries)
nRows - the number of rowsnCols - the number of columnsentries - the list of entriespublic CSRSparseMatrix(int nRows,
int nCols,
List<SparseMatrix.Entry> entries,
boolean areEntriesSorted)
nRows - the number of rowsnCols - the number of columnsentries - the list of entriesareEntriesSorted - true if the list of entries are sorted in row-major orderpublic CSRSparseMatrix(Matrix A)
A - a matrixpublic CSRSparseMatrix(CSRSparseMatrix that)
that - the matrix to be copiedpublic int nRows()
Tablepublic int nCols()
Tablepublic List<SparseMatrix.Entry> getEntryList()
SparseMatrixSparseMatrix.Entrys. This is useful
for converting between the different formats of SparseMatrix.Entry. For example,
// construct matrix using DOK DOKSparseMatrix dok = new DOKSparseMatrix(5, 5); // ... insert some values to DOK matrix // convert to CSR matrix for efficient matrix operations CSRSparseMatrix csr = new CSRSparseMatrix(5, 5, dok.getEntryList());
getEntryList in interface SparseMatrixpublic SparseMatrix.ValueArray getValueArray()
SparseMatrixgetValueArray in interface SparseMatrixSparseMatrix.ValueArraypublic void set(int row,
int col,
double value)
MatrixAccessset in interface MatrixAccessrow - the row indexcol - the column indexvalue - the value to set A[i,j] topublic double get(int i,
int j)
MatrixAccessget in interface MatrixAccessi - the row indexj - the column indexpublic SparseVector getRow(int i) throws MatrixAccessException
MatrixgetRow in interface Matrixi - the row indexMatrixAccessException - when i < 1, or when i > the number of rowspublic SparseVector getColumn(int j) throws MatrixAccessException
MatrixgetColumn in interface Matrixj - the column indexMatrixAccessException - when j < 1, or when j > the number of columnspublic Matrix add(Matrix that)
MatrixRingadd in interface MatrixRingadd in interface AbelianGroup<Matrix>that - a matrixthis and thatpublic Matrix minus(Matrix that)
MatrixRingminus in interface MatrixRingminus in interface AbelianGroup<Matrix>that - a matrixthis and thatpublic Matrix multiply(Matrix that)
MatrixRingmultiply in interface MatrixRingmultiply in interface Monoid<Matrix>that - a matrixthis and thatpublic Vector multiply(Vector v)
Matrixpublic CSRSparseMatrix scaled(double c)
Matrixpublic CSRSparseMatrix opposite()
MatrixRingopposite in interface MatrixRingopposite in interface AbelianGroup<Matrix>public CSRSparseMatrix t()
MatrixRingt in interface MatrixRingpublic DenseMatrix toDense()
DensifiableDenseMatrix.toDense in interface DensifiableDenseMatrixpublic CSRSparseMatrix ZERO()
MatrixRingZERO in interface MatrixRingZERO in interface AbelianGroup<Matrix>public CSRSparseMatrix ONE()
MatrixRingONE in interface MatrixRingONE in interface Monoid<Matrix>public CSRSparseMatrix deepCopy()
DeepCopyablethis by the copy
constructor of the class, or just this if the instance itself is
immutable.deepCopy in interface MatrixdeepCopy in interface DeepCopyablepublic int nNonZeros()
SparseStructurenNonZeros in interface SparseStructureCopyright © 2010-2020 NM FinTech Ltd.. All Rights Reserved.