473,385 Members | 1,655 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,385 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 7093
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.