public class SparseMatrix extends Object implements Matrix
1.0 0.0 4.0
0.0 3.0 5.0
2.0 0.0 6.0
is stored as values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
,
rowIndices=[0, 2, 1, 0, 1, 2]
, colPointers=[0, 2, 3, 6]
.
param: numRows number of rows
param: numCols number of columns
param: colPtrs the index corresponding to the start of a new column (if not transposed)
param: rowIndices the row index of the entry (if not transposed). They must be in strictly
increasing order for each column
param: values nonzero matrix entries in column major (if not transposed)
param: isTransposed whether the matrix is transposed. If true, the matrix can be considered
Compressed Sparse Row (CSR) format, where colPtrs
behaves as rowPtrs,
and rowIndices
behave as colIndices, and values
are stored in row major.
Constructor and Description |
---|
SparseMatrix(int numRows,
int numCols,
int[] colPtrs,
int[] rowIndices,
double[] values)
Column-major sparse matrix.
|
SparseMatrix(int numRows,
int numCols,
int[] colPtrs,
int[] rowIndices,
double[] values,
boolean isTransposed) |
Modifier and Type | Method and Description |
---|---|
double |
apply(int i,
int j)
Gets the (i, j)-th element.
|
scala.collection.Iterator<Vector> |
colIter()
Returns an iterator of column vectors.
|
int[] |
colPtrs() |
static Matrix |
compressed() |
static Matrix |
compressedColMajor() |
static Matrix |
compressedRowMajor() |
SparseMatrix |
copy()
Get a deep copy of the matrix.
|
boolean |
equals(Object o) |
void |
foreachActive(scala.Function3<Object,Object,Object,scala.runtime.BoxedUnit> f)
Applies a function
f to all the active elements of dense and sparse matrix. |
static SparseMatrix |
fromCOO(int numRows,
int numCols,
scala.collection.Iterable<scala.Tuple3<Object,Object,Object>> entries)
Generate a
SparseMatrix from Coordinate List (COO) format. |
int |
hashCode() |
boolean |
isTransposed()
Flag that keeps track whether the matrix is transposed or not.
|
static DenseMatrix |
multiply(DenseMatrix y) |
static DenseVector |
multiply(DenseVector y) |
static DenseVector |
multiply(Vector y) |
int |
numActives()
Find the number of values stored explicitly.
|
int |
numCols()
Number of columns.
|
int |
numNonzeros()
Find the number of non-zero active values.
|
int |
numRows()
Number of rows.
|
int[] |
rowIndices() |
static scala.collection.Iterator<Vector> |
rowIter() |
static SparseMatrix |
spdiag(Vector vector)
Generate a diagonal matrix in
SparseMatrix format from the supplied values. |
static SparseMatrix |
speye(int n)
Generate an Identity Matrix in
SparseMatrix format. |
static SparseMatrix |
sprand(int numRows,
int numCols,
double density,
java.util.Random rng)
Generate a
SparseMatrix consisting of i.i.d . |
static SparseMatrix |
sprandn(int numRows,
int numCols,
double density,
java.util.Random rng)
Generate a
SparseMatrix consisting of i.i.d . |
static double[] |
toArray() |
static DenseMatrix |
toDense() |
static DenseMatrix |
toDenseColMajor() |
static DenseMatrix |
toDenseRowMajor() |
static SparseMatrix |
toSparse() |
static SparseMatrix |
toSparseColMajor() |
static SparseMatrix |
toSparseRowMajor() |
static String |
toString() |
static String |
toString(int maxLines,
int maxLineWidth) |
SparseMatrix |
transpose()
Transpose the Matrix.
|
double[] |
values() |
compressed, compressedColMajor, compressedRowMajor, getDenseSizeInBytes, getSparseSizeInBytes, isColMajor, isRowMajor, multiply, multiply, multiply, rowIter, toArray, toDense, toDenseColMajor, toDenseRowMajor, toSparse, toSparseColMajor, toSparseRowMajor, toString, toString
public SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed)
public SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values)
1.0 0.0 4.0
0.0 3.0 5.0
2.0 0.0 6.0
is stored as values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
,
rowIndices=[0, 2, 1, 0, 1, 2]
, colPointers=[0, 2, 3, 6]
.
numRows
- number of rowsnumCols
- number of columnscolPtrs
- the index corresponding to the start of a new columnrowIndices
- the row index of the entry. They must be in strictly increasing
order for each columnvalues
- non-zero matrix entries in column majorpublic static SparseMatrix fromCOO(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object,Object,Object>> entries)
SparseMatrix
from Coordinate List (COO) format. Input must be an array of
(i, j, value) tuples. Entries that have duplicate values of i and j are
added together. Tuples where value is equal to zero will be omitted.numRows
- number of rows of the matrixnumCols
- number of columns of the matrixentries
- Array of (i, j, value) tuplesSparseMatrix
public static SparseMatrix speye(int n)
SparseMatrix
format.n
- number of rows and columns of the matrixSparseMatrix
with size n
x n
and values of ones on the diagonalpublic static SparseMatrix sprand(int numRows, int numCols, double density, java.util.Random rng)
SparseMatrix
consisting of i.i.d
. uniform random numbers. The number of non-zero
elements equal the ceiling of numRows
x numCols
x density
numRows
- number of rows of the matrixnumCols
- number of columns of the matrixdensity
- the desired density for the matrixrng
- a random number generatorSparseMatrix
with size numRows
x numCols
and values in U(0, 1)public static SparseMatrix sprandn(int numRows, int numCols, double density, java.util.Random rng)
SparseMatrix
consisting of i.i.d
. gaussian random numbers.numRows
- number of rows of the matrixnumCols
- number of columns of the matrixdensity
- the desired density for the matrixrng
- a random number generatorSparseMatrix
with size numRows
x numCols
and values in N(0, 1)public static SparseMatrix spdiag(Vector vector)
SparseMatrix
format from the supplied values.vector
- a Vector
that will form the values on the diagonal of the matrixSparseMatrix
with size values.length
x values.length
and non-zero
values
on the diagonalpublic static double[] toArray()
public static scala.collection.Iterator<Vector> rowIter()
public static DenseMatrix multiply(DenseMatrix y)
public static DenseVector multiply(DenseVector y)
public static DenseVector multiply(Vector y)
public static String toString()
public static String toString(int maxLines, int maxLineWidth)
public static SparseMatrix toSparseColMajor()
public static SparseMatrix toSparseRowMajor()
public static SparseMatrix toSparse()
public static DenseMatrix toDense()
public static DenseMatrix toDenseRowMajor()
public static DenseMatrix toDenseColMajor()
public static Matrix compressedColMajor()
public static Matrix compressedRowMajor()
public static Matrix compressed()
public int numRows()
Matrix
public int numCols()
Matrix
public int[] colPtrs()
public int[] rowIndices()
public double[] values()
public boolean isTransposed()
Matrix
isTransposed
in interface Matrix
public int hashCode()
hashCode
in class Object
public boolean equals(Object o)
equals
in class Object
public double apply(int i, int j)
Matrix
public SparseMatrix copy()
Matrix
public SparseMatrix transpose()
Matrix
Matrix
instance sharing the same underlying data.public void foreachActive(scala.Function3<Object,Object,Object,scala.runtime.BoxedUnit> f)
Matrix
f
to all the active elements of dense and sparse matrix. The ordering
of the elements are not defined.
foreachActive
in interface Matrix
f
- the function takes three parameters where the first two parameters are the row
and column indices respectively with the type Int
, and the final parameter is the
corresponding value in the matrix with type Double
.public int numNonzeros()
Matrix
numNonzeros
in interface Matrix
public int numActives()
Matrix
numActives
in interface Matrix