Still about Matrix, since lately I handle a lot of web-based projects that involving Matrix computations, maybe someday you’ll need this.
I’m in need of this kind of library when creating web-based ANP (Analytic Network Process) problem solver where you have to power the weighted supermatrix with some high integer number to gain the limit supermatrix.
Here’s the codes inside the library, you can save it to whatever php file:
http://pastebin.com/RmUXpBiT
(I’m sorry I don’t post the full source code on this post, WordPress always messed up the code because it contains ‘<‘ and ‘>’)
How to use it?
As simple as this:
1 2 |
// the matrix to be computed have to be a square matrix and is a PHP array() $m_power=mat_pow($the_matrix, 512); // here we calculate the power of matrix $the_matrix to 512 |
Comparison to MATLAB
We’ll compare MATLAB result and our library result for the correctness and calculation speed.
The matrix will be taken from a CSV file and will be 15×15 in size. We will power this matrix to 512.
This is the MALTAB code used:
1 2 3 4 5 |
m=dlmread('the_matrix.csv') tStart=tic; m_power=m^512 tElapsed=toc(tStart); time_needed=tElapsed*1000 % time needed in miliseconds |
This is the partial screenshot of the MATLAB’s code result
And finally our PHP code:
You can see it here: http://pastebin.com/x3hFi6tm
I won’t take the result screenshot as you can try it on http://demo.ahowto.net/matrix_power.php
If you run the MATLAB code and the PHP code, you’ll see that our PHP code yield the same result as the MATLAB code but it run much faster than the MATLAB (both already timed in miliseconds)! I absolutely don’t know why powering matrix using PHP is faster than using MATLAB but I’m happy with the result. 😀
0 Comments.