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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.0, 2009-08-10
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../../');
  35. }
  36.  
  37. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/trend/bestFitClass.php';
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/JAMA/Matrix.php';
  39.  
  40.  
  41. /**
  42.  * PHPExcel_Polynomial_Best_Fit
  43.  *
  44.  * @category   PHPExcel
  45.  * @package    PHPExcel_Shared_Best_Fit
  46.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  47.  */
  48. {
  49.     protected $_bestFitType        = 'polynomial';
  50.  
  51.     protected $_order            = 0;
  52.  
  53.  
  54.     public function getOrder({
  55.         return $this->_order;
  56.     }    //    function getOrder()
  57.  
  58.  
  59.     public function getValueOfYForX($xValue{
  60.         $retVal $this->getIntersect();
  61.         $slope $this->getSlope();
  62.         foreach($slope as $key => $value{
  63.             if ($value != 0.0{
  64.                 $retVal += $value pow($xValue$key 1);
  65.             }
  66.         }
  67.         return $retVal;
  68.     }    //    function getValueOfYForX()
  69.  
  70.  
  71.     public function getValueOfXForY($yValue{
  72.         return ($yValue $this->getIntersect()) $this->getSlope();
  73.     }    //    function getValueOfXForY()
  74.  
  75.  
  76.     public function getEquation($dp=0{
  77.         $slope $this->getSlope($dp);
  78.         $intersect $this->getIntersect($dp);
  79.  
  80.         $equation 'Y = '.$intersect;
  81.         foreach($slope as $key => $value{
  82.             if ($value != 0.0{
  83.                 $equation .= ' + '.$value.' * X';
  84.                 if ($key 0{
  85.                     $equation .= '^'.($key 1);
  86.                 }
  87.             }
  88.         }
  89.         return $equation;
  90.     }    //    function getEquation()
  91.  
  92.  
  93.     public function getSlope($dp=0{
  94.         if ($dp != 0{
  95.             $coefficients array();
  96.             foreach($this->_slope as $coefficient{
  97.                 $coefficients[round($coefficient,$dp);
  98.             }
  99.             return $coefficients;
  100.         }
  101.         return $this->_slope;
  102.     }    //    function getSlope()
  103.  
  104.  
  105.     public function getCoefficients($dp=0{
  106.         return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
  107.     }    //    function getCoefficients()
  108.  
  109.  
  110.     private function _polynomial_regression($order$yValues$xValues$const{
  111.         // calculate sums
  112.         $x_sum array_sum($xValues);
  113.         $y_sum array_sum($yValues);
  114.         $xx_sum $xy_sum 0;
  115.         for($i 0$i $this->_valueCount++$i{
  116.             $xy_sum += $xValues[$i$yValues[$i];
  117.             $xx_sum += $xValues[$i$xValues[$i];
  118.             $yy_sum += $yValues[$i$yValues[$i];
  119.         }
  120.         /*
  121.          *    This routine uses logic from the PHP port of polyfit version 0.1
  122.          *    written by Michael Bommarito and Paul Meagher
  123.          *
  124.          *    The function fits a polynomial function of order $order through
  125.          *    a series of x-y data points using least squares.
  126.          *
  127.          */
  128.         for ($i 0$i $this->_valueCount++$i{
  129.             for ($j 0$j <= $order++$j{
  130.                 $A[$i][$jpow($xValues[$i]$j);
  131.             }
  132.         }
  133.         for ($i=0$i $this->_valueCount++$i{
  134.             $B[$iarray($yValues[$i]);
  135.         }
  136.         $matrixA new Matrix($A);
  137.         $matrixB new Matrix($B);
  138.         $C $matrixA->solve($matrixB);
  139.  
  140.         $coefficients array();
  141.         for($i 0$i $C->m++$i{
  142.             $r $C->get($i0);
  143.             if (abs($r<= pow(10-9)) {
  144.                 $r 0;
  145.             }
  146.             $coefficients[$r;
  147.         }
  148.  
  149.         $this->_intersect = array_shift($coefficients);
  150.         $this->_slope = $coefficients;
  151.  
  152.         $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
  153.         foreach($this->_xValues as $xKey => $xValue{
  154.             $this->_yBestFitValues[$xKey$this->getValueOfYForX($xValue);
  155.         }
  156.     }    //    function _polynomial_regression()
  157.  
  158.  
  159.     function __construct($order$yValues$xValues=array()$const=True{
  160.         if (parent::__construct($yValues$xValues!== False{
  161.             if ($order $this->_valueCount{
  162.                 $this->_bestFitType .= '_'.$order;
  163.                 $this->_order = $order;
  164.                 $this->_polynomial_regression($order$yValues$xValues$const);
  165.                 if (($this->getGoodnessOfFit(0.0|| ($this->getGoodnessOfFit(1.0)) {
  166.                     $this->_error = True;
  167.                 }
  168.             else {
  169.                 $this->_error = True;
  170.             }
  171.         }
  172.     }    //    function __construct()
  173.  
  174. }    //    class polynomialBestFit

Documentation generated on Mon, 10 Aug 2009 08:07:23 +0200 by phpDocumentor 1.4.1