$29.99
Specification 2 - Implementing a Class - Mathematical Matrix
General Assignment Requirements
The purposes of this assignment are
To practice implementing a stand alone class.
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.
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. 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:
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 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. The Wikipedia article may also be useful.
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.
You may use any other classes and methods from the Java standard library you wish on this assignment. The Arrays class has some useful methods for dealing with arrays. You may use other classes and methods from the standard library.
Add conditional statements (ifs) that throw IllegalArgumentExceptions to methods to check preconditions.
You must use a "native" two dimensional array of ints as your underlying storage container in the matrix class:
private int[][] values;
// or nums or cells or some other appropriate name
// DO NOT USE some variation of mathMatrix or matrix.
// That is much too confusing. The two dimensional array of ints
// is NOT a MathMatrix!
The first row of a MathMatrix is numbered 0. The first column of a MathMatrix is numbered 0.
The provided source file, MathMatrixTester.java contains various tests for the MathMatrix class. Some of these tests may be incorrect. You must find and fix any incorrect tests. Your MathMatrix class must pass the included tests. Use the class discussion group to identify incorrect tests and share your own tests.
Add at least 2 new tests per public method to this class. (24 tests total.) I encourage you to share you tests with others via the class discussion group.
You are encouraged to create private helper methods and use other public methods in the MathMatrix class when completing methods in the MathMatrix class if this simplifies the solution.
Note, once a MathMatrix object is created there are no methods to alter its size. So unlike the IntList we created in class it does not make sense to have extra capacity. The size of the 2d array of ints will be the same size as the Mathematical Matrix it is representing. (This implies it is not necessary to keep track of the number of rows and columns with separate instance variables.)
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.
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 Stopwtach. 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 the following experiment 1000 times and note the average time of the 1000 experiments for each value of N. (3 total, N, 2N, 4N)
Use the Stopwatch class to record the time it takes to add the 2 MathMatrix objects together.
You must choose a value for the number of rows and columns so that all of the 1000 tests give a result of at least 5 milliseconds elapsed time per test. (5 millisecond is 0.005 seconds) You should, of course, automate these 1000 repetitions.
On my old computer a MathMatrix dimension equal to 800 (So the MathMatrix was 800 by 800, 640,000 total elements) led to all measured times being greater than 5 milliseconds. Your results will vary based on the speed of the computer you run the test on.
Record the dimension of the matrix and the average time it took for the add operation based on 1000 repetitions.
Now double the dimension of the matrix, create two matrices with random values, and repeat the experiment. In my example the original MathMatrix was 800 by 800. In this step the size would be increased to 1600 by 1600.
Record the dimension of the matrix and the average time it took for the add operation on the larger matrix based on 1000 repetitions.
Double the dimension of the matrix one more time, create two matrices with random values, and conduct 1000 additions, and determine the average time.
If you get an out of heap space error, increase the size of the heap. All of the possible command line flags are on this page. In Eclipse you can set a command line flag for your program. Follow the instructions for enabling assertions and include the flag -Xmxsize where size is the new requested heap size. For example, to increase the heap size to 120 mb include the command line flag -Xmx120m.
Experiment 2:
Perform the same basic experiment as experiment 1, but use the multiply method instead of the add method. Repeat the experiment 1000 times to get the average time.
You can use a much smaller dimension than in experiment 1 and still avoid measured times of less than 1 millisecond. You must choose a size that results in at least 10 milliseconds for the experiment.On my old computer a dimension of 200 (a 200 by 200 matrix. 40,000 elements) avoided any times below 5 milliseconds. I then ran 100 experiments for a 200 x 200, 400 x 400, and 800 x 800 arrays.
Questions. Answer the following questions. Place your answers in your comment at the top of MathMatrixTester.java along with the results of your experiments.
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?
What do you think the Big O of the add operation is given two N by N matrices? Does your timing data support this?
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?
What do you think the Big O of the multiply operation is given two N by N matrices? Does your timing data support this?
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.)
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 [case sensitive! Do not name the file 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.
Ensure you are turning in the version of MathMatrix.java that has your completed methods and the version of MathMatrixTester.java with your extra tests added and the original tests deleted. You may have more than one version of the files on your system. Do not turn in the .class file; turn in the Java source code. If you turn in the wrong one you will get a zero on the assignment.
Ensure you files are named MathMatrix.java and MathMatrixTester.java. Failure to do so will result in points off.
Ensure MathMatrix.java and MathMatrixTester are part of the default package. Do not add a package statement to the either file.
Ensure your zip has no internal directory structure. When the file is unzipped no directories (folders) are created.
Ensure you submit the assignment under Programming Assignment 2 in Canvas.
Checklist: Did you remember to:
review and follow the general assignment requirements?
work on the assignment by yourself and complete all the solution code on you own?
fill in the headers in the MathMatrix and MathMatrixTester classes? If you are using Eclipse be sure to expand the header comment.
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?
add your own tests (at least 2 per public method) to the main method of MathMatrixTester?
complete the experiments and place you answers to the questions in a comment at the top of the MathMatrixTester file?
turn in your files (MathMatrix.java and MathMatrixTester.java) in a zip named a2.zip with no internal directory structure?
turn in your zip named a2.zip to Programming Assignment 2 via Canvas no later than 11 pm on Thursday, February 12?
Tips:
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 add(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];
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.
If possible use other methods from the MathMatrix class instead of repeating code.
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.
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 StringBuffers) and if statements is a very 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.
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 don't have to be zero, just the ones below it. A 1 by 1 matrix is upper triangular.