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.
- All Implemented Interfaces:
ProtobufSerializable
,StructSerializable
,WPISerializable
- Direct Known Subclasses:
Vector
This class is intended to be used alongside the state space library.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final org.ejml.simple.SimpleMatrix
Storage for underlying EJML matrix. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionassignBlock
(int startingRow, int startingCol, Matrix<R2, C2> other) Assign a matrix of a given size and start position.block
(int height, int width, int startingRow, int startingCol) Extracts a matrix of a given size and start position with new underlying storage.Extracts a matrix of a given size and start position with new underlying storage.changeBoundsUnchecked
(Matrix<?, ?> mat) Reassigns dimensions of aMatrix
to allow for operations with other matrices that have wildcard dimensions.copy()
Returns a copy of this matrix.final double
det()
Returns the determinant of this matrix.diag()
Returns the diagonal elements inside a vector or square matrix.div
(double value) Divides all elements of this matrix by the given value.div
(int value) Divides all elements of this matrix by the given value.elementPower
(double b) Returns a matrix which is the result of an element by element power of "this" and b.elementPower
(int b) Returns a matrix which is the result of an element by element power of "this" and b.final double
Computes the sum of all the elements in the matrix.elementTimes
(Matrix<R, C> other) Returns a matrix which is the result of an element by element multiplication of "this" and other.boolean
Checks if an object is equal to thisMatrix
.exp()
Computes the matrix exponential using Eigen's solver.extractColumnVector
(int column) Extracts a given column into a column vector with new underlying storage.extractFrom
(int startingRow, int startingCol, Matrix<R2, C2> other) Extracts a submatrix from the supplied matrix and inserts it in a submatrix in "this".extractRowVector
(int row) Extracts a given row into a row vector with new underlying storage.eye
(D dim) Creates the identity matrix of the given dimension.Creates the identity matrix of the given dimension.void
fill
(double value) Sets all the elements in "this" matrix equal to the specified value.final 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.final int
Gets the number of columns in this matrix.final int
Gets the number of rows in this matrix.static <R extends Num,
C extends Num>
MatrixProto<R,C> Creates an implementation of theProtobuf
interface for matrices.org.ejml.simple.SimpleMatrix
Gets the underlyingSimpleMatrix
that thisMatrix
wraps.static <R extends Num,
C extends Num>
MatrixStruct<R,C> Creates an implementation of theStruct
interfaces for matrices.int
hashCode()
inv()
Returns the inverse matrix of "this" matrix.boolean
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.lltDecompose
(boolean lowerTriangular) Decompose "this" matrix using Cholesky Decomposition.final double
max()
Returns the largest element of this matrix.final double
maxAbs()
Returns the absolute value of the element in this matrix with the largest absolute value.final double
mean()
Calculates the mean of the elements in this matrix.final double
Returns the smallest element of this matrix.minus
(double value) Subtracts the given value from all the elements of this matrix.Subtracts the given matrix from this matrix.final double
normF()
Computes the Frobenius normal of the matrix.final double
Computes the induced p = 1 matrix norm.plus
(double value) Adds the given value to all the elements of this matrix.Adds the given matrix to this matrix.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).final void
set
(int row, int col, double value) Sets the value at the given indices.final void
Sets a column to a given column vector.final void
Sets a row to a given row vector.Returns the solution x to the equation Ax = b, where A is "this" matrix.solveFullPivHouseholderQr
(Matrix<R2, C2> other) Solves the least-squares problem Ax=B using a QR decomposition with full pivoting, where this matrix is A.times
(double value) Multiplies all the elements of this matrix by the given scalar.Multiplies this matrix with another that has C rows.toString()
final double
trace()
Computes the trace of the matrix.Calculates the transpose, Mᵀ of this matrix.
-
Field Details
-
m_storage
Storage for underlying EJML matrix.
-
-
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
.- Parameters:
rows
- The number of rows of the matrix.columns
- The number of columns of the matrix.storage
- The double array to back this value.
-
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.
-
changeBoundsUnchecked
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
-
getProto
Creates an implementation of theProtobuf
interface for matrices.- Type Parameters:
R
- The number of rows of the matrices this serializer processes.C
- The number of cols of the matrices this serializer processes.- Parameters:
rows
- The number of rows of the matrices this serializer processes.cols
- The number of cols of the matrices this serializer processes.- Returns:
- The protobuf implementation.
-
getStruct
Creates an implementation of theStruct
interfaces for matrices.- Type Parameters:
R
- The number of rows of the matrices this serializer processes.C
- The number of cols of the matrices this serializer processes.- Parameters:
rows
- The number of rows of the matrices this serializer processes.cols
- The number of cols of the matrices this serializer processes.- Returns:
- The struct implementation.
-