xref: /trunk/main/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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_charttools.hxx"
26 #include "LogarithmicRegressionCurveCalculator.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 
LogarithmicRegressionCurveCalculator()41 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
42         m_fSlope( 0.0 ),
43         m_fIntercept( 0.0 )
44 {
45     ::rtl::math::setNan( & m_fSlope );
46     ::rtl::math::setNan( & m_fIntercept );
47 }
48 
~LogarithmicRegressionCurveCalculator()49 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
50 {}
51 
52 // ____ XRegressionCurve ____
recalculateRegression(const uno::Sequence<double> & aXValues,const uno::Sequence<double> & aYValues)53 void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression(
54     const uno::Sequence< double >& aXValues,
55     const uno::Sequence< double >& aYValues )
56 {
57     RegressionCalculationHelper::tDoubleVectorPair aValues(
58         RegressionCalculationHelper::cleanup(
59             aXValues, aYValues,
60             RegressionCalculationHelper::isValidAndXPositive()));
61 
62     const size_t nMax = aValues.first.size();
63     if( nMax == 0 )
64     {
65         ::rtl::math::setNan( & m_fSlope );
66         ::rtl::math::setNan( & m_fIntercept );
67         ::rtl::math::setNan( & m_fCorrelationCoeffitient );
68         return;
69     }
70 
71     double fAverageX = 0.0, fAverageY = 0.0;
72     size_t i = 0;
73     for( i = 0; i < nMax; ++i )
74     {
75         fAverageX += log( aValues.first[i] );
76         fAverageY += aValues.second[i];
77     }
78 
79     const double fN = static_cast< double >( nMax );
80     fAverageX /= fN;
81     fAverageY /= fN;
82 
83     double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
84     for( i = 0; i < nMax; ++i )
85     {
86         double fDeltaX = log( aValues.first[i] ) - fAverageX;
87         double fDeltaY = aValues.second[i] - fAverageY;
88 
89         fQx  += fDeltaX * fDeltaX;
90         fQy  += fDeltaY * fDeltaY;
91         fQxy += fDeltaX * fDeltaY;
92     }
93 
94     m_fSlope = fQxy / fQx;
95     m_fIntercept = fAverageY - m_fSlope * fAverageX;
96     m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
97 }
98 
getCurveValue(double x)99 double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
100 {
101     double fResult;
102     ::rtl::math::setNan( & fResult );
103 
104     if( ! ( ::rtl::math::isNan( m_fSlope ) ||
105             ::rtl::math::isNan( m_fIntercept )))
106     {
107         fResult = m_fSlope * log( x ) + m_fIntercept;
108     }
109 
110     return fResult;
111 }
112 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)113 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
114     double min, double max, ::sal_Int32 nPointCount,
115     const uno::Reference< chart2::XScaling >& xScalingX,
116     const uno::Reference< chart2::XScaling >& xScalingY,
117     ::sal_Bool bMaySkipPointsInCalculation )
118 {
119     if( bMaySkipPointsInCalculation &&
120         isLogarithmicScaling( xScalingX ) &&
121         isLinearScaling( xScalingY ))
122     {
123         // optimize result
124         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
125         aResult[0].X = min;
126         aResult[0].Y = this->getCurveValue( min );
127         aResult[1].X = max;
128         aResult[1].Y = this->getCurveValue( max );
129 
130         return aResult;
131     }
132     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
133 }
134 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const135 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
136     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
137     ::sal_Int32 nNumberFormatKey ) const
138 {
139     OUStringBuffer aBuf( C2U( "f(x) = " ));
140 
141     bool bHaveSlope = false;
142 
143     if( m_fSlope != 0.0 )
144     {
145         if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
146         {
147             if( m_fSlope < 0 )
148                 aBuf.append( UC_MINUS_SIGN );
149         }
150         else
151         {
152             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
153             aBuf.append( UC_SPACE );
154         }
155         aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
156         bHaveSlope = true;
157     }
158 
159     if( bHaveSlope )
160     {
161         if( m_fIntercept < 0.0 )
162         {
163             aBuf.append( UC_SPACE );
164             aBuf.append( UC_MINUS_SIGN );
165             aBuf.append( UC_SPACE );
166             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
167         }
168         else if( m_fIntercept > 0.0 )
169         {
170             aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
171             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
172         }
173     }
174     else
175     {
176         aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
177     }
178 
179     return aBuf.makeStringAndClear();
180 }
181 
182 } //  namespace chart
183