Class Matrix<R extends Num,C extends Num>
- Type Parameters:
R
- The number of rows in this matrix.C
- The number of columns in this matrix.
- Direct Known Subclasses:
Vector
public class Matrix<R extends Num,C extends Num> extends Object
This class is intended to be used alongside the state space library.
-
Field Summary
Fields Modifier and Type Field Description protected org.ejml.simple.SimpleMatrix
m_storage
-
Constructor Summary
Constructors Constructor Description Matrix(Matrix<R,C> other)
Constructs a new matrix with the storage of the supplied matrix.Matrix(Nat<R> rows, Nat<C> columns)
Constructs an empty zero matrix of the given dimensions.Matrix(org.ejml.simple.SimpleMatrix storage)
Constructs a newMatrix
with the given storage. -
Method Summary
Modifier and Type Method Description <R2 extends Num, C2 extends Num>
voidassignBlock(int startingRow, int startingCol, Matrix<R2,C2> other)
Assign a matrix of a given size and start position.<R2 extends Num, C2 extends Num>
Matrix<R2,C2>block(int height, int width, int startingRow, int startingCol)
Extracts a matrix of a given size and start position with new underlying storage.<R2 extends Num, C2 extends Num>
Matrix<R2,C2>block(Nat<R2> height, Nat<C2> width, int startingRow, int startingCol)
Extracts a matrix of a given size and start position with new underlying storage.static <R1 extends Num, C1 extends Num>
Matrix<R1,C1>changeBoundsUnchecked(Matrix<?,?> mat)
Reassigns dimensions of aMatrix
to allow for operations with other matrices that have wildcard dimensions.Matrix<R,C>
copy()
Returns a copy of this matrix.double
det()
Returns the determinant of this matrix.Matrix<R,C>
diag()
Returns the diagonal elements inside a vector or square matrix.Matrix<R,C>
div(double value)
Divides all elements of this matrix by the given value.Matrix<R,C>
div(int value)
Divides all elements of this matrix by the given value.Matrix<R,C>
elementPower(double b)
Returns a matrix which is the result of an element by element power of "this" and b.Matrix<R,C>
elementPower(int b)
Returns a matrix which is the result of an element by element power of "this" and b.double
elementSum()
Computes the sum of all the elements in the matrix.Matrix<R,C>
elementTimes(Matrix<R,C> other)
Returns a matrix which is the result of an element by element multiplication of "this" and other.boolean
equals(Object other)
Checks if an object is equal to thisMatrix
.Matrix<R,C>
exp()
Computes the matrix exponential using Eigen's solver.Matrix<R,N1>
extractColumnVector(int column)
Extracts a given column into a column vector with new underlying storage.<R2 extends Num, C2 extends Num>
voidextractFrom(int startingRow, int startingCol, Matrix<R2,C2> other)
Extracts a submatrix from the supplied matrix and inserts it in a submatrix in "this".Matrix<N1,C>
extractRowVector(int row)
Extracts a given row into a row vector with new underlying storage.static <D extends Num>
Matrix<D,D>eye(D dim)
Creates the identity matrix of the given dimension.static <D extends Num>
Matrix<D,D>eye(Nat<D> dim)
Creates the identity matrix of the given dimension.void
fill(double value)
Sets all the elements in "this" matrix equal to the specified value.double
get(int row, int col)
Get an element of this matrix.double[]
getData()
Returns the row major data of this matrix as a double array.int
getNumCols()
Gets the number of columns in this matrix.int
getNumRows()
Gets the number of rows in this matrix.org.ejml.simple.SimpleMatrix
getStorage()
Gets the underlyingSimpleMatrix
that thisMatrix
wraps.int
hashCode()
Matrix<R,C>
inv()
Returns the inverse matrix of "this" matrix.boolean
isEqual(Matrix<?,?> other, double tolerance)
Checks if anotherMatrix
is equal to "this" within a specified tolerance.boolean
isIdentical(Matrix<?,?> other, double tolerance)
Checks if anotherMatrix
is identical to "this" one within a specified tolerance.Matrix<R,C>
lltDecompose(boolean lowerTriangular)
Decompose "this" matrix using Cholesky Decomposition.static <R extends Num, C extends Num>
MatBuilder<R,C>mat(Nat<R> rows, Nat<C> cols)
Entrypoint to theMatBuilder
class for creation of custom matrices with the given dimensions and contents.double
max()
Returns the largest element of this matrix.double
maxAbs()
Returns the absolute value of the element in this matrix with the largest absolute value.double
mean()
Calculates the mean of the elements in this matrix.double
minInternal()
Returns the smallest element of this matrix.Matrix<R,C>
minus(double value)
Subtracts the given value from all the elements of this matrix.Matrix<R,C>
minus(Matrix<R,C> value)
Subtracts the given matrix from this matrix.double
normF()
Computes the Frobenius normal of the matrix.double
normIndP1()
Computes the induced p = 1 matrix norm.Matrix<R,C>
plus(double value)
Adds the given value to all the elements of this matrix.Matrix<R,C>
plus(Matrix<R,C> value)
Adds the given matrix to this matrix.Matrix<R,C>
pow(double exponent)
Computes the matrix power using Eigen's solver.void
rankUpdate(Matrix<R,N1> v, double sigma, boolean lowerTriangular)
Performs an inplace Cholesky rank update (or downdate).void
set(int row, int col, double value)
Sets the value at the given indices.void
setColumn(int column, Matrix<R,N1> val)
Sets a column to a given column vector.void
setRow(int row, Matrix<N1,C> val)
Sets a row to a given row vector.<C2 extends Num>
Matrix<C,C2>solve(Matrix<R,C2> b)
Returns the solution x to the equation Ax = b, where A is "this" matrix.<R2 extends Num, C2 extends Num>
Matrix<C,C2>solveFullPivHouseholderQr(Matrix<R2,C2> other)
Solves the least-squares problem Ax=B using a QR decomposition with full pivoting, where this matrix is A.Matrix<R,C>
times(double value)
Multiplies all the elements of this matrix by the given scalar.<C2 extends Num>
Matrix<R,C2>times(Matrix<C,C2> other)
Multiplies this matrix with another that has C rows.String
toString()
double
trace()
Computes the trace of the matrix.Matrix<C,R>
transpose()
Calculates the transpose, Mᵀ of this matrix.
-
Field Details
-
Constructor Details
-
Matrix
Constructs an empty zero matrix of the given dimensions.- Parameters:
rows
- The number of rows of the matrix.columns
- The number of columns of the matrix.
-
Matrix
Constructs a newMatrix
with the given storage. Caller should make sure that the provided generic bounds match the shape of the providedMatrix
.NOTE:It is not recommend to use this constructor unless the
SimpleMatrix
API is absolutely necessary due to the desired function not being accessible through theMatrix
wrapper.- Parameters:
storage
- TheSimpleMatrix
to back this value.
-
Matrix
Constructs a new matrix with the storage of the supplied matrix.- Parameters:
other
- TheMatrix
to copy the storage of.
-
-
Method Details
-
getStorage
Gets the underlyingSimpleMatrix
that thisMatrix
wraps.NOTE:The use of this method is heavily discouraged as this removes any guarantee of type safety. This should only be called if the
SimpleMatrix
API is absolutely necessary due to the desired function not being accessible through theMatrix
wrapper.- Returns:
- The underlying
SimpleMatrix
storage.
-
getNumCols
Gets the number of columns in this matrix.- Returns:
- The number of columns, according to the internal storage.
-
getNumRows
Gets the number of rows in this matrix.- Returns:
- The number of rows, according to the internal storage.
-
get
Get an element of this matrix.- Parameters:
row
- The row of the element.col
- The column of the element.- Returns:
- The element in this matrix at row,col.
-
set
Sets the value at the given indices.- Parameters:
row
- The row of the element.col
- The column of the element.value
- The value to insert at the given location.
-
setRow
Sets a row to a given row vector.- Parameters:
row
- The row to set.val
- The row vector to set the given row to.
-
setColumn
Sets a column to a given column vector.- Parameters:
column
- The column to set.val
- The column vector to set the given row to.
-
fill
Sets all the elements in "this" matrix equal to the specified value.- Parameters:
value
- The value each element is set to.
-
diag
Returns the diagonal elements inside a vector or square matrix.If "this"
Matrix
is a vector then a square matrix is returned. If a "this"Matrix
is a matrix then a vector of diagonal elements is returned.- Returns:
- The diagonal elements inside a vector or a square matrix.
-
max
Returns the largest element of this matrix.- Returns:
- The largest element of this matrix.
-
maxAbs
Returns the absolute value of the element in this matrix with the largest absolute value.- Returns:
- The absolute value of the element with the largest absolute value.
-
minInternal
Returns the smallest element of this matrix.- Returns:
- The smallest element of this matrix.
-
mean
Calculates the mean of the elements in this matrix.- Returns:
- The mean value of this matrix.
-
times
Multiplies this matrix with another that has C rows.As matrix multiplication is only defined if the number of columns in the first matrix matches the number of rows in the second, this operation will fail to compile under any other circumstances.
- Type Parameters:
C2
- The number of columns in the second matrix.- Parameters:
other
- The other matrix to multiply by.- Returns:
- The result of the matrix multiplication between "this" and the given matrix.
-
times
Multiplies all the elements of this matrix by the given scalar.- Parameters:
value
- The scalar value to multiply by.- Returns:
- A new matrix with all the elements multiplied by the given value.
-
elementTimes
Returns a matrix which is the result of an element by element multiplication of "this" and other.ci,j = ai,j*otheri,j
- Parameters:
other
- The otherMatrix
to perform element multiplication on.- Returns:
- The element by element multiplication of "this" and other.
-
minus
Subtracts the given value from all the elements of this matrix.- Parameters:
value
- The value to subtract.- Returns:
- The resultant matrix.
-
minus
Subtracts the given matrix from this matrix.- Parameters:
value
- The matrix to subtract.- Returns:
- The resultant matrix.
-
plus
Adds the given value to all the elements of this matrix.- Parameters:
value
- The value to add.- Returns:
- The resultant matrix.
-
plus
Adds the given matrix to this matrix.- Parameters:
value
- The matrix to add.- Returns:
- The resultant matrix.
-
div
Divides all elements of this matrix by the given value.- Parameters:
value
- The value to divide by.- Returns:
- The resultant matrix.
-
div
Divides all elements of this matrix by the given value.- Parameters:
value
- The value to divide by.- Returns:
- The resultant matrix.
-
transpose
Calculates the transpose, Mᵀ of this matrix.- Returns:
- The transpose matrix.
-
copy
Returns a copy of this matrix.- Returns:
- A copy of this matrix.
-
inv
Returns the inverse matrix of "this" matrix.- Returns:
- The inverse of "this" matrix.
- Throws:
org.ejml.data.SingularMatrixException
- If "this" matrix is non-invertable.
-
solve
Returns the solution x to the equation Ax = b, where A is "this" matrix.The matrix equation could also be written as x = A-1b. Where the pseudo inverse is used if A is not square.
Note that this method does not support solving using a QR decomposition with full-pivoting, as only column-pivoting is supported. For full-pivoting, use
solveFullPivHouseholderQr(edu.wpi.first.math.Matrix<R2, C2>)
.- Type Parameters:
C2
- Columns in b.- Parameters:
b
- The right-hand side of the equation to solve.- Returns:
- The solution to the linear system.
-
solveFullPivHouseholderQr
public final <R2 extends Num, C2 extends Num> Matrix<C,C2> solveFullPivHouseholderQr(Matrix<R2,C2> other)Solves the least-squares problem Ax=B using a QR decomposition with full pivoting, where this matrix is A.- Type Parameters:
R2
- Number of rows in B.C2
- Number of columns in B.- Parameters:
other
- The B matrix.- Returns:
- The solution matrix.
-
exp
Computes the matrix exponential using Eigen's solver. This method only works for square matrices, and will otherwise throw anMatrixDimensionException
.- Returns:
- The exponential of A.
-
pow
Computes the matrix power using Eigen's solver. This method only works for square matrices, and will otherwise throw anMatrixDimensionException
.- Parameters:
exponent
- The exponent.- Returns:
- The exponential of A.
-
det
Returns the determinant of this matrix.- Returns:
- The determinant of this matrix.
-
normF
Computes the Frobenius normal of the matrix.normF = Sqrt{ ∑i=1:m ∑j=1:n { aij2} }
- Returns:
- The matrix's Frobenius normal.
-
normIndP1
Computes the induced p = 1 matrix norm.||A||1= max(j=1 to n; sum(i=1 to m; |aij|))
- Returns:
- The norm.
-
elementSum
Computes the sum of all the elements in the matrix.- Returns:
- Sum of all the elements.
-
trace
Computes the trace of the matrix.- Returns:
- The trace of the matrix.
-
elementPower
Returns a matrix which is the result of an element by element power of "this" and b.ci,j = ai,j ^ b
- Parameters:
b
- Scalar.- Returns:
- The element by element power of "this" and b.
-
elementPower
Returns a matrix which is the result of an element by element power of "this" and b.ci,j = ai,j ^ b
- Parameters:
b
- Scalar.- Returns:
- The element by element power of "this" and b.
-
extractRowVector
Extracts a given row into a row vector with new underlying storage.- Parameters:
row
- The row to extract a vector from.- Returns:
- A row vector from the given row.
-
extractColumnVector
Extracts a given column into a column vector with new underlying storage.- Parameters:
column
- The column to extract a vector from.- Returns:
- A column vector from the given column.
-
block
public final <R2 extends Num, C2 extends Num> Matrix<R2,C2> block(Nat<R2> height, Nat<C2> width, int startingRow, int startingCol)Extracts a matrix of a given size and start position with new underlying storage.- Type Parameters:
R2
- Number of rows to extract.C2
- Number of columns to extract.- Parameters:
height
- The number of rows of the extracted matrix.width
- The number of columns of the extracted matrix.startingRow
- The starting row of the extracted matrix.startingCol
- The starting column of the extracted matrix.- Returns:
- The extracted matrix.
-
block
public final <R2 extends Num, C2 extends Num> Matrix<R2,C2> block(int height, int width, int startingRow, int startingCol)Extracts a matrix of a given size and start position with new underlying storage.- Type Parameters:
R2
- Number of rows to extract.C2
- Number of columns to extract.- Parameters:
height
- The number of rows of the extracted matrix.width
- The number of columns of the extracted matrix.startingRow
- The starting row of the extracted matrix.startingCol
- The starting column of the extracted matrix.- Returns:
- The extracted matrix.
-
assignBlock
public <R2 extends Num, C2 extends Num> void assignBlock(int startingRow, int startingCol, Matrix<R2,C2> other)Assign a matrix of a given size and start position.- Type Parameters:
R2
- Rows in block assignment.C2
- Columns in block assignment.- Parameters:
startingRow
- The row to start at.startingCol
- The column to start at.other
- The matrix to assign the block to.
-
extractFrom
public <R2 extends Num, C2 extends Num> void extractFrom(int startingRow, int startingCol, Matrix<R2,C2> other)Extracts a submatrix from the supplied matrix and inserts it in a submatrix in "this". The shape of "this" is used to determine the size of the matrix extracted.- Type Parameters:
R2
- Number of rows to extract.C2
- Number of columns to extract.- Parameters:
startingRow
- The starting row in the supplied matrix to extract the submatrix.startingCol
- The starting column in the supplied matrix to extract the submatrix.other
- The matrix to extract the submatrix from.
-
lltDecompose
Decompose "this" matrix using Cholesky Decomposition. If the "this" matrix is zeros, it will return the zero matrix.- Parameters:
lowerTriangular
- Whether we want to decompose to the lower triangular Cholesky matrix.- Returns:
- The decomposed matrix.
- Throws:
RuntimeException
- if the matrix could not be decomposed(i.e. is not positive semidefinite).
-
getData
Returns the row major data of this matrix as a double array.- Returns:
- The row major data of this matrix as a double array.
-
eye
Creates the identity matrix of the given dimension.- Type Parameters:
D
- The dimension of the desired matrix as a generic.- Parameters:
dim
- The dimension of the desired matrix as aNat
.- Returns:
- The DxD identity matrix.
-
eye
Creates the identity matrix of the given dimension.- Type Parameters:
D
- The dimension of the desired matrix as a generic.- Parameters:
dim
- The dimension of the desired matrix as aNum
.- Returns:
- The DxD identity matrix.
-
mat
Entrypoint to theMatBuilder
class for creation of custom matrices with the given dimensions and contents.- Type Parameters:
R
- The number of rows of the desired matrix as a generic.C
- The number of columns of the desired matrix as a generic.- Parameters:
rows
- The number of rows of the desired matrix.cols
- The number of columns of the desired matrix.- Returns:
- A builder to construct the matrix.
-
changeBoundsUnchecked
public static <R1 extends Num, C1 extends Num> Matrix<R1,C1> changeBoundsUnchecked(Matrix<?,?> mat)Reassigns dimensions of aMatrix
to allow for operations with other matrices that have wildcard dimensions.- Type Parameters:
R1
- Row dimension to assign.C1
- Column dimension to assign.- Parameters:
mat
- TheMatrix
to remove the dimensions from.- Returns:
- The matrix with reassigned dimensions.
-
isIdentical
Checks if anotherMatrix
is identical to "this" one within a specified tolerance.This will check if each element is in tolerance of the corresponding element from the other
Matrix
or if the elements have the same symbolic meaning. For two elements to have the same symbolic meaning they both must be either Double.NaN, Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY.NOTE:It is recommended to use
isEqual(Matrix, double)
over this method when checking if two matrices are equal asisEqual(Matrix, double)
will return false if an element is uncountable. This method should only be used when uncountable elements need to be compared.- Parameters:
other
- TheMatrix
to check against this one.tolerance
- The tolerance to check equality with.- Returns:
- true if this matrix is identical to the one supplied.
-
isEqual
Checks if anotherMatrix
is equal to "this" within a specified tolerance.This will check if each element is in tolerance of the corresponding element from the other
Matrix
.tol ≥ |aij - bij|
- Parameters:
other
- TheMatrix
to check against this one.tolerance
- The tolerance to check equality with.- Returns:
- true if this matrix is equal to the one supplied.
-
rankUpdate
Performs an inplace Cholesky rank update (or downdate).If this matrix contains L where A = LLᵀ before the update, it will contain L where LLᵀ = A + σvvᵀ after the update.
- Parameters:
v
- Vector to use for the update.sigma
- Sigma to use for the update.lowerTriangular
- Whether this matrix is lower triangular.
-
toString
-
equals
Checks if an object is equal to thisMatrix
.aij == bij
-
hashCode
-