# Cholesky decomposition in PHP

Today I have released NumPHP 1.0.2. I have added the Cholesky decomposition. It factors a matrix into a lower and a upper triangular matrix like the LU Decomposition. The disadvantage is, the Cholesky decomposition works only for symmetric positive definite matrices. The advantage is, it is faster than the LU decomposition and the resulting matrices are L (lower triangular) and LT (transposed of L, upper triangular). This can be helpful to solve systems of linear equations with symmetric positive definite matrices faster.

## Cholesky Decomposition Example

Again, I have build a simple example at NumPHP-examples and here is a small excerpt.

``````use NumPHP\Core\NumArray;
use NumPHP\LinAlg\LinAlg;

\$matrixA = new NumArray(
[
[  49,  -525,     7,   -315],
[-525,  6921, -3279,   3483],
[   7, -3279,  8178,   -328],
[-315,  3483,  -328, 624556]
]
);

\$matrixL = LinAlg::cholesky(\$matrixL);
echo \$cholesky;
``````
The output contains only the matrix L, cause the user can create LT pretty easy with the function NumArray::getTranspose.
``````\$matrixR = \$matrixL->getTranspose();
``````
``````Matrix A:
NumArray([
[49, -525, 7, -315],
[-525, 6921, -3279, 3483],
[7, -3279, 8178, -328],
[-315, 3483, -328, 624556]
])

Cholesky:
NumArray([
[7, 0, 0, 0],
[-75, 36, 0, 0],
[1, -89, 16, 0],
[-45, 3, -1, 789]
])

Time for calculation: 0.0032660961151123 sec
``````

## Testing with Numpy

I did test all my functions with numpy. Here is a small script that calculates the cholesky decomposition with the same example.

``````import time
import numpy

A = numpy.array([[49., -525., 7., -315], [-525., 6921., -3279., 3483.], [7., -3279., 8178., -328.], [-315., 3483., -328., 624556.]])

time1 = time.time()
L = numpy.linalg.cholesky(A)
timeDiff = time.time()-time1

print("Cholesky")
print(L)
print("Time for calculation in sec:")
print "%.16f" % timeDiff
``````
The output of the python script is
``````Cholesky
[[   7.   -0.    0.   -0.]
[ -75.   36.   -0.    0.]
[   1.  -89.   16.   -0.]
[ -45.    3.   -1.  789.]]
Time for calculation in sec:
0.0001330375671387
``````
nice to know that it works, but NumPHP is 25 times slower than Numpy:(