Today I have released NumPHP 1.0.0-dev7. It contains the possibility to calculate the inverse of an invertible matrix in PHP. With a small example I would like to show how to use this function.
I assume that NumPHP is installed.
use NumPHP\Core\NumArray;
use NumPHP\LinAlg\LinAlg;
$matrix = new NumArray(
[
[-3, 4, 7/6],
[ 2, 0.1, 0],
[23, -5, 8]
]
);
$inverse = LinAlg::inv($matrix);
I have build a Symfony Command for this example under NumPHP-examples. The command has the following output
Matrix:
NumArray([
[-3, 4, 1.1666666666667],
[2, 0.1, 0],
[23, -5, 8]
])
Inverse:
NumArray([
[-0.0099071207430341, 0.46852425180599, 0.0014447884416925],
[0.19814241486068, 0.62951496388029, -0.028895768833849],
[0.15232198142415, -0.95356037151703, 0.10278637770898]
])
Time for calculation: 0.0073750019073486 sec
To test my solution I have created a short numpy script with the same problem
import time
import numpy
A = numpy.array([[-3., 4., 7./6.], [2., 0.1, 0.], [23., -5., 8.]])
time1 = time.time()
inv = numpy.linalg.inv(A)
timeDiff = time.time()-time1
print("Inverse")
print(inv)
print("Time for calculation in sec:")
print "%.16f" % timeDiff
with the following output
Inverse
[[-0.00990712 0.46852425 0.00144479]
[ 0.19814241 0.62951496 -0.02889577]
[ 0.15232198 -0.95356037 0.10278638]]
Time for calculation in sec:
0.0000939369201660
My inverted matrix is correct but numpy is 79 times faster:(