xref: /trunk/main/chart2/source/tools/LinearRegressionCurveCalculator.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 "LinearRegressionCurveCalculator.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 
LinearRegressionCurveCalculator()41 LinearRegressionCurveCalculator::LinearRegressionCurveCalculator() :
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 
~LinearRegressionCurveCalculator()49 LinearRegressionCurveCalculator::~LinearRegressionCurveCalculator()
50 {}
51 
52 // ____ XRegressionCurveCalculator ____
recalculateRegression(const uno::Sequence<double> & aXValues,const uno::Sequence<double> & aYValues)53 void SAL_CALL LinearRegressionCurveCalculator::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::isValid()));
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     const double fN = static_cast< double >( nMax );
72     double fSumX = 0.0, fSumY = 0.0, fSumXSq = 0.0, fSumYSq = 0.0, fSumXY = 0.0;
73     for( size_t i = 0; i < nMax; ++i )
74     {
75         fSumX   += aValues.first[i];
76         fSumY   += aValues.second[i];
77         fSumXSq += aValues.first[i]  * aValues.first[i];
78         fSumYSq += aValues.second[i] * aValues.second[i];
79         fSumXY  += aValues.first[i]  * aValues.second[i];
80     }
81 
82     m_fSlope = (fN * fSumXY - fSumX * fSumY) / ( fN * fSumXSq - fSumX * fSumX );
83     m_fIntercept = (fSumY - m_fSlope * fSumX) / fN;
84 
85     m_fCorrelationCoeffitient = ( fN * fSumXY - fSumX * fSumY ) /
86         sqrt( ( fN * fSumXSq - fSumX * fSumX ) *
87               ( fN * fSumYSq - fSumY * fSumY ) );
88 }
89 
getCurveValue(double x)90 double SAL_CALL LinearRegressionCurveCalculator::getCurveValue( double x )
91 {
92     double fResult;
93     ::rtl::math::setNan( & fResult );
94 
95     if( ! ( ::rtl::math::isNan( m_fSlope ) ||
96             ::rtl::math::isNan( m_fIntercept )))
97     {
98         fResult = m_fSlope * x + m_fIntercept;
99     }
100 
101     return fResult;
102 }
103 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)104 uno::Sequence< geometry::RealPoint2D > SAL_CALL LinearRegressionCurveCalculator::getCurveValues(
105     double min, double max, ::sal_Int32 nPointCount,
106     const uno::Reference< chart2::XScaling >& xScalingX,
107     const uno::Reference< chart2::XScaling >& xScalingY,
108     ::sal_Bool bMaySkipPointsInCalculation )
109 {
110     if( bMaySkipPointsInCalculation &&
111         isLinearScaling( xScalingX ) &&
112         isLinearScaling( xScalingY ))
113     {
114         // optimize result
115         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
116         aResult[0].X = min;
117         aResult[0].Y = this->getCurveValue( min );
118         aResult[1].X = max;
119         aResult[1].Y = this->getCurveValue( max );
120 
121         return aResult;
122     }
123     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
124 }
125 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const126 OUString LinearRegressionCurveCalculator::ImplGetRepresentation(
127     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
128     ::sal_Int32 nNumberFormatKey ) const
129 {
130     OUStringBuffer aBuf( C2U( "f(x) = " ));
131 
132     bool bHaveSlope = false;
133 
134     if( m_fSlope != 0.0 )
135     {
136         if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
137         {
138             if( m_fSlope < 0 )
139                 aBuf.append( UC_MINUS_SIGN );
140         }
141         else
142             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
143         aBuf.append( sal_Unicode( 'x' ));
144         bHaveSlope = true;
145     }
146 
147     if( bHaveSlope )
148     {
149         if( m_fIntercept < 0.0 )
150         {
151             aBuf.append( UC_SPACE );
152             aBuf.append( UC_MINUS_SIGN );
153             aBuf.append( UC_SPACE );
154             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
155         }
156         else if( m_fIntercept > 0.0 )
157         {
158             aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
159             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
160         }
161     }
162     else
163     {
164         aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
165     }
166 
167     return aBuf.makeStringAndClear();
168 }
169 
170 } //  namespace chart
171