CS 314 Specification 2 - Implementing a Class - Mathematical Matrix

 "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 2: Individual Assignment. You must complete this assignment on your own. 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 the assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve the assignment. You may get help from the instructional staff. You may discuss general ideas and approaches with other students but you may not develop code together. Review the class policy on collaboration from the syllabus.

The purposes of this assignment are

  1. To implement a stand-alone class.
  2. To work with two dimensional arrays.

Provided Files:

  File Responsibility
Implementation MathMatrix.java Provided by me and you. (Okay, mostly you.)
Documentation MathMatrix.html Provided by me.
Implementation Stopwatch.java (For use in experiments) Provided my me
Documentation Stopwatch.html Provided by me
Testing MathMatrixTester.java Provided by me and you

Description: Implement a class that represents a mathematical matrix. You are implementing a stand alone class that is a new data type.

 Matrices are used in applications such as physics, engineering, probability and statistics, economics, biology, and computer science. (especially in the area of computer graphics. One use of mathematical matrices is to solve systems of linear equations. Here is a page on how matrices are used to perform rotations on 3d objects in a graphics system. )  There is a course in the UT Math department that covers matrices, 340L, and many CS students take this course. Dr. Maggie Meyers and CS Professor Robert van de Geijn offer an online linear algebra with a programming component.

Matrices appear in the following form:

[[1 5 10 5], [6 4 12 4], [10 5 12 11], [5 11 23 9]]     = [[4], [5], [12], [7]]

These matrices could 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 the matrices will only contain java ints. 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.


Requirements: The provided source file MathMatrix.java contains a skeleton implementation of a class to model mathematical matrices.

Implement all of the methods in MathMatrix.java under the constraints of the general requirements.


Experiment: In addition to completing the MathMatrix.java class and adding tests to the MathMatrixTester.java class, perform the following experiments and answer the following questions. Place your results and answers in a comment at the top of MathMatrixTester.java. Recall, you cannot share your experiment code with others. You CAN share tests on Piazza to help each test your solution code with a wide variety of test cases. The tests you turn in must be your own.

Include the code that conducts the experiments in the MathMatrixTester.java class, but comment it out.

Use the Stopwatch class to record the time it takes to perform various operations on MathMatrix objects.

Stopwatch s = new Stopwatch();
s.start();
//code to time
s.stop();

The Stopwatch class has methods that return the elapsed time in seconds or nanoseconds between starting and stopping the Stopwatch. See the Stopwatch class documentation for more details.

Experiment 1: Create two matrices and fill them with random values. Initially try matrices that are 1000 by 1000 in size. You may have to adjust the dimension as described below. Reuse the same matrices for the following experiments. Repeat each experiment 1000 times and note the total time of the 1000 experiments for each value of N. (3 total, N, 2N, 4N)

Experiment 2:

Questions. Answer the following questions. Place your answers in your comment at the top of MathMatrixTester.java along with the results of your experiments.

  1. Based on the results of experiment 1, how long do you expect the add method to take if you doubled the dimension size of the MathMatrix objects again?

  2. What is the Big O of the add operation given two N by N matrices based on an analysis of your code? Does your timing data support this?

  3. Based on the results of experiment 2, how long do you expect the multiply method to take if you doubled the dimension size of the MathMatrix objects again?

  4. What is the Big O of the multiply operation given two N by N matrices based on analysis of your code? Does your timing data support this?

  5. How large a matrix can you create before your program runs out of heap memory?  (When using the default heap size. No command line flag to increase heap size.)  In other words what size matrix causes a Java OutOfMemoryError, Estimate the amount of memory your program is allocated based on the largest possible matrix object it can create successfully. (Recall, an int in Java requires 4 bytes.) State your answer in megabytes. What percentage of your computer's RAM did your program use before crashing?


Submission: Fill in the header for MathMatrix.java and MathMatrixTester.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own as required. I will use plagiarism detection software on your submissions. If you copy solution code from another source you are cheating. I will submit an academic dishonesty case with a recommended penalty of an F in the course.

Create a zip file name a2.zip with your MathMatrix.java and MathMatrixTester.java files. The zip file must not contain any directory structure, just the two required files.

See this page for instructions on how to create a zip via Eclipse.

Turn in a2.zip via your Canvas account to programming assignment 2 on Canvas.

Checklist: Did you remember to:

  1. review and follow the general assignment requirements?
  2. work on the assignment by yourself and complete all the solution code on you own?
  3. fill in the headers in the MathMatrix and MathMatrixTester classes? Some IDEs may collapse block comments.
  4. implement the required methods?
  5. ensure your program does not suffer a compile error or runtime error?
  6. find and fix any incorrect tests in MathMatrixTester?
  7. ensure your program passes the tests in MathMatrixTester?
  8. add your own tests (at least 2 per public method) to the main method of MathMatrixTester?
  9. complete the experiments and place you answers to the questions in a comment at the top of the MathMatrixTester file?
  10. turn in your files (MathMatrix.java and MathMatrixTester.java) in a zip named a2.zip with no internal directory structure?
  11. turn in your zip named a2.zip to Programming Assignment 2 via Canvas no later than 11 pm on Thursday, February 1.

Tips:

  1. Be clear on the difference between MathMatrix objects and the 2d array of ints that serves as the storage container.

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

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

        // the 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 of ints.
     
  3. An explanation of the requirements for the toString method.

    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. For example, given the following MathMatrix.

    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|

    Note, the last line includes a newline character on ALL of the rows, even the last one.

    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();

    Implementing the toString method using just loops and Strings (and / or StringBuilders) and if statements is an interesting exercise. Alternatively you can learn how to use the format method from the String class and formatting string syntax. Here is an introduction to formatting String syntax. Avoid creating too many unnecessary Strings.
     

  4. 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 consists of the cells whose row and column are equal. (Runs for the top left to the bottom right.) The values of the elements on the main diagonal and above it don't have to be zero, just the ones below it. A 1 by 1 matrix is upper triangular.

Back to the CS 314 homepage.