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

Source for file polynomialBestFitClass.php

Documentation is available at polynomialBestFitClass.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Shared_Best_Fit
  23.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/trend/bestFitClass.php';
  30. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/JAMA/Matrix.php';
  31.  
  32.  
  33. /**
  34.  * PHPExcel_Polynomial_Best_Fit
  35.  *
  36.  * @category   PHPExcel
  37.  * @package    PHPExcel_Shared_Best_Fit
  38.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  39.  */
  40. {
  41.     protected $_bestFitType        = 'polynomial';
  42.  
  43.     protected $_order            = 0;
  44.  
  45.  
  46.     public function getOrder({
  47.         return $this->_order;
  48.     }    //    function getOrder()
  49.  
  50.  
  51.     public function getValueOfYForX($xValue{
  52.         $retVal $this->getIntersect();
  53.         $slope $this->getSlope();
  54.         foreach($slope as $key => $value{
  55.             if ($value != 0.0{
  56.                 $retVal += $value pow($xValue$key 1);
  57.             }
  58.         }
  59.         return $retVal;
  60.     }    //    function getValueOfYForX()
  61.  
  62.  
  63.     public function getValueOfXForY($yValue{
  64.         return ($yValue $this->getIntersect()) $this->getSlope();
  65.     }    //    function getValueOfXForY()
  66.  
  67.  
  68.     public function getEquation($dp=0{
  69.         $slope $this->getSlope($dp);
  70.         $intersect $this->getIntersect($dp);
  71.  
  72.         $equation 'Y = '.$intersect;
  73.         foreach($slope as $key => $value{
  74.             if ($value != 0.0{
  75.                 $equation .= ' + '.$value.' * X';
  76.                 if ($key 0{
  77.                     $equation .= '^'.($key 1);
  78.                 }
  79.             }
  80.         }
  81.         return $equation;
  82.     }    //    function getEquation()
  83.  
  84.  
  85.     public function getSlope($dp=0{
  86.         if ($dp != 0{
  87.             $coefficients array();
  88.             foreach($this->_slope as $coefficient{
  89.                 $coefficients[round($coefficient,$dp);
  90.             }
  91.             return $coefficients;
  92.         }
  93.         return $this->_slope;
  94.     }    //    function getSlope()
  95.  
  96.  
  97.     public function getCoefficients($dp=0{
  98.         return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
  99.     }    //    function getCoefficients()
  100.  
  101.  
  102.     private function _polynomial_regression($order$yValues$xValues$const{
  103.         // calculate sums
  104.         $x_sum array_sum($xValues);
  105.         $y_sum array_sum($yValues);
  106.         $xx_sum $xy_sum 0;
  107.         for($i 0$i $this->_valueCount++$i{
  108.             $xy_sum += $xValues[$i$yValues[$i];
  109.             $xx_sum += $xValues[$i$xValues[$i];
  110.             $yy_sum += $yValues[$i$yValues[$i];
  111.         }
  112.         /*
  113.          *    This routine uses logic from the PHP port of polyfit version 0.1
  114.          *    written by Michael Bommarito and Paul Meagher
  115.          *
  116.          *    The function fits a polynomial function of order $order through
  117.          *    a series of x-y data points using least squares.
  118.          *
  119.          */
  120.         for ($i 0$i $this->_valueCount++$i{
  121.             for ($j 0$j <= $order++$j{
  122.                 $A[$i][$jpow($xValues[$i]$j);
  123.             }
  124.         }
  125.         for ($i=0$i $this->_valueCount++$i{
  126.             $B[$iarray($yValues[$i]);
  127.         }
  128.         $matrixA new Matrix($A);
  129.         $matrixB new Matrix($B);
  130.         $C $matrixA->solve($matrixB);
  131.  
  132.         $coefficients array();
  133.         for($i 0$i $C->m++$i{
  134.             $r $C->get($i0);
  135.             if (abs($r<= pow(10-9)) {
  136.                 $r 0;
  137.             }
  138.             $coefficients[$r;
  139.         }
  140.  
  141.         $this->_intersect = array_shift($coefficients);
  142.         $this->_slope = $coefficients;
  143.  
  144.         $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
  145.         foreach($this->_xValues as $xKey => $xValue{
  146.             $this->_yBestFitValues[$xKey$this->getValueOfYForX($xValue);
  147.         }
  148.     }    //    function _polynomial_regression()
  149.  
  150.  
  151.     function __construct($order$yValues$xValues=array()$const=True{
  152.         if (parent::__construct($yValues$xValues!== False{
  153.             if ($order $this->_valueCount{
  154.                 $this->_bestFitType .= '_'.$order;
  155.                 $this->_order = $order;
  156.                 $this->_polynomial_regression($order$yValues$xValues$const);
  157.                 if (($this->getGoodnessOfFit(0.0|| ($this->getGoodnessOfFit(1.0)) {
  158.                     $this->_error = True;
  159.                 }
  160.             else {
  161.                 $this->_error = True;
  162.             }
  163.         }
  164.     }    //    function __construct()
  165.  
  166. }    //    class polynomialBestFit

Documentation generated on Sun, 27 Feb 2011 16:33:28 -0800 by phpDocumentor 1.4.3