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.   * Decomposition storage
  20.   * @var array 
  21.   * @access private
  22.   */
  23.   var $L = array();
  24.   
  25.   /**
  26.   * Matrix row and column dimension
  27.   * @var int 
  28.   * @access private
  29.   */
  30.   var $m;
  31.   
  32.   /**
  33.   * Symmetric positive definite flag
  34.   * @var boolean 
  35.   * @access private
  36.   */
  37.   var $isspd = true;
  38.   
  39.   /**
  40.   * CholeskyDecomposition
  41.   * Class constructor - decomposes symmetric positive definite matrix
  42.   * @param mixed Matrix square symmetric positive definite matrix
  43.   */
  44.   function CholeskyDecomposition( $A = null ) {
  45.     if( is_a($A, 'Matrix') ) {
  46.       $this->L = $A->getArray();
  47.       $this->m = $A->getRowDimension();
  48.       
  49.       for( $i = 0; $i < $this->m; $i++ ) {
  50.         for( $j = $i; $j < $this->m; $j++ ) {
  51.           for( $sum = $this->L[$i][$j], $k = $i - 1; $k >= 0; $k-- )
  52.             $sum -= $this->L[$i][$k] * $this->L[$j][$k];
  53.  
  54.           if( $i == $j ) {
  55.             if( $sum >= 0 ) {
  56.               $this->L[$i][$i] = sqrt( $sum );
  57.             } else {
  58.               $this->isspd = false;
  59.             }
  60.           } else {
  61.             if( $this->L[$i][$i] != 0 )
  62.               $this->L[$j][$i] = $sum / $this->L[$i][$i];
  63.           }
  64.         }
  65.       
  66.         for ($k = $i+1; $k < $this->m; $k++)
  67.           $this->L[$i][$k] = 0.0;
  68.       }
  69.     } else {
  70.     }
  71.   }
  72.   
  73.   /** 
  74.   * Is the matrix symmetric and positive definite?
  75.   * @return boolean 
  76.   */
  77.   function isSPD () {
  78.     return $this->isspd;
  79.   }
  80.   
  81.   /** 
  82.   * getL
  83.   * Return triangular factor.
  84.   * @return Matrix Lower triangular matrix
  85.   */
  86.   function getL () {
  87.     return new Matrix($this->L);
  88.   }
  89.   
  90.   /** 
  91.   * Solve A*X = B
  92.   * @param $B Row-equal matrix
  93.   * @return Matrix L * L' * X = B
  94.   */
  95.   function solve ( $B = null ) {
  96.     if( is_a($B, 'Matrix') ) {
  97.       if ($B->getRowDimension() == $this->m) {
  98.         if ($this->isspd) {
  99.           $X  = $B->getArrayCopy();
  100.           $nx = $B->getColumnDimension();
  101.           
  102.           for ($k = 0; $k < $this->m; $k++) {
  103.             for ($i = $k + 1; $i < $this->m; $i++)
  104.               for ($j = 0; $j < $nx; $j++)
  105.                 $X[$i][$j] -= $X[$k][$j] * $this->L[$i][$k];
  106.             
  107.             for ($j = 0; $j < $nx; $j++)
  108.               $X[$k][$j] /= $this->L[$k][$k];
  109.           }
  110.           
  111.           for ($k = $this->m - 1; $k >= 0; $k--) {
  112.             for ($j = 0; $j < $nx; $j++)
  113.               $X[$k][$j] /= $this->L[$k][$k];
  114.             
  115.             for ($i = 0; $i < $k; $i++)
  116.               for ($j = 0; $j < $nx; $j++)
  117.                 $X[$i][$j] -= $X[$k][$j] * $this->L[$k][$i];
  118.           }
  119.           
  120.           return new Matrix($X, $this->m, $nx);
  121.         } else {
  122.           trigger_error(MatrixSPDException, ERROR);
  123.         }
  124.       } else {
  125.       }
  126.     } else {
  127.     }
  128.   }
  129. }

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