ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Trend.php
Go to the documentation of this file.
1<?php
2
4
5class Trend
6{
7 const TREND_LINEAR = 'Linear';
8 const TREND_LOGARITHMIC = 'Logarithmic';
9 const TREND_EXPONENTIAL = 'Exponential';
10 const TREND_POWER = 'Power';
11 const TREND_POLYNOMIAL_2 = 'Polynomial_2';
12 const TREND_POLYNOMIAL_3 = 'Polynomial_3';
13 const TREND_POLYNOMIAL_4 = 'Polynomial_4';
14 const TREND_POLYNOMIAL_5 = 'Polynomial_5';
15 const TREND_POLYNOMIAL_6 = 'Polynomial_6';
16 const TREND_BEST_FIT = 'Bestfit';
17 const TREND_BEST_FIT_NO_POLY = 'Bestfit_no_Polynomials';
18
24 private static $trendTypes = [
29 ];
30
36 private static $trendTypePolynomialOrders = [
42 ];
43
49 private static $trendCache = [];
50
51 public static function calculate($trendType = self::TREND_BEST_FIT, $yValues = [], $xValues = [], $const = true)
52 {
53 // Calculate number of points in each dataset
54 $nY = count($yValues);
55 $nX = count($xValues);
56
57 // Define X Values if necessary
58 if ($nX === 0) {
59 $xValues = range(1, $nY);
60 } elseif ($nY !== $nX) {
61 // Ensure both arrays of points are the same size
62 trigger_error('Trend(): Number of elements in coordinate arrays do not match.', E_USER_ERROR);
63 }
64
65 $key = md5($trendType . $const . serialize($yValues) . serialize($xValues));
66 // Determine which Trend method has been requested
67 switch ($trendType) {
68 // Instantiate and return the class for the requested Trend method
73 if (!isset(self::$trendCache[$key])) {
74 $className = '\PhpOffice\PhpSpreadsheet\Shared\Trend\\' . $trendType . 'BestFit';
75 self::$trendCache[$key] = new $className($yValues, $xValues, $const);
76 }
77
78 return self::$trendCache[$key];
84 if (!isset(self::$trendCache[$key])) {
85 $order = substr($trendType, -1);
86 self::$trendCache[$key] = new PolynomialBestFit($order, $yValues, $xValues);
87 }
88
89 return self::$trendCache[$key];
92 // If the request is to determine the best fit regression, then we test each Trend line in turn
93 // Start by generating an instance of each available Trend method
94 $bestFit = [];
95 $bestFitValue = [];
96 foreach (self::$trendTypes as $trendMethod) {
97 $className = '\PhpOffice\PhpSpreadsheet\Shared\Trend\\' . $trendType . 'BestFit';
98 $bestFit[$trendMethod] = new $className($yValues, $xValues, $const);
99 $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
100 }
101 if ($trendType != self::TREND_BEST_FIT_NO_POLY) {
102 foreach (self::$trendTypePolynomialOrders as $trendMethod) {
103 $order = substr($trendMethod, -1);
104 $bestFit[$trendMethod] = new PolynomialBestFit($order, $yValues, $xValues);
105 if ($bestFit[$trendMethod]->getError()) {
106 unset($bestFit[$trendMethod]);
107 } else {
108 $bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit();
109 }
110 }
111 }
112 // Determine which of our Trend lines is the best fit, and then we return the instance of that Trend class
113 arsort($bestFitValue);
114 $bestFitType = key($bestFitValue);
115
116 return $bestFit[$bestFitType];
117 default:
118 return false;
119 }
120 }
121}
An exception for terminatinating execution or to throw for unit testing.
static calculate($trendType=self::TREND_BEST_FIT, $yValues=[], $xValues=[], $const=true)
Definition: Trend.php:51
getError()
returns error @access public
$key
Definition: croninfo.php:18