Numeric Matrix Processor (Java). Stage 5/6

Determined!

Report a typo

Description

In this stage, you should write a program that calculates a determinant of a matrix. You can check out some videos about linear algebra to understand the essence of the determinant and why it is important. To see how to calculate the determinant of any square matrix, watch a video about minors and cofactors and computing the nxn determinant. Also, here's nice graphic explanation on minors and cofactors.

A determinant is a single number that can be computed from the elements of a square matrix. There is a classical way to find the determinant of a matrix with an order <3<3.

A determinant of a 2-order matrix is equal to the difference between the product of elements on the main diagonal and the product of elements on the side diagonal:

det(a11a12a21a22)=a11a22a12a21\det\begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{pmatrix} = a_{11}\cdot a_{22} - a_{12}\cdot a_{21}

Now let's move on to the minor and the cofactor of a matrix.

Minor(i,j)\mathit{Minor}_{(i, j)} of a matrix is the determinant of the submatrix we get from the remaining elements after removing the i row and j column from this matrix.

Below is an example of Minor(2,2)\mathit{Minor}_{(2, 2)} for matrix A3×3 A_{3 \times 3} :

M2,2(a1,1a1,2a1,3a2,1a2,2a2,3a3,1a3,2a3,3)=det(a1,1a1,3a3,1a3,3)M_{2,2}\begin{pmatrix} a_{1,1} & {\color{red}a_{1,2}}&a_{1,3} \\ {\color{red}a_{2,1}} & {\color{red}a_{2,2}} &{\color{red}a_{2,3}} \\ a_{3,1} & {\color{red}a_{3,2}} & a_{3,3} \end{pmatrix} =\det\begin{pmatrix} a_{1,1} & a_{1,3} \\ a_{3,1} & a_{3,3} \end{pmatrix}

Cofactor(i,j)\mathit{Cofactor}_{(i, j)} of a matrix is the corresponding Minor(i,j)\mathit{Minor}_{(i, j)} multiplied by (1)i+j (-1)^{i+j} . Notice that the cofactor is always preceded by a positive ++ or negative - sign.

We often need to find the determinant of a matrix of the order greater than 22. In this case, we have to use expansion by rows or columns where the determinant is equal to a sum of a single row or a single column multiplied by the cofactors of the elements in the corresponding row or column. To do this, you should use a recursive method.

Below is an example of computing the determinant of a matrix of order 44 by first-row expansion, where cc stands for the Cofactor\mathit{Cofactor} :

det(a1,1a1,2a1,3a1,4a2,1a2,2a2,3a2,4a3,1a3,2a3,3a3,4a4,1a4,2a4,3a4,4)=a1,1c1,1+a1,2c1,2+a1,3c1,3+a1,4c1,4\det\begin{pmatrix} a_{1,1} & a_{1,2} & a_{1,3} & a_{1,4} \\ a_{2,1} & a_{2,2} & a_{2,3} & a_{2,4} \\ a_{3,1} & a_{3,2} & a_{3,3} & a_{3,4} \\ a_{4,1} & a_{4,2} & a_{4,3} & a_{4,4} \end{pmatrix} = a_{1,1}\cdot c_{1,1} + a_{1,2}\cdot c_{1,2}+a_{1,3}\cdot c_{1,3} +a_{1,4}\cdot c_{1,4}

Objectives

In this stage, your program should support calculating the determinant of a matrix. Refer to the example to see how it should be implemented.

Example

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

Example 1: the user chooses to calculate determinants of two matrices and quits the program after the operation

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
4. Transpose matrix
5. Calculate a determinant
0. Exit
Your choice: > 5
Enter matrix size: > 3 3
Enter matrix:
> 1 7 7
> 6 6 4
> 4 2 1
The result is:
-16

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
4. Transpose matrix
5. Calculate a determinant
0. Exit
Your choice: > 5
Enter matrix size: > 5 5
Enter matrix:
> 1 2 3 4 5
> 4 5 6 4 3
> 0 0 0 1 5
> 1 3 9 8 7
> 5 8 4 7 11
The result is:
191

1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
4. Transpose matrix
5. Calculate a determinant
0. Exit
Your choice: > 0
Write a program
package processor;

public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

___

Create a free account to access the full topic