Class BigMatrix
- java.lang.Object
-
- com.seedfinding.latticg.math.component.BigMatrix
-
public final class BigMatrix extends java.lang.Object
A matrix ofBigFraction
values
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
BigMatrix.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 BigMatrix(int rowCount, int columnCount)
Constructs the zero matrix of the given sizeBigMatrix(int rowCount, int columnCount, BigMatrix.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 BigMatrix
add(BigMatrix m)
Adds the given matrix to this matrix, stores the result in a new matrix and returns that matrixBigMatrix
addAndSet(BigMatrix m)
Adds the given matrix to this matrix, modifying this matrixBigMatrix
copy()
Creates a copy of this matrixBigMatrix
divide(BigFraction scalar)
Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrixBigMatrix
divideAndSet(BigFraction scalar)
Divides this matrix by the given scalar, modifying this matrixboolean
equals(java.lang.Object other)
static BigMatrix
fromString(java.lang.String raw)
Parses a string in wolfram-style matrix notationBigFraction
get(int row, int col)
Gets a single value from the matrixBigVector
getColumn(int columnIndex)
Gets a vector view of the column of the given index.int
getColumnCount()
Gets the number of columns in the matrixBigVector
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 BigMatrix
identityMatrix(int size)
Constructs the identity matrix of the given sizeBigMatrix
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 matrixBigMatrix
multiply(BigFraction scalar)
Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrixBigMatrix
multiply(BigMatrix m)
Computesthis * m
, stores the result in a new matrix and returns that matrixBigVector
multiply(BigVector v)
Computesthis * v
, treating the vector as a column vector, stores the result in a new vector and returns that vectorBigMatrix
multiplyAndSet(BigFraction scalar)
Multiplies this matrix by the given scalar, modifying this matrixBigMatrix
multiplyAndSet(BigMatrix m)
Computesthis * m
and stores the result in this matrix, modifying this matrix.static BigMatrix
parse(StringParser parser)
Parses a matrix from a string parservoid
set(int row, int col, BigFraction value)
Sets a single value in the matrixvoid
setColumn(int columnIndex, BigVector newColumn)
Sets the column at the given index to be the same as the given vector.void
setRow(int rowIndex, BigVector newRow)
Sets the row at the given index to be the same as the given vector.BigMatrix
shiftRows(int startIndex, int endIndex)
Place the row at endIndex before startIndex and shifts all the rows in betweenBigMatrix
submatrix(int startRow, int startColumn, int rowCount, int columnCount)
Gets a submatrix view starting at the given position and of the given size.BigMatrix
subtract(BigMatrix m)
Adds the given matrix from this matrix, stores the result in a new matrix and returns that matrixBigMatrix
subtractAndSet(BigMatrix m)
Subtracts the given matrix from this matrix, modifying this matrixBigMatrix
swapElements(int row1, int col1, int row2, int col2)
Swaps the two elements at the given indices, stores the result in a new matrix and returns that matrixBigMatrix
swapElementsAndSet(int row1, int col1, int row2, int col2)
Swaps the two elements at the given indices, modifying this matrixBigMatrix
swapRows(int row1, int row2)
Swaps the two rows at the given indices, stores the result in a new matrix and returns that matrixBigMatrix
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
toPrettyString(boolean approximate)
Formats this matrix nicely into a human-readable multi-line stringjava.lang.String
toString()
BigMatrix
transpose()
Computes the transpose of this matrix, stores the result in a new matrix and returns that matrix
-
-
-
Constructor Detail
-
BigMatrix
public BigMatrix(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
-
BigMatrix
public BigMatrix(int rowCount, int columnCount, BigMatrix.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 BigFraction 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, BigFraction 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 BigVector 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 BigVector 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, BigVector 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, BigVector 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 BigMatrix 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 BigMatrix add(BigMatrix 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 BigMatrix subtract(BigMatrix 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 BigMatrix multiply(BigFraction 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 BigMatrix multiply(BigMatrix 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 BigVector multiply(BigVector 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 BigMatrix divide(BigFraction 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
- Throws:
java.lang.ArithmeticException
- If the given scalar is zero
-
inverse
public BigMatrix 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 BigMatrix 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
-
swapElements
public BigMatrix swapElements(int row1, int col1, int row2, int col2)
Swaps the two elements at the given indices, stores the result in a new matrix and returns that matrix- Parameters:
row1
- The row to swap withrow2
col1
- The col to swap withcol2
row2
- The row to swap withrow1
col2
- The col to swap withcol1
- Returns:
- A new matrix containing the result
- Throws:
java.lang.IndexOutOfBoundsException
- Ifrow1
,col1
,row2
orcol2
is out of bounds
-
transpose
public BigMatrix 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 BigMatrix addAndSet(BigMatrix 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 BigMatrix subtractAndSet(BigMatrix 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 BigMatrix multiplyAndSet(BigFraction 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 BigMatrix multiplyAndSet(BigMatrix 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 BigMatrix divideAndSet(BigFraction scalar)
Divides this matrix by the given scalar, modifying this matrix- Parameters:
scalar
- The scalar to divide this matrix by- Returns:
- This matrix
- Throws:
java.lang.ArithmeticException
- If the given scalar is zero
-
swapRowsAndSet
public BigMatrix 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
-
swapElementsAndSet
public BigMatrix swapElementsAndSet(int row1, int col1, int row2, int col2)
Swaps the two elements at the given indices, modifying this matrix- Parameters:
row1
- The row to swap withrow2
col1
- The col to swap withcol2
row2
- The row to swap withrow1
col2
- The col to swap withcol1
- Returns:
- This matrix
- Throws:
java.lang.IndexOutOfBoundsException
- Ifrow1
,col1
,row2
orcol2
is out of bounds
-
shiftRows
public BigMatrix shiftRows(int startIndex, int endIndex)
Place the row at endIndex before startIndex and shifts all the rows in between- Parameters:
startIndex
- The starting index to swapendIndex
- The ending index to swap- Returns:
- This matrix
- Throws:
java.lang.IllegalArgumentException
- IfstartIndex
is greater thanendIndex
-
copy
public BigMatrix 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
-
toPrettyString
public java.lang.String toPrettyString(boolean approximate)
Formats this matrix nicely into a human-readable multi-line string- Parameters:
approximate
- a boolean to specify if the result should be converted to double- Returns:
- The formatted 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 BigMatrix 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 BigMatrix 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
-
identityMatrix
public static BigMatrix 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
-
-