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

Documentation generated on Mon, 05 Jan 2009 20:36:40 +0100 by phpDocumentor 1.4.1