Class MathMatrix

java.lang.Object
  extended by MathMatrix

public class MathMatrix
extends Object

A class that models systems of linear equations (Math Matrices) as used in linear algebra.

Version:
Skeleton file for students

Constructor Summary
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
 MathMatrix add(MathMatrix rightHandSide)
          implements MathMatrix addition, (this MathMatrix) + rightHandSide.
 void changeElement(int row, int col, int newValue)
          change the value of one of the cells in this MathMatrix.
 boolean equals(Object rightHandSide)
          override equals.
 MathMatrix getTranspose()
          accessor: get a transpose of this MathMatrix.
 int getVal(int row, int col)
          get the value of a cell in this MathMatrix.
 boolean isUpperTriangular()
          Return true if this MathMatrix is upper triangular.
 MathMatrix multiply(MathMatrix rightHandSide)
          implements matrix multiplication, (this MathMatrix) * rightHandSide.
 int numCols()
          Get the number of columns.
 int numRows()
          Get the number of rows.
 void scale(int factor)
          Multiply all elements of this MathMatrix by factor.
 MathMatrix subtract(MathMatrix rightHandSide)
          implements MathMatrix subtraction, (this MathMatrix) - rightHandSide.
 String toString()
          override toString.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 Detail

changeElement

public void changeElement(int row,
                          int col,
                          int newValue)
change the value of one of the cells in this MathMatrix.
pre: 0 <= row < numRows(), 0 <= col < numCols()
post: getVal(row, col) = newValue

Parameters:
row - 0 <= row < numRows()
col - 0 <= col < numCols()

numRows

public int numRows()
Get the number of rows.

Returns:
the number of rows in this MathMatrix

numCols

public int numCols()
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 < numRows(), col 0 <= col < numCols()

Parameters:
row - 0 <= row < numRows()
col - 0 <= col < numCols()
Returns:
the value at the specified position

add

public MathMatrix add(MathMatrix rightHandSide)
implements MathMatrix addition, (this MathMatrix) + rightHandSide.
pre: rightHandSide.numRows() = numRows(), rightHandSide.numCols() = numCols()
post: This method does not alter the calling object or rightHandSide

Parameters:
rightHandSide - rightHandSide.numRows() = numRows(), rightHandSide.numCols() = numCols()
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.numRows() = numRows(), rightHandSide.numCols() = numCols()
post: This method does not alter the calling object or rightHandSide

Parameters:
rightHandSide - rightHandSide.numRows() = numRows(), rightHandSide.numCols() = numCols()
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.numRows() = numCols()
post: This method should not alter the calling object or rightHandSide

Parameters:
rightHandSide - rightHandSide.numRows() = numCols()
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.

scale

public void scale(int factor)
Multiply all elements of this MathMatrix by factor.
pre: none
post: all elements in this matrix have been multiplied by factor. In other words after this method has been called getVal(r,c) = old getVal(r, c) * factor for all valid r and c.

Parameters:
factor - the value to multipy every cell in this Matrix by.

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 seperate 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. numRows() == numCols()

Returns:
true if this MathMatrix is upper triangular, false otherwise.