JAMA
[ class tree: JAMA ] [ index: JAMA ] [ all elements ]

Source for file CholeskyDecomposition.php

Documentation is available at CholeskyDecomposition.php

  1. <?php
  2. /**
  3.  *    @package JAMA
  4.  *
  5.  *     Cholesky decomposition class
  6.  *
  7.  *     For a symmetric, positive definite matrix A, the Cholesky decomposition
  8.  *     is an lower triangular matrix L so that A = L*L'.
  9.  *
  10.  *     If the matrix is not symmetric or positive definite, the constructor
  11.  *     returns a partial decomposition and sets an internal flag that may
  12.  *     be queried by the isSPD() method.
  13.  *
  14.  *    @author Paul Meagher
  15.  *    @author Michael Bommarito
  16.  *    @version 1.2
  17.  */
  18.  
  19.     /**
  20.      *    Decomposition storage
  21.      *    @var array 
  22.      *    @access private
  23.      */
  24.     private $L array();
  25.  
  26.     /**
  27.      *    Matrix row and column dimension
  28.      *    @var int 
  29.      *    @access private
  30.      */
  31.     private $m;
  32.  
  33.     /**
  34.      *    Symmetric positive definite flag
  35.      *    @var boolean 
  36.      *    @access private
  37.      */
  38.     private $isspd true;
  39.  
  40.  
  41.     /**
  42.      *    CholeskyDecomposition
  43.      *
  44.      *    Class constructor - decomposes symmetric positive definite matrix
  45.      *    @param mixed Matrix square symmetric positive definite matrix
  46.      */
  47.     public function __construct($A null{
  48.         if ($A instanceof Matrix{
  49.             $this->$A->getArray();
  50.             $this->$A->getRowDimension();
  51.  
  52.             for($i 0$i $this->m++$i{
  53.                 for($j $i$j $this->m++$j{
  54.                     for($sum $this->L[$i][$j]$k $i 1$k >= 0--$k{
  55.                         $sum -= $this->L[$i][$k$this->L[$j][$k];
  56.                     }
  57.                     if ($i == $j{
  58.                         if ($sum >= 0{
  59.                             $this->L[$i][$isqrt($sum);
  60.                         else {
  61.                             $this->isspd false;
  62.                         }
  63.                     else {
  64.                         if ($this->L[$i][$i!= 0{
  65.                             $this->L[$j][$i$sum $this->L[$i][$i];
  66.                         }
  67.                     }
  68.                 }
  69.  
  70.                 for ($k $i+1$k $this->m++$k{
  71.                     $this->L[$i][$k0.0;
  72.                 }
  73.             }
  74.         else {
  75.             throw new Exception(JAMAError(ArgumentTypeException));
  76.         }
  77.     }    //    function __construct()
  78.  
  79.  
  80.     /**
  81.      *    Is the matrix symmetric and positive definite?
  82.      *
  83.      *    @return boolean 
  84.      */
  85.     public function isSPD({
  86.         return $this->isspd;
  87.     }    //    function isSPD()
  88.  
  89.  
  90.     /**
  91.      *    getL
  92.      *
  93.      *    Return triangular factor.
  94.      *    @return Matrix Lower triangular matrix
  95.      */
  96.     public function getL({
  97.         return new Matrix($this->L);
  98.     }    //    function getL()
  99.  
  100.  
  101.     /**
  102.      *    Solve A*X = B
  103.      *
  104.      *    @param $B Row-equal matrix
  105.      *    @return Matrix L * L' * X = B
  106.      */
  107.     public function solve($B null{
  108.         if ($B instanceof Matrix{
  109.             if ($B->getRowDimension(== $this->m{
  110.                 if ($this->isspd{
  111.                     $X  $B->getArrayCopy();
  112.                     $nx $B->getColumnDimension();
  113.  
  114.                     for ($k 0$k $this->m++$k{
  115.                         for ($i $k 1$i $this->m++$i{
  116.                             for ($j 0$j $nx++$j{
  117.                                 $X[$i][$j-= $X[$k][$j$this->L[$i][$k];
  118.                             }
  119.                         }
  120.                         for ($j 0$j $nx++$j{
  121.                             $X[$k][$j/= $this->L[$k][$k];
  122.                         }
  123.                     }
  124.  
  125.                     for ($k $this->1$k >= 0--$k{
  126.                         for ($j 0$j $nx++$j{
  127.                             $X[$k][$j/= $this->L[$k][$k];
  128.                         }
  129.                         for ($i 0$i $k++$i{
  130.                             for ($j 0$j $nx++$j{
  131.                                 $X[$i][$j-= $X[$k][$j$this->L[$k][$i];
  132.                             }
  133.                         }
  134.                     }
  135.  
  136.                     return new Matrix($X$this->m$nx);
  137.                 else {
  138.                     throw new Exception(JAMAError(MatrixSPDException));
  139.                 }
  140.             else {
  141.                 throw new Exception(JAMAError(MatrixDimensionException));
  142.             }
  143.         else {
  144.             throw new Exception(JAMAError(ArgumentTypeException));
  145.         }
  146.     }    //    function solve()
  147.  
  148. }    //    class CholeskyDecomposition

Documentation generated on Sun, 27 Feb 2011 16:29:03 -0800 by phpDocumentor 1.4.3