Class BigMatrix


  • public final class BigMatrix
    extends java.lang.Object
    A matrix of BigFraction 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 size
      BigMatrix​(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 matrix
      BigMatrix addAndSet​(BigMatrix m)
      Adds the given matrix to this matrix, modifying this matrix
      BigMatrix copy()
      Creates a copy of this matrix
      BigMatrix divide​(BigFraction scalar)
      Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrix
      BigMatrix divideAndSet​(BigFraction scalar)
      Divides this matrix by the given scalar, modifying this matrix
      boolean equals​(java.lang.Object other)  
      static BigMatrix fromString​(java.lang.String raw)
      Parses a string in wolfram-style matrix notation
      BigFraction get​(int row, int col)
      Gets a single value from the matrix
      BigVector getColumn​(int columnIndex)
      Gets a vector view of the column of the given index.
      int getColumnCount()
      Gets the number of columns in the matrix
      BigVector getRow​(int rowIndex)
      Gets a vector view of the row of the given index.
      int getRowCount()
      Gets the number of rows in the matrix
      int hashCode()  
      static BigMatrix identityMatrix​(int size)
      Constructs the identity matrix of the given size
      BigMatrix inverse()
      Computes the inverse of this matrix, this-1, stores the result in a new matrix and returns that matrix
      boolean isSquare()
      Returns whether this is a square matrix
      BigMatrix multiply​(BigFraction scalar)
      Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrix
      BigMatrix multiply​(BigMatrix m)
      Computes this * m, stores the result in a new matrix and returns that matrix
      BigVector multiply​(BigVector v)
      Computes this * v, treating the vector as a column vector, stores the result in a new vector and returns that vector
      BigMatrix multiplyAndSet​(BigFraction scalar)
      Multiplies this matrix by the given scalar, modifying this matrix
      BigMatrix multiplyAndSet​(BigMatrix m)
      Computes this * m and stores the result in this matrix, modifying this matrix.
      static BigMatrix parse​(StringParser parser)
      Parses a matrix from a string parser
      void set​(int row, int col, BigFraction value)
      Sets a single value in the matrix
      void 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 between
      BigMatrix 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 matrix
      BigMatrix subtractAndSet​(BigMatrix m)
      Subtracts the given matrix from this matrix, modifying this matrix
      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
      BigMatrix swapElementsAndSet​(int row1, int col1, int row2, int col2)
      Swaps the two elements at the given indices, modifying this matrix
      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
      BigMatrix swapRowsAndSet​(int row1, int row2)
      Swaps the two rows at the given indices, modifying this matrix
      java.lang.String toPrettyString()
      Formats this matrix nicely into a human-readable multi-line string
      java.lang.String toPrettyString​(boolean approximate)
      Formats this matrix nicely into a human-readable multi-line string
      java.lang.String toString()  
      BigMatrix transpose()
      Computes the transpose of this matrix, stores the result in a new matrix and returns that matrix
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BigMatrix

        public BigMatrix​(int rowCount,
                         int columnCount)
        Constructs the zero matrix of the given size
        Parameters:
        rowCount - The number of rows
        columnCount - The number of columns
        Throws:
        java.lang.IllegalArgumentException - If rowCount or columnCount 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 rows
        columnCount - The number of columns
        gen - 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 get
        col - The column of the value to get
        Returns:
        The value in (row, col)
        Throws:
        java.lang.IndexOutOfBoundsException - If row or col 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 set
        col - The column of the value to set
        value - The value to set
        Throws:
        java.lang.IndexOutOfBoundsException - If row or col 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 - If rowIndex 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 - If columnIndex 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 set
        newRow - 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 matrix
        java.lang.IndexOutOfBoundsException - If rowIndex 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 set
        newColumn - 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 matrix
        java.lang.IndexOutOfBoundsException - If columnIndex 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 submatrix
        startColumn - The column on the left of the submatrix
        rowCount - The number of rows in the submatrix
        columnCount - The number of columns in the submatrix
        Returns:
        A view of the submatrix
        Throws:
        java.lang.IndexOutOfBoundsException - If startRow, startColumn, rowCount or columnCount 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)
        Computes this * 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)
        Computes this * 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 matrix
        java.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 with row2
        row2 - The row to swap with row1
        Returns:
        A new matrix containing the result
        Throws:
        java.lang.IndexOutOfBoundsException - If row1 or row2 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 with row2
        col1 - The col to swap with col2
        row2 - The row to swap with row1
        col2 - The col to swap with col1
        Returns:
        A new matrix containing the result
        Throws:
        java.lang.IndexOutOfBoundsException - If row1, col1, row2 or col2 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)
        Computes this * 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 with row2
        row2 - The row to swap with row1
        Returns:
        This matrix
        Throws:
        java.lang.IndexOutOfBoundsException - If row1 or row2 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 with row2
        col1 - The col to swap with col2
        row2 - The row to swap with row1
        col2 - The col to swap with col1
        Returns:
        This matrix
        Throws:
        java.lang.IndexOutOfBoundsException - If row1, col1, row2 or col2 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 swap
        endIndex - The ending index to swap
        Returns:
        This matrix
        Throws:
        java.lang.IllegalArgumentException - If startIndex is greater than endIndex
      • 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 class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.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 - If size isn't positive