CS 313e Assignment #4, Implementing a Class - Mathematical Matrix

Note: Many thanks to Mike Scott for allowing me to borrow the ideas and documentation for this project.

"Linear algebra is a fantastic subject On the one hand it is clean and beautiful. If you have three vectors in 12 dimensional space, you can almost see them."
- Gilbert Strang, Linear Algebra and its Applications

Programming Assignment 4 Individual Assignment. You must complete this assignment on your own. You may not discuss your work with anyone except the instructor and other members of the instructional staff (TA, section leader, or lab proctor). You may not acquire from any source (e.g., another student or an internet site) a partial or complete solution to a problem or project that has been assigned. You may not show another student your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve an assignment.

Placed online: Oct 7
Due: no later than 11 pm, Friday, October 16

Description The purposes of this assignment are:
1. Practice implementing a stand alone class
2. Work with two dimensional arrays
3. Learn the difference between shallow and deep copy

Implement a class that represents a mathematical matrix.

Mathematical matrices are used  to solve systems of linear equations. Matrices are used in applications such as physics, engineering, probability and statistics, economics, biology, and computer science. (especially in the area of computer graphics)

Matrices appear in the following form:

These matrices represent this system of linear equations:

x +  5y + 10z + 5w =   4
6x +  4y + 12z + 4w =   5
10x +  5y + 12z + 11w = 12
5x + 11y + 23z + 9w  =  7

The above matrix has 4 rows and 4 columns, but the number of rows and columns do not have to be equal. In other words mathematical matrices do not need to be square, but they must be rectangular. Each entry can be an integer or real number. For this assignment you will only deal with matrices of integers. You will implement a class, `MathMatrix`, that models a mathematical matrix and supports various operations on matrices. See this page for an explanation of the mathematical operations you are implementing. The Wikipedia article may also be useful. After calculus, some students then take a course entitled Matrices and Matrix Calculations.

Files
 File Responsibility Implementation MathMatrix.java Provided by me and you, mostly you. You add your code and then submit via turnin. Documentation MathMatrix.html Provided by me. Testing MathMatrixTester.java Provided by me and you Submission Submitted files include MathMatrix.java, MathMatrixTester.java Provided by you.
Checklist Did you remember to:
• review and follow the assignment requirements?
• work on the assignment individually?
• implement the required methods?
• ensure your program does not suffer a compile error or runtime error?
• find and fix any incorrect tests in MathMatrixTester?
• ensure your program passes the tests in MathMatrixTester?
• if possible use other methods from the class instead of repeating code?
• Turn in your files to the proper account in the Microlab via the turnin program before 11 pm, on Oct 17?
Tips
1. Be clear on the difference between `MathMatrix` objects and the 2d array of ints that serves as the storage container for the ints that make up a `MathMatrix` object.

Assume the 2d array of int that is the instance variable for each MathMatrix object is named myCells.

{   MathMatrix result = new MathMatrix(numRows(), numCols(), 0);
int valueFromThisMathMatrix = myCells[0][0];
int valueFromRightHandSide = rightHandSide.myCells[0][0];
int valueFromResult = result.myCells[0][0];

// following line results in syntax error
// valueFromRightHandSide = rightHandSide[0][0];

2. Familiarize yourself with the concept of deep copying. (As opposed to shallow copying.) One of the constructors requires you make a deep copy of a 2d array. Here is the Wikipedia article on object copying.

3. If possible use other methods from the `MathMatrix` class instead of repeating code.

4. There are two methods involving transposing a matrix, a mutator and an accessor. One of these is necessary, but we are providing both as a convenience to the users of our class.

5. An explanation of the requirements for the `toString` method.

If the String that is returned by toString() is printed, each row should appear on a separate line, with entries separated by tabs within a line. This is the minimum requirement. For extra credit, read further.

In the String that is returned from the `toString` method the space for each "cell" is equal to the longest value in the matrix plus 1. (Don't forget to consider a minus sign in on of the values.) All cell entries are right justified with newline characters  between rows. The last row does not end in a newline character. For example, given the following `MathMatrix`.

6.  10 100 101 -1000 1000 10 55 4 1 -1 4 0

You should return a String that would appear like this. Use newline characters ("\n") to create line breaks.

10   100   101 -1000
1000    10    55     4
1    -1     4     0

In example above it can be hard to tell how many spaces there are between numbers. In this example the spaces have been replaced by periods to the number of "spaces" is more clearly shown.

....10...100...101.-1000
..1000....10....55.....4
.....1....-1.....4.....0

Please note, the last line does not include a newline character.

One way of finding the length of an int is to convert it to a `String` and find the length of the `String`. Here is an example:

int x;
//code to give x a value.
String s = "" + x;
int lengthOfInt = s.length();

//or more simply given an int x
int lengthOfX = ("" + x).length();

Doing the `toString` method using just loops and `String`s and if statements is actually a very interesting exercise. Or you can learn how to use the format method from the String class and formatting string syntax. You could use printf().

7. The `isUpperTriangular` method determines if the `MathMatrix` is an upper triangular matrix. A matrix is upper triangular if it is a square matrix and all values below the main diagonal are 0. The main diagonal is all the cells whose row and column are equal. The values of the elements on the main diagonal don't have to be zero, just the ones below it. A 1 by 1 matrix is considered upper triangular.