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