Author Topic: help with matrix  (Read 94 times)

super_human

  • Regular Member
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
help with matrix
« on: January 26, 2012, 09:35:11 AM »
Hey guys ,
I made a small program that does matrix calculations but seem to be getting an unusual problem:
There are two pages.
matrix.php
PHP Code: [Select]

<html>
<
title>Matrix Tool</title>
<
body>
<
strong>Matrix A</strong>
<
br />
<
form action='matrix_core.php' method="get">
A<sub>11</sub><input type="text" name="a11" />A<sub>12</sub><input type="text" name="a12" />A<sub>13</sub><input type="text" name="a13" /><br />
A<sub>21</sub><input type="text" name="a21" />A<sub>22</sub><input type="text" name="a22" />A<sub>23</sub><input type="text" name="a23" /><br />
A<sub>31</sub><input type="text" name="a31" />A<sub>32</sub><input type="text" name="a32" />A<sub>33</sub><input type="text" name="a33" /><br />
<
br />
<
strong>Matrix B</strong>
<
br />

B<sub>11</sub><input type="text" name="b11" />B<sub>12</sub><input type="text" name="b12" />B<sub>13</sub><input type="text" name="b13" /><br />
B<sub>21</sub><input type="text" name="b21" />B<sub>22</sub><input type="text" name="b22" />B<sub>23</sub><input type="text" name="b23" /><br />
B<sub>31</sub><input type="text" name="b31" />B<sub>32</sub><input type="text" name="b32" />B<sub>33</sub><input type="text" name="b33" /><br />
<
br />
Operation:
<
select name="operation">
<
option value="AxB">AxB</option>
<
option value="Add">Add</option>
<
option value="Subtract">Subtract</option>
<
option value="Determenant Of A">Determenant Of A</option>
<
option value="Determenant Of B">Determenant Of B</option>
</
select>
<
input type="submit" value="Submit" />
</
form>
</
body>
</
html>


matrix_core.php
PHP Code: [Select]

<html>
<
title>Answer</title>
<?
php
@$a11 $_GET['a11'];
@
$a12 $_GET['a12']; 
@
$a13 $_GET['a13'];
@
$a21 $_GET['a21'];
@
$a22 $_GET['a22'];
@
$a23 $_GET['a23'];
@
$a31 $_GET['a31'];
@
$a32 $_GET['a32'];
@
$a33 $_GET['a33'];

@
$b11 $_GET['b11'];
@
$b12 $_GET['b12']; 
@
$b13 $_GET['b13'];
@
$b21 $_GET['b21'];
@
$b22 $_GET['b22'];
@
$b23 $_GET['b23'];
@
$b31 $_GET['b31'];
@
$b32 $_GET['b32'];
@
$b33 $_GET['b33'];

@
$operation $_GET['operation'];

//Determenant
@$delta1 $a11*(($a22*$a33)-($a32*$a23));
@
$delta2 $a12*(($a21*$a33)-($a31*$a23));
@
$delta3 $a13*(($a21*$a32)-($a31*$a22));
@
$determenant_a $delta1-$delta2+$delta3;
@
$delta4 $b11*(($b22*$b33)-($b32*$b23));
@
$delta5 $b12*(($b21*$b33)-($b31*$b23));
@
$delta6 $b13*(($b21*$b32)-($b31*$b22));
@
$determenant_b $delta4-$delta5+$delta6;
//Add 
@$a1 $a11+$b11;
@
$a2 $a12+$b12;
@
$a3 $a13+$b13;
@
$a4 $a21+$b21;
@
$a5 $a22+$b22;
@
$a6 $a23+$b23;
@
$a7 $a31+$b31;
@
$a8 $a32+$b32;
@
$a9 $a33+$b33;
//Subtract
@$a10 $a11-$b11;
@
$a11 $a12-$b12;
@
$a12 $a13-$b13;
@
$a13 $a21-$b21;
@
$a14 $a22-$b22;
@
$a15 $a23-$b23;
@
$a16 $a31-$b31;
@
$a17 $a32-$b32;
@
$a18 $a33-$b33;
//A*B
@$row11 = ($a11*$b11)+($a12*$b21)+($a13*$b31);
@
$row12 = ($a11*$b12)+($a12*$b22)+($a13*$b32);
@
$row13 = ($a11*$b13)+($a12*$b23)+($a13*$b33);

