473,226 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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..:(
guide me please...
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.

And show me if you tried something, I'll help you further..
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');

$adjoint = array();
$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
}
display_matrix($adjoint, "<b>Adjoint</b>");

for ($i=0; $i<$size; $i++)
for ($j=0; $j<$size; $j++)
$inverse[$i][$j] = $adjoint[$i][$j]/$I_matrix_I;

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)
{
echo $name."<br><table border=\"1px\" cellpadding=\"2px\" style=\"border-collapse:collapse;\">";
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;

$adjoint = array();
$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);
}
}
$adjoint[$j][$i] = solve_matrix($cofactor)*pow((-1),($i+$j));
}

for ($i=0; $i<$size; $i++)
for ($j=0; $j<$size; $j++)
$inverse[$i][$j] = $adjoint[$i][$j]/$I_matrix_I;

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

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
6
by: David C. Fox | last post by:
Is there a function which takes a list of tuples and returns a list of lists made up of the first element of each tuple, the second element of each tuple, etc.? In other words, the the inverse...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
0
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.