powerBestFitClass.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_Trend
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php';
  28. /**
  29. * PHPExcel_Power_Best_Fit
  30. *
  31. * @category PHPExcel
  32. * @package PHPExcel_Shared_Trend
  33. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  34. */
  35. class PHPExcel_Power_Best_Fit extends PHPExcel_Best_Fit
  36. {
  37. /**
  38. * Algorithm type to use for best-fit
  39. * (Name of this trend class)
  40. *
  41. * @var string
  42. **/
  43. protected $_bestFitType = 'power';
  44. /**
  45. * Return the Y-Value for a specified value of X
  46. *
  47. * @param float $xValue X-Value
  48. * @return float Y-Value
  49. **/
  50. public function getValueOfYForX($xValue) {
  51. return $this->getIntersect() * pow(($xValue - $this->_Xoffset),$this->getSlope());
  52. } // function getValueOfYForX()
  53. /**
  54. * Return the X-Value for a specified value of Y
  55. *
  56. * @param float $yValue Y-Value
  57. * @return float X-Value
  58. **/
  59. public function getValueOfXForY($yValue) {
  60. return pow((($yValue + $this->_Yoffset) / $this->getIntersect()),(1 / $this->getSlope()));
  61. } // function getValueOfXForY()
  62. /**
  63. * Return the Equation of the best-fit line
  64. *
  65. * @param int $dp Number of places of decimal precision to display
  66. * @return string
  67. **/
  68. public function getEquation($dp=0) {
  69. $slope = $this->getSlope($dp);
  70. $intersect = $this->getIntersect($dp);
  71. return 'Y = '.$intersect.' * X^'.$slope;
  72. } // function getEquation()
  73. /**
  74. * Return the Value of X where it intersects Y = 0
  75. *
  76. * @param int $dp Number of places of decimal precision to display
  77. * @return string
  78. **/
  79. public function getIntersect($dp=0) {
  80. if ($dp != 0) {
  81. return round(exp($this->_intersect),$dp);
  82. }
  83. return exp($this->_intersect);
  84. } // function getIntersect()
  85. /**
  86. * Execute the regression and calculate the goodness of fit for a set of X and Y data values
  87. *
  88. * @param float[] $yValues The set of Y-values for this regression
  89. * @param float[] $xValues The set of X-values for this regression
  90. * @param boolean $const
  91. */
  92. private function _power_regression($yValues, $xValues, $const) {
  93. foreach($xValues as &$value) {
  94. if ($value < 0.0) {
  95. $value = 0 - log(abs($value));
  96. } elseif ($value > 0.0) {
  97. $value = log($value);
  98. }
  99. }
  100. unset($value);
  101. foreach($yValues as &$value) {
  102. if ($value < 0.0) {
  103. $value = 0 - log(abs($value));
  104. } elseif ($value > 0.0) {
  105. $value = log($value);
  106. }
  107. }
  108. unset($value);
  109. $this->_leastSquareFit($yValues, $xValues, $const);
  110. } // function _power_regression()
  111. /**
  112. * Define the regression and calculate the goodness of fit for a set of X and Y data values
  113. *
  114. * @param float[] $yValues The set of Y-values for this regression
  115. * @param float[] $xValues The set of X-values for this regression
  116. * @param boolean $const
  117. */
  118. function __construct($yValues, $xValues=array(), $const=True) {
  119. if (parent::__construct($yValues, $xValues) !== False) {
  120. $this->_power_regression($yValues, $xValues, $const);
  121. }
  122. } // function __construct()
  123. } // class powerBestFit