public class DenseMatrix extends AbstractMatrix
Modifier and Type | Class and Description |
---|---|
static class |
DenseMatrix.LU
Simple container class for LU decompositions.
|
static class |
DenseMatrix.QR
Container class that stores the Q and R matrices formed by the QR
decomposition of this.
|
static class |
DenseMatrix.SVD
Simple container class for Singular Value Decomposition (SVD) results.
|
Modifier | Constructor and Description |
---|---|
protected |
DenseMatrix()
This should never be called by anything or anyone other than Java's
serialization code.
|
|
DenseMatrix(DenseMatrix m)
Copy constructor that creates a deep copy of the input matrix.
|
|
DenseMatrix(double[][] values)
Constructor for creating a dense matrix from a 2-d double array.
|
|
DenseMatrix(int numRows,
int numCols)
Creates a zero matrix of the specified dimensions
|
|
DenseMatrix(int numRows,
int numCols,
double defaultVal)
Creates a matrix of default val in all cells of the specified dimensions
|
|
DenseMatrix(java.util.List<java.util.List<java.lang.Double>> values)
Constructor for creating a dense matrix from a 2-d double List.
|
|
DenseMatrix(Matrix m)
Copy constructor that copies any input Matrix -- creating a deep copy of
the matrix.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkSolveDimensions(Matrix b)
Checks that the input matrix has the same numRows as this's numRows
(otherwise, there can be no soluation for Ax = b).
|
protected void |
checkSolveDimensions(Vector b)
Checks that the input vector has the same dimensionality as this's
numRows (otherwise, there can be no solution for Ax = b).
|
protected void |
checkSubmatrixRange(int minRow,
int maxRow,
int minCol,
int maxCol)
Helper method checks that the input submatrix range is acceptable for
this matrix, including that the max is greater than or equal to the min.
|
Matrix |
clone()
Returns a deep copy of this.
|
void |
convertFromVector(Vector v)
uploads a matrix from a column-stacked vector of parameters, so that
v(k) = A(i,j) = A( k%M, k/M )
|
Vector |
convertToVector()
Creates a column-stacked version of this, so that
v(k) = A(i,j) = v(j*M+i)
|
Matrix |
dotTimes(Matrix other)
Element-wise multiplication of
this and other |
void |
dotTimesEquals(DenseMatrix other)
Type-specific version of dotTimesEquals for combining whatever type this
is with the input dense matrix.
|
void |
dotTimesEquals(DiagonalMatrix other)
Type-specific version of dotTimesEquals for combining whatever type this
is with the input diagonal matrix.
|
void |
dotTimesEquals(Matrix other)
Inline element-wise multiplication of
this and
other |
void |
dotTimesEquals(SparseMatrix other)
Type-specific version of dotTimesEquals for combining whatever type this
is with the input sparse matrix.
|
double |
get(int rowIndex,
int columnIndex)
Gets the value of the element of the matrix at the zero-based row and
column indices.
|
Vector |
getColumn(int columnIndex)
Gets the specified column from the zero-based index and returns a
vector that corresponds to that column.
|
double |
getElement(int rowIndex,
int columnIndex)
Gets the Matrix element at the specified zero-based indices
throws ArrayIndexOutOfBoundsException if either rowIndex or columnIndex
are less than 0, or greater than the number of rows (columns) minus one
(0 <= index <= num-1)
|
int |
getEntryCount()
Gets the number of active entries in the matrix.
|
MatrixFactory<?> |
getMatrixFactory()
Gets a matrix factory, typically one associated with this type of matrix.
|
int |
getNonZeroCount()
Simple method that computes the number of non-zero elements stored in
this.
|
double |
getNonZeroPercent()
Returns the percentage of this that is non-zero elements
|
int |
getNumColumns()
Returns the number of columns in the Matrix
|
int |
getNumRows()
Returns the number of rows in the Matrix
|
Vector |
getRow(int rowIndex)
Gets the specified row from the zero-based index and returns a vector
that corresponds to that column
|
Matrix |
getSubMatrix(int minRow,
int maxRow,
int minColumn,
int maxColumn)
Gets the embedded submatrix inside of the Matrix, specified by the
inclusive, zero-based indices such that the result matrix will have size
(maxRow-minRow+1) x (maxColum-minCcolumn+1)
|
void |
identity()
Formats the matrix as an identity matrix.
|
Matrix |
inverse()
Computes the full-blown inverse of
this , which must be a
square matrix |
boolean |
isSparse()
Returns true if this matrix has a potentially sparse underlying
structure.
|
boolean |
isSymmetric(double effectiveZero)
Determines if the matrix is effectively symmetric
|
java.util.Iterator<MatrixEntry> |
iterator() |
ComplexNumber |
logDeterminant()
Computes the natural logarithm of the determinant of
this . |
DenseMatrix.LU |
luDecompose()
Leverages LAPACK to create the LU decomposition of this.
|
Matrix |
minus(Matrix other)
Arithmetic subtraction of
other from this |
void |
minusEquals(DenseMatrix other)
Type-specific version of minusEquals for combining whatever type this is
with the input dense matrix.
|
void |
minusEquals(DiagonalMatrix other)
Type-specific version of minusEquals for combining whatever type this is
with the input diagonal matrix.
|
void |
minusEquals(Matrix other)
Inline arithmetic subtraction of
other from
this |
void |
minusEquals(SparseMatrix other)
Type-specific version of minusEquals for combining whatever type this is
with the input sparse matrix.
|
double |
normFrobenius()
Compute the Frobenius norm of
this , which is just a fancy
way of saying that I will square each element, add those up, and square
root the result. |
double |
normFrobeniusSquared()
Compute the squared Frobenius norm of
this , which is just a
fancy way of saying that I will square each element and add those up. |
Matrix |
plus(Matrix other)
Arithmetic addition of
this and other |
void |
plusEquals(DenseMatrix other)
Type-specific version of plusEquals for combining whatever type this is
with the input dense matrix.
|
void |
plusEquals(DiagonalMatrix other)
Type-specific version of plusEquals for combining whatever type this is
with the input diagonal matrix.
|
void |
plusEquals(Matrix other)
Inline arithmetic addition of
this and other |
void |
plusEquals(SparseMatrix other)
Type-specific version of plusEquals for combining whatever type this is
with the input sparse matrix.
|
Vector |
preTimes(DenseVector vector)
Type-specific version of pre-times for combining whatever type this is
with the input dense vector.
|
Vector |
preTimes(SparseVector vector)
Type-specific version of pre-times for combining whatever type this is
with the input sparse vector.
|
Vector |
preTimes(Vector vector)
Package-private method that puts the vector * matrix code in the matrix
class instead of in the vector class (why should vectors know the
internals of matrices?).
|
Matrix |
pseudoInverse(double effectiveZero)
Computes the effective pseudo-inverse of
this , using a
rather expensive procedure (SVD) |
DenseMatrix.QR |
qrDecompose()
Leverage LAPACK to compute the QR Decomposition of this.
|
int |
rank(double effectiveZero)
Computes the effective rank of
this , which is the number of
linearly independent rows and columns in this . |
void |
scaledPlusEquals(DenseMatrix other,
double scaleFactor)
Type-specific version of scaledPlusEquals for combining whatever type
this is with the input dense matrix.
|
void |
scaledPlusEquals(DiagonalMatrix other,
double scaleFactor)
Type-specific version of scaledPlusEquals for combining whatever type
this is with the input diagonal matrix.
|
void |
scaledPlusEquals(double scaleFactor,
Matrix other)
Inline arithmetic addition of
this and other after
element-wise scaling of other by scaleFactor . |
void |
scaledPlusEquals(SparseMatrix other,
double scaleFactor)
Type-specific version of scaledPlusEquals for combining whatever type
this is with the input sparse matrix.
|
void |
scaleEquals(double scaleFactor)
Inline element-wise scaling of
this by
scaleFactor |
void |
set(int rowIndex,
int columnIndex,
double value)
Sets the value of the element of the matrix at the zero-based row and
column indices.
|
void |
setElement(int rowIndex,
int columnIndex,
double value)
Sets the Matrix element at the specified zero-based indices
throws ArrayIndexOutOfBoundsException if either rowIndex or columnIndex
are less than 0, or greater than the number of rows (columns) minus one
(0 <= index <= num-1)
|
Matrix |
solve(Matrix B)
Solves for "X" in the equation: this*X = B
|
Vector |
solve(Vector b)
Solves for "x" in the equation: this*x = b
|
DenseMatrix.SVD |
svdDecompose()
Leverages LAPACK to compute the Singular Value Decomposition (SVD) of
this.
|
Matrix |
times(DenseMatrix other)
Type-specific version of times for combining whatever type this is with
the input dense matrix.
|
Vector |
times(DenseVector vector)
Type-specific version of times for combining whatever type this is with
the input dense vector.
|
Matrix |
times(DiagonalMatrix other)
Type-specific version of times for combining whatever type this is with
the input diagonal matrix.
|
Matrix |
times(Matrix other)
Matrix multiplication of
this and matrix ,
operates like the "* " operator in Matlab |
Matrix |
times(SparseMatrix other)
Type-specific version of times for combining whatever type this is with
the input sparse matrix.
|
Vector |
times(SparseVector vector)
Type-specific version of times for combining whatever type this is with
the input sparse vector.
|
Vector |
times(Vector vector)
Returns the column vector from the equation
return = this * vector
|
java.lang.String |
toString(java.text.NumberFormat format)
Converts the vector to a
String , using the given formatter. |
Matrix |
transpose()
Returns the transpose of
this |
assertMultiplicationDimensions, assertSameDimensions, checkMultiplicationDimensions, checkSameDimensions, decrement, decrement, dotDivide, dotDivideEquals, equals, equals, getColumnInto, getRowInto, hashCode, increment, increment, isSquare, isSymmetric, isZero, pseudoInverse, rank, setColumn, setRow, setSubMatrix, sumOfColumns, sumOfRows, toArray, trace, valuesAsList
isZero, negative, negativeEquals, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
isZero, negative, negativeEquals, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
public DenseMatrix(int numRows, int numCols)
numRows
- The number of rows in the matrixnumCols
- The number of columns in the matrixpublic DenseMatrix(int numRows, int numCols, double defaultVal)
numRows
- The number of rows in the matrixnumCols
- The number of columns in the matrixdefaultVal
- The value to set all elements topublic DenseMatrix(DenseMatrix m)
m
- The matrix to copypublic DenseMatrix(Matrix m)
m
- The matrix to copy.public DenseMatrix(double[][] values)
values
- The 2-d double array that specifies the dimensions and values
to be stored in the new matrix.public DenseMatrix(java.util.List<java.util.List<java.lang.Double>> values)
values
- The 2-d double array that specifies the dimensions and values
to be stored in the matrix.protected DenseMatrix()
public final Matrix clone()
clone
in interface Matrix
clone
in interface Vectorizable
clone
in interface Ring<Matrix>
clone
in interface CloneableSerializable
clone
in class AbstractRing<Matrix>
public void scaledPlusEquals(SparseMatrix other, double scaleFactor)
other
- A sparse matrix to add to thisscaleFactor
- The amount to scale other bypublic void scaledPlusEquals(DenseMatrix other, double scaleFactor)
other
- A dense matrix to add to thisscaleFactor
- The amount to scale other bypublic void scaledPlusEquals(DiagonalMatrix other, double scaleFactor)
other
- A diagonal matrix to add to thisscaleFactor
- The amount to scale other bypublic final void plusEquals(SparseMatrix other)
other
- A sparse matrix to add to thispublic final void plusEquals(DenseMatrix other)
other
- A dense matrix to add to thispublic final void plusEquals(DiagonalMatrix other)
other
- A diagonal matrix to add to thispublic final void minusEquals(SparseMatrix other)
other
- A sparse matrix to subtract from thispublic final void minusEquals(DenseMatrix other)
other
- A dense matrix to subtract from thispublic final void minusEquals(DiagonalMatrix other)
other
- A diagonal matrix to subtract from thispublic final void dotTimesEquals(SparseMatrix other)
other
- A sparse matrix to dot with thispublic final void dotTimesEquals(DenseMatrix other)
other
- A dense matrix to dot with thispublic final void dotTimesEquals(DiagonalMatrix other)
other
- A diagonal matrix to dot with thispublic final Matrix times(SparseMatrix other)
other
- A sparse matrix to multiply with thispublic final Matrix times(DenseMatrix other)
other
- A dense matrix to multiply with thispublic final Matrix times(DiagonalMatrix other)
other
- A diagonal matrix to multiply with thispublic final Vector times(SparseVector vector)
vector
- A sparse vector to multiply with thispublic final Vector times(DenseVector vector)
vector
- A dense vector to multiply with thispublic final void scaleEquals(double scaleFactor)
Ring
this
by
scaleFactor
scaleFactor
- amount to scale the elements of this
public final int getNumRows()
Matrix
public final int getNumColumns()
Matrix
public double get(int rowIndex, int columnIndex)
Matrix
getElement
.rowIndex
- The zero-based row index. Must be between 0 (inclusive) and the
number of rows (exclusive).columnIndex
- The zero-based column index. Must be between 0 (inclusive) and the
number of columns (exclusive).public final double getElement(int rowIndex, int columnIndex)
Matrix
rowIndex
- Zero-based index into the MatrixcolumnIndex
- Zero-based index into the Matrixpublic void set(int rowIndex, int columnIndex, double value)
Matrix
setElement
.rowIndex
- The zero-based row index. Must be between 0 (inclusive) and the
number of rows (exclusive).columnIndex
- The zero-based column index. Must be between 0 (inclusive) and the
number of columns (exclusive).value
- The value to set at the given row and column in the matrix.public final void setElement(int rowIndex, int columnIndex, double value)
Matrix
rowIndex
- Zero-based index into the rows of the MatrixcolumnIndex
- Zero-based index into the columns of the Matrixvalue
- Value to set at the specified indexpublic final Matrix getSubMatrix(int minRow, int maxRow, int minColumn, int maxColumn)
minRow
- Zero-based index into the rows of the Matrix, must be less
than or equal to maxRowmaxRow
- Zero-based index into the rows of the Matrix, must be
greater than or equal to minRowminColumn
- Zero-based index into the rows of the Matrix, must be
less than or equal to maxColumnmaxColumn
- Zero-based index into the rows of the Matrix, must be
greater than or equal to minColumnpublic final boolean isSymmetric(double effectiveZero)
Matrix
effectiveZero
- tolerance to determine symmetrypublic final Matrix transpose()
Matrix
this
this.getElement(i, j) == this.transpose().getElement(j, i)
for any valid i, j
.public final Matrix inverse()
Matrix
this
, which must be a
square matrixthis
, such that
this.times(this.inverse()) == this.inverse().times(this)
==
identity matrixpublic final Matrix pseudoInverse(double effectiveZero)
Matrix
this
, using a
rather expensive procedure (SVD)effectiveZero
- effective zero to pass along to the SVDthis
@PublicationReference(author="Wikipedia",title="Triagular Matrix / Special Properties",type=WebPage,year=2013,url="http://en.wikipedia.org/wiki/Triangular_matrix#Special_properties") @PublicationReference(author="Wikipedia",title="Determinant / Relation to Eigenvalues and Trace",type=WebPage,year=2013,url="http://en.wikipedia.org/wiki/Determinant#Relation_to_eigenvalues_and_trace") @PublicationReference(author="Wikipedia",title="Logarithm",type=WebPage,year=2013,url="http://en.wikipedia.org/wiki/Logarithm") public final ComplexNumber logDeterminant()
Matrix
this
.
Very computationally intensive. Please THINK LONG AND HARD before
invoking this method on sparse matrices, as they have to be converted
to a DenseMatrix first.this
public final int rank(double effectiveZero)
Matrix
this
, which is the number of
linearly independent rows and columns in this
. Rank is
typically based on the SVD, which is a fairly computationally expensive
procedure and should be used carefullyeffectiveZero
- parameter to pass along to SVD to determine linear dependencethis
, equivalent to the number of linearly
indepenedent rows and columns in this
public double normFrobeniusSquared()
Matrix
this
, which is just a
fancy way of saying that I will square each element and add those up.this
public final double normFrobenius()
Matrix
this
, which is just a fancy
way of saying that I will square each element, add those up, and square
root the result. This is probably the most intuitive of the matrix normsthis
public boolean isSparse()
Matrix
public int getEntryCount()
Matrix
public final DenseMatrix.SVD svdDecompose()
java.lang.IllegalStateException
- if LAPACK fails to decompose this for an
unknown reason.public final DenseMatrix.LU luDecompose()
@PublicationReference(author="NetLib", title="DGEQRF", type=WebPage, year=2013, url="http://icl.cs.utk.edu/projectsfiles/f2j/javadoc/org/netlib/lapack/Dgeqrf.html") public final DenseMatrix.QR qrDecompose()
public final Matrix solve(Matrix B)
Matrix
B
- Must satisfy this.getNumColumns() == B.getNumRows();public final Vector solve(Vector b)
Matrix
b
- must satisfy this.getNumColumns() == b.getDimensionality()public final void identity()
Matrix
public final Vector getColumn(int columnIndex)
Matrix
columnIndex
- zero-based index into the matrixpublic final Vector getRow(int rowIndex)
Matrix
rowIndex
- zero-based index into the matrixpublic final void convertFromVector(Vector v)
Matrix
v
- column-stacked version of thispublic final Vector convertToVector()
Matrix
public final Vector preTimes(SparseVector vector)
vector
- A sparse vector to multiply with thispublic final Vector preTimes(DenseVector vector)
vector
- A dense vector to multiply with thispublic final int getNonZeroCount()
public final double getNonZeroPercent()
public MatrixFactory<?> getMatrixFactory()
Matrix
public final Matrix plus(Matrix other)
Ring
this
and other
public final Matrix minus(Matrix other)
Ring
other
from this
public final Matrix dotTimes(Matrix other)
Ring
this
and other
public final void plusEquals(Matrix other)
Ring
this
and other
plusEquals
in interface Ring<Matrix>
plusEquals
in class AbstractMatrix
other
- object to add to this
public final void scaledPlusEquals(double scaleFactor, Matrix other)
Ring
this
and other
after
element-wise scaling of other
by scaleFactor
.
If this is x, other is y, and scaleFactor is a, then this method is
equivalent to x += a * y. It is typically a more efficient way of doing
this.plusEquals(other.scale(scaleFactor))
since it can avoid
intermediate object creation.scaledPlusEquals
in interface Ring<Matrix>
scaledPlusEquals
in class AbstractMatrix
scaleFactor
- The scale factor to multiply by the elements of other before
adding to the elements of this.other
- Object to scale and then add to this.public final void minusEquals(Matrix other)
Ring
other
from
this
minusEquals
in interface Ring<Matrix>
minusEquals
in class AbstractMatrix
other
- object to subtract from this
public final void dotTimesEquals(Matrix other)
Ring
this
and
other
dotTimesEquals
in interface Ring<Matrix>
dotTimesEquals
in class AbstractMatrix
other
- elements of other will be multiplied to the corresponding
elements of this
public final Matrix times(Matrix other)
Matrix
this
and matrix
,
operates like the "*
" operator in Matlabtimes
in interface Matrix
times
in class AbstractMatrix
other
- this.getNumColumns()==matrix.getNumRows()
this
and
matrix
, will this.getNumRows()
rows and
matrix.getNumColumns()
columnspublic final Vector times(Vector vector)
Matrix
times
in interface Matrix
times
in class AbstractMatrix
vector
- Vector by which to post-multiply this, must have the
same number of rows as thispublic final Vector preTimes(Vector vector)
vector
- The vector to pre-multiply this byDimensionalityMismatchException
- if the input vectors's dimensions
doesn't match this's numRows.protected final void checkSubmatrixRange(int minRow, int maxRow, int minCol, int maxCol)
minRow
- The minimum row to return (inclusive)maxRow
- The maximum row to return (inclusive)minCol
- The minimum column to return (inclusive)maxCol
- The maximum column to return (inclusive)java.lang.ArrayIndexOutOfBoundsException
- if the input range is illegal or
outside the bounds of this.protected final void checkSolveDimensions(Vector b)
b
- The RHS vectorjava.lang.IllegalArgumentException
- if the input's size doesn't match this's
numRowsprotected final void checkSolveDimensions(Matrix b)
b
- The RHS matrixjava.lang.IllegalArgumentException
- if the input's size doesn't match this's
numRowspublic final java.lang.String toString(java.text.NumberFormat format)
Matrix
String
, using the given formatter.format
- The number format to use.public java.util.Iterator<MatrixEntry> iterator()