@
$row21 = ($a21*$b11)+($a22*$b21)+($a23*$b31);
@
$row22 = ($a21*$b12)+($a22*$b21)+($a23*$b31);
@
$row23 = ($a21*$b13)+($a22*$b23)+($a23*$b33);

@
$row31 = ($a31*$b11)+($a32*$b21)+($a33*$b31);
@
$row32 = ($a31*$b12)+($a32*$b22)+($a33*$b32);
@
$row33 = ($a31*$b13)+($a32*$b23)+($a33*$b33);

if (
$operation == 'Determenant Of A'){
	
echo 
"The Determenent of <strong>A</strong> Matrix is  ";
	
echo 
$determenant_a.'.';
}
elseif (
$operation == 'Determenant Of B'){
	
echo 
"The Determenent of <strong>B</strong> Matrix is  ";
	
echo 
$determenant_b.'.';
	
}

elseif (
$operation == 'Add'){
	
echo 
'The answer is: ';
	
echo 
'<br />';
	
echo 
$a1.'
	
'
.$a2.'   '.$a3;
	
echo 
'<br />';
	
echo 
$a4.'   '.$a5.'   '.$a6;
	
echo 
'<br />';
	
echo 
$a7.'   '.$a8.'   '.$a9;
}
elseif (
$operation == 'Subtract'){
	
echo 
'The answer is: ';
	
echo 
'<br />';
	
echo 
$a10.'
	
'
.$a11.'   '.$a12;
	
echo 
'<br />';
	
echo 
$a13.'   '.$a14.'   '.$a15;
	
echo 
'<br />';
	
echo 
$a16.'   '.$a17.'   '.$a18;
}
elseif (
$operation == 'AxB'){
	
echo 
'The answer is: ';
	
echo 
'<br />';
	
echo 
$row11.'   '.$row12.'   '.$row13;
	
echo 
"<br />";
	
echo 
$row21.'   '.$row22.'   '.$row23;
	
echo 
"<br />";
	
echo 
$row31.'   '.$row32.'   '.$row33;
}




?>
</html>



Its all working well ,except for the multiplication part(A*B).
The first 3 numbers are coming as "0 0 0"

Consider an example
Matrix A
3 1 2
4 3 1
2 5 4

Matrix B
3 1 2
4 3 1
2 5 4

The answer is :
17 16 15
26 18 15
34 37 25

But what is displayed when i run the above code is:
0 0 0
26 18 15
34 37 25

I don't know what i am doing wrong. :(
Pls Help
« Last Edit: January 26, 2012, 09:57:26 AM by super_human »

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 782
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: help with matrix
« Reply #1 on: January 26, 2012, 10:41:51 AM »
There is logical mistake. Why do you calculate everything each time the form is submitted, if you only need to calculate selected operation? Just do each calculation within the corresponding if block, like this:
PHP Code: [Select]
if ($operation == 'Determenant Of A'){
  
// calculate determenant of A here
  // display results here
}
elseif (
$operation == 'Determenant Of B'){
  
// calculate determenant of B here
  // display results here
}
elseif (
$operation == 'Add'){
  
// calculate sum of 2 matrix here
  // display results here
}
// etc.


In your current code, when you calculate Substruction, you mess all your temporary variables $a11 etc. and then using these modified values when calculating A * B

Also, why don't you use multi-dimensional arrays? That would simplify output, i.e.:
PHP Code: [Select]
for($i=1;$i<=3;$i++){
  echo 
$a[1][$i];
}
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

super_human

  • Regular Member
  • **
  • Posts: 47
  • Karma: +0/-0
    • View Profile
Re: help with matrix
« Reply #2 on: January 26, 2012, 02:43:44 PM »
i messed up the variables, gave them same names. When i changed the subtraction variables ,it worked perfectly. :)

Also put the calculations in their respective if blocks.
Thanks for your help :)