Matrix Assignment

../../_images/Matrix_definition.png

Matrices have many different uses and applications both in math and science. In fact, many of you will use matrices in physics in order to solve systems of equations.

In this assignment, you will write a class to find the inverse of a given matrix, if in fact the inverse exists. The inverse of a matrix is one such that when multiplied to the original matrix produces the identity matrix (a square matrix of all zeros except for ones along the diaganol). You can read about why the inverse is useful here .

There are various techniques for finding the inverse of a matrix. This assignment asks you to find it using elementary row operations . Each elementary row operation will be a method in your Matrix class, except for invert() which will call the other methods.

As with the Riemann Sums assignment, there is a Matrix JavaDoc to guide you. You will also create a MatrixApp class to test your Matrix and a MatrixTest test class with several test cases.

This assignment can seem very daunting at first. Before you decide what instructions you want to give your computer, you have to determine how you would manipulate a matrix first. So, we recommend that you start by trying to modify a few different matrices into the identity matrix by hand.

Note

Except for the methods that start with set__, none of the methods should actually modify the original Matrix. Instead, they return an entirely new Matrix. Make sure, when you are implementing these methods, that you are not modifying the original Matrix, but rather the Matrix that you will return.

It will be very important to identify your edge cases when you are testing. Again, much of the work of this project will be to refine your algorithm once you think you know what the basic logic should be. How will you handle non-square matrices? Uninvertible matrices?

Additionally, think about how you can tell your program is working correctly. It’s hard to look at an inversion of a matrix and tell if it’s correct, so try to use the properties of inverse matrices in order to make testing easier. Also remember that it is very hard to naturally come by a singular (i.e. uninvertable) matrix by chance, so you might want to intentionally create singular matrices.

Matrix Assignment

Remember to document as you go. Each method you write should have a documentation comment (ideally in the JavaDoc format) before it:

/**
 * [DESCRIPTION OF WHAT THE METHOD DOES]
 *
 * @param left [DESCRIPTION OF THE 'left' PARAMETER]
 * @param right [DESCRIPTION OF THE 'right' PARAMETER]
 * @param subintervals [DESCRIPION OF THE 'subintervals' PARAMETER]
 * @return [DESCRIPTION OF WHAT THE METHOD RETURNS]
 */
public double calculateDeltaX(double left, double right, int subintervals) {
    // the actual method
}

Base Assignment

1. Matrix Class

Exercise

Summary: Create a Matrix object that performs common matrix operations.

  1. In src folder, create a package namespace called com.[yourname].matrix.
  2. In matrix create a class called Matrix.
  3. Add a private attribute called matrix that is a two-dimensional array of double s.
  4. Write the Matrix constructors according to the Matrix JavaDoc .
  5. Implement the rest of the methods in the Matrix JavaDoc .
  6. Optionally, create other helper methods to aid in inverting a matrix.

2. Test Classes

Exercise

Summary: Test the Matrix methods.

  1. In the test folder, create a class called MatrixTest and import all the necessary JUnit libraries.
  2. In the class, but outside of any methods, create a global Matrix variable of some size, e.g. Matrix matrix = new Matrix(10, 10);
  3. Write several test methods to test every method in the matrix object. You can check the answers to an inverse matrix using a matrix inverse calculator.

When all of the tests pass you are done with this exercise.

3. MatrixApp

Exercise

Summary: Use the Matrix object.

  1. Back in matrix, create a new class called MatrixApp.
  2. Create a main method.
  3. Create an example Matrix object and print it to the console. Then print its inverse, like this:
../../_images/matrix.png

Extensions

After the base assignment is done, do more with your Matrix. You can use one of the ideas below or think of your own.

Extension Ideas

  1. Another way to find the inverse of a matrix by using its determinant. Implement methods to find the determinant and use it to find the inverse.
  2. Use your Matrix class to interpolate a polynomial given a set of points. Think about whether given n points what is the degree of the polynomial you will need to make.
  3. See if you can find a relationship between the derivative of an accumulation function (recall the Riemann sum assignment), and the accumulation function of the derivative of the function.
  4. Use your Matrix class to balance a chemical equation. See this research paper on Balancing of Chemical Equations using Matrix Algebra.

You could also make a GUI. Even if you choose not to make a GUI, you should definitely have a way in which people can input their own polynomials through a command line interface.