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