Source for file CholeskyDecomposition.php
Documentation is available at CholeskyDecomposition.php
* Cholesky decomposition class
* For a symmetric, positive definite matrix A, the Cholesky decomposition
* is an lower triangular matrix L so that A = L*L'.
* If the matrix is not symmetric or positive definite, the constructor
* returns a partial decomposition and sets an internal flag that may
* be queried by the isSPD() method.
* @author Michael Bommarito
* Matrix row and column dimension
* Symmetric positive definite flag
* Class constructor - decomposes symmetric positive definite matrix
* @param mixed Matrix square symmetric positive definite matrix
if( is_a($A, 'Matrix') ) {
$this->L = $A->getArray();
$this->m = $A->getRowDimension();
for( $i = 0; $i < $this->m; $i++ ) {
for( $j = $i; $j < $this->m; $j++ ) {
for( $sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; $k-- )
$sum -= $this->L[$i][$k] * $this->L[$j][$k];
$this->L[$i][$i] = sqrt( $sum );
if( $this->L[$i][$i] != 0 )
$this->L[$j][$i] = $sum / $this->L[$i][$i];
for ($k = $i+ 1; $k < $this->m; $k++ )
* Is the matrix symmetric and positive definite?
* Return triangular factor.
* @return Matrix Lower triangular matrix
* @param $B Row-equal matrix
* @return Matrix L * L' * X = B
function solve ( $B = null ) {
if( is_a($B, 'Matrix') ) {
if ($B->getRowDimension() == $this->m) {
$nx = $B->getColumnDimension();
for ($k = 0; $k < $this->m; $k++ ) {
for ($i = $k + 1; $i < $this->m; $i++ )
for ($j = 0; $j < $nx; $j++ )
$X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
for ($j = 0; $j < $nx; $j++ )
$X[$k][$j] /= $this->L[$k][$k];
for ($k = $this->m - 1; $k >= 0; $k-- ) {
for ($j = 0; $j < $nx; $j++ )
$X[$k][$j] /= $this->L[$k][$k];
for ($i = 0; $i < $k; $i++ )
for ($j = 0; $j < $nx; $j++ )
$X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
return new Matrix($X, $this->m, $nx);
|