# PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.

 It is currently Fri Jan 18, 2019 10:05 am

 All times are UTC - 5 hours

 Page 2 of 2 [ 30 posts ] Go to page Previous  1, 2
 Print view Previous topic | Next topic
Author Message
 Post subject: Posted: Mon May 21, 2007 12:41 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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.

Top

 Post subject: Posted: Mon May 21, 2007 1:14 pm

Joined: Tue Sep 09, 2003 6:04 pm
Posts: 14293
Location: Fremont, CA, USA
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.

Top

 Post subject: Posted: Mon May 21, 2007 2:20 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
I suggest to start even smaller... Check if the multiplication between the 2 matrices is actually defined

Top

 Post subject: Posted: Wed May 23, 2007 4:55 pm
 Forum Contributor

Joined: Sun May 20, 2007 12:13 pm
Posts: 151
since a matrix is like a two-dimensional array:
[1  2  3]
[4  5  6]
[7  8  9]

equals:
\$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:

\$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.

Top

 Post subject: Posted: Wed May 23, 2007 5:31 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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

Top

 Post subject: Posted: Thu May 24, 2007 12:34 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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

Top

 Post subject: Posted: Thu May 24, 2007 2:15 pm
 Forum Contributor

Joined: Sun May 20, 2007 12:13 pm
Posts: 151
Ok! I'm back, and I have the code to multiply matrices:
#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.

Top

 Post subject: Posted: Thu May 24, 2007 2:36 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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):

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) ""
}

Top

 Post subject: Posted: Sun May 27, 2007 11:23 am
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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

Top

 Post subject: Posted: Mon May 28, 2007 9:10 pm
 Forum Contributor

Joined: Sun May 20, 2007 12:13 pm
Posts: 151
Maybe implementing some kind of static method to return an identity matrix.
Some functions for reduced row echelon forms, determinants, etc would be useful

Top

 Post subject: Posted: Tue May 29, 2007 2:49 am
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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).

Top

 Post subject: Posted: Sun Jun 10, 2007 12:01 pm
 DevNet Resident

Joined: Fri Apr 07, 2006 5:13 am
Posts: 1640
Location: Israel
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:

Quote:
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

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

Top

 Post subject: Re: How to Multiply matrix?Posted: Wed Apr 25, 2018 1:20 pm
 Forum Newbie

Joined: Wed Apr 25, 2018 12:55 pm
Posts: 2
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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<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>

<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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?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]

Top

 Post subject: Re: How to Multiply matrix?Posted: Wed Apr 25, 2018 1:30 pm
 Forum Newbie

Joined: Wed Apr 25, 2018 12:55 pm
Posts: 2
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

Top

 Post subject: Re: How to Multiply matrix?Posted: Sun Apr 29, 2018 11:38 pm
 Forum Regular

Joined: Fri Sep 05, 2008 3:34 pm
Posts: 534
Location: Victoria, BC
https://en.wikipedia.org/wiki/Matrix_multiplication

_________________
Hardcore Games Legendary is the Only Way to Play!
Vegan Advocate 16 lbs grain to make only 1 lb meat, water for 6 months of showers and 34.1 lbs carbon dioxide
My sites are made with WordPress, which is a content management system which now accounts for more than 60% of all websites

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 2 [ 30 posts ] Go to page Previous  1, 2

 All times are UTC - 5 hours

#### Who is online

Users browsing this forum: Exabot [Bot], Majestic-12 [Bot] and 8 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Jump to:  Select a forum ------------------ General    General Discussion    Site News    PHPDN Suggestions    Tutorials Programming    PHP - Code    PHP - Security    PHP - Theory and Design    PHP - Frameworks    Javascript    Miscellaneous    Testing    Regex    Code Snippets    Coding Critique Design    HTML, CSS and other UI Design Technologies    Graphics    UI Design/Usability System    Databases    Linux    Installation and Configuration Business    The Enterprise    Job Hunt    Volunteer Work PHPDN Member Forums    PHPDN    PHPGD.com    Swift Mailer    Microsoft Open Q&A