Matrix Calculator Applet

Matrix-Calculator-Applet

Online tool for matrix calculation with javascript.

All Matrices must be n x n
Maximum Matrix Size: 50 x 50
Enter Matrix Elements Row by Row seperated by spaces.
Ex. (3×3)

1 2 3
4 5 6
7 8 9

Or if you like, as one line:

1 2 3 4 5 6 7 8 9
Both of these example matrices are the same.
Most results will be placed in the C matrix.

Trans(A) calculates the transpose of matrix A.
inv(A) calculates the inverse of matrix A
adj(A) is the adjoint of matrix A
UpperTri(A) converts A to an upper triangular matrix
The calculation of the determinant, by definition, is based upon a factorial number of calculations with respect to the size of the matrix. ie. a 3×3 matrix would have 6 calculations (3!) to make, whereas a 20×20 matrix would have 2.43 x 10^18 calculations (20!). So instead of brute forcing the calculations, I first do some operations on the matrix, which converts it to a upper triangular matrix, and then calculate the determinant by multipling down the diagonal, since everything below is 0, this will give the determinant.

For some reason computers aren’t as accurate as I think they are, probably my calculation techniques, but right now I can’t find my numerical analysis book, so I’m not sure what I’m doing wrong. Anyways, the accuracy of the numbers are probably only to 3 maybe 2 decimal places. If you keep applying operations to matrices and then use the resultant matrix a couple of times, it will not be that accurate. The matrix times its inverse is a good example of this.

Some Theories that You can Test

The determinant of A-inverse equals 1 over the determinant of A.
If two rows of matrix A are equal, the determinant of A equals 0.
det(A*B)=det(A)det(B)
A*B does not necessarily equal B*A
The determinant of A-transpose equals the determinant of A.
If the matrix B is constructed by interchanging two rows (columns) in matrix A, then the determinant of B equals the negative determinant of A
You can test, adj(A) = det(A) * inv(A), but this is the theorem I use to calculate the inverse, so it better work.

//___________________________________________________________
//
// LinearAlgebra Calculator – Java Applet Ver
// Date: October 1997
//
// Author: Marcus Kazmierczak
// marcus@mkaz.com
//
// (c) 1997-98 mkaz.com
// http://www.mkaz.com/math/
//
//
//___________________________________________________________
//————————————————————–
//———–Mathematical Functions — The REAL Work ———–
//————————————————————–

public float[][] AddMatrix(float[][] a, float[][] b) throws MatrixException
{

if (nA != nB)
{ throw new MatrixException(“The matrices are not the same size.”); }

float matrix[][] = new float[n][n];

for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
{
matrix[i][j] = a[i][j] + b[i][j];
}

return matrix;
}

//————————————————————–
public float[][] MultiplyMatrix(float[][] a, float[][] b) throws MatrixException
{

if (nA != nB)
{ throw new MatrixException(“The matrices are not the same size.”); }

float matrix[][] = new float[n][n];

for (int i=0; i<n; i++)
for (int j=0; j<n; j++) matrix[i][j]=0;

for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
{
for (int p=0; p<n; p++)
matrix[i][j] += a[i][p]*b[p][j];
}

return matrix;
}
//————————————————————–

public float[][] Transpose(float[][] a)
{
float m[][] = new float[n][n];

for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
{
m[i][j] = a[j][i];
}

return m;
}

//————————————————————–

public float[][] Inverse(float[][] a) throws MatrixException
{
// inv(A) = 1/det(A) * adj(A)

float m[][] = new float[n][n];
float mm[][] = Adjoint(a);

float det = Determinant(a);
float dd;

if (det==0)
{ taC.setText(“”);
throw new MatrixException(“Determinant Equals 0, Not Invertible.”); }

dd = 1/det;

for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
{ m[i][j] = dd * mm[i][j]; }

return m;
}

//————————————————————–

public float[][] Adjoint(float[][] a) throws MatrixException
{
float m[][] = new float[n][n];

int t = n;
float det;

for (int i=0; i<t; i++)
for (int j=0; j<t; j++)
{

float ap[][] = new float[t-1][t-1];
taTemp.setText(“”);

for (int ii=0; ii<t; ii++)
for (int jj=0; jj<t; jj++)
{
if ((ii!=i) && (jj!=j))
{
taTemp.appendText(a[ii][jj] + ” “);
}
}

ap = ReadInMatrix(“temp”);

det = Determinant(ap);

m[i][j] = (float)Math.pow(-1, i+j) * det;

}

n= t;

m = Transpose(m);

return m;
}

//————————————————————–

public float[][] UpperTriangle(float[][] a)
{
float m[][] = new float[n][n];
float f1;
float temp;
iDF=1;

int v=1;

m = a;

for (int col=0; col<n-1; col++)
for (int row=col+1; row<n; row++)
{
v=1;

outahere:
while(m[col][col] == 0) // check if 0 in diagonal
{ // if so switch until not
if (col+v >= n) // check if switched all rows
{ iDF=0;
break outahere; }
else
{
for(int c=0; c<n; c++)
{ temp = m[col][c];
m[col][c]=m[col+v][c]; // switch rows
m[col+v][c] = temp; }
v++; // row switch
iDF = iDF * -1; } // determinant factor
}
if ( m[col][col] != 0 )
{
f1 = -m[row][col]/m[col][col];

for (int i=col; i<n; i++)
m[row][i] = f1*m[col][i] + m[row][i];
}
}

return m;
}

//————————————————————–

public float Determinant(float[][] a)
{
float matrix[][] = new float[n][n];
float det=1;

matrix = UpperTriangle(a);

for (int i=0; i<n; i++)
{ det = det * matrix[i][i]; } // multiply down diagonal

det = det * iDF;

return det;
}

//————————————————————–

public String getAppletInfo()
{
return “Name: Matrix Calculator\r\n” +
“Version: 1.2\r\n” +
“Author: Marcus Kazmierczak\r\n” +
“Oct 5th, 1997. All Rights Reserved.”;
}

//————————————————————–



Copyright 2017 JSResource.com. All rights reserved.

Posted February 1, 2017 by in category JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *