Class Matrix
- java.lang.Object
-
- com.seedfinding.latticg.math.component.Matrix
-
public final class Matrix extends java.lang.Object
A matrix of double values
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Matrix.DataProvider
A 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 Matrix
add(Matrix m)
Adds the given matrix to this matrix, stores the result in a new matrix and returns that matrixMatrix
addAndSet(Matrix m)
Adds the given matrix to this matrix, modifying this matrixMatrix
copy()
Creates a copy of this matrixMatrix
divide(double scalar)
Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrixMatrix
divideAndSet(double scalar)
Divides this matrix by the given scalar, modifying this matrixboolean
equals(Matrix other, double tolerance)
Returns whether this matrix has the same dimensions as the given matrix, and all elements of this matrix are withintolerance
of the corresponding elements in the given matrixboolean
equals(java.lang.Object other)
static Matrix
fromBigMatrix(BigMatrix m)
Converts aBigMatrix
to a matrix of doublesstatic Matrix
fromString(java.lang.String raw)
Parses a string in wolfram-style matrix notationdouble
get(int row, int col)
Gets a single value from the matrixVector
getColumn(int columnIndex)
Gets a vector view of the column of the given index.int
getColumnCount()
Gets the number of columns in the matrixVector
getRow(int rowIndex)
Gets a vector view of the row of the given index.int
getRowCount()
Gets the number of rows in the matrixint
hashCode()
static Matrix
identityMatrix(int size)
Constructs the identity matrix of the given sizeMatrix
inverse()
Computes the inverse of this matrix,this
-1, stores the result in a new matrix and returns that matrixboolean
isSquare()
Returns whether this is a square matrixMatrix
multiply(double scalar)
Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrixMatrix
multiply(Matrix m)
Computesthis * m
, stores the result in a new matrix and returns that matrixVector
multiply(Vector v)
Computesthis * v
, treating the vector as a column vector, stores the result in a new vector and returns that vectorMatrix
multiplyAndSet(double scalar)
Multiplies this matrix by the given scalar, modifying this matrixMatrix
multiplyAndSet(Matrix m)
Computesthis * m
and stores the result in this matrix, modifying this matrix.static Matrix
parse(StringParser parser)
Parses a matrix from a string parservoid
set(int row, int col, double value)
Sets a single value in the matrixvoid
setColumn(int columnIndex, Vector newColumn)
Sets the column at the given index to be the same as the given vector.void
setRow(int rowIndex, Vector newRow)
Sets the row at the given index to be the same as the given vector.Matrix
submatrix(int startRow, int startColumn, int rowCount, int columnCount)
Gets a submatrix view starting at the given position and of the given size.Matrix
subtract(Matrix m)
Adds the given matrix from this matrix, stores the result in a new matrix and returns that matrixMatrix
subtractAndSet(Matrix m)
Subtracts the given matrix from this matrix, modifying this matrixMatrix
swapRows(int row1, int row2)
Swaps the two rows at the given indices, stores the result in a new matrix and returns that matrixMatrix
swapRowsAndSet(int row1, int row2)
Swaps the two rows at the given indices, modifying this matrixjava.lang.String
toPrettyString()
Formats this matrix nicely into a human-readable multi-line stringjava.lang.String
toString()
Matrix
transpose()
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
- IfrowCount
orcolumnCount
isn'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
- Ifrow
orcol
is 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
- Ifrow
orcol
is 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
- IfrowIndex
is 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
- IfcolumnIndex
is 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
- IfrowIndex
is 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
- IfcolumnIndex
is 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
,rowCount
orcolumnCount
is 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 withrow2
row2
- The row to swap withrow1
- Returns:
- A new matrix containing the result
- Throws:
java.lang.IndexOutOfBoundsException
- Ifrow1
orrow2
is 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 * m
and 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 withrow2
row2
- The row to swap withrow1
- Returns:
- This matrix
- Throws:
java.lang.IndexOutOfBoundsException
- Ifrow1
orrow2
is 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 withintolerance
of 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:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in 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 aBigMatrix
to a matrix of doubles- Parameters:
m
- TheBigMatrix
to 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
- Ifsize
isn't positive
-
-