|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmaspack.matrix.MatrixObject
maspack.matrix.MatrixNd
Implements general m x n matrices, along with most the commonly used operations associated with such matrices.
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. The exception is when this matrix
is either a submatrix, or is being referenced by a submatrix (see SubMatrixNd
).
Constructor Summary | |
MatrixNd(double[][] values)
Creates a matrix from a two dimensional array of doubles. |
|
MatrixNd(int numRows,
int numCols)
Creates a matrix of a specific size, and initializes its elements to 0. |
|
MatrixNd(int numRows,
int numCols,
double[] values)
Creates a matrix of specific size and initializes its elements from an array of values. |
|
MatrixNd(MatrixObject M)
Creates a matrix whose size and elements are the same as an existing MatrixObject. |
Method Summary | |
void |
absolute()
Sets the elements of this matrix to their absolute values. |
void |
add(MatrixNd M1)
Adds this matrix to M1 and places the result in this matrix. |
void |
add(MatrixNd M1,
MatrixNd M2)
Adds matrix M1 to M2 and places the result in this matrix. |
int |
colSize()
Returns the number of columns in this matrix. |
void |
copySubMatrix(int baseRowSrc,
int baseColSrc,
int numRows,
int numCols,
MatrixNd Msrc,
int baseRowDest,
int baseColDest)
Copies a sub matrix from a specified source matrix Msrc
to a region of this matrix. |
boolean |
epsilonEquals(MatrixNd M1,
double epsilon)
Returns true if the elements of this matrix equal those of matrix M1 within a
prescribed tolerance epsilon . |
boolean |
equals(MatrixNd M1)
Returns true if the elements of this matrix exactly equal those of matrix M1 . |
double |
frobeniusNorm()
Returns the Frobenius norm of this matrix. |
void |
get(double[] values)
Copies the elements of this matrix into an array of doubles. |
double |
get(int i,
int j)
Gets a single element of this matrix. |
double[] |
getBuffer()
Returns the internal buffer used to store the elements in this matrix. |
int |
getBufferBase()
Returns the internal buffer base index of this matrix, used for computing the index necessary to reference a particular element. |
int |
getBufferWidth()
Returns the internal buffer width of this matrix, used for computing the index necessary to reference a particular element. |
void |
getColumn(int j,
double[] values)
Copies a column of this matrix into an array of doubles. |
void |
getColumn(int j,
VectorObject v)
Copies a column of this matrix into a VectorObject . |
void |
getRow(int i,
double[] values)
Copies a row of this matrix into an array of doubles. |
void |
getRow(int i,
VectorObject v)
Copies a row of this matrix into a VectorObject . |
void |
getSubMatrix(int[] rows,
int[] cols,
MatrixNd Mdest)
Gets a submatrix of this matrix. |
void |
getSubMatrix(int baseRow,
int baseCol,
MatrixNd Mdest)
Gets a submatrix of this matrix. |
double |
infinityNorm()
Returns the infinity norm of this matrix. |
boolean |
invert()
Inverts this matrix in place, returning false if the matrix is detected to be singular. |
boolean |
invert(MatrixNd M1)
Inverts the matrix M1 and places the result in this matrix, returning false if the matrix is detected to be singular. |
boolean |
isFixedSize()
Returns true if this matrix is of fixed size. |
boolean |
isSubMatrix()
Returns true if the this matrix is a submatrix; i.e., is a member of the subclass SubMatrixNd . |
static void |
main(java.lang.String[] args)
|
double |
maxElement()
Returns the maximum element value of this matrix. |
double |
minElement()
Returns the minimum element value of this matrix. |
void |
mul(MatrixNd M1)
Multiplies this matrix by M1 and places the result in this matrix. |
void |
mul(MatrixNd M1,
MatrixNd M2)
Multiplies matrix M1 by M2 and places the result in this matrix. |
void |
mul(VectorNd vr,
VectorNd v1)
Multiplies this matrix by the column vector v1 and places the result in the vector vr. |
void |
mulDiagonalLeft(double[] diag)
Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles. |
void |
mulDiagonalLeft(VectorNd diag)
Pre-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector. |
void |
mulDiagonalRight(double[] diag)
Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by the leading elements of an array of doubles. |
void |
mulDiagonalRight(VectorNd diag)
Post-multiplies, in place, this matrix by a diagonal matrix whose (diagonal) elements are specified by a vector. |
void |
mulTranspose(MatrixNd M1)
Multiplies this matrix by the transpose of M1 and places the result in this matrix. |
void |
mulTranspose(VectorNd vr,
VectorNd v1)
Multiplies the transpose of this matrix by the vector v1 and places the result in vr. |
void |
mulTransposeBoth(MatrixNd M1,
MatrixNd M2)
Multiplies the transpose of matrix M1 by the transpose of M2 and places the result in this matrix. |
void |
mulTransposeLeft(MatrixNd M1,
MatrixNd M2)
Multiplies the transpose of matrix M1 by M2 and places the result in this matrix. |
void |
mulTransposeRight(MatrixNd M1,
MatrixNd 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(MatrixNd M1)
Sets this matrix to the negative of M1. |
double |
oneNorm()
Returns the 1 norm of this matrix. |
void |
permuteColumns(int[] permutation)
Rearrange the columns of this matrix according to the specified permutation, such that each column j is replaced by column permutation[j]. |
void |
permuteRows(int[] permutation)
Rearrange the rows of this matrix according to the specified permutation, such that each row i is replaced by row permutation[i]. |
int |
rowSize()
Returns the number of rows in this matrix. |
void |
scale(double s)
Scales the elements of this matrix by s . |
void |
scale(double s,
MatrixNd M1)
Scales the elements of matrix M1 by s and places
the results in this matrix. |
void |
set(double[] values)
Sets the elements of this matrix from an array of doubles. |
void |
set(int i,
int j,
double value)
Sets a single element of this matrix. |
void |
set(MatrixNd M1)
Sets the size and values of this matrix to those of matrix M1. |
void |
setBuffer(int numRows,
int numCols,
double[] buffer,
int bufWidth)
Explicitly sets the size and internal buffer associated with this matrix. |
void |
setColumn(int j,
double[] values)
Sets a column of this matrix from an array of doubles. |
void |
setColumn(int j,
VectorObject v)
Sets a column of this matrix from a VectorObject . |
void |
setDiagonal(double[] diag)
Sets this matrix to a diagonal matrix whose diagonal elements are specified by the leading elements of an array of doubles |
void |
setDiagonal(VectorNd diag)
Sets this matrix to a diagonal matrix whose diagonal elements are specified by a vector. |
void |
setIdentity()
Sets this matrix to the identity matrix. |
void |
setRandom()
Sets the elements of this matrix to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive). |
void |
setRandom(double lower,
double upper)
Sets the elements of this matrix to uniformly distributed random values in a specified range. |
void |
setRandom(double lower,
double upper,
java.util.Random generator)
Sets the elements of this matrix to uniformly distributed random values in a specified range, using a supplied random number generator. |
void |
setRandomOrthogonal()
Sets this matrix to a random orthogonal matrix. |
void |
setRandomOrthogonal(java.util.Random generator)
Sets this matrix to a random orthogonal matrix, using a supplied random number generator. |
void |
setRandomSvd(double[] singularValues)
Sets this matrix to a random matrix with specified singular values. |
void |
setRandomSvd(double[] singularValues,
java.util.Random generator)
Sets this matrix to a random matrix with specified singular values, using a supplied random number generator. |
void |
setRow(int i,
double[] values)
Set a row of this matrix from an array of doubles. |
void |
setRow(int i,
VectorObject v)
Sets a row of this matrix from a VectorObject . |
void |
setSize(int numRows,
int numCols)
Sets the size of this matrix. |
void |
setSubMatrix(int[] rows,
int[] cols,
MatrixNd Msrc)
Sets a submatrix of this matrix. |
void |
setSubMatrix(int baseRow,
int baseCol,
MatrixNd Msrc)
Sets a submatrix of this matrix. |
void |
setZero()
Sets the elements of this matrix to zero. |
void |
sub(MatrixNd M1)
Subtracts this matrix from M1 and places the result in this matrix. |
void |
sub(MatrixNd M1,
MatrixNd M2)
Subtracts matrix M1 from M2 and places the result in this matrix. |
void |
transpose()
Replaces this matrix by its tranpose. |
void |
transpose(MatrixNd M1)
Takes the transpose of matrix M1 and places the result in this matrix. |
void |
unsetBuffer()
Removes an explicit buffer provided for this matrix and replaces it with a default implicit buffer. |
Methods inherited from class maspack.matrix.MatrixObject |
determinant, epsilonEquals, equals, equals, get, scan, set, set, toString, toString, toString |
Methods inherited from class java.lang.Object |
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public MatrixNd(int numRows, int numCols)
numRows
- number of rowsnumCols
- number of columns
ImproperSizeException
- if numRows or numCols are negativepublic MatrixNd(double[][] values)
values
- element values for the new matrixpublic MatrixNd(int numRows, int numCols, double[] values)
numRows
- number of rowsnumCols
- number of columnsvalues
- element values for the matrix, with element (i,j)
stored at location i*numCols+j
public MatrixNd(MatrixObject M)
M
- matrix object to be copied.Method Detail |
public boolean isFixedSize()
SubMatrixNd
). If the
matrix is not of fixed size, then it can be resized dynamically,
either explicitly using setSize
, or implicitly when
being used as a result for various matrix operations.
isFixedSize
in class MatrixObject
setSize(int, int)
public boolean isSubMatrix()
SubMatrixNd
.
public double[] getBuffer()
Roughly speaking, elements are stored in the buffer
in row-column order. However, the buffer may be larger than
the matrix, and there may be extra space at the beginning
of the buffer. The buffer index which access the element (i,j)
is computed as i*w + j + b
, where w
and b
are the width and base values returned
by getBufferWidth
and
getBufferBase
,
respectively.
Note also that if this matrix is a submatrix, the buffer will actually belong to the root matrix, and so modifying values in the buffer (including those outside the nominal range of this matrix) will change the root matrix as well.
If this matrix is resized, then the internal buffer may change and the buffer previously returned by this routine may no longer be valid.
getBufferWidth()
,
getBufferBase()
,
setBuffer(int, int, double[], int)
public int getBufferWidth()
getBuffer()
public int getBufferBase()
getBuffer()
public void setSize(int numRows, int numCols)
isFixedSize
returns false. If necessary, this
operation will enlarge the internal buffer associated with this
matrix, invalidating buffers previously returned by getBuffer
.
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 class MatrixObject
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 final int rowSize()
rowSize
in class MatrixObject
public final int colSize()
colSize
in class MatrixObject
public void setBuffer(int numRows, int numCols, double[] buffer, int bufWidth)
[i*bufWidth+j]
.
In particular, bufWidth
must satisfy
bufWidth >= numCols
, and the length of
buffer
must satisfy
buffer.length >= numRows*bufWidth
.
The matrix can continue to be resized as long as
requested sizes stay do not violate these bounds.
The value of bufWidth
will become the
value returned by getBufferWidth
,
while the value returned by getBufferBase
will be 0.
numRows
- new row sizenumCols
- new column sizebuffer
- explicit buffer for this matrixbufWidth
- width for the explicit buffer
java.lang.IllegalStateException
- if this matrix has referring
submatrices or is itself a submatrix
ImproperSizeException
- if the specified buffer and/or
width is too small for the requested sizegetBufferWidth()
,
getBufferBase()
,
unsetBuffer()
public void unsetBuffer()
java.lang.IllegalStateException
- if this matrix does
not have an explicit buffer given by
setBuffer
java.lang.IllegalStateException
- if this matrix has referring
submatricessetBuffer(int, int, double[], int)
public void copySubMatrix(int baseRowSrc, int baseColSrc, int numRows, int numCols, MatrixNd Msrc, int baseRowDest, int baseColDest) throws ImproperSizeException
Msrc
to a region of this matrix. The base row and column of the source
and destination may be different.
baseRowSrc
- starting submatrix row in the source matrixbaseColSrc
- starting submatrix column in the source matrixnumRows
- number of rows in the submatrixnumCols
- number of columns in the submatrixMsrc
- source matrixbaseRowDest
- starting submatrix row in this matrixbaseColDest
- starting submatrix column in this matrix
ImproperSizeException
- if the specified submatrix
dimensions are infeasiblegetSubMatrix(int,int,MatrixNd)
,
setSubMatrix(int,int,MatrixNd)
public final double get(int i, int j)
get
in class MatrixObject
i
- element row indexj
- element column index
public final void get(double[] values)
(i,j)
is stored at
location i*colSize()+j
.
get
in class MatrixObject
values
- array into which values are copiedpublic final void getColumn(int j, double[] values)
getColumn
in class MatrixObject
j
- column indexvalues
- array into which the column is copiedpublic final void getRow(int i, double[] values)
getRow
in class MatrixObject
i
- row indexvalues
- array into which the row is copiedpublic final void getColumn(int j, VectorObject v)
VectorObject
.
getColumn
in class MatrixObject
j
- column indexv
- vector into which the column is copied
ImproperSizeException
- vector's size not equal to the number
of matrix rows and the vector cannot be resizedpublic final void getRow(int i, VectorObject v)
VectorObject
.
getRow
in class MatrixObject
i
- row indexv
- vector into which the row is copied
ImproperSizeException
- vector's size not equal to the number
of matrix columns and the vector cannot be resizedpublic void getSubMatrix(int baseRow, int baseCol, MatrixNd Mdest) throws ImproperSizeException
baseRow
and
baseCol
, and the values are written into the matrix
Mdest
. The size of the submatrix is determined by the
dimensions of Mdest
.
baseRow
- first row of the submatrixbaseCol
- first column of the submatrixMdest
- destination for submatrix values
ImproperSizeException
- if baseRow
or baseCol
are negative, or if the
submatrix exceeds the current matrix bounds.copySubMatrix(int, int, int, int, maspack.matrix.MatrixNd, int, int)
public void getSubMatrix(int[] rows, int[] cols, MatrixNd Mdest) throws ImproperSizeException
rows
and the
cols
, respectively, and the values are written into the
matrix Mdest
. The size of the submatrix is determined
by the existing dimensions of Mdest
.
rows
- row indices of the submatrixcols
- column indices of the submatrixMdest
- destination for submatrix values
ImproperSizeException
- if the number of rows
or columns exceed the dimensions of Mdest
, or if
any of the specified indices lie outside the bounds of this
matrix.public final void set(int i, int j, double value)
set
in class MatrixObject
i
- element row indexj
- element column indexvalue
- element valuepublic final void set(double[] values)
(i,j)
is stored at location
i*colSize()+j
.
set
in class MatrixObject
values
- array from which values are copiedpublic final void setColumn(int j, double[] values)
setColumn
in class MatrixObject
j
- column indexvalues
- array from which column values are copiedpublic final void setRow(int i, double[] values)
setRow
in class MatrixObject
i
- row indexvalues
- array from which the row is copiedpublic void setColumn(int j, VectorObject v)
VectorObject
.
setColumn
in class MatrixObject
j
- column indexv
- vector from which the column is copied
ImproperSizeException
- vector's size not equal to the number
of matrix rowspublic void setRow(int i, VectorObject v)
VectorObject
.
setRow
in class MatrixObject
i
- row indexv
- vector from which the row is copied
ImproperSizeException
- vector's size not equal to the number
of matrix columnspublic void setSubMatrix(int baseRow, int baseCol, MatrixNd 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 boundscopySubMatrix(int, int, int, int, maspack.matrix.MatrixNd, int, int)
public void setSubMatrix(int[] rows, int[] cols, MatrixNd Msrc) throws ImproperSizeException
rows
and the
cols
, respectively, and the new values are given by the
matrix Msrc
. The size of the submatrix is determined
by the existing dimensions of Msrc
.
rows
- row indices of the submatrixcols
- column indices of the submatrixMsrc
- new values for the submatrix
ImproperSizeException
- if the number of rows
or columns exceed the dimensions of Msrc
, or if
any of the specified indices lie outside the bounds of this
matrix.public void setRandom()
public void setRandom(double lower, double upper)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)public void setRandom(double lower, double upper, java.util.Random generator)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)generator
- random number generatorpublic void setRandomOrthogonal()
public void setRandomOrthogonal(java.util.Random generator)
generator
- random number generatorsetRandomOrthogonal()
public void setRandomSvd(double[] singularValues)
singularValues
- singular values for this
matrix
ImproperSizeException
- if the length of
singularValues
is less than the
minimum matrix dimensionpublic void setRandomSvd(double[] singularValues, java.util.Random generator)
singularValues
- singular values for this
matrixgenerator
- random number generator
ImproperSizeException
- if the length of
singularValues
is less than the
minimum matrix dimensionsetRandomSvd(double[])
public void mul(MatrixNd M1)
M1
- right-hand matrix
ImproperSizeException
- if this matrix and
M1 do not conform, or if this matrix needs resizing but is of fixed
sizepublic void mul(MatrixNd M1, MatrixNd 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(MatrixNd M1)
M1
- right-hand matrix
ImproperSizeException
- if this matrix and the transpose of M1
do not conform, or if this matrix needs resizing but is of fixed
sizepublic void mulTransposeRight(MatrixNd M1, MatrixNd 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(MatrixNd M1, MatrixNd 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(MatrixNd M1, MatrixNd 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 mulDiagonalLeft(VectorNd diag)
diag
- specifies the diagonal elements of
the implied left-hand matrix
ImproperSizeException
- if the size of diag
does not equal the row size of this matrixpublic void mulDiagonalLeft(double[] diag)
diag
- specifies the diagonal elements of
the implied left-hand matrix
ImproperSizeException
- if the length of diag
is
less than the row size of this matrixpublic void mulDiagonalRight(VectorNd diag)
diag
- specifies the diagonal elements of the implied
right-hand matrix
ImproperSizeException
- if the size of diag
does not equal the column size of this matrixpublic void mulDiagonalRight(double[] diag)
diag
- specifies the diagonal elements of
the implied right-hand matrix
ImproperSizeException
- if the length of diag
is
less than the column size of this matrixpublic void set(MatrixNd M1) throws ImproperSizeException
M1
- matrix whose size and values are copied
ImproperSizeException
- if matrices have different sizes
and this matrix cannot be resized accordinglypublic void absolute()
public double maxElement()
public double minElement()
public void add(MatrixNd M1) throws ImproperSizeException
M1
- right-hand matrix
ImproperSizeException
- if this matrix and
M1 have different sizespublic void add(MatrixNd M1, MatrixNd M2) throws ImproperSizeException
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(MatrixNd M1) throws ImproperSizeException
M1
- right-hand matrix
ImproperSizeException
- if this matrix and
M1 have different sizespublic void sub(MatrixNd M1, MatrixNd M2) throws ImproperSizeException
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(MatrixNd M1) throws ImproperSizeException
M1
- matrix to negate
ImproperSizeException
- if this matrix needs
resizing but is of fixed sizepublic void negate()
public void scale(double s, MatrixNd M1) throws ImproperSizeException
s
and places
the results in this matrix. This matrix is resized if necessary.
s
- scaling factorM1
- matrix to be scaled
ImproperSizeException
- if this matrix needs
resizing but is of fixed sizepublic void scale(double s)
s
.
s
- scaling factorpublic void transpose()
ImproperSizeException
- if this matrix needs resizing but is
of fixed sizepublic void transpose(MatrixNd M1) throws ImproperSizeException
M1
- matrix to take the transpose of
ImproperSizeException
- if this matrix needs resizing but is
of fixed sizepublic void setIdentity()
public void setZero()
public void setDiagonal(VectorNd diag)
diag
- diagonal elements for this matrix
ImproperSizeException
- if the size of diag
does not equal the minimum matrix dimensionpublic void setDiagonal(double[] diag)
diag
- diagonal elements for this matrix
ImproperSizeException
- if the length of diag
is less than the minimum matrix dimensionpublic boolean epsilonEquals(MatrixNd M1, double epsilon)
M1
within a
prescribed tolerance epsilon
.
M1
- matrix to compare withepsilon
- comparison tolerance
public boolean equals(MatrixNd M1)
M1
.
M1
- matrix to compare with
public double infinityNorm()
infinityNorm
in class MatrixObject
public double oneNorm()
oneNorm
in class MatrixObject
public double frobeniusNorm()
frobeniusNorm
in class MatrixObject
public boolean invert() throws ImproperSizeException
ImproperSizeException
- if this matrix is not squarepublic boolean invert(MatrixNd M1) throws ImproperSizeException
M1
- matrix to take the inverse of
ImproperSizeException
- if matrix M1 is not square,
or if this matrix needs resizing but is of fixed sizepublic void permuteColumns(int[] permutation)
permutation
- describes the column exchanges
ImproperSizeException
- if the length of
permutation
is less than the column size
of this matrix.public void permuteRows(int[] permutation)
permutation
- describes the row exchanges
ImproperSizeException
- if the length of
permutation
is less than the row size
of this matrix.public void mul(VectorNd vr, VectorNd v1)
vr = M v1The vector vr is resized if necessary.
vr
- result vectorv1
- vector to multiply by
ImproperSizeException
- if the size of v1 does not equal the
number of columns of this matrix, or if vr needs resizing
but is of fixed sizepublic void mulTranspose(VectorNd vr, VectorNd v1)
vr = v1 MThe vector vr is resized if necessary.
vr
- result vectorv1
- vector to multiply by
ImproperSizeException
- if the size of v1 does not equal the
number of rows of this matrix, or if vr needs resizing but is of
fixed sizepublic static void main(java.lang.String[] args)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |