Package edu.wpi.first.wpiutil.math
Class Matrix<R extends Num,C extends Num>
- java.lang.Object
-
- edu.wpi.first.wpiutil.math.Matrix<R,C>
-
- Type Parameters:
R- The number of rows in this matrix.C- The number of columns in this matrix.
public class Matrix<R extends Num,C extends Num> extends Object
A shape-safe wrapper over Efficient Java Matrix Library (EJML) matrices.This class is intended to be used alongside the state space library.
-
-
Constructor Summary
Constructors Constructor Description Matrix(org.ejml.simple.SimpleMatrix storage)Constructs a new matrix with the given storage.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrix<R,C>copy()Returns a copy of this matrix.doubledet()Returns the determinant of this matrix.Matrix<R,C>diag()If a vector then a square matrix is returned if a matrix then a vector of diagonal elements is returned.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.doubleelementSum()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 'b'.Matrix<R,C>epow(double b)Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ b.Matrix<R,C>epow(int b)Returns a matrix which is the result of an element by element power of 'this' and 'b': ci,j = ai,j ^ b.doubleget(int row, int col)Get an element of this matrix.intgetNumCols()Gets the number of columns in this matrix.intgetNumRows()Gets the number of rows in this matrix.org.ejml.simple.SimpleMatrixgetStorage()Returns the EJMLSimpleMatrixbacking this wrapper.Matrix<R,C>inv()Returns the inverse matrix of this matrix.doublemaxInternal()Returns the largest element of this matrix.doublemean()Calculates the mean of the elements in this matrix.doubleminInternal()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.doublenormF()Computes the Frobenius normal of the matrix.
normF = Sqrt{ ∑i=1:m ∑j=1:n { aij2} }doublenormIndP1()Computes the induced p = 1 matrix norm.
||A||1= max(j=1 to n; sum(i=1 to m; |aij|))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.voidset(int row, int col, double value)Sets the value at the given indices.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.doubletrace()Computes the trace of the matrix.Matrix<C,R>transpose()Calculates the transpose, M^T of this matrix.
-
-
-
Method Detail
-
getNumCols
public final int getNumCols()
Gets the number of columns in this matrix.- Returns:
- The number of columns, according to the internal storage.
-
getNumRows
public final int getNumRows()
Gets the number of rows in this matrix.- Returns:
- The number of rows, according to the internal storage.
-
get
public final double get(int row, int col)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
public final void set(int row, int col, double value)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.
-
diag
public final Matrix<R,C> diag()
If a vector then a square matrix is returned if a matrix then a vector of diagonal elements is returned.- Returns:
- Diagonal elements inside a vector or a square matrix with the same diagonal elements.
-
maxInternal
public final double maxInternal()
Returns the largest element of this matrix.- Returns:
- The largest element of this matrix.
-
minInternal
public final double minInternal()
Returns the smallest element of this matrix.- Returns:
- The smallest element of this matrix.
-
mean
public final double mean()
Calculates the mean of the elements in this matrix.- Returns:
- The mean value of this matrix.
-
times
public final <C2 extends Num> Matrix<R,C2> times(Matrix<C,C2> other)
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
public final Matrix<R,C> times(double value)
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
public final Matrix<R,C> elementTimes(Matrix<R,C> other)
Returns a matrix which is the result of an element by element multiplication of 'this' and 'b'. ci,j = ai,j*bi,j
- Parameters:
other- A matrix.- Returns:
- The element by element multiplication of 'this' and 'b'.
-
minus
public final Matrix<R,C> minus(double value)
Subtracts the given value from all the elements of this matrix.- Parameters:
value- The value to subtract.- Returns:
- The resultant matrix.
-
minus
public final Matrix<R,C> minus(Matrix<R,C> value)
Subtracts the given matrix from this matrix.- Parameters:
value- The matrix to subtract.- Returns:
- The resultant matrix.
-
plus
public final Matrix<R,C> plus(double value)
Adds the given value to all the elements of this matrix.- Parameters:
value- The value to add.- Returns:
- The resultant matrix.
-
plus
public final Matrix<R,C> plus(Matrix<R,C> value)
Adds the given matrix to this matrix.- Parameters:
value- The matrix to add.- Returns:
- The resultant matrix.
-
div
public final Matrix<R,C> div(int value)
Divides all elements of this matrix by the given value.- Parameters:
value- The value to divide by.- Returns:
- The resultant matrix.
-
div
public final Matrix<R,C> div(double value)
Divides all elements of this matrix by the given value.- Parameters:
value- The value to divide by.- Returns:
- The resultant matrix.
-
transpose
public final Matrix<C,R> transpose()
Calculates the transpose, M^T of this matrix.- Returns:
- The tranpose matrix.
-
copy
public final Matrix<R,C> copy()
Returns a copy of this matrix.- Returns:
- A copy of this matrix.
-
inv
public final Matrix<R,C> 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.
-
det
public final double det()
Returns the determinant of this matrix.- Returns:
- The determinant of this matrix.
-
normF
public final double normF()
Computes the Frobenius normal of the matrix.
normF = Sqrt{ ∑i=1:m ∑j=1:n { aij2} }- Returns:
- The matrix's Frobenius normal.
-
normIndP1
public final double 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
public final double elementSum()
Computes the sum of all the elements in the matrix.- Returns:
- Sum of all the elements.
-
trace
public final double trace()
Computes the trace of the matrix.- Returns:
- The trace of the matrix.
-
epow
public final Matrix<R,C> epow(double b)
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'.
-
epow
public final Matrix<R,C> epow(int b)
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'.
-
getStorage
public final org.ejml.simple.SimpleMatrix getStorage()
Returns the EJMLSimpleMatrixbacking this wrapper.- Returns:
- The untyped EJML
SimpleMatrix.
-
-