public class DiagonalMatrix extends AbstractMatrix
Modifier | Constructor and Description |
---|---|
protected |
DiagonalMatrix()
This should never be called by anything or anyone other than Java's
serialization code.
|
|
DiagonalMatrix(DiagonalMatrix d)
Copy constructor.
|
|
DiagonalMatrix(int n)
Creates a square (n x n) diagonal matrix initialized to zero.
|
|
DiagonalMatrix(Matrix m)
Creates a diagonal matrix as a copy of the given 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()
This makes public the clone method on the
Object class and
removes the exception that it throws. |
void |
convertFromVector(Vector parameters)
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)
Returns the value stored at the input locations
|
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 |
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 |
isSquare()
Determines if the matrix is square (numRows == numColumns)
|
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 . |
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) |
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)
Set the value stored at the input locations to the input value
|
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
|
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, 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 DiagonalMatrix(int n)
n
- The dimensions (num rows and num columns) of the matrixpublic DiagonalMatrix(DiagonalMatrix d)
d
- The diagonal matrix to copypublic DiagonalMatrix(Matrix m)
m
- The matrix to copy.protected DiagonalMatrix()
public final Matrix clone()
AbstractCloneableSerializable
Object
class and
removes the exception that it throws. Its default behavior is to
automatically create a clone of the exact type of object that the
clone is called on and to copy all primitives but to keep all references,
which means it is a shallow copy.
Extensions of this class may want to override this method (but call
super.clone()
to implement a "smart copy". That is, to target
the most common use case for creating a copy of the object. Because of
the default behavior being a shallow copy, extending classes only need
to handle fields that need to have a deeper copy (or those that need to
be reset). Some of the methods in ObjectUtil
may be helpful in
implementing a custom clone method.
Note: The contract of this method is that you must use
super.clone()
as the basis for your implementation.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 byjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic void scaledPlusEquals(DenseMatrix other, double scaleFactor)
other
- A dense matrix to add to thisscaleFactor
- The amount to scale other byjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic 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 thisjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic final void plusEquals(DenseMatrix other)
other
- A dense matrix to add to thisjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic 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 thisjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic final void minusEquals(DenseMatrix other)
other
- A dense matrix to subtract from thisjava.lang.IllegalArgumentException
- if the input has any non-zero off-axis
elements as that would make this a non-diagonal matrixpublic 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)
rowIndex
- The row indexcolumnIndex
- The column indexjava.lang.ArrayIndexOutOfBoundsException
- if the input values are outside
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)
rowIndex
- The row indexcolumnIndex
- The column indexvalue
- The new value for the input locationjava.lang.ArrayIndexOutOfBoundsException
- if the input indices are outside
the matrixjava.lang.IllegalArgumentException
- if the input indices attempt to set a
non-zero value off the main axispublic final Matrix getSubMatrix(int minRow, int maxRow, int minColumn, int maxColumn)
Matrix
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()
this
, which must be a
square matrixthis
, such that
this.times(this.inverse()) == this.inverse().times(this)
==
identity matrixjava.lang.UnsupportedOperationException
- if this doesn't span the space, so
can't be inverted.public final Matrix pseudoInverse(double effectiveZero)
Matrix
this
, using a
rather expensive procedure (SVD)effectiveZero
- effective zero to pass along to the SVDthis
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 final boolean isSquare()
Matrix
isSquare
in interface Matrix
isSquare
in class AbstractMatrix
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 parameters)
parameters
- column-stacked version of thisjava.lang.IllegalArgumentException
- if input vector doesn't have enough
elements to cover all elements of this or if the input vector specifies
non-zero values in off-diagonal elements.public 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 boolean isSparse()
Matrix
public MatrixFactory<?> getMatrixFactory()
Matrix
public int getEntryCount()
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()