How to Multiply matrix?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Everah wrote:Start with one dimension (say 2X2 matrix multiplication) then expand into multiple dimensions with greater/more complex matrix types.
How's that a one dimension matrix?
A matrix doesn't have a dimension as far as I know. It has a size/order, but not dimenstion - and its order is represented by 2 numbers, not one.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sorry, I couldn't think of a better word. What I meant was a single type. Start with something simple, then expand the complexity from there. Dimension was probably not the best word to use.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I suggest to start even smaller... Check if the multiplication between the 2 matrices is actually defined :P
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

since a matrix is like a two-dimensional array:

Code: Select all

[1  2  3]
[4  5  6]
[7  8  9]
equals:

Code: Select all

$mtrx=array();
$mtrx[0]=array(1,2,3);
$mtrx[1]=array(4,5,6);
$mtrx[2]=array(7,8,9);
and multiplying matrices means summing the products of M. A rows with M. B columns, if you could find a good way to transpose matrix B, then it would be as simple as a few loops to find the product. maybe something like:

Code: Select all

$ma=...;#Matrix A as formatted above in arrays
$mb=...;#Matrix B
$mR=array();#Result Matrix
$dimA=array(sizeof($ma)-1,sizeof($ma[0])-1);#Produces Dimensions of $ma assuming it has at least 1 row & col
$dimB=array(sizeof($mb)-1,sizeof($mb[0])-1);#same deal for b
$mbT=array();#mb transposed

#note: matrix vals can be accessed via $mtrx[row][col] and dimensions: $dim[0] for rows and [1] for cols

#begin the transposition of b:
#for each column in b
for($i=$dimB[1];$i>0;$i--){
  #and for each row in b
  for($j=0;$j<$dimB[0];$j++){
    $mbT[$i][$j]=$mb[$j][$i];
    }
  }

#mb is now transposed into mbT
#theoretically, now, adding products of each row's columns should yield a result
*Heaving Breaths* Wow! that hurt my head. I have to go now, so I'll let someone do the part about actually doing the multiplying. Have fun with it.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

You know what... matrices multiplication is very useful in Comp. Sci. and has many uses, so I might post here in few days (if I won't forget by then) a class that does that.

Cheers :D
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Just to give you some heads up... I've just finished to code the class which will be used to represent a matrix.
I think that now I'll be working on a class which takes a matrix and returns its transposed.

Cheers :D
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Ok! I'm back, and I have the code to multiply matrices:

Code: Select all

#note: this starts where my last post left off
for($i=0;$i<sizeof($ma);$i++){#for each row in a
  for($j=0;$i<sizeof($mbT);$j++){#and for each column (not really, but effectively) in b
    $val=0;#initiate the value for the result matrix
    for($c=0;$c<sizeof($mbT[$j]);$c++){#for each row in that column (once again, not really, but effectively)
      $val += $ma[$j][$c] * $mbT[$j][$c];#multiply a's val by b's val and add to $val
    }
    $mR[$i][$j]=$val;#set the value in the result
  }
}
I believe that will work, but i don't have enough time to test it right now.
Oren, let me know when you are done w/ the class, it will be useful to have.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, I'm going to finish it myself, but any comments will be great.
Also note that I decided to represent a matrix as an object. I did this so I can save some data about the matrix. The matrix itself is an array though. My matrix object contains the size of the matrix (as long as the matrix is valid), its size, whether the matrix is valid or not, and if not - the error. It also has few getters and setters and other methods.

Here is an example of an object representing the matrix (1):

Code: Select all

object(matrix)#3 (5) {
  ["matrix:protected"]=>
  array(1) {
    [0]=>
    array(1) {
      [0]=>
      int(1)
    }
  }
  ["rows:protected"]=>
  int(1)
  ["cols:protected"]=>
  int(1)
  ["is_valid:protected"]=>
  bool(true)
  ["error:protected"]=>
  string(0) ""
}
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Just some more heads up... I finished the code almost 24 hours ago, but I believe I won't be posting it before next weekend when I'll have more time to review it and test it a little bit.
Any other ideas/features that you had in mind? I guess by the time I post it, it'll support multiplication of more than just 2 matrices, matrix multiplication by scalar, matrices addition and maybe more (the inverse of a matrix perhaps?).

P.S If someone is willing to write test cases for my classes I will be more than happy :D
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Maybe implementing some kind of static method to return an identity matrix.
Some functions for reduced row echelon forms, determinants, etc would be useful
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

As for the identity matrix, I don't see why we need it. Do you have any good reason?
As for the other 2, I think it's too much work not worthing it. I already thought about determinants, but as I said, it's too much work and it might have a very bad time complexity (just a thought, I didn't check or even think about it - just a feeling).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Ok, I know it's been a while and I'm very sorry, but I just don't have the time. I finished the code few days after I posted this:
You know what... matrices multiplication is very useful in Comp. Sci. and has many uses, so I might post here in few days (if I won't forget by then) a class that does that.

Cheers :D
But I just can't find the time to review the code and make the final touches. If someone is actually waiting for this, just let me know and I'll send you the code. If not, then just wait until I find the time and then I'll post it on the Code Snippets forum :wink:
mahalakshmi agalya
Forum Newbie
Posts: 2
Joined: Wed Apr 25, 2018 12:55 pm

Re: How to Multiply matrix?

Post by mahalakshmi agalya »

index.PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="GET" action="mat.php">
MATRIX MULTIPLICATION </br>
Enter rows & columns of M1:
<input type="text" name="m1" />
<input type="text" name="n1"/></br>
Enter rows & columns of M2:
<input type="text" name="m2" />
<input type="text" name="n2"/></br>
<br /><br />
<input type="submit" name="sub" value="get elements" />
</form>
</body>
</html>

mat.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
$m1=$_GET['m1'];
$n1=$_GET['n1'];
$m2=$_GET['m2'];
$n2=$_GET['n2'];
echo "Enter elements of matrix1:"."</br>";
for($i=1;$i<=$m1;$i++)
{
for($j=1;$j<=$n1;$j++)
{
?>
<form method="get" action="print.php">
<input type="text" name="text1<?php echo $i.$j; ?>" />
<?php
}
echo "</br>";
}
echo "Enter elements of matrix2:"."</br>";
for($i=1;$i<=$m2;$i++)
{
for($j=1;$j<=$n2;$j++)
{
?>
<form method="get" action="print.php">
<input type="text" name="text2<?php echo $i.$j; ?>" />
<?php
}
echo "</br>";
}
?>
<input type="hidden" name="m1" value="<?php echo $m1; ?>" />
<input type="hidden" name="n1" value="<?php echo $n1; ?>" />
<input type="hidden" name="m2" value="<?php echo $m2; ?>" />
<input type="hidden" name="n2" value="<?php echo $n2; ?>" />
<input type="submit" value="click" />
</form>
</body>
</html>

print.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
$m1=$_GET['m1'];
$n1=$_GET['n1'];
$m2=$_GET['m2'];
$n2=$_GET['n2'];
if($n2==$n1)
{
for($i=1;$i<=$m1;$i++)
{
for($j=1;$j<=$n1;$j++)
{
$a[$i][$j]=$_GET['text1'.$i.$j];
}
}
for($i=1;$i<=$m2;$i++)
{
for($j=1;$j<=$n2;$j++)
{
$b[$i][$j]=$_GET['text2'.$i.$j];
}
}
for($i=1;$i<=$m1;$i++)
{
for($j=1;$j<=$n2;$j++)
{
$c[$i][$j]=0;
for($k=1;$k<=$n1;$k++)
$c[$i][$j]=$c[$i][$j]+ $a[$i][$k]*$b[$k][$j];
}
}
for($i=1;$i<=$m1;$i++)
{
for($j=1;$j<=$n2;$j++)
{
echo $c[$i][$j]. " "." "." ";
}
echo "</br>";
}
}
else
{
echo "multiplication not possible";
}
?>
<body>
</body>
</html>

OUTPUT:

[img]C:\Users\HP\Pictures\Saved%20Pictures\Capture2.png[/img]
[img]C:\Users\HP\Pictures\Saved%20Pictures\Capture3.png[/img]
[img]C:\Users\HP\Pictures\Saved%20Pictures\Capture4.png[/img]
mahalakshmi agalya
Forum Newbie
Posts: 2
Joined: Wed Apr 25, 2018 12:55 pm

Re: How to Multiply matrix?

Post by mahalakshmi agalya »

output

MATRIX MULTIPLICATION
Enter rows & columns of M1: 3 3
Enter rows & columns of M2: 3 3
Get Elements

Enter elements of matrix1:
1 2 3
4 5 6
7 8 9

Enter elements of matrix2:
1 0 0
0 1 0
0 0 1

result:
1 2 3
4 5 6
7 8 9
Post Reply