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