CSSE 230 Determinant Programming Assignment

Overview

This is an individual assignment, designed to give you practice with nontrivial recursion.  Submit your Determinant.java file on AFS, to the Determinant folder in your turnin folder.   Be sure to run our grading script (./check Determinant); it might point out flaws in your program so you can correct them before the program is graded.

You are to create a public class called Determinant. It has only one required public method:
      public static int det(int[][] a) throws IllegalArgumentException

If  a is a square matrix, a call to det(a) returns the determinant of that matrix (often written by Mathematicians as |a| ); otherwise det(a) throws an IllegalArgumentException. The determinant is calculated recursively using the “Laplacian expansion” method of minors and cofactors. 

http://mathworld.wolfram.com/DeterminantExpansionbyMinors.html

http://www.richland.cc.il.us/james/lecture/m116/matrices/determinant.html

As an example, if we expand along a row for the 3x3 case, we get

How to implement the det method

Your public method should call a recursive private method that does the actual determinant calculation, calling itself recursively to find each "minor" determinant after "crossing out" a row and column.  The temptation for one of the arguments to the recursive calls is to copy the matrix itself, removing the crossed-out row and column.  If the matrix size (N) is large, this is a very time-consuming operation – O(N2) for each recursive call.

Instead, we can pass the original matrix to all of the recursive calls, along with two sets of integers that keep track of the "non-crossed-out" rows and columns.  Before making a recursive call, we can copy the two sets, remove the appropriate row and column numbers from the copies, and pass the copies as arguments to the recursive call. (Hopefully this description is specific enough to get you started thinking but vague enough to allow you to discover some things for yourself).

Example Interface code

To illustrate how the interface works, here is some code that could be used to test your method:
		int [][] a = {{1, 2}, {3, 4}};
		System.out.println("Determinant 1: " +  Determinant.det(a));
		int [][] b = {{1, 2, 3}, {4, 5, 6}, {2, 0, 0}};
		System.out.println("Determinant 2: " + Determinant.det(b));
		int [][] c = {{1, 1, 1, 1}, {1, 2, 4, 8}, 
				      {1, 3, 9, 27}, {1, 4, 16, 64}};
		System.out.println("Determinant 3: " + Determinant.det(c));		
		int [][] d = {{1, 2, 3}, {4, 6}, {2, 0, 0}};
		System.out.println("Determinant 4: " + Determinant.det(d));


Running the above code should produce output something like this:

Determinant 1: -2
Determinant 2: -6
Determinant 3: 12
Exception in thread "main" java.lang.IllegalArgumentException: matrix not square
	at Determinant.checkDimension(Determinant.java:73)
	at Determinant.det(Determinant.java:12)
	at Determinant.main(Determinant.java:88)