public class SparseMatrix extends java.lang.Object implements Matrix, MatrixMultiplication<SparseMatrix,SparseMatrix>
Operations using standard dense matrix structures and algorithms are slow and consume large amounts of memory when applied to large sparse matrices. Indeed, some very large sparse matrices are infeasible to manipulate with the standard dense algorithms. Sparse data is by nature easily compressed, and this compression almost always results in significantly less computer data storage usage.
This class employs Harwell-Boeing column-compressed sparse matrix format. Nonzero values are stored in an array (top-to-bottom, then left-to-right-bottom). The row indices corresponding to the values are also stored. Besides, a list of pointers are indexes where each column starts. This format is efficient for arithmetic operations, column slicing, and matrix-vector products. One typically uses SparseDataset for construction of SparseMatrix.
| Constructor and Description |
|---|
SparseMatrix(double[][] D)
Constructor.
|
SparseMatrix(double[][] D,
double tol)
Constructor.
|
SparseMatrix(int nrows,
int ncols,
double[] x,
int[] rowIndex,
int[] colIndex)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
SparseMatrix |
aat()
Returns A * A'
|
SparseMatrix |
abmm(SparseMatrix B)
Returns the matrix multiplication C = A * B.
|
SparseMatrix |
abtmm(SparseMatrix B)
Returns the result of matrix multiplication A * B'.
|
SparseMatrix |
ata()
Returns A' * A
|
SparseMatrix |
atbmm(SparseMatrix B)
Returns the result of matrix multiplication A' * B.
|
double[] |
atx(double[] x,
double[] y)
y = A' * x
|
double[] |
atxpy(double[] x,
double[] y)
y = A' * x + y
|
double[] |
atxpy(double[] x,
double[] y,
double b)
y = A' * x + b * y
|
double[] |
ax(double[] x,
double[] y)
y = A * x
|
double[] |
axpy(double[] x,
double[] y)
y = A * x + y
|
double[] |
axpy(double[] x,
double[] y,
double b)
y = A * x + b * y
|
double[] |
diag()
Returns the diagonal elements.
|
EigenValueDecomposition |
eigen(int k)
Returns the k largest eigen pairs.
|
double |
get(int i,
int j)
Returns the entry value at row i and column j.
|
int |
ncols()
Returns the number of columns.
|
int |
nrows()
Returns the number of rows.
|
int |
size()
Returns the number of nonzero values.
|
SparseMatrix |
transpose()
Returns the matrix transpose.
|
double[] |
values()
Returns all nonzero values.
|
public SparseMatrix(int nrows,
int ncols,
double[] x,
int[] rowIndex,
int[] colIndex)
nrows - the number of rows in the matrix.ncols - the number of columns in the matrix.rowIndex - the row indices of nonzero values.colIndex - the index of the start of columns.x - the array of nonzero values stored column by column.public SparseMatrix(double[][] D)
D - a dense matrix to converted into sparse matrix format.public SparseMatrix(double[][] D,
double tol)
D - a dense matrix to converted into sparse matrix format.tol - the tolerance to regard a value as zero if |x| < tol.public int nrows()
Matrixpublic int ncols()
Matrixpublic int size()
public double[] values()
public double get(int i,
int j)
Matrixpublic double[] ax(double[] x,
double[] y)
Matrixpublic double[] axpy(double[] x,
double[] y)
Matrixpublic double[] axpy(double[] x,
double[] y,
double b)
Matrixpublic double[] atx(double[] x,
double[] y)
Matrixpublic double[] atxpy(double[] x,
double[] y)
Matrixpublic double[] atxpy(double[] x,
double[] y,
double b)
Matrixpublic SparseMatrix transpose()
Matrixpublic SparseMatrix abmm(SparseMatrix B)
abmm in interface MatrixMultiplication<SparseMatrix,SparseMatrix>public SparseMatrix abtmm(SparseMatrix B)
MatrixMultiplicationabtmm in interface MatrixMultiplication<SparseMatrix,SparseMatrix>public SparseMatrix atbmm(SparseMatrix B)
MatrixMultiplicationatbmm in interface MatrixMultiplication<SparseMatrix,SparseMatrix>public SparseMatrix ata()
Matrixpublic SparseMatrix aat()
Matrixpublic EigenValueDecomposition eigen(int k)