0
How can I code a php file to multiply matrices?
I've been able to manage upto 2*2 and 2*3 matrices however things got difficult at 3*3
2 Answers
+ 2
From your tags, it looks like you're doing this in PHP.
There are libraries if you want to do this more efficiently and with less of your own custom code. I didn't test this but I came across: https://github.com/MarkBaker/PHPMatrix
If you want to implement from scratch even though libraries already do this, teach yourself how to do matrix multiplication on arbitrary dimensions first. There is nothing fundamentally special about multiplying 2 by 2 or 2 by 3 matrices.
There are some simpler operations to learn that will help you understand matrix multiplication. These include:
1. How do you calculate dot product between any 2 vectors of equal length?
2. What 2 dimensions of the multiplied matrices need to be the same size for matrix multiplication to be possible?
3. How do you calculate the dimensions of the output matrix from the 2 matrices being multiplied together?
After getting that groundwork complete, go through examples here to do the multiplication on paper:
https://www.mathsisfun.com/algebra/matrix-multiplying.html
Get confident that you know the steps to multiply matrices because you can't teach a computer how to do it until you already know.
Programming it in PHP will involve the same simpler operations. I might actually start implementing a dot product function between arrays of equal length. An array in PHP is analogous to a vector in math terminology. A matrix would be an array of array in PHP. I would then implement a function taking the 2 matrices as parameters, validate dimensions to make sure multiplication is possible, and then see if I could reuse the dot function to find the resulting matrix.
0
Thanks. Pretty detailed Josh Greig