Class 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 size
      Matrix​(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 matrix
      Matrix addAndSet​(Matrix m)
      Adds the given matrix to this matrix, modifying this matrix
      Matrix copy()
      Creates a copy of this matrix
      Matrix divide​(double scalar)
      Divides this matrix with the given scalar, stores the result in a new matrix and returns that matrix
      Matrix divideAndSet​(double scalar)
      Divides this matrix by the given scalar, modifying this matrix
      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 within tolerance of the corresponding elements in the given matrix
      boolean equals​(java.lang.Object other)  
      static Matrix fromBigMatrix​(BigMatrix m)
      Converts a BigMatrix to a matrix of doubles
      static Matrix fromString​(java.lang.String raw)
      Parses a string in wolfram-style matrix notation
      double get​(int row, int col)
      Gets a single value from the matrix
      Vector getColumn​(int columnIndex)
      Gets a vector view of the column of the given index.
      int getColumnCount()
      Gets the number of columns in the matrix
      Vector 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 Matrix identityMatrix​(int size)
      Constructs the identity matrix of the given size
      Matrix 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
      Matrix multiply​(double scalar)
      Multiplies this matrix with the given scalar, stores the result in a new matrix and returns that matrix
      Matrix multiply​(Matrix m)
      Computes this * m, stores the result in a new matrix and returns that matrix
      Vector multiply​(Vector v)
      Computes this * v, treating the vector as a column vector, stores the result in a new vector and returns that vector
      Matrix multiplyAndSet​(double scalar)
      Multiplies this matrix by the given scalar, modifying this matrix
      Matrix multiplyAndSet​(Matrix m)
      Computes this * m and stores the result in this matrix, modifying this matrix.
      static Matrix parse​(StringParser parser)
      Parses a matrix from a string parser
      void set​(int row, int col, double value)
      Sets a single value in the matrix
      void 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 matrix
      Matrix subtractAndSet​(Matrix m)
      Subtracts the given matrix from this matrix, modifying this matrix
      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
      Matrix 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 toString()  
      Matrix 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

      • Matrix

        public Matrix​(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
      • 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 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 double 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,
                        double 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 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 - If rowIndex 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 - If columnIndex 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 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,
                              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 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 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 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 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)
        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 Vector multiply​(Vector 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 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 matrix
        java.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 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
      • 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)
        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 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 with row2
        row2 - The row to swap with row1
        Returns:
        This matrix
        Throws:
        java.lang.IndexOutOfBoundsException - If row1 or row2 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 within tolerance of the corresponding elements in the given matrix
        Parameters:
        other - The matrix to test against
        tolerance - 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 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 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 a BigMatrix to a matrix of doubles
        Parameters:
        m - The BigMatrix 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 - If size isn't positive