Hurray for F^cking Matrices
4 posters
Page 1 of 1
Hurray for F^cking Matrices
- Code:
<script language=JavaScript>
<!-- hide
// test case
function test()
{
// mat A
document.matrix.a11.value = 1;
document.matrix.a12.value = 1;
document.matrix.a13.value = -1;
document.matrix.a14.value = 0;
document.matrix.a21.value = 1;
document.matrix.a22.value = 2;
document.matrix.a23.value = 1;
document.matrix.a24.value = 8;
document.matrix.a31.value = 2;
document.matrix.a32.value = -1;
document.matrix.a33.value = 1;
document.matrix.a34.value = 3;
// answer: x = {1, 2, 3}
}
var M = new Array(12);
var state = parseInt("0");
var ROWS = parseInt("3");
var COLS = parseInt("4");
function init()
// initialize state of algorithm
{
state = parseInt("0");
next_step( state );
}
function next_step( n )
// display next action to user
{
var s;
switch( n )
{
case 0: s = "Check A11"; break;
case 1: s = "Zeroize A21"; break;
case 2: s = "Zeroize A31"; break;
case 3: s = "Check A22"; break;
case 4: s = "Zeroize A32"; break;
case 5: s = "Check A33"; break;
case 6: s = "Zeroize A23"; break;
case 7: s = "Zeroize A13"; break;
case 8: s = "Zeroize A12"; break;
case 9: s = "A11 --> 1"; break;
case 10: s = "A22 --> 1"; break;
case 11: s = "A33 --> 1"; break;
default: s = "All Done"; break;
}
document.matrix.state.value = s;
}
function setA( row, col, val )
// calc array index for matrix element
// and set new value
{
var n = (col-1) + COLS*(row-1);
M[n] = val;
}
function getA( row, col )
// calc array index for matrix element
// and return current value
{
var n = (col-1) + COLS*(row-1);
return M[n];
}
function rowop( j, i )
// perform a row operation to zeroize non-diagonal element A(j,i)
//
// A(j) = A(j) - m * A(i)
//
// NOTE: (j != i)
{
var k;
var m = getA(j,i) / getA(i,i); // choose m so that A(j,i) --> 0
for( k = COLS; k >= i; k-- ) // do each element in row
{
var t = getA(j,k) - m * getA(i,k);
setA( j, k, t );
}
}
function swap( i )
// find first row below A(i,i) with non-zero element in column i
// if such a row is available, swap with row i
// otherwise there is no solution. Algorithm must detect this
// condition.
{
var first = parseInt("0");
var j, k;
for( j = i; j <= ROWS; j++ )
{
if( Math.abs( getA(j,i) ) > 0 )
{
first = j;
break;
}
}
if( first > 0 ) // a row is available
{
for( k = 1; k <= COLS; k++ ) // do each element
{
var t = getA(i,k); // save elem in row i
setA( i, k, getA(first,k) ); // replace i with first
setA( first, k, t ); // replace first with i
}
}
}
function normalize( i )
// multiplies each element of a row by the inverse
// of A(i,i) so that A(i,i) --> 1
// applies rounding at last step of process
{
var j;
if( Math.abs( getA(i,i) ) > 0 )
{
var m = 1 / getA(i,i);
for( j = i; j <= COLS; j++ )
{
var r = m*getA(i,j);
r = Math.round( 100000 * r ) / 100000;
setA( i, j, r );
}
}
}
function read_matrix()
// reads current values of matrix into array
{
setA(1,1,parseFloat(document.matrix.a11.value, 10));
setA(1,2,parseFloat(document.matrix.a12.value, 10));
setA(1,3,parseFloat(document.matrix.a13.value, 10));
setA(1,4,parseFloat(document.matrix.a14.value, 10));
setA(2,1,parseFloat(document.matrix.a21.value, 10));
setA(2,2,parseFloat(document.matrix.a22.value, 10));
setA(2,3,parseFloat(document.matrix.a23.value, 10));
setA(2,4,parseFloat(document.matrix.a24.value, 10));
setA(3,1,parseFloat(document.matrix.a31.value, 10));
setA(3,2,parseFloat(document.matrix.a32.value, 10));
setA(3,3,parseFloat(document.matrix.a33.value, 10));
setA(3,4,parseFloat(document.matrix.a34.value, 10));
}
function round( r )
{
if( Math.abs( r ) < 0.0000000001 )
r = 0.0;
return r;
}
function write_matrix()
// updates values of matrix to the current solution state
{
document.matrix.a11.value = round( getA(1,1) );
document.matrix.a12.value = round( getA(1,2) );
document.matrix.a13.value = round( getA(1,3) );
document.matrix.a14.value = round( getA(1,4) );
document.matrix.a21.value = round( getA(2,1) );
document.matrix.a22.value = round( getA(2,2) );
document.matrix.a23.value = round( getA(2,3) );
document.matrix.a24.value = round( getA(2,4) );
document.matrix.a31.value = round( getA(3,1) );
document.matrix.a32.value = round( getA(3,2) );
document.matrix.a33.value = round( getA(3,3) );
document.matrix.a34.value = round( getA(3,4) );
}
function step()
// do one step of Gauss Jordan algorithm
{
read_matrix();
switch( state )
{
case 0: // check A11
if( getA(1,1) == 0.0 ) swap(1);
if( getA(1,1) == 0.0 )
{
alert( "Singular!" );
break;
}
state = 1;
break;
case 1: // zeroize A21
rowop( 2, 1 );
state = 2;
break;
case 2: // zeroize A31
rowop( 3, 1 );
state = 3;
break;
case 3: // check A22
if( getA(2,2) == 0.0 ) swap(2);
if( getA(2,2) == 0.0 )
{
alert( "Singular!" );
break;
}
state = 4;
break;
case 4: // zeroize A32
rowop( 3, 2 );
state = 5;
break;
case 5: // check A33
if( getA(3,3) == 0.0 )
{
alert( "Singular!" );
break;
}
state = 6;
break;
case 6: // zeroize A23
rowop( 2, 3 );
state = 7;
break;
case 7: // zeroize A13
rowop( 1, 3 );
state = 8;
break;
case 8: // zeroize A12
rowop( 1, 2 );
state = 9;
break;
case 9: // A11 --> 1
normalize( 1 );
state = 10;
break;
case 10: // A22 --> 1
normalize( 2 );
state = 11;
break;
case 11: // A33 --> 1
normalize( 3 );
state = -1;
break;
}
next_step( state );
write_matrix();
}
// unhide -->
</script>
this sexy bitch got me through math yesterday.. its a 3x3 Matrix Solver
InternetHateMachine- Posts : 107
Join date : 2010-01-08
Age : 32
Location : Kiddy Land
Re: Hurray for F^cking Matrices
3x3 Matrices are easy shit, especially on your graphing calculator. We just finished doing that shit before midterms.
Fabianxx13- Posts : 20
Join date : 2010-01-10
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum