473,226 Members | 1,620 Online

# how to inverse 3x3 matrices

hello guys!!
really need your help rite now..i;m having problem on how to inverse 3x3 matrix using php.
hope guys out there who had better info bout this can help me..:(
Mar 28 '08 #1
6 7076
hsriat
1,654 Expert 1GB
hello guys!!
really need your help rite now..i;m having problem on how to inverse 3x3 matrix using php.
hope guys out there who had better info bout this can help me..:(
Is that mathematical inversion of the matrix or just interchanging rows and columns?
Mar 28 '08 #2
Is that mathematical inversion of the matrix or just interchanging rows and columns?
emm..it is the mathematical inversion of the matrix...its really make me mad..:(
Mar 28 '08 #3
hsriat
1,654 Expert 1GB
Tell me the mathematical way how to do that (or give link to a matrix tutorial). Its been long I studied them, and I have forgotten the way to invert it.

Mar 28 '08 #4
hsriat
1,654 Expert 1GB
[PHP]<?php

//SIZE OF SQUARE MATRIX (CAN BE ANY DEPENDING UPON solve_matrix(\$m) FUNCTION)
\$size = 3;
if (!isset(\$_POST['submit']))
{
echo "<form action=\"".\$_SERVER['PHP_SELF']."\" method=\"post\"><center><br><br><h2>Calculate ".\$size."x".\$size." Matrix Inverse</h2><br>";
for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)
{
echo "<input type=\"text\" name=\"x_".\$i."_".\$j."\" style=\"width:50px;text-align:center;\">";
if (\$j==\$size-1) echo "<br>";
}
echo "<br><input name=\"submit\" type=\"submit\" value=\"Calculate Inverse\"></center></form>";

}

else
{

for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)
if (filter_var(\$_POST['x_'.\$i.'_'.\$j], FILTER_VALIDATE_INT)===false)
die ('<br><center><h3>Invalid Data Type</h3></center>');

echo "<center>";

\$matrix = array();
for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)
\$matrix[\$i][\$j] = \$_POST['x_'.\$i.'_'.\$j];
unset(\$_POST);

display_matrix(\$matrix, "<b>Matrix</b>");

\$I_matrix_I = solve_matrix(\$matrix);
if (\$I_matrix_I==0) die('NULL MATRIX');

\$inverse = array();
for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)
{
\$cofactor = array();
for (\$k=0; \$k<\$size; \$k++)
{
if (\$i!=\$k)
{
\$temp = array();
for (\$l=0; \$l<\$size; \$l++)
{
if (\$j!=\$l)
array_push(\$temp, \$matrix[\$k][\$l]);
}
array_push(\$cofactor, \$temp);
}
}
display_matrix(\$cofactor, "<b>Cofactor</b> <sub>".(\$i+1).(\$j+1)."</sub>");
\$adjoint[\$j][\$i] = solve_matrix(\$cofactor)*pow((-1),(\$i+\$j)); //INTERCHANGING \$i \$j RESULTS TRANPOSE
}

for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)

display_matrix(\$inverse, "<b>Inverse</b>");
}

//WORKS ONLY FOR UPTO 3x3
function solve_matrix(\$m)
{
\$_size = count(\$m);
switch (\$_size)
{
case 1:
return \$m[0][0];
case 2:
return ((\$m[0][0] * \$m[1][1]) - (\$m[0][1] * \$m[1][0]));
case 3:
return (\$m[0][0]*(\$m[1][1]*\$m[2][2]-\$m[1][2]*\$m[2][1])) - (\$m[0][1]*(\$m[1][0]*\$m[2][2]-\$m[2][0]*\$m[1][2])) + (\$m[0][2]*(\$m[1][0]*\$m[2][1]-\$m[2][0]*\$m[1][1]));

//CAN BE INCREASED TO ANY NUMBER
//YOU CAN MAKE A BETTER FUNCTION WITH FOR LOOP WHICH COULD GO TO ANY LIMIT
//USE THE ABOVE COFACTOR LOGIC FOR THAT
default:
return 0;
}
}

//DISPLAY A MATRIX IN A TABLE
function display_matrix(\$m, \$name)
{
foreach(\$m as \$row)
{
echo "<tr>";
foreach (\$row as \$value)
{
echo "<td>".\$value."</td>";
}
echo "</tr>";
}
echo "</table>
<br>";
}
?>[/PHP]

See how's this... ;)

Regards,
Harpreet
Mar 28 '08 #5
dear Harpreet..
tenkiu so much for giving me that..:)
but i cant run it when i combine ur program with mine...
emm...can u help me futhermore??
this is the link for invertible matrix eqn...http://en.wikipedia.org/wiki/Invertible_matrix

and below is part of my program that needs me to inverse it.. really hope that u can help me..=)
Expand|Select|Wrap|Line Numbers
1. //B'-MATRIX
2. print ("B'-matrix<br>");
3. for(\$i=1;\$i<=\$totalbus;\$i++)
4.   {
5.    if((\$buscode[\$i]==0)||(\$buscode[\$i]==2))
6.    {
7.     for(\$j=1;\$j<=\$totalbus;\$j++)
8.     {
9.      if((\$buscode[\$j]==0)||(\$buscode[\$j]==2))
10.      {
11.       \$B1matrix[\$busnum[\$i][\$j]]=\$matrix_imgpart[\$i][\$j];
12.       print(\$B1matrix[\$busnum[\$i][\$j]]."                     ");
13.      }
14.     }
15.    }
16.    print("<br>");
17.   }
18. print("<br>");
19.
20. //B''-MATRIX
21. print ("B''-matrix");
22. for(\$i=1;\$i<=\$totalbus;\$i++)
23.   {
24.    if(\$buscode[\$i]==2)
25.    {
26.     for(\$j=1;\$j<=\$totalbus;\$j++)
27.     {
28.      if(\$buscode[\$j]==2)
29.      {
30.       \$B2matrix[\$busnum[\$i][\$j]]=\$matrix_imgpart[\$i][\$j];
31.       print(\$B2matrix[\$busnum[\$i][\$j]]."                         ");
32.      }
33.     }
34.    }
35.    print("<br>");
36.   }
37.
Mar 29 '08 #6
hsriat
1,654 Expert 1GB
[PHP]function invert_matrix(\$matrix)
{
\$size = count(\$matrix);

if (\$size>3)
return false;

if (\$size!=count(\$matrix[0]))
return false;

\$I_matrix_I = solve_matrix(\$matrix);
if (\$I_matrix_I==0)
return false;

\$inverse = array();
for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)
{
\$cofactor = array();
for (\$k=0; \$k<\$size; \$k++)
{
if (\$i!=\$k)
{
\$temp = array();
for (\$l=0; \$l<\$size; \$l++)
{
if (\$j!=\$l)
array_push(\$temp, \$matrix[\$k][\$l]);
}
array_push(\$cofactor, \$temp);
}
}
}

for (\$i=0; \$i<\$size; \$i++)
for (\$j=0; \$j<\$size; \$j++)

return \$inverse;
}
function solve_matrix(\$m)
{
\$_size = count(\$m);
switch (\$_size)
{
case 1:
return \$m[0][0];
case 2:
return ((\$m[0][0] * \$m[1][1]) - (\$m[0][1] * \$m[1][0]));
case 3:
return (\$m[0][0]*(\$m[1][1]*\$m[2][2]-\$m[1][2]*\$m[2][1])) - (\$m[0][1]*(\$m[1][0]*\$m[2][2]-\$m[2][0]*\$m[1][2])) + (\$m[0][2]*(\$m[1][0]*\$m[2][1]-\$m[2][0]*\$m[1][1]));
default:
return 0;
}
}[/PHP]
Use invert_matrix(\$matrix) to invert any matrix upto size 3

Regards,
Harpreet
Mar 29 '08 #7