public class SparseMatrixCRS extends SparseMatrixBase implements LinearTransformNd
Matrix.Partition, Matrix.WriteFormat
INDEFINITE, POSITIVE_DEFINITE, SPD, SYMMETRIC
Constructor and Description |
---|
SparseMatrixCRS()
Creates an empty sparse matrix with 0 size.
|
SparseMatrixCRS(double[] vals,
int[] cols,
int[] rowOffs)
Creates a sparse matrix from CRS data.
|
SparseMatrixCRS(int numRows,
int numCols)
Creates an empty sparse matrix with a specified size.
|
SparseMatrixCRS(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(SparseMatrixCRS M)
Adds this matrix to M and places the result in this matrix.
|
void |
add(SparseMatrixCRS M1,
SparseMatrixCRS M2)
Adds matrix M1 to M2 and places the result in this matrix.
|
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.
|
int |
colSize()
Number of columns in the matrix associated with this transformation.
|
void |
combine(double a,
SparseMatrixCRS M1,
double b,
SparseMatrixCRS 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.
|
void |
ensureCapacity(int newcap)
Ensures that the nonzero storage capacity of this matrix is at least n.
|
double |
get(int i,
int j)
Gets a single element of this matrix.
|
int |
getCapacity()
Returns the nonzero storage capacity of this matrix
|
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. |
int |
getNonzeroElements(double[] values)
Returns the nonzero element values in row-major order.
|
boolean |
isFixedSize()
Returns true if this matrix is of fixed size.
|
double |
maxNorm()
Returns the max-norm of this matrix.
|
void |
mul(SparseMatrixCRS M)
Multiplies this matrix by M and places the result in this matrix.
|
void |
mul(SparseMatrixCRS M1,
SparseMatrixCRS M2)
Multiplies matrix M1 by M2 and places the result in this matrix.
|
void |
mulTranspose(SparseMatrixCRS M)
Multiplies this matrix by the transpose of M and places the result in this
matrix.
|
void |
mulTransposeBoth(SparseMatrixCRS M1,
SparseMatrixCRS M2)
Multiplies the transpose of matrix M1 by the transpose of M2 and places
the result in this matrix.
|
void |
mulTransposeLeft(SparseMatrixCRS M1,
SparseMatrixCRS M2)
Multiplies the transpose of matrix M1 by M2 and places the result in this
matrix.
|
void |
mulTransposeRight(SparseMatrixCRS M1,
SparseMatrixCRS 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(SparseMatrixCRS M)
Sets this matrix to the negative of another matrix M.
|
int |
numNonzeroElements()
Returns the number of nonzero elements in this matrix.
|
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. |
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,
SparseMatrixCRS M)
Scales the elements of matrix M1 by
s and places the
results in this matrix. |
void |
scaledAdd(double s,
SparseMatrixCRS M1,
SparseMatrixCRS 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 value) |
void |
set(Matrix M)
Sets the values of this matrix from an existing general matrix M.
|
void |
set(Matrix M,
Matrix.Partition part)
Sets the values of this matrix from an existing general matrix M,
assuming the supplied partition type
|
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 |
setNonzeroElements(double[] values)
Sets the existing nonzero element values, in row-major order.
|
void |
setRandom()
Sets this matrix to have a random sparsity structure and values,
with between one and four 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(SparseMatrixCRS M)
Subtracts M from this matrix.
|
void |
sub(SparseMatrixCRS M1,
SparseMatrixCRS 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(SparseMatrixCRS M)
Sets this matrix to the transpose of M, changing the size and
sparsity structure of this matrix if necessary.
|
void |
trimCapacity()
Trims the nonzero storage capacity of this matrix to fit
the exact number of nonzeros.
|
void |
zeroColumn(int j) |
void |
zeroRow(int i) |
setCCSValues, setRandom, setRandom
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, mul, mul, mul, mulAdd, mulAdd, mulAdd, mulTranspose, mulTranspose, mulTranspose, mulTransposeAdd, mulTransposeAdd, mulTransposeAdd, numNonZeroVals, oneNorm, scan, setCRSValues, setDefaultFormat, trace, write, write, write, write, write, write, writeToFile
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
mul
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, numNonZeroVals, oneNorm, scan, trace, write, write, write
public SparseMatrixCRS()
public SparseMatrixCRS(int numRows, int numCols)
numRows
- number of rowsnumCols
- number of columnspublic SparseMatrixCRS(Matrix M)
M
- matrix to copypublic SparseMatrixCRS(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 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 void ensureCapacity(int newcap)
newcap
- desired storage capacitypublic int getCapacity()
public void trimCapacity()
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 int getNonzeroElements(double[] values)
values
- collects the nonzero element valuesjava.lang.ArrayIndexOutOfBoundsException
- if the array is not large enough to store all the valuespublic void setNonzeroElements(double[] values)
values
- new values for the nonzero elementsjava.lang.ArrayIndexOutOfBoundsException
- if the array is not large enough to provide all the valuespublic int numNonzeroElements()
public void set(int i, int j, double value)
public 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, Matrix.Partition part)
M
- matrix used to set valuespart
- partition typepublic void set(Matrix M)
set
in interface Matrix
set
in class SparseMatrixBase
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(SparseMatrixCRS M)
M
- right-hand matrixImproperSizeException
- if this matrix and M do not conformpublic void mul(SparseMatrixCRS M1, SparseMatrixCRS M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if this matrix and M1 and M2 do not conform.public void mulTranspose(SparseMatrixCRS M)
M
- right-hand matrixImproperSizeException
- if this matrix and the transpose of M do not conformpublic void mulTransposeRight(SparseMatrixCRS M1, SparseMatrixCRS M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose M2 do not comformpublic void mulTransposeLeft(SparseMatrixCRS M1, SparseMatrixCRS M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose M2 do not comformpublic void mulTransposeBoth(SparseMatrixCRS M1, SparseMatrixCRS 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(SparseMatrixCRS M)
M
- right-hand matrixImproperSizeException
- if this matrix and M have different sizes.public void add(SparseMatrixCRS M1, SparseMatrixCRS M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes.public void scaledAdd(double s, SparseMatrixCRS M1, SparseMatrixCRS M2)
s
- scaling factorM1
- matrix to be scaledM2
- matrix to be addedImproperSizeException
- if matrices M1 and M2 have different sizespublic void sub(SparseMatrixCRS M)
M
- matrix to subtractImproperSizeException
- if this matrix and M have different sizes.public void sub(SparseMatrixCRS M1, SparseMatrixCRS M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes.public void combine(double a, SparseMatrixCRS M1, double b, SparseMatrixCRS 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(SparseMatrixCRS M)
M
- matrix to bepublic void scale(double s)
s
.scale
in interface SparseMatrix
s
- scaling factorpublic void scale(double s, SparseMatrixCRS 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(SparseMatrixCRS M)
M
- matrix to take the transpose ofpublic 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(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 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.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 void zeroRow(int i)
public void zeroColumn(int j)
public double maxNorm()
Matrix
maxNorm
in interface Matrix
maxNorm
in class MatrixBase
public void add(int i, int j, double v)