Class MathMatrix

java.lang.Object
MathMatrix

public class MathMatrix extends Object
A class that models systems of linear equations (Math Matrices) as used in linear algebra.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MathMatrix(int[][] mat)
    create a MathMatrix with cells equal to the values in mat.
    MathMatrix(int numRows, int numCols, int initialVal)
    create a MathMatrix of the specified size with all cells set to the intialValue.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(MathMatrix rightHandSide)
    implements MathMatrix addition, (this MathMatrix) + rightHandSide.
    boolean
    equals(Object rightHandSide)
    override equals.
    int
    Get the number of columns.
    int
    Get the number of rows.
    getScaledMatrix(int factor)
    Create and return a new Matrix that is a copy of this matrix, but with all values multiplied by a scale value.
    accessor: get a transpose of this MathMatrix.
    int
    getVal(int row, int col)
    get the value of a cell in this MathMatrix.
    boolean
    Return true if this MathMatrix is upper triangular.
    multiply(MathMatrix rightHandSide)
    implements matrix multiplication, (this MathMatrix) * rightHandSide.
    static boolean
    rectangularMatrix(int[][] mat)
     
    subtract(MathMatrix rightHandSide)
    implements MathMatrix subtraction, (this MathMatrix) - rightHandSide.
    override toString.

    Methods inherited from class java.lang.Object

    getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MathMatrix

      public MathMatrix(int[][] mat)
      create a MathMatrix with cells equal to the values in mat. A "deep" copy of mat is made. Changes to mat after this constructor do not affect this Matrix and changes to this MathMatrix do not affect mat
      Parameters:
      mat - mat !=null, mat.length > 0, mat[0].length > 0, mat is a rectangular matrix
    • MathMatrix

      public MathMatrix(int numRows, int numCols, int initialVal)
      create a MathMatrix of the specified size with all cells set to the intialValue.
      pre: numRows > 0, numCols > 0
      post: create a matrix with numRows rows and numCols columns. All elements of this matrix equal initialVal. In other words after this method has been called getVal(r,c) = initialVal for all valid r and c.
      Parameters:
      numRows - numRows > 0
      numCols - numCols > 0
      initialVal - all cells of this Matrix are set to initialVal
  • Method Details

    • getNumRows

      public int getNumRows()
      Get the number of rows.
      Returns:
      the number of rows in this MathMatrix
    • getNumColumns

      public int getNumColumns()
      Get the number of columns.
      Returns:
      the number of columns in this MathMatrix
    • getVal

      public int getVal(int row, int col)
      get the value of a cell in this MathMatrix.
      pre: row 0 <= row < getNumRows(), col 0 <= col < getNumColumns()
      Parameters:
      row - 0 <= row < getNumRows()
      col - 0 <= col < getNumColumns()
      Returns:
      the value at the specified position
    • add

      public MathMatrix add(MathMatrix rightHandSide)
      implements MathMatrix addition, (this MathMatrix) + rightHandSide.
      pre: rightHandSide != null, rightHandSide.getNumRows() = getNumRows(), rightHandSide.numCols() = getNumColumns()
      post: This method does not alter the calling object or rightHandSide
      Parameters:
      rightHandSide - rightHandSide.getNumRows() = getNumRows(), rightHandSide.numCols() = getNumColumns()
      Returns:
      a new MathMatrix that is the result of adding this Matrix to rightHandSide. The number of rows in the returned Matrix is equal to the number of rows in this MathMatrix. The number of columns in the returned Matrix is equal to the number of columns in this MathMatrix.
    • subtract

      public MathMatrix subtract(MathMatrix rightHandSide)
      implements MathMatrix subtraction, (this MathMatrix) - rightHandSide.
      pre: rightHandSide != null, rightHandSide.getNumRows() = getNumRows(), rightHandSide.numCols() = getNumColumns()
      post: This method does not alter the calling object or rightHandSide
      Parameters:
      rightHandSide - rightHandSide.getNumRows() = getNumRows(), rightHandSide.numCols() = getNumColumns()
      Returns:
      a new MathMatrix that is the result of subtracting rightHandSide from this MathMatrix. The number of rows in the returned MathMatrix is equal to the number of rows in this MathMatrix.The number of columns in the returned MathMatrix is equal to the number of columns in this MathMatrix.
    • multiply

      public MathMatrix multiply(MathMatrix rightHandSide)
      implements matrix multiplication, (this MathMatrix) * rightHandSide.
      pre: rightHandSide != null, rightHandSide.getNumRows() = getNumColumns()
      post: This method should not alter the calling object or rightHandSide
      Parameters:
      rightHandSide - rightHandSide.getNumRows() = getNumColumns()
      Returns:
      a new MathMatrix that is the result of multiplying this MathMatrix and rightHandSide. The number of rows in the returned MathMatrix is equal to the number of rows in this MathMatrix. The number of columns in the returned MathMatrix is equal to the number of columns in rightHandSide.
    • getScaledMatrix

      public MathMatrix getScaledMatrix(int factor)
      Create and return a new Matrix that is a copy of this matrix, but with all values multiplied by a scale value.
      pre: none
      post: returns a new Matrix with all elements in this matrix multiplied by factor. In other words after this method has been called returned_matrix.getVal(r,c) = original_matrix.getVal(r, c) * factor for all valid r and c.
      Parameters:
      factor - the value to multiply every cell in this Matrix by.
      Returns:
      a MathMatrix that is a copy of this MathMatrix, but with all values in the result multiplied by factor.
    • getTranspose

      public MathMatrix getTranspose()
      accessor: get a transpose of this MathMatrix. This Matrix is not changed.
      pre: none
      Returns:
      a transpose of this MathMatrix
    • equals

      public boolean equals(Object rightHandSide)
      override equals.
      Overrides:
      equals in class Object
      Returns:
      true if rightHandSide is the same size as this MathMatrix and all values in the two MathMatrix objects are the same, false otherwise
    • toString

      public String toString()
      override toString.
      Overrides:
      toString in class Object
      Returns:
      a String with all elements of this MathMatrix. Each row is on a separate line. Spacing based on longest element in this Matrix.
    • isUpperTriangular

      public boolean isUpperTriangular()
      Return true if this MathMatrix is upper triangular. To be upper triangular all elements below the main diagonal must be 0.
      pre: this is a square matrix. getNumRows() == getNumColumns()
      Returns:
      true if this MathMatrix is upper triangular, false otherwise.
    • rectangularMatrix

      public static boolean rectangularMatrix(int[][] mat)