public class SparseMatrixNd extends SparseMatrixBase implements LinearTransformNd
Normally, these matrices can be resized, either explicitly through a call to
setSize
, or implicitly through operations that require the
matrix size to be modified. However, specialized sub-classes (such as those
implementing sub-matrices) may have a fixed size.
Matrix.Partition, Matrix.WriteFormat
INDEFINITE, POSITIVE_DEFINITE, SPD, SYMMETRIC
Constructor and Description |
---|
SparseMatrixNd(int numRows,
int numCols)
Creates a sparse matrix of a specific size, and initializes its elements
to 0.
|
SparseMatrixNd(Matrix M)
Creates a sparse matrix whose size and elements are the same as an
existing Matrix.
|
SparseMatrixNd(SparseMatrixNd M)
Creates a sparse matrix which is a copy of an existing one.
|
Modifier and Type | Method and Description |
---|---|
void |
add(SparseMatrixNd M)
Adds this matrix to M and places the result in this matrix.
|
void |
add(SparseMatrixNd M1,
SparseMatrixNd M2)
Adds matrix M1 to M2 and places the result in this matrix.
|
void |
addEntry(SparseMatrixCell cell,
SparseMatrixCell rowPrev,
SparseMatrixCell colPrev) |
void |
checkConsistency()
Check that the internal structures of this matrix are consistent.
|
int |
colSize()
Number of columns in the matrix associated with this transformation.
|
double |
get(int i,
int j)
Gets a single element 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. |
SparseMatrixCell |
getCell(int i,
int j) |
SparseMatrixCell |
getCol(int j) |
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 |
getExplicitElements(double[] values)
Returns the explicit element values in row-major order.
|
SparseMatrixCell |
getRow(int i) |
boolean |
isFixedSize()
Returns true if this matrix is of fixed size.
|
double |
maxNorm()
Returns the max-norm of this matrix.
|
void |
mul(SparseMatrixNd M)
Multiplies this matrix by M and places the result in this matrix.
|
void |
mul(SparseMatrixNd M1,
SparseMatrixNd M2)
Multiplies matrix M1 by M2 and places the result in this matrix.
|
void |
mulTranspose(SparseMatrixNd M)
Multiplies this matrix by the transpose of M and places the result in this
matrix.
|
void |
mulTransposeBoth(SparseMatrixNd M1,
SparseMatrixNd M2)
Multiplies the transpose of matrix M1 by the transpose of M2 and places
the result in this matrix.
|
void |
mulTransposeLeft(SparseMatrixNd M1,
SparseMatrixNd M2)
Multiplies the transpose of matrix M1 by M2 and places the result in this
matrix.
|
void |
mulTransposeRight(SparseMatrixNd M1,
SparseMatrixNd 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(SparseMatrixNd M)
Sets this matrix to the negative of M1.
|
int |
numExplicitElements()
Returns the number of explicit elements in this matrix.
|
int |
numNonZeroVals(Matrix.Partition part,
int nrows,
int ncols)
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. |
SparseMatrixCell |
prevColEntry(int i,
int j) |
SparseMatrixCell |
prevRowEntry(int i,
int j) |
void |
removeEntry(SparseMatrixCell cell,
SparseMatrixCell rowPrev,
SparseMatrixCell colPrev) |
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,
SparseMatrixNd M)
Scales the elements of matrix M1 by
s and places the
results in this matrix. |
void |
set(double[] values,
int[] indices,
int numNonZeroEntries)
Sets the elements of this matrix from an array of doubles and a list of
indices.
|
void |
set(int[] rowIdxs,
int[] colIdxs,
double[] values,
int numNonZeroEntries)
Sets the elements of this matrix from two index arrays and an array of
doubles that contain, respectively, the row index, column index, and
numeric value for each non-zero entry.
|
void |
set(int i,
int j,
double value) |
void |
set(SparseMatrixNd M)
Sets the size and values of this matrix to those of matrix M.
|
void |
setDiagonal(double[] vals)
Sets this matrix to a diagonal matrix whose diagonal elements are
specified by an array.
|
void |
setExplicitElements(double[] values)
Sets the existing explicit element values, in row-major order.
|
void |
setIdentity()
Sets this matrix to the identity matrix.
|
void |
setRandom()
Sets some randomly selected elements of this matrix to random values.
|
void |
setRandom(double lower,
double upper,
int numElements)
Sets some randomly selected elements of this matrix to random values.
|
void |
setRandom(double lower,
double upper,
int numElements,
java.util.Random generator)
Sets some randomly selected elements of this matrix to random values.
|
void |
setSize(int numRows,
int numCols)
Sets the size of this matrix.
|
void |
setSubMatrix(int baseRow,
int baseCol,
SparseMatrixNd Msrc)
Sets a submatrix of this matrix.
|
void |
setZero()
Sets the elements of this matrix to zero.
|
void |
setZero(int i,
int j) |
void |
sub(SparseMatrixNd M)
Subtracts this matrix from M1 and places the result in this matrix.
|
void |
sub(SparseMatrixNd M1,
SparseMatrixNd M2)
Subtracts matrix 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()
Replaces this matrix by its tranpose.
|
void |
transpose(SparseMatrixNd M)
Takes the transpose of matrix M and places the result in this matrix.
|
set, setCCSValues, setCRSValues, 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, numNonZeroVals, oneNorm, scan, scan, setCRSValues, setDefaultFormat, trace, write, 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 SparseMatrixNd(int numRows, int numCols)
numRows
- number of rowsnumCols
- number of columnsImproperSizeException
- if numRows or numCols are negativepublic SparseMatrixNd(Matrix M)
M
- matrix object to be copied.public SparseMatrixNd(SparseMatrixNd M)
M
- matrix object to be copied.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 being used as a result for
various matrix operations.isFixedSize
in interface Matrix
isFixedSize
in class MatrixBase
MatrixNd.setSize(int, int)
public void setSize(int numRows, int numCols)
isFixedSize
returns false.
If a matrix is resized, then any previous element values which are still within the new matrix dimensions are preserved. Other (new) element values are undefined.
setSize
in interface Matrix
setSize
in class MatrixBase
numRows
- new row sizenumCols
- new column sizeImproperSizeException
- if this matrix has an explicit internal buffer and that buffer is too
small to support the requested sizejava.lang.UnsupportedOperationException
- if this matrix has fixed sizeisFixedSize()
public double get(int i, int j)
get
in interface Matrix
get
in class MatrixBase
i
- element row indexj
- element column indexpublic SparseMatrixCell getCell(int i, int j)
public int getExplicitElements(double[] values)
values
- collects the explicit element valuesjava.lang.ArrayIndexOutOfBoundsException
- if the array is not large enough to store all the valuespublic void setExplicitElements(double[] values)
values
- new values for the explicit elementsjava.lang.ArrayIndexOutOfBoundsException
- if the array is not large enough to provide all the valuespublic int numExplicitElements()
public void set(int i, int j, double value)
public void setZero(int i, int j)
public void set(double[] values, int[] indices, int numNonZeroEntries)
indices[k*2]
and
indices[k*2+1]
, respectively. All non-specified elements
are set to zero.public void set(int[] rowIdxs, int[] colIdxs, double[] values, int numNonZeroEntries)
numNonZeroEntries
,
and each of the arrays should have at least this many elements.rowIdxs
- row index values for each non-zero entrycolIdxs
- column index values for each non-zero entryvalues
- numeric values for each non-zero entrynumNonZeroEntries
- number of non-zero entrespublic void addEntry(SparseMatrixCell cell, SparseMatrixCell rowPrev, SparseMatrixCell colPrev)
public void removeEntry(SparseMatrixCell cell, SparseMatrixCell rowPrev, SparseMatrixCell colPrev)
public SparseMatrixCell prevColEntry(int i, int j)
public SparseMatrixCell prevRowEntry(int i, int j)
public void set(SparseMatrixNd M)
M
- matrix tp be copiedImproperSizeException
- if matrices have different sizes and this matrix cannot be resized
accordinglypublic void setIdentity()
public void setZero()
public void setDiagonal(double[] vals)
vals
- diagonal elements for this matrixImproperSizeException
- if the size of diag
does not equal the minimum matrix
dimensionpublic void setRandom()
public void setRandom(double lower, double upper, int numElements)
numElements
, and the values are uniformly distributed in in
a specified range. Other elements are set to zero.lower
- lower random value (inclusive)upper
- upper random value (exclusive)numElements
- number of explicit elements to randomly selectpublic void setRandom(double lower, double upper, int numElements, java.util.Random generator)
numElements
, and the values are uniformly distributed in in
a specified range. Other elements are set to zero. Random numbers are
generated by a supplied random number generator.lower
- lower random value (inclusive)upper
- upper random value (exclusive)numElements
- number of explicit values to randomly selectgenerator
- random number generatorpublic void mul(SparseMatrixNd M)
M
- right-hand matrixImproperSizeException
- if this matrix and M do not conform, or if this matrix needs resizing but
is of fixed sizepublic void mul(SparseMatrixNd M1, SparseMatrixNd M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 do not comform, or if this matrix needs resizing but
is of fixed sizepublic void mulTranspose(SparseMatrixNd M)
M
- right-hand matrixImproperSizeException
- if this matrix and the transpose of M do not conform, or if this matrix
needs resizing but is of fixed sizepublic void mulTransposeRight(SparseMatrixNd M1, SparseMatrixNd M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if M1 and the transpose of M2 do not comform, or if this matrix needs
resizing but is of fixed sizepublic void mulTransposeLeft(SparseMatrixNd M1, SparseMatrixNd M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if the transpose of M1 and M2 do not comform, or if this matrix needs
resizing but is of fixed sizepublic void mulTransposeBoth(SparseMatrixNd M1, SparseMatrixNd M2) throws ImproperSizeException
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if the transpose of M1 and the transpose of M2 do not comform, or if this
matrix needs resizing but is of fixed sizepublic void add(SparseMatrixNd M)
M
- right-hand matrixImproperSizeException
- if this matrix and M have different sizespublic void add(SparseMatrixNd M1, SparseMatrixNd M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes, or if this matrix needs
resizing but is of fixed sizepublic void sub(SparseMatrixNd M)
M
- right-hand matrixImproperSizeException
- if this matrix and M have different sizespublic void sub(SparseMatrixNd M1, SparseMatrixNd M2)
M1
- left-hand matrixM2
- right-hand matrixImproperSizeException
- if matrices M1 and M2 have different sizes, or if this matrix needs
resizing but is of fixed sizepublic void negate()
public void negate(SparseMatrixNd M)
M
- matrix to negateImproperSizeException
- if this matrix needs resizing but is of fixed sizepublic void scale(double s, SparseMatrixNd M) throws ImproperSizeException
s
and places the
results in this matrix. This matrix is resized if necessary.s
- scaling factorM
- matrix to be scaledImproperSizeException
- if this matrix needs resizing but is of fixed sizepublic void scale(double s)
s
.scale
in interface SparseMatrix
s
- scaling factorpublic void transpose()
ImproperSizeException
- if this matrix needs resizing but is of fixed sizepublic void transpose(SparseMatrixNd M) throws ImproperSizeException
M
- matrix to take the transpose ofImproperSizeException
- if this matrix needs resizing but is of fixed sizepublic 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 nrows, int ncols)
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 examinednrows
- number of rows delimiting the sub-matrixncols
- number of columns delimiting the sub-matrixpublic 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 setSubMatrix(int baseRow, int baseCol, SparseMatrixNd Msrc) throws ImproperSizeException
baseRow
and baseCol
, and the
new values are given by the matrix Msrc
. The size of the
submatrix is determined by the dimensions of Msrc
.baseRow
- index of the first row of the submatrixbaseCol
- index of the first column of the submatrixMsrc
- new values for the submatrix.ImproperSizeException
- if baseRow
or baseCol
are negative, or if
the submatrix exceeds the current matrix boundspublic SparseMatrixCell getRow(int i)
public SparseMatrixCell getCol(int j)
public void checkConsistency()
checkConsistency
in interface Matrix
public double maxNorm()
Matrix
maxNorm
in interface Matrix
maxNorm
in class MatrixBase