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