|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmaspack.matrix.MatrixBase
maspack.matrix.SparseMatrixBase
maspack.matrix.SparseMatrixNd
public class SparseMatrixNd
Implements general sparse m x n matrices, along with most the commonly used operations associated with such matrices. A sparse matrix is implemented as a linked list of cells representing the explicit elements, with links running in both the row and column directions. In general, explicit elements are non-zero and non-explicit elements are zero, although zero-valued explicit elements may arise computationally.
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.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface maspack.matrix.Matrix |
---|
Matrix.Partition, Matrix.WriteFormat |
Field Summary |
---|
Fields inherited from interface maspack.matrix.Matrix |
---|
INDEFINITE, POSITIVE_DEFINITE, SPD, SYMMETRIC |
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
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. |
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 numValues)
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(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. |
Methods inherited from class maspack.matrix.SparseMatrixBase |
---|
set, setCCSValues, setCRSValues, setRandom, setRandom |
Methods inherited from class maspack.matrix.MatrixBase |
---|
containsNaN, determinant, epsilonEquals, equals, frobeniusNorm, get, get, getCCSIndices, getCCSValues, getColumn, getColumn, getColumn, getCRSIndices, 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, writeToFile |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface maspack.matrix.LinearTransformNd |
---|
mul |
Methods inherited from interface maspack.matrix.Matrix |
---|
determinant, epsilonEquals, equals, frobeniusNorm, get, getCCSIndices, getCCSValues, getColumn, getColumn, getColumn, getCRSIndices, 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 |
Constructor Detail |
---|
public SparseMatrixNd(int numRows, int numCols)
numRows
- number of rowsnumCols
- number of columns
ImproperSizeException
- 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.Method Detail |
---|
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 size
ImproperSizeException
- if this matrix has an explicit internal buffer and that buffer is too
small to support the requested size
java.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 index
public SparseMatrixCell getCell(int i, int j)
public int getExplicitElements(double[] values)
values
- collects the explicit element values
java.lang.ArrayIndexOutOfBoundsException
- if the array is not large enough to store all the valuespublic void setExplicitElements(double[] values)
values
- new values for the explicit elements
java.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 numValues)
indices[k*2]
and
indices[k*2+1]
, respectively. All non-specified elements
are set to zero.
set
in interface Matrix
values
- explicit values for specific matrix locationsindices
- i and j indices for each explicit valuenumValues
- number of explicit valuespublic 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 copied
ImproperSizeException
- 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 matrix
ImproperSizeException
- if the size of diag
does not equal the minimum matrix
dimensionpublic void setRandom()
public void setRandom(double lower, double upper, int numElements)
numValues
, 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)
numValues
, 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- if this matrix and M have different sizespublic void add(SparseMatrixNd M1, SparseMatrixNd M2)
M1
- left-hand matrixM2
- right-hand matrix
ImproperSizeException
- 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 matrix
ImproperSizeException
- if this matrix and M have different sizespublic void sub(SparseMatrixNd M1, SparseMatrixNd M2)
M1
- left-hand matrixM2
- right-hand matrix
ImproperSizeException
- 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 negate
ImproperSizeException
- 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 scaled
ImproperSizeException
- 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 of
ImproperSizeException
- 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 class MatrixBase
fmtStr
- numeric format specification
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 format
public 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-matrix
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-matrix
public 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-matrix
public 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-matrix
public 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-matrix
public 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
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |