PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/trend/trendClass.php

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