# A Matrix Calculator - by Dr. Nicholas Duchon

## Notes:

This Java applet will calculate the following matrix operations:

• Sum of two matrices
• Difference of two matrices
• Product of two matrices
• Determinant of a matrix - notes on a Java version of this algorithm
• Inverse of a matrix.
• Show Steps - only applies to the Inverse calculation.
Modes: This calculator will work in one of four modes:
• Integer - the basic Java data type is int, and all calculations except inverse and determinant are done using integer arithmetic.The display is also integers.
• Decimal - the basic Java data type is double.
• Rational - the basic Java data type is a custom class, Rational, which is a quotient of two int's. All calculations and displays are done as fractions.
• Complex - the basic Java data type is a custom class, with the real and imaginary parts represented as doubles. All calculations and displays are done using double values.
The Exponential Notation check box can be used to specify that the double output be formatted using scientific notation or the default notation for double numbers.

Slider - Use this slider to set the number of digits displayed in the decimal representation of the resulting matrix. These numbers are rounded up or down to the nearest digit.

The top left box is used to enter the values of the A matrix. The values in a row are separated by one or more spaces, and a new row is started with a <return>. The top right box is used to contain the values of the B matrix.

As many check boxes may selected as desired. The results of the computation are shown in the lower box. Each calculation will list the input matrices and the results of the specified operation. This display scrolls, so large outputs can be accommodated.

All of the text areas support copy/paste operations. Thus, the results of one operation can be used as the input for the next. See an example of chaining row operations below.

## Input format examples:

 Integer Decimal Rational Complex No spaces between real and complex parts Numbers in double format 1 2 3 33 44 55 123 443 12 3 2 9 1.2 3.4 -6 4.5 -1.4 2.3 -1 -2 -3 2 3.5 9.1 12/10 34/10 -6 45/10 -14/10 23/10 -1 -2 -3 1+2i   3-4i -12+17i -5-2i 1   2  3  33  44 55 123 443 12   3   2  9 1e4      2e4      3e4 2.2e5   -1.3e4   -6.2e5 3.2e-7  -1.4e-7   1.7e-8 12/10   34/10    -6 45/10  -14/10 23/10    -1      -2    -3 3.47+6.9i     -2.07e3-7.11i -3.4-4.96e5i  4.34e-3+9.46e5i     17             -6i

The second row for Integer and Rational shows the use of spacing for a nicer display. The numbers are handled the same way in either case. The second row of Decimal shows the full use of the double data input format available in Java, and so in this applet.

NOTE: Be sure to check inverses since the program is subject to limited precision and overflow and underflow errors.

## Example: Manually computing the inverse of the matrix shown in the Rational formatting example above:

12/10   34/10    -6 1 0 0
45/10  -14/10 23/10 0 1 0
-1      -2    -3 0 0 1

use A to do the row operations. First, normalize the 1,1 position:

A          *                 B

10/12 0 0
12/10   34/10    -6 1 0 0
0     1 0  times:  45/10  -14/10 23/10 0 1 0
0     0 1             -1      -2    -3 0 0 1

Result:

1 17/6    -5 5/6 0 0
9/2 -7/5 23/10   0 1 0
-1   -2    -3   0 0 1

Use the 1,1 as a pivot to clear the first column:

A:
1 0 0
-9/2 1 0
1 0 1

Result:

1 17/6 -5 5/6 0 0
0 -283/20 124/5 -15/4 1 0
0 5/6 -8 5/6 0 1

Now normalize the 2,2 position, multiply the second row by -20/283:

A:
1       0 0
0 -20/283 0
0       0 1

Result:

1 17/6       -5    5/6       0 0
0    1 -496/283 75/283 -20/283 0
0  5/6       -8    5/6       0 1

Now pivot on the 2,2 position:

A:
1 -17/6 0
0     1 0
0  -5/6 1

Result:
1 0   -29/849  70/849 170/849 0
0 1  -496/283  75/283 -20/283 0
0 0 -5552/849 520/849  50/849 1

A tad ugly, but we persist - normalize 3,3 position by mulitplying by -849/5552:

A:
1 0         0
0 1         0
0 0 -849/5552

Result:
1 0  -29/849  70/849  170/849        0
0 1 -496/283  75/283  -20/283        0
0 0        1 -65/694 -25/2776 -849/5552

Last calculation, pivot on 3,3 position:

A:
1 0  29/849
0 1 496/283
0 0       1

Result:
1 0 0  55/694 555/2776  -29/5552
0 1 0  35/347  -30/347   -93/347
0 0 1 -65/694 -25/2776 -849/5552

55/694 555/2776  -29/5552
35/347  -30/347   -93/347
-65/694 -25/2776 -849/5552

Check:
Array A:
6/5 17/5    -6
9/2 -7/5 23/10
-1   -2    -3

Array B:
55/694 555/2776  -29/5552
35/347  -30/347   -93/347
-65/694 -25/2776 -849/5552

Array C = A * B:
1 0 0
0 1 0
0 0 1