exponentialBestFitClass.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. require_once(PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php');
  8. /**
  9. * PHPExcel_Exponential_Best_Fit
  10. *
  11. * Copyright (c) 2006 - 2015 PHPExcel
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * @category PHPExcel
  28. * @package PHPExcel_Shared_Trend
  29. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  30. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  31. * @version ##VERSION##, ##DATE##
  32. */
  33. class PHPExcel_Exponential_Best_Fit extends PHPExcel_Best_Fit
  34. {
  35. /**
  36. * Algorithm type to use for best-fit
  37. * (Name of this trend class)
  38. *
  39. * @var string
  40. **/
  41. protected $bestFitType = 'exponential';
  42. /**
  43. * Return the Y-Value for a specified value of X
  44. *
  45. * @param float $xValue X-Value
  46. * @return float Y-Value
  47. **/
  48. public function getValueOfYForX($xValue)
  49. {
  50. return $this->getIntersect() * pow($this->getSlope(), ($xValue - $this->xOffset));
  51. }
  52. /**
  53. * Return the X-Value for a specified value of Y
  54. *
  55. * @param float $yValue Y-Value
  56. * @return float X-Value
  57. **/
  58. public function getValueOfXForY($yValue)
  59. {
  60. return log(($yValue + $this->yOffset) / $this->getIntersect()) / log($this->getSlope());
  61. }
  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. {
  70. $slope = $this->getSlope($dp);
  71. $intersect = $this->getIntersect($dp);
  72. return 'Y = ' . $intersect . ' * ' . $slope . '^X';
  73. }
  74. /**
  75. * Return the Slope of the line
  76. *
  77. * @param int $dp Number of places of decimal precision to display
  78. * @return string
  79. **/
  80. public function getSlope($dp = 0)
  81. {
  82. if ($dp != 0) {
  83. return round(exp($this->_slope), $dp);
  84. }
  85. return exp($this->_slope);
  86. }
  87. /**
  88. * Return the Value of X where it intersects Y = 0
  89. *
  90. * @param int $dp Number of places of decimal precision to display
  91. * @return string
  92. **/
  93. public function getIntersect($dp = 0)
  94. {
  95. if ($dp != 0) {
  96. return round(exp($this->intersect), $dp);
  97. }
  98. return exp($this->intersect);
  99. }
  100. /**
  101. * Execute the regression and calculate the goodness of fit for a set of X and Y data values
  102. *
  103. * @param float[] $yValues The set of Y-values for this regression
  104. * @param float[] $xValues The set of X-values for this regression
  105. * @param boolean $const
  106. */
  107. private function exponentialRegression($yValues, $xValues, $const)
  108. {
  109. foreach ($yValues as &$value) {
  110. if ($value < 0.0) {
  111. $value = 0 - log(abs($value));
  112. } elseif ($value > 0.0) {
  113. $value = log($value);
  114. }
  115. }
  116. unset($value);
  117. $this->leastSquareFit($yValues, $xValues, $const);
  118. }
  119. /**
  120. * Define the regression and calculate the goodness of fit for a set of X and Y data values
  121. *
  122. * @param float[] $yValues The set of Y-values for this regression
  123. * @param float[] $xValues The set of X-values for this regression
  124. * @param boolean $const
  125. */
  126. public function __construct($yValues, $xValues = array(), $const = true)
  127. {
  128. if (parent::__construct($yValues, $xValues) !== false) {
  129. $this->exponentialRegression($yValues, $xValues, $const);
  130. }
  131. }
  132. }