Class Matrix
- java.lang.Object
-
- com.seedfinding.latticg.math.component.Matrix
-
public final class Matrix extends java.lang.ObjectA matrix of double values
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMatrix.DataProviderA function that returns the value that should go in a cell in a matrix based on the row and column
-
Constructor Summary
Constructors Constructor Description Matrix(int rowCount, int columnCount)Constructs the zero matrix of the given sizeMatrix(int rowCount, int columnCount, Matrix.DataProvider gen)Constructs a matrix of the given size, using the given function to fill in each element.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrixadd(Matrix m)Adds the given matrix to this matrix, stores the result in a new matrix and returns that matrixMatrixaddAndSet(Matrix m)Adds the given matrix to this matrix, modifying this matrixMatrixcopy()Creates a copy of this matrixMatrixdivide(double scalar)Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrixMatrixdivideAndSet(double scalar)Divides this matrix by the given scalar, modifying this matrixbooleanequals(Matrix other, double tolerance)Returns whether this matrix has the same dimensions as the given matrix, and all elements of this matrix are withintoleranceof the corresponding elements in the given matrixbooleanequals(java.lang.Object other)static MatrixfromBigMatrix(BigMatrix m)Converts aBigMatrixto a matrix of doublesstatic MatrixfromString(java.lang.String raw)Parses a string in wolfram-style matrix notationdoubleget(int row, int col)Gets a single value from the matrixVectorgetColumn(int columnIndex)Gets a vector view of the column of the given index.intgetColumnCount()Gets the number of columns in the matrixVectorgetRow(int rowIndex)Gets a vector view of the row of the given index.intgetRowCount()Gets the number of rows in the matrixinthashCode()static MatrixidentityMatrix(int size)Constructs the identity matrix of the given sizeMatrixinverse()Computes the inverse of this matrix,this-1, stores the result in a new matrix and returns that matrixbooleanisSquare()Returns whether this is a square matrixMatrixmultiply(double scalar)Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrixMatrixmultiply(Matrix m)Computesthis * m, stores the result in a new matrix and returns that matrixVectormultiply(Vector v)Computesthis * v, treating the vector as a column vector, stores the result in a new vector and returns that vectorMatrixmultiplyAndSet(double scalar)Multiplies this matrix by the given scalar, modifying this matrixMatrixmultiplyAndSet(Matrix m)Computesthis * mand stores the result in this matrix, modifying this matrix.static Matrixparse(StringParser parser)Parses a matrix from a string parservoidset(int row, int col, double value)Sets a single value in the matrixvoidsetColumn(int columnIndex, Vector newColumn)Sets the column at the given index to be the same as the given vector.voidsetRow(int rowIndex, Vector newRow)Sets the row at the given index to be the same as the given vector.Matrixsubmatrix(int startRow, int startColumn, int rowCount, int columnCount)Gets a submatrix view starting at the given position and of the given size.Matrixsubtract(Matrix m)Adds the given matrix from this matrix, stores the result in a new matrix and returns that matrixMatrixsubtractAndSet(Matrix m)Subtracts the given matrix from this matrix, modifying this matrixMatrixswapRows(int row1, int row2)Swaps the two rows at the given indices, stores the result in a new matrix and returns that matrixMatrixswapRowsAndSet(int row1, int row2)Swaps the two rows at the given indices, modifying this matrixjava.lang.StringtoPrettyString()Formats this matrix nicely into a human-readable multi-line stringjava.lang.StringtoString()Matrixtranspose()Computes the transpose of this matrix, stores the result in a new matrix and returns that matrix
-
-
-
Constructor Detail
-
Matrix
public Matrix(int rowCount, int columnCount)Constructs the zero matrix of the given size- Parameters:
rowCount- The number of rowscolumnCount- The number of columns- Throws:
java.lang.IllegalArgumentException- IfrowCountorcolumnCountisn't positive
-
Matrix
public Matrix(int rowCount, int columnCount, Matrix.DataProvider gen)Constructs a matrix of the given size, using the given function to fill in each element.- Parameters:
rowCount- The number of rowscolumnCount- The number of columnsgen- The function to call for each element of the matrix
-
-
Method Detail
-
getRowCount
public int getRowCount()
Gets the number of rows in the matrix- Returns:
- The number of rows in the matrix
-
getColumnCount
public int getColumnCount()
Gets the number of columns in the matrix- Returns:
- The number of columns in the matrix
-
isSquare
public boolean isSquare()
Returns whether this is a square matrix- Returns:
- Whether this is a square matrix
-
get
public double get(int row, int col)Gets a single value from the matrix- Parameters:
row- The row of the value to getcol- The column of the value to get- Returns:
- The value in (row, col)
- Throws:
java.lang.IndexOutOfBoundsException- Ifroworcolis out of bounds
-
set
public void set(int row, int col, double value)Sets a single value in the matrix- Parameters:
row- The row of the value to setcol- The column of the value to setvalue- The value to set- Throws:
java.lang.IndexOutOfBoundsException- Ifroworcolis out of bounds
-
getRow
public Vector getRow(int rowIndex)
Gets a vector view of the row of the given index. Modifying this vector will modify the original matrix- Parameters:
rowIndex- The index of the row to get- Returns:
- A view of the row at
rowIndex - Throws:
java.lang.IndexOutOfBoundsException- IfrowIndexis out of bounds
-
getColumn
public Vector getColumn(int columnIndex)
Gets a vector view of the column of the given index. Modifying this vector will modify the original matrix- Parameters:
columnIndex- The index of the column to get- Returns:
- A view of the column at
columnIndex - Throws:
java.lang.IndexOutOfBoundsException- IfcolumnIndexis out of bounds
-
setRow
public void setRow(int rowIndex, Vector newRow)Sets the row at the given index to be the same as the given vector. Copies that vector into this matrix- Parameters:
rowIndex- The index of the row to setnewRow- The vector to set the row to- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not equal to the number of columns in this matrixjava.lang.IndexOutOfBoundsException- IfrowIndexis out of bounds
-
setColumn
public void setColumn(int columnIndex, Vector newColumn)Sets the column at the given index to be the same as the given vector. Copies that vector into this matrix- Parameters:
columnIndex- The index of the column to setnewColumn- The vector to set the column to- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not equal to the number of rows in this matrixjava.lang.IndexOutOfBoundsException- IfcolumnIndexis out of bounds
-
submatrix
public Matrix submatrix(int startRow, int startColumn, int rowCount, int columnCount)
Gets a submatrix view starting at the given position and of the given size. Modifying this matrix will modify the original matrix- Parameters:
startRow- The row of the top of the submatrixstartColumn- The column on the left of the submatrixrowCount- The number of rows in the submatrixcolumnCount- The number of columns in the submatrix- Returns:
- A view of the submatrix
- Throws:
java.lang.IndexOutOfBoundsException- IfstartRow,startColumn,rowCountorcolumnCountis out of bounds
-
add
public Matrix add(Matrix m)
Adds the given matrix to this matrix, stores the result in a new matrix and returns that matrix- Parameters:
m- The matrix to add- Returns:
- A new matrix containing the result
- Throws:
java.lang.IllegalArgumentException- If the given matrix is not the same size as this matrix
-
subtract
public Matrix subtract(Matrix m)
Adds the given matrix from this matrix, stores the result in a new matrix and returns that matrix- Parameters:
m- The matrix to subtract- Returns:
- A new matrix containing the result
- Throws:
java.lang.IllegalArgumentException- If the given matrix is not the same size as this matrix
-
multiply
public Matrix multiply(double scalar)
Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrix- Parameters:
scalar- The scalar to multiply by- Returns:
- A new matrix containing the result
-
multiply
public Matrix multiply(Matrix m)
Computesthis * m, stores the result in a new matrix and returns that matrix- Parameters:
m- The matrix to right-multiply by- Returns:
- A new matrix containing the result
- Throws:
java.lang.IllegalArgumentException- If the number of columns in this matrix is not equal to the number of rows in the given matrix
-
multiply
public Vector multiply(Vector v)
Computesthis * v, treating the vector as a column vector, stores the result in a new vector and returns that vector- Parameters:
v- The vector to right-multiply by- Returns:
- A new vector containing the result
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not equal to the number of columns in this matrix
-
divide
public Matrix divide(double scalar)
Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrix- Parameters:
scalar- The scalar to divide by- Returns:
- A new matrix containing the result
-
inverse
public Matrix inverse()
Computes the inverse of this matrix,this-1, stores the result in a new matrix and returns that matrix- Returns:
- A new matrix containing the result
- Throws:
java.lang.UnsupportedOperationException- If this is not a square matrixjava.lang.IllegalStateException- If this matrix is singular
-
swapRows
public Matrix swapRows(int row1, int row2)
Swaps the two rows at the given indices, stores the result in a new matrix and returns that matrix- Parameters:
row1- The row to swap withrow2row2- The row to swap withrow1- Returns:
- A new matrix containing the result
- Throws:
java.lang.IndexOutOfBoundsException- Ifrow1orrow2is out of bounds
-
transpose
public Matrix transpose()
Computes the transpose of this matrix, stores the result in a new matrix and returns that matrix- Returns:
- A new matrix containing the result
-
addAndSet
public Matrix addAndSet(Matrix m)
Adds the given matrix to this matrix, modifying this matrix- Parameters:
m- The matrix to add to this matrix- Returns:
- This matrix
- Throws:
java.lang.IllegalArgumentException- If the given matrix is not the same size as this matrix
-
subtractAndSet
public Matrix subtractAndSet(Matrix m)
Subtracts the given matrix from this matrix, modifying this matrix- Parameters:
m- The matrix to subtract from this matrix- Returns:
- This matrix
- Throws:
java.lang.IllegalArgumentException- If the given matrix is not the same size as this matrix
-
multiplyAndSet
public Matrix multiplyAndSet(double scalar)
Multiplies this matrix by the given scalar, modifying this matrix- Parameters:
scalar- The scalar to multiply this matrix by- Returns:
- This matrix
-
multiplyAndSet
public Matrix multiplyAndSet(Matrix m)
Computesthis * mand stores the result in this matrix, modifying this matrix. This operation can only be performed on square matrices.- Parameters:
m- The matrix to right-multiply this matrix by- Returns:
- This matrix
- Throws:
java.lang.IllegalArgumentException- If this matrix is not a square matrix, or the given matrix is not the same size as this matrix
-
divideAndSet
public Matrix divideAndSet(double scalar)
Divides this matrix by the given scalar, modifying this matrix- Parameters:
scalar- The scalar to divide this matrix by- Returns:
- This matrix
-
swapRowsAndSet
public Matrix swapRowsAndSet(int row1, int row2)
Swaps the two rows at the given indices, modifying this matrix- Parameters:
row1- The row to swap withrow2row2- The row to swap withrow1- Returns:
- This matrix
- Throws:
java.lang.IndexOutOfBoundsException- Ifrow1orrow2is out of bounds
-
copy
public Matrix copy()
Creates a copy of this matrix- Returns:
- A copy of this matrix
-
toPrettyString
public java.lang.String toPrettyString()
Formats this matrix nicely into a human-readable multi-line string- Returns:
- The formatted matrix
-
equals
public boolean equals(Matrix other, double tolerance)
Returns whether this matrix has the same dimensions as the given matrix, and all elements of this matrix are withintoleranceof the corresponding elements in the given matrix- Parameters:
other- The matrix to test againsttolerance- The maximum amount each element is allowed to differ- Returns:
- Whether this matrix is close enough to the given matrix
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fromString
public static Matrix fromString(java.lang.String raw)
Parses a string in wolfram-style matrix notation- Parameters:
raw- The string in wolfram-style matrix notation- Returns:
- The parsed matrix
- Throws:
ParseException- If the input is malformed
-
parse
public static Matrix parse(StringParser parser)
Parses a matrix from a string parser- Parameters:
parser- The parser to parse the matrix from- Returns:
- The parsed matrix
- Throws:
ParseException- If the input is malformed
-
fromBigMatrix
public static Matrix fromBigMatrix(BigMatrix m)
Converts aBigMatrixto a matrix of doubles- Parameters:
m- TheBigMatrixto copy from- Returns:
- A matrix of doubles with the same values as the input matrix, where possible
-
identityMatrix
public static Matrix identityMatrix(int size)
Constructs the identity matrix of the given size- Parameters:
size- The width and height of the identity matrix to construct- Returns:
- The identity matrix of the given size
- Throws:
java.lang.IllegalArgumentException- Ifsizeisn't positive
-
-