File Source for polynomialBestFitClass.php

PHPExcel_Shared_Best_Fit

# 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. */
29. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php';
30. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/Matrix.php';
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. class PHPExcel_Polynomial_Best_Fit extends PHPExcel_Best_Fit
41. {
42. protected $_bestFitType = 'polynomial';
44. protected $_order = 0;
47. public function getOrder() {
48. return $this->_order;
49. } // function getOrder()
52. public function getValueOfYForX($xValue) {
53. $retVal = $this-> getIntersect();
54. $slope = $this-> getSlope();
55. foreach($slope as $key => $value) {
56. if ($value != 0.0) {
57. $retVal += $value * pow($xValue, $key + 1);
58. }
59. }
60. return $retVal;
61. } // function getValueOfYForX()
64. public function getValueOfXForY($yValue) {
65. return ($yValue - $this-> getIntersect()) / $this-> getSlope();
66. } // function getValueOfXForY()
69. public function getEquation($dp=0) {
70. $slope = $this-> getSlope($dp);
71. $intersect = $this-> getIntersect($dp);
73. $equation = 'Y = '.$intersect;
74. foreach($slope as $key => $value) {
75. if ($value != 0.0) {
76. $equation .= ' + '.$value.' * X';
77. if ($key > 0) {
78. $equation .= '^'.($key + 1);
79. }
80. }
81. }
82. return $equation;
83. } // function getEquation()
86. public function getSlope($dp=0) {
87. if ($dp != 0) {
88. $coefficients = array();
89. foreach($this->_slope as $coefficient) {
90. $coefficients[] = round($coefficient,$dp);
91. }
92. return $coefficients;
93. }
94. return $this->_slope;
95. } // function getSlope()
98. public function getCoefficients($dp=0) {
99. return array_merge(array($this-> getIntersect($dp)),$this-> getSlope($dp));
100. } // function getCoefficients()
103. private function _polynomial_regression($order, $yValues, $xValues, $const) {
104. // calculate sums
105. $x_sum = array_sum($xValues);
106. $y_sum = array_sum($yValues);
107. $xx_sum = $xy_sum = 0;
108. for($i = 0; $i < $this->_valueCount; ++$i) {
109. $xy_sum += $xValues[$i] * $yValues[$i];
110. $xx_sum += $xValues[$i] * $xValues[$i];
111. $yy_sum += $yValues[$i] * $yValues[$i];
112. }
113. /*
114. * This routine uses logic from the PHP port of polyfit version 0.1
115. * written by Michael Bommarito and Paul Meagher
116. *
117. * The function fits a polynomial function of order $order through
118. * a series of x-y data points using least squares.
119. *
120. */
121. for ($i = 0; $i < $this->_valueCount; ++$i) {
122. for ($j = 0; $j <= $order; ++$j) {
123. $A[$i][$j] = pow($xValues[$i], $j);
124. }
125. }
126. for ($i=0; $i < $this->_valueCount; ++$i) {
127. $B[$i] = array($yValues[$i]);
128. }
129. $matrixA = new Matrix($A);
130. $matrixB = new Matrix($B);
131. $C = $matrixA->solve($matrixB);
133. $coefficients = array();
134. for($i = 0; $i < $C->m; ++$i) {
135. $r = $C->get($i, 0);
136. if ( abs($r) <= pow(10, -9)) {
137. $r = 0;
138. }
139. $coefficients[] = $r;
140. }
142. $this->_intersect = array_shift($coefficients);
143. $this->_slope = $coefficients;
145. $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
146. foreach($this->_xValues as $xKey => $xValue) {
147. $this->_yBestFitValues[$xKey] = $this-> getValueOfYForX($xValue);
148. }
149. } // function _polynomial_regression()
152. function __construct($order, $yValues, $xValues=array(), $const=True) {
153. if (parent::__construct($yValues, $xValues) !== False) {
154. if ($order < $this->_valueCount) {
155. $this->_bestFitType .= '_'.$order;
156. $this->_order = $order;
157. $this->_polynomial_regression($order, $yValues, $xValues, $const);
158. if (($this-> getGoodnessOfFit() < 0.0) || ($this-> getGoodnessOfFit() > 1.0)) {
159. $this->_error = True;
160. }
161. } else {
162. $this->_error = True;
163. }
164. }
165. } // function __construct()
167. } // class polynomialBestFit

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