ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
trendClass.php
Go to the documentation of this file.
1 <?php
2 
3 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/linearBestFitClass.php';
4 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/logarithmicBestFitClass.php';
5 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/exponentialBestFitClass.php';
6 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/powerBestFitClass.php';
7 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/polynomialBestFitClass.php';
8 
9 
11 {
12  const TREND_LINEAR = 'Linear';
13  const TREND_LOGARITHMIC = 'Logarithmic';
14  const TREND_EXPONENTIAL = 'Exponential';
15  const TREND_POWER = 'Power';
16  const TREND_POLYNOMIAL_2 = 'Polynomial_2';
17  const TREND_POLYNOMIAL_3 = 'Polynomial_3';
18  const TREND_POLYNOMIAL_4 = 'Polynomial_4';
19  const TREND_POLYNOMIAL_5 = 'Polynomial_5';
20  const TREND_POLYNOMIAL_6 = 'Polynomial_6';
21  const TREND_BEST_FIT = 'Bestfit';
22  const TREND_BEST_FIT_NO_POLY = 'Bestfit_no_Polynomials';
23 
24  private static $_trendTypes = array( self::TREND_LINEAR,
25  self::TREND_LOGARITHMIC,
26  self::TREND_EXPONENTIAL,
27  self::TREND_POWER
28  );
29  private static $_trendTypePolyOrders = array( self::TREND_POLYNOMIAL_2,
30  self::TREND_POLYNOMIAL_3,
31  self::TREND_POLYNOMIAL_4,
32  self::TREND_POLYNOMIAL_5,
33  self::TREND_POLYNOMIAL_6
34  );
35 
36  private static $_trendCache = array();
37 
38 
39  public static function calculate($trendType=self::TREND_BEST_FIT, $yValues, $xValues=array(), $const=True) {
40  // Calculate number of points in each dataset
41  $nY = count($yValues);
42  $nX = count($xValues);
43 
44  // Define X Values if necessary
45  if ($nX == 0) {
46  $xValues = range(1,$nY);
47  $nX = $nY;
48  } elseif ($nY != $nX) {
49  // Ensure both arrays of points are the same size
50  trigger_error("trend(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
51  }
52 
53  $key = md5($trendType.$const.serialize($yValues).serialize($xValues));
54  // Determine which trend method has been requested
55  switch ($trendType) {
56  // Instantiate and return the class for the requested trend method
57  case self::TREND_LINEAR :
58  case self::TREND_LOGARITHMIC :
59  case self::TREND_EXPONENTIAL :
60  case self::TREND_POWER :
61  if (!isset(self::$_trendCache[$key])) {
62  $className = 'PHPExcel_'.$trendType.'_Best_Fit';
63  self::$_trendCache[$key] = new $className($yValues,$xValues,$const);
64  }
65  return self::$_trendCache[$key];
66  break;
67  case self::TREND_POLYNOMIAL_2 :
68  case self::TREND_POLYNOMIAL_3 :
69  case self::TREND_POLYNOMIAL_4 :
70  case self::TREND_POLYNOMIAL_5 :
71  case self::TREND_POLYNOMIAL_6 :
72  if (!isset(self::$_trendCache[$key])) {
73  $order = substr($trendType,-1);
74  self::$_trendCache[$key] = new PHPExcel_Polynomial_Best_Fit($order,$yValues,$xValues,$const);
75  }
76  return self::$_trendCache[$key];
77  break;
78  case self::TREND_BEST_FIT :
79  case self::TREND_BEST_FIT_NO_POLY :
80  // If the request is to determine the best fit regression, then we test each trend line in turn
81  // Start by generating an instance of each available trend method
82  foreach(self::$_trendTypes as $trendMethod) {
83  $className = 'PHPExcel_'.$trendMethod.'BestFit';
84  $bestFit[$trendMethod] = new $className($yValues,$xValues,$const);
85  $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
86  }
87  if ($trendType != self::TREND_BEST_FIT_NO_POLY) {
88  foreach(self::$_trendTypePolyOrders as $trendMethod) {
89  $order = substr($trendMethod,-1);
90  $bestFit[$trendMethod] = new PHPExcel_Polynomial_Best_Fit($order,$yValues,$xValues,$const);
91  if ($bestFit[$trendMethod]->getError()) {
92  unset($bestFit[$trendMethod]);
93  } else {
94  $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
95  }
96  }
97  }
98  // Determine which of our trend lines is the best fit, and then we return the instance of that trend class
99  arsort($bestFitValue);
100  $bestFitType = key($bestFitValue);
101  return $bestFit[$bestFitType];
102  break;
103  default :
104  return false;
105  }
106  } // function calculate()
107 
108 } // class trendClass