Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "ExponentialRegressionCurveCalculator.hxx"
21 : #include "macros.hxx"
22 : #include "RegressionCalculationHelper.hxx"
23 :
24 : #include <rtl/math.hxx>
25 : #include <rtl/ustrbuf.hxx>
26 :
27 : using namespace ::com::sun::star;
28 :
29 : namespace chart
30 : {
31 :
32 0 : ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() :
33 : m_fLogSlope( 0.0 ),
34 0 : m_fLogIntercept( 0.0 )
35 : {
36 0 : ::rtl::math::setNan( & m_fLogSlope );
37 0 : ::rtl::math::setNan( & m_fLogIntercept );
38 0 : }
39 :
40 0 : ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
41 0 : {}
42 :
43 : // ____ XRegressionCurveCalculator ____
44 0 : void SAL_CALL ExponentialRegressionCurveCalculator::recalculateRegression(
45 : const uno::Sequence< double >& aXValues,
46 : const uno::Sequence< double >& aYValues )
47 : throw (uno::RuntimeException, std::exception)
48 : {
49 : RegressionCalculationHelper::tDoubleVectorPair aValues(
50 : RegressionCalculationHelper::cleanup(
51 : aXValues, aYValues,
52 0 : RegressionCalculationHelper::isValidAndYPositive()));
53 :
54 0 : const size_t nMax = aValues.first.size();
55 0 : if( nMax == 0 )
56 : {
57 0 : ::rtl::math::setNan( & m_fLogSlope );
58 0 : ::rtl::math::setNan( & m_fLogIntercept );
59 0 : ::rtl::math::setNan( & m_fCorrelationCoeffitient );// actual it is coefficient of determination
60 0 : return;
61 : }
62 :
63 0 : double fAverageX = 0.0, fAverageY = 0.0;
64 0 : double fLogIntercept = mForceIntercept ? log(mInterceptValue) : 0.0;
65 0 : std::vector<double> yVector;
66 0 : yVector.resize(nMax, 0.0);
67 :
68 0 : size_t i = 0;
69 0 : for( i = 0; i < nMax; ++i )
70 : {
71 0 : double yValue = log(aValues.second[i]);
72 0 : if (mForceIntercept)
73 : {
74 0 : yValue -= fLogIntercept;
75 : }
76 : else
77 : {
78 0 : fAverageX += aValues.first[i];
79 0 : fAverageY += yValue;
80 : }
81 0 : yVector[i] = yValue;
82 : }
83 :
84 0 : const double fN = static_cast< double >( nMax );
85 0 : fAverageX /= fN;
86 0 : fAverageY /= fN;
87 :
88 0 : double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
89 0 : for( i = 0; i < nMax; ++i )
90 : {
91 0 : double fDeltaX = aValues.first[i] - fAverageX;
92 0 : double fDeltaY = yVector[i] - fAverageY;
93 :
94 0 : fQx += fDeltaX * fDeltaX;
95 0 : fQy += fDeltaY * fDeltaY;
96 0 : fQxy += fDeltaX * fDeltaY;
97 : }
98 :
99 0 : m_fLogSlope = fQxy / fQx;
100 0 : m_fLogIntercept = mForceIntercept ? fLogIntercept : fAverageY - m_fLogSlope * fAverageX;
101 0 : m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
102 : }
103 :
104 0 : double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x )
105 : throw (lang::IllegalArgumentException,
106 : uno::RuntimeException, std::exception)
107 : {
108 : double fResult;
109 0 : ::rtl::math::setNan( & fResult );
110 :
111 0 : if( ! ( ::rtl::math::isNan( m_fLogSlope ) ||
112 0 : ::rtl::math::isNan( m_fLogIntercept )))
113 : {
114 0 : fResult = exp(m_fLogIntercept + x * m_fLogSlope);
115 : }
116 :
117 0 : return fResult;
118 : }
119 :
120 0 : uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalculator::getCurveValues(
121 : double min, double max, ::sal_Int32 nPointCount,
122 : const uno::Reference< chart2::XScaling >& xScalingX,
123 : const uno::Reference< chart2::XScaling >& xScalingY,
124 : sal_Bool bMaySkipPointsInCalculation )
125 : throw (lang::IllegalArgumentException,
126 : uno::RuntimeException, std::exception)
127 : {
128 0 : if( bMaySkipPointsInCalculation &&
129 0 : isLinearScaling( xScalingX ) &&
130 0 : isLogarithmicScaling( xScalingY ))
131 : {
132 : // optimize result
133 0 : uno::Sequence< geometry::RealPoint2D > aResult( 2 );
134 0 : aResult[0].X = min;
135 0 : aResult[0].Y = this->getCurveValue( min );
136 0 : aResult[1].X = max;
137 0 : aResult[1].Y = this->getCurveValue( max );
138 :
139 0 : return aResult;
140 : }
141 :
142 0 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
143 : }
144 :
145 0 : OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
146 : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
147 : ::sal_Int32 nNumberFormatKey ) const
148 : {
149 0 : double fIntercept = exp(m_fLogIntercept);
150 0 : double fSlope = exp(m_fLogSlope);
151 0 : bool bHasSlope = !rtl::math::approxEqual( fSlope, 1.0 );
152 0 : bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 );
153 :
154 0 : OUStringBuffer aBuf( "f(x) = ");
155 :
156 0 : if ( fIntercept == 0.0)
157 : {
158 : // underflow, a true zero is impossible
159 0 : aBuf.append( "exp( ");
160 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
161 0 : aBuf.append( (m_fLogSlope < 0.0) ? " - " : " + ");
162 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
163 0 : aBuf.append( " x )");
164 : }
165 : else
166 : {
167 0 : if (bHasIntercept)
168 : {
169 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept) );
170 0 : aBuf.append( " exp( ");
171 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
172 0 : aBuf.append( " x )");
173 : }
174 : else
175 : {
176 : // show logarithmic output, if intercept and slope both are near one
177 : // otherwise drop output of intercept, which is 1 here
178 0 : aBuf.append( " exp( ");
179 0 : if (!bHasSlope)
180 : {
181 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
182 0 : aBuf.append( (m_fLogSlope < 0.0) ? " - " : " + ");
183 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
184 : }
185 : else
186 : {
187 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
188 : }
189 0 : aBuf.append( " x )");
190 : }
191 : }
192 :
193 0 : return aBuf.makeStringAndClear();
194 : }
195 :
196 : } // namespace chart
197 :
198 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|