# Project 5: Matrix Abstract Data Type

## Due: Tuesday 12 November 2007

### Description (Draft)

#### The Code

In this project you are asked to implement several methods in a file Matrix.java, which will contain a class that represents a matrix. The final Matrix objects should be able to perform the following nine operations: add, subtract, (matrix and scalar) multiply, transpose, identity matrix, trace, and random matrix, and a method that converts a matrix to an array. In this project, you will need to rely on your knowledge of loops and build on your understanding of arrays, constructors, method overloading, exception handling, and static and non-static methods. In this project you will decide on the underlying implementation. I will, of course, review your code. But unlike before, I will also subject your Matrix class to a series of correctness tests. To pass the correctness tests, your implementation need only follow the specifications described below.

First, we need to know how to create new Matrix objects. Here is a list of constructors that you need to implement:

• Matrix(int m, int n). Creates a new matrix with m rows and n columns whose initial entries are all zero.
• Matrix(double[][] elements). Creates a new matrix from the two-dimensional array elements. (You may assume elements is not a ragged array.)

The methods that you need to implement are:

• Matrix add(Matrix M). Returns a new Matrix object which represents the element-wise sum of the current matrix and another matrix M and throws a Exception if the addition is not defined. Matrix addition is valid on for two matrices of the same size. For example, if the matrices below are represented by the Matrix objects A and B,

then the Java expression A.add(B) should evaluate to their sum (shown below).
• Matrix subtract(Matrix M). Returns a Matrix whose elements are the entry-wise subtraction of the current matrix and another matrix M when subtraction is well-defined and throws an Exception otherwise.
• Matrix multiply(double scalar). Returns a Matrix object which results when each entry of the current matrix is multiplied by the factor scalar. For example, if the matrix A is as above, then scalar multiplication of A by the scalar factor 8 is:
• Matrix multiply(Matrix M). Returns a Matrix object that represents the result of multiplying the current matrix by the matrix M on the right if the multiplication is valid and throws an Exception otherwise.

Matrix multiplication is valid only if the number of columns of the first matrix is the same as the number of rows of the second matrix. For notational convenience, let's say that the matrix C is the matrix product A·B. The the element in row i and column j of C, Cij is the sum

Cij = ∑kAikBkj,

where k ranges between 1 and the number of columns in A. For a fuller account, come to lecture or read the appropriate article on matrix multiplication at Wikipedia. Notice that matrix multiplication does not commute. That is, A.multiply(B) does not necessarily give the same matrix as B.multiply(A).

• Matrix transpose(). Returns a Matrix whose i, j-th element is the same as the j, i-th element of the current matrix. Transposition swaps elements in corresponding positions across the diagonal of a matrix. With A as above, A.transpose() should return the matrix shown below.
• Matrix identity(int n). Returns an n-by-n matrix whose diagonal elements are 1 and whose off-diagonal elements are 0.
• Matrix random(int m, int n). Returns an m-by-n matrix whose entries are random numbers between 0 and 1.
• double trace(). Returns the sum of the diagonal elements of the current matrix. For example, A.trace() should return the value 18 (1+9+8), while B.trace() evaluates to 6 (1+2+3).
• double[][] toArray(). Returns the current matrix represented as a two-dimensional array.

Be sure to comment your code fully and prefix your Matrix class file with comments that include your name, course title, date, and the project number and title. For example:

// Joshua Reyes
// CSIT 114
// 12 November 2007

#### The Write-up

Once you have implemented the method above, write longer essay detailing your experience programming in a plain text file called memo.txt. (Do not use an editor like MS Word.) Remember that your analysis counts for the majority of your grade. Please write in clear, proper English. As above, be sure to include your name, class, and project title.

Here are some questions to keep in mind:

• How did you test each of your functions? Did you use matrices that were not square?
• How and why did you decide whether to make each of your methods static?
• You can place error-checking in many places; where in your code did you handle exceptions? How did you decide on those locations?
• Were you able to reuse your code in anyway? For example, subtraction can be thought of as the composition of scalar multiplication followed by addition. Were there other instances to reuse code?
• What about your coding experience did you find suprising? What did you expect would happen instead?
• Why or why didn't you like this project? How could it be improved?

If you worked with others, explain what each person contributed to your project. Cite others appropriately. Even if you worked with others, the code you pass in ought to be your own.

### Turning the Project In

Use a secure FTP client to create a subdirectory of csit114 called project4. Upload your Picture.java (and Pixel.java, SimplePicture.java if necessary) and memo.txt to that directory by Tuesday, 16 October 2007. Please make sure that your file names are spelled correctly. It is frustrating for me to have to search for your files by hand.