Package me.autobot.lib.math.linalg
Class Matrix
java.lang.Object
me.autobot.lib.math.linalg.Matrix
A matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds the matrix to another matrixstatic Matrix
createIdentityMatrix
(int size) Creates a new identity matrix with the given sizedouble
Gets the determinant of the matrixint
Gets the dimension of the matrixdouble
get
(int row, int col) Gets the value at the given row and columnint
getCols()
Gets the number of columns in the matrixdouble[][]
Gets the matrix as a 2D arrayint
getRows()
Gets the number of rows in the matrixinverse()
Inverts the matrixMultiplies the matrix by another matrixMultiplies the matrix by a vectorscale
(double scalar) Scales the matrix by a scalarvoid
set
(int row, int col, double value) Sets the value at the given row and columnsubmatrix
(int row, int col) Gets the submatrix of the matrixSubtracts the matrix from another matrixvoid
swapRows
(int row1, int row2) Swaps two rows in this matrixtoString()
Gets the string representation of the matrixtoString
(int decimals) Gets the string representation of the matrixTransposes the matrix
-
Constructor Details
-
Matrix
public Matrix(double[][] matrix) Creates a new matrix with the given 2D array- Parameters:
matrix
- The 2D array to create the matrix with
-
Matrix
public Matrix(int rows, int cols) Creates a new matrix with the given number of rows and columns- Parameters:
rows
- The number of rows in the matrixcols
- The number of columns in the matrix
-
-
Method Details
-
createIdentityMatrix
Creates a new identity matrix with the given size- Parameters:
size
- The size of the identity matrix- Returns:
- The identity matrix
-
getMatrix
public double[][] getMatrix()Gets the matrix as a 2D array- Returns:
- The matrix as a 2D array
-
getRows
public int getRows()Gets the number of rows in the matrix- Returns:
- The number of rows in the matrix
-
getCols
public int getCols()Gets the number of columns in the matrix- Returns:
- The number of columns in the matrix
-
get
public double get(int row, int col) Gets the value at the given row and column- Parameters:
row
- The row to get the value fromcol
- The column to get the value from- Returns:
- The value at the given row and column
-
set
public void set(int row, int col, double value) Sets the value at the given row and column- Parameters:
row
- The row to set the value atcol
- The column to set the value atvalue
- The value to set
-
swapRows
public void swapRows(int row1, int row2) Swaps two rows in this matrix- Parameters:
row1
- The first row to swap The row must be within the bounds of the matrixrow2
- The second row to swap The row must be within the bounds of the matrix
-
add
Adds the matrix to another matrix- Parameters:
other
- The matrix to add The matrix must have the same dimensions as this matrix- Returns:
- The sum of the two matrices
-
subtract
Subtracts the matrix from another matrix- Parameters:
other
- The matrix to subtract- Returns:
- The difference of the two matrices
-
multiply
Multiplies the matrix by another matrix- Parameters:
other
- The matrix to multiply by- Returns:
- The product of the two matrices
-
multiply
Multiplies the matrix by a vector- Parameters:
v
- The vector to multiply by- Returns:
- The product of the matrix and the vector
-
scale
Scales the matrix by a scalar- Parameters:
scalar
- The scalar to scale the matrix by- Returns:
- The scaled matrix
-
transpose
Transposes the matrix- Returns:
- The transposed matrix
-
submatrix
Gets the submatrix of the matrix- Parameters:
row
- The row to removecol
- The column to remove- Returns:
- The submatrix
-
determinant
public double determinant()Gets the determinant of the matrix- Returns:
- The determinant of the matrix
-
inverse
Inverts the matrix- Returns:
- The inverted matrix
-
dimension
public int dimension()Gets the dimension of the matrix- Returns:
- The dimension of the matrix
-
toString
Gets the string representation of the matrix -
toString
Gets the string representation of the matrix- Parameters:
decimals
- The number of decimals to round to- Returns:
- The string representation of the matrix
-