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
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
int
s. 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
IllegalArgumentException
s to methods to check preconditions.
You must use a "native" two dimensional array of
int
s 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:
Tips:
MathMatrix
objects and the 2d array of int
s
that serves as the storage container.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
String
s (and / or StringBuffer
s) 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.