public class SparseCRSMatrix extends SparseMatrixBase implements Clonable
Matrix.Partition, Matrix.WriteFormat
INDEFINITE, POSITIVE_DEFINITE, SPD, SYMMETRIC
Constructor and Description |
---|
SparseCRSMatrix()
Creates an empty SparseCRSMatrix with 0 x 0 size.
|
SparseCRSMatrix(double[] vals,
int[] cols,
int[] rowOffs)
Creates a sparse matrix from CRS data.
|
SparseCRSMatrix(int numRows,
int numCols)
Creates an empty sparse matrix with a specified size.
|
SparseCRSMatrix(Matrix M)
Creates a sparse matrix from an existing general matrix.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int i,
int j,
double v) |
void |
add(SparseCRSMatrix M)
Adds this matrix to M and places the result in this matrix.
|
void |
add(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Adds matrix M1 to M2 and places the result in this matrix.
|
void |
addRow(DynamicIntArray colIdxs,
DynamicDoubleArray values) |
void |
addRow(int[] colIdxs,
double[] values,
int numVals) |
void |
addScaledRow(VectorNd v,
double s,
int i)
Adds s times the i-th row to the vector v.
|
void |
checkConsistency()
Check that the internal structures of this matrix are consistent.
|
SparseCRSMatrix |
clone() |
int |
colSize()
Number of columns in the matrix associated with this transformation.
|
void |
combine(double a,
SparseCRSMatrix M1,
double b,
SparseCRSMatrix M2)
Forms the combination a M1 + b M2 and places the result in this
matrix.
|
double |
dotRow(int i,
VectorNd v)
Returns the dot product of the i-th row with a vector v.
|
double |
dotRowSelf(int i)
Returns the dot product of the i-th row with itself.
|
double |
get(int i,
int j)
Gets a single element of this matrix.
|
void |
get(MatrixNd M)
Copies the contents of this matrix into a dense
MatrixNd . |
int |
getCCSIndices(int[] rowIdxs,
int[] colOffs,
Matrix.Partition part,
int numRows,
int numCols)
Gets the compressed column storage (CCS) indices for a principal
sub-matrix of this matrix delimited by the first
numRows
rows and the first numCols columns. |
int |
getCCSValues(double[] vals,
Matrix.Partition part,
int numRows,
int numCols)
Gets the compressed column storage (CCS) values for a principal
sub-matrix of this matrix delimited by the first
numRows
rows and the first numCols columns. |
int |
getCRSIndices(int[] colIdxs,
int[] rowOffs,
Matrix.Partition part,
int numRows,
int numCols)
Gets the compressed row storage (CRS) indices for a principal sub-matrix
of this matrix delimited by the first
numRows rows and the
first numCols columns. |
int |
getCRSValues(double[] vals,
Matrix.Partition part,
int numRows,
int numCols)
Gets the compressed row storage (CRS) values for a principal sub-matrix
of this matrix delimited by the first
numRows rows and the
first numCols columns. |
boolean |
isFixedSize()
Returns true if this matrix is of fixed size.
|
double |
maxNorm()
Returns the max-norm of this matrix.
|
void |
mul(MatrixNd MR,
MatrixNd M1)
Computes the product
|
void |
mul(SparseCRSMatrix M)
Multiplies this matrix by M and places the result in this matrix.
|
void |
mul(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Multiplies matrix M1 by M2 and places the result in this matrix.
|
void |
mulLeft(MatrixNd MR,
MatrixNd M1)
Computes the product
|
void |
mulTranspose(SparseCRSMatrix M)
Multiplies this matrix by the transpose of M and places the result in this
matrix.
|
void |
mulTransposeBoth(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Multiplies the transpose of matrix M1 by the transpose of M2 and places
the result in this matrix.
|
void |
mulTransposeLeft(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Multiplies the transpose of matrix M1 by M2 and places the result in this
matrix.
|
void |
mulTransposeRight(MatrixNd MR,
MatrixNd M1)
Computes the product
|
void |
mulTransposeRight(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Multiplies matrix M1 by the transpose of M2 and places the result in this
matrix.
|
void |
negate()
Negates this matrix in place.
|
void |
negate(SparseCRSMatrix M)
Sets this matrix to the negative of another matrix M.
|
int |
numNonZeroVals()
Returns the number of non-zero values in this matrix object.
|
int |
numNonZeroVals(Matrix.Partition part,
int numRows,
int numCols)
Returns the number of non-zero values for a specified partition of a
principal sub-matrix of this matrix delimited by the first
numRows rows and the first numCols columns. |
void |
removeRow(int idx) |
void |
removeRows(int[] idxs) |
int |
rowSize()
Number of rows in the matrix associated with this transformation.
|
void |
scale(double s)
Scales the elements of this matrix by
s . |
void |
scale(double s,
SparseCRSMatrix M)
Scales the elements of matrix M1 by
s and places the
results in this matrix. |
void |
scaledAdd(double s,
SparseCRSMatrix M1,
SparseCRSMatrix M2)
Computes s M1 + M2 and places the result in this matrix.
|
void |
set(double[] values,
int[] indices,
int numVals)
Sets the elements of this matrix from an array of doubles and a list of
indices.
|
void |
set(int i,
int j,
double val)
Sets a single element of this matrix.
|
void |
set(Matrix M)
Sets the values of this matrix from an existing general matrix M.
|
void |
set(SparseCRSMatrix M)
Sets the values of this matrix from an existing SparseCRSMatrix M.
|
void |
setCRSValues(double[] vals,
int[] colIdxs,
int[] rowOffs,
int numVals,
int numRows,
Matrix.Partition part)
Sets the contents of this matrix given a set of values in compressed row
storage (CRS).
|
void |
setDiagonal(double[] vals)
Sets this matrix to a diagonal matrix whose diagonal elements are
specified by an array.
|
void |
setIdentity()
Sets this matrix to the identity matrix.
|
void |
setRandom()
Sets this matrix to have a random sparsity structure and values,
with between one and ten nonzero elements per row.
|
void |
setRandomValues()
Sets the nonzero elements of this matrix to random values
in the range -0.5 (inclusive) to 0.5 (exclusive).
|
void |
setRandomValues(double lower,
double upper,
java.util.Random generator)
Sets the nonzero elements of this matrix to random values
in a specified range.
|
void |
setSize(int numRows,
int numCols)
Sets the size of this matrix.
|
void |
setZero()
Sets the elements of this matrix to zero.
|
void |
sub(SparseCRSMatrix M)
Subtracts M from this matrix.
|
void |
sub(SparseCRSMatrix M1,
SparseCRSMatrix M2)
Subtracts M1 from M2 and places the result in this matrix.
|
java.lang.String |
toString()
Returns a String representation of this sparse matrix.
|
java.lang.String |
toString(NumberFormat fmt)
Returns a String representation of this sparse matrix.
|
java.lang.String |
toString(java.lang.String fmtStr)
Returns a String representation of this sparse matrix.
|
void |
transpose()
Sets this matrix its transpose, changing its size and
sparsity structure if necessary.
|
void |
transpose(SparseCRSMatrix M)
Sets this matrix to the transpose of M, changing the size and
sparsity structure of this matrix if necessary.
|
void |
zeroColumn(int j) |
void |
zeroRow(int i) |
setCCSValues, setRandom, setRandom, writeToFileCRS
containsNaN, determinant, epsilonEquals, equals, frobeniusNorm, frobeniusNormSquared, get, get, getCCSIndices, getCCSIndices, getCCSValues, getCCSValues, getColumn, getColumn, getColumn, getCRSIndices, getCRSIndices, getCRSValues, getCRSValues, getDefaultFormat, getRow, getRow, getRow, getSize, getSubMatrix, hasNaN, idString, infinityNorm, isSymmetric, isWritable, mul, mul, mul, mulAdd, mulAdd, mulAdd, mulTranspose, mulTranspose, mulTranspose, mulTransposeAdd, mulTransposeAdd, mulTransposeAdd, oneNorm, scan, scan, setCRSValues, setDefaultFormat, trace, write, write, write, write, write, write, write, writeToFile
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
determinant, epsilonEquals, equals, frobeniusNorm, frobeniusNormSquared, get, getCCSIndices, getCCSIndices, getCCSValues, getCCSValues, getColumn, getColumn, getColumn, getCRSIndices, getCRSIndices, getCRSValues, getCRSValues, getRow, getRow, getRow, getSize, getSubMatrix, infinityNorm, isSymmetric, mul, mul, mul, mulAdd, mulAdd, mulAdd, mulTranspose, mulTranspose, mulTranspose, mulTransposeAdd, mulTransposeAdd, mulTransposeAdd, oneNorm, scan, trace, write, write, write
public SparseCRSMatrix()
public SparseCRSMatrix(int numRows, int numCols)
numRows
- number of rowsnumCols
- number of columnspublic SparseCRSMatrix(double[] vals, int[] cols, int[] rowOffs)
vals
- if non-null, gives all the nonzero values, in row ordercols
- column index for each value, in row order.rowOffs
- gives the starting offset of each row into the cols and vals arraysjava.lang.IllegalArgumentException
- if the specified storgae format is inconsistentpublic SparseCRSMatrix(Matrix M)
M
- matrix to copypublic void setSize(int numRows, int numCols)
MatrixBase
isFixedSize
returns false.setSize
in interface Matrix
setSize
in class MatrixBase
numRows
- new row sizenumCols
- new column sizeMatrix.isFixedSize()
public int rowSize()
mul
.rowSize
in interface LinearTransformNd
rowSize
in interface Matrix
rowSize
in class MatrixBase
public int colSize()
mul
.colSize
in interface LinearTransformNd
colSize
in interface Matrix
colSize
in class MatrixBase
public boolean isFixedSize()
setSize
, or implicitly when used as a result for various
matrix operations.isFixedSize
in interface Matrix
isFixedSize
in class MatrixBase
Matrix.setSize(int, int)
public double get(int i, int j)
get
in interface Matrix
get
in class MatrixBase
i
- element row indexj
- element column indexpublic void get(MatrixNd M)
MatrixNd
.M
- matrix to receive the contents of this matrixpublic void set(int i, int j, double val)
i
- element row indexj
- element column indexval
- new element valuepublic void set(double[] values, int[] indices, int numVals)
indices[k*2]
and
indices[k*2+1]
, respectively. All non-specified elements
are set to zero.public void set(Matrix M)
set
in interface Matrix
set
in class SparseMatrixBase
M
- matrix used to set valuespublic void set(SparseCRSMatrix M)
M
- matrix used to set valuespublic void setIdentity()
This method does not trim matrix capacity.
public void setDiagonal(double[] vals)
This method does not trim matrix capacity.
vals
- diagonal elements for this matrixImproperSizeException
- if the length of diag
does not equal the minimum matrix
dimensionpublic void setZero()
public void setRandom()
public void setRandomValues()
public void setRandomValues(double lower, double upper, java.util.Random generator)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)generator
- random number generatorpublic void mul(SparseCRSMatrix M)
M
- right-hand matrixImproperSizeException
- if this matrix and M do not conformpublic void mul(SparseCRSMatrix M1, SparseCRSMatrix M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if this matrix and M1 and M2 do not conform.public void mulTranspose(SparseCRSMatrix M)
M
- right-hand matrixImproperSizeException
- if this matrix and the transpose of M do not conformpublic void mulTransposeRight(SparseCRSMatrix M1, SparseCRSMatrix M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose M2 do not comformpublic void mulTransposeLeft(SparseCRSMatrix M1, SparseCRSMatrix M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose M2 do not comformpublic void mulTransposeBoth(SparseCRSMatrix M1, SparseCRSMatrix M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose M2 do not comformpublic double dotRowSelf(int i)
i
- row indexpublic double dotRow(int i, VectorNd v)
i
- row indexv
- vector to take dot product withpublic void addScaledRow(VectorNd v, double s, int i)
v
- vector to add scaled row tos
- scaling factori
- row indexpublic void add(SparseCRSMatrix M)
M
- right-hand matrixImproperSizeException
- if this matrix and M have different sizes.public void add(SparseCRSMatrix M1, SparseCRSMatrix M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes.public void scaledAdd(double s, SparseCRSMatrix M1, SparseCRSMatrix M2)
s
- scaling factorM1
- matrix to be scaledM2
- matrix to be addedImproperSizeException
- if matrices M1 and M2 have different sizespublic void sub(SparseCRSMatrix M)
M
- matrix to subtractImproperSizeException
- if this matrix and M have different sizes.public void sub(SparseCRSMatrix M1, SparseCRSMatrix M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes.public void combine(double a, SparseCRSMatrix M1, double b, SparseCRSMatrix M2)
a
- scaling factor for M1M1
- left-hand matrixb
- scaling factor for M2M2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes.public void negate()
public void negate(SparseCRSMatrix M)
M
- matrix to bepublic void scale(double s)
s
.scale
in interface SparseMatrix
s
- scaling factorpublic void scale(double s, SparseCRSMatrix M)
s
and places the
results in this matrix. This matrix is resized if necessary.s
- scaling factorM
- matrix to be scaledpublic void transpose()
public void transpose(SparseCRSMatrix M)
M
- matrix to take the transpose ofpublic void mul(MatrixNd MR, MatrixNd M1)
MR = MS M1where
MS
is this matrix, and stores the result in MR
,
which is sized appropriately. If MR == M1
and the required
size of MR
does not equal that of M1
, then an internal
copy of M1
is made for computational purposes and M1
is resized as required.public void mulLeft(MatrixNd MR, MatrixNd M1)
MR = M1 MSwhere
MS
is this matrix, and stores the result in MR
,
which is sized appropriately. If MR == M1
and the required
size of MR
does not equal that of M1
, then an internal
copy of M1
is made for computational purposes and M1
is resized as required.public void mulTransposeRight(MatrixNd MR, MatrixNd M1)
MR = MS M1^There
MS
is this matrix, and stores the result in MR
,
which will be sized appropriately. If MR == M1
, then an internal
copy will be made of M1
for computational purposes
and M1
will be resized as needed to store the result.public java.lang.String toString()
( i j
value)
,
one tuple per line. Elements which are not printed are zero.toString
in class MatrixBase
MatrixBase.toString(String)
public java.lang.String toString(java.lang.String fmtStr)
toString
, except that
elements values are themselves formatted using a C printf
style format string. For a description of the format string syntax, see
NumberFormat
.toString
in interface Matrix
toString
in class MatrixBase
fmtStr
- numeric format specificationNumberFormat
public java.lang.String toString(NumberFormat fmt)
toString
, except that
elements values are themselves formatted using a C printf
style as decribed by the parameter NumberFormat
. When
called numerous times, this routine can be more efficient than
toString(String)
, because the
NumberFormat
does not need to be
recreated each time from a specification string.toString
in interface Matrix
toString
in class MatrixBase
fmt
- numeric formatpublic int numNonZeroVals()
MatrixBase
numNonZeroVals
in interface Matrix
numNonZeroVals
in class MatrixBase
public int numNonZeroVals(Matrix.Partition part, int numRows, int numCols)
MatrixBase
numRows
rows and the first numCols
columns.
If the matrix is dense and the partition is Full
,
then this will simply be the product of the number of rows times the
number of columns.numNonZeroVals
in interface Matrix
numNonZeroVals
in class MatrixBase
part
- matrix parition to be examinednumRows
- number of rows delimiting the sub-matrixnumCols
- number of columns delimiting the sub-matrixpublic void setCRSValues(double[] vals, int[] colIdxs, int[] rowOffs, int numVals, int numRows, Matrix.Partition part)
part
specifies what portion of
the matrix is supplied. If it is Full
, then the
entire matrix is supplied. If it is Matrix.Partition.UpperTriangular
,
then only the upper triangular part is assumed to be supplied, and the
lower triangular part is set from its transpose (matrix bounds
permitting). For Partition.UpperTriangular, supplied entries which are
not upper triangular will either be ignored or generate an exception.
All specified data must fit within the current matrix bounds; the matrix is not resized.
CRS data takes the form of three arrays:
vals
and
colIdxs
corresponding to the first non-zero element in each
row. All values are 1-based, so that first offset value is
1, the second offset value is n+1, where n is the number of non-zero
elements in the first row, etc. The final value is set to nvals+1, where
nvals is the number of non-zero elements in the matrix.setCRSValues
in interface Matrix
setCRSValues
in class SparseMatrixBase
vals
- non-zero element values. This array must have a length equal at
least to nvals
.colIdxs
- column indices for each non-zero element.
This array must have a length equal at
least to nvals
.rowOffs
- row start offsets into vals
and
colIdxs
. This array must have a length equal at least to
nrows+1, where nrows is the number of rows in this matrix.numVals
- number of non-zero valuesnumRows
- number of specified rows. Indicates the maximum value of
rowOffs
that will be used; will not resize matrix.part
- must be either Full
or
UpperTriangular
.public int getCRSIndices(int[] colIdxs, int[] rowOffs, Matrix.Partition part, int numRows, int numCols)
numRows
rows and the
first numCols
columns. Indices are 1-based and supplied in
row-major order. For a detailed description of the CRS format, see setCRSValues()
. Some matrix types may place restrictions
on the sub-matrix; for instance, a block-structured matrix may require
that the sub-matrix be block aligned.getCRSIndices
in interface Matrix
getCRSIndices
in class MatrixBase
colIdxs
- returns the column indices of each non-zero element,
in row-major order. This array must have a length equal
at least to the number of non-zero elements.rowOffs
- returns the row start offsets into colIdxs
,
followed by nvals+1, where nvals is the number of non-zero elements in
the sub-matrix. This array must have a length equal at least to nrows+1,
where nrows is the number of rows in the sub-matrix.part
- specifies what portion of the sub-matrix to store; must be either
Full
or
UpperTriangular
numRows
- number of rows delimiting the sub-matrixnumCols
- number of columns delimiting the sub-matrixpublic int getCRSValues(double[] vals, Matrix.Partition part, int numRows, int numCols)
numRows
rows and the
first numCols
columns. Values are supplied in row-major
order. For a detailed decsription of the CRS format, see setCRSValues()
. Some matrix types may place restrictions
on the sub-matrix; for instance, a block-structured matrix may require
that the sub-matrix be block aligned.getCRSValues
in interface Matrix
getCRSValues
in class MatrixBase
vals
- returns the value of each non-zero element. This array must have a length
equal at least to the number of non-zero elements in the sub-matrix.part
- specifies what portion of the sub-matrix to store; must be either
Full
or
UpperTriangular
numRows
- number of rows delimiting the sub-matrixnumCols
- number of columns delimiting the sub-matrixpublic int getCCSIndices(int[] rowIdxs, int[] colOffs, Matrix.Partition part, int numRows, int numCols)
numRows
rows and the first numCols
columns. Indices are 1-based and
supplied in column-major order. For a detailed decsription of the CCS
format, see setCCSValues()
. Some matrix types may
place restrictions on the sub-matrix; for instance, a block-structured
matrix may require that the sub-matrix be block aligned.getCCSIndices
in interface Matrix
getCCSIndices
in class MatrixBase
rowIdxs
- returns the row indices of each non-zero element,
in column-major order. This array must have a length equal
at least to the number of non-zero elements.colOffs
- returns the column start offsets into rowIdxs
,
followed by nvals+1, where nvals is the number of non-zero elements in
the sub-matrix. This array must have a length equal at least to ncol+1,
where ncols is the number of columns in the sub-matrix.part
- specifies what portion of the sub-matrix to store; must be either
Full
or
LowerTriangular
numRows
- number of rows delimiting the sub-matrixnumCols
- number of columns delimiting the sub-matrixpublic int getCCSValues(double[] vals, Matrix.Partition part, int numRows, int numCols)
numRows
rows and the first numCols
columns. Values are supplied in
column-major order. For a detailed decsription of the CCS format, see
setCCSValues()
. Some matrix types may place
restrictions on the sub-matrix; for instance, a block-structured matrix
may require that the sub-matrix be block aligned.getCCSValues
in interface Matrix
getCCSValues
in class MatrixBase
vals
- returns the value of each non-zero element. This array must have a length
equal at least to the number of non-zero elements in the sub-matrix.part
- specifies what portion of the sub-matrix to store; must be either
Full
or
LowerTriangular
numRows
- number of rows delimiting the sub-matrixnumCols
- number of columns delimiting the sub-matrixpublic void checkConsistency()
checkConsistency
in interface Matrix
public SparseCRSMatrix clone()
public double maxNorm()
Matrix
maxNorm
in interface Matrix
maxNorm
in class MatrixBase
public void zeroRow(int i)
public void zeroColumn(int j)
public void add(int i, int j, double v)
public void removeRow(int idx)
public void removeRows(int[] idxs)
public void addRow(int[] colIdxs, double[] values, int numVals)
public void addRow(DynamicIntArray colIdxs, DynamicDoubleArray values)