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