bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / LinearRegressionCurveCalculator.cxx
blob5b388ee4139e0cd4108b097b433e4f504e51e137
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "LinearRegressionCurveCalculator.hxx"
21 #include "macros.hxx"
22 #include "RegressionCalculationHelper.hxx"
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star;
30 namespace chart
33 LinearRegressionCurveCalculator::LinearRegressionCurveCalculator() :
34 m_fSlope( 0.0 ),
35 m_fIntercept( 0.0 )
37 ::rtl::math::setNan( & m_fSlope );
38 ::rtl::math::setNan( & m_fIntercept );
41 LinearRegressionCurveCalculator::~LinearRegressionCurveCalculator()
44 // ____ XRegressionCurveCalculator ____
45 void SAL_CALL LinearRegressionCurveCalculator::recalculateRegression(
46 const uno::Sequence< double >& aXValues,
47 const uno::Sequence< double >& aYValues )
48 throw (uno::RuntimeException)
50 RegressionCalculationHelper::tDoubleVectorPair aValues(
51 RegressionCalculationHelper::cleanup(
52 aXValues, aYValues,
53 RegressionCalculationHelper::isValid()));
55 const size_t nMax = aValues.first.size();
56 if( nMax == 0 )
58 ::rtl::math::setNan( & m_fSlope );
59 ::rtl::math::setNan( & m_fIntercept );
60 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
61 return;
64 const double fN = static_cast< double >( nMax );
65 double fSumX = 0.0, fSumY = 0.0, fSumXSq = 0.0, fSumYSq = 0.0, fSumXY = 0.0;
66 for( size_t i = 0; i < nMax; ++i )
68 fSumX += aValues.first[i];
69 fSumY += aValues.second[i];
70 fSumXSq += aValues.first[i] * aValues.first[i];
71 fSumYSq += aValues.second[i] * aValues.second[i];
72 fSumXY += aValues.first[i] * aValues.second[i];
75 m_fSlope = (fN * fSumXY - fSumX * fSumY) / ( fN * fSumXSq - fSumX * fSumX );
76 m_fIntercept = (fSumY - m_fSlope * fSumX) / fN;
78 m_fCorrelationCoeffitient = ( fN * fSumXY - fSumX * fSumY ) /
79 sqrt( ( fN * fSumXSq - fSumX * fSumX ) *
80 ( fN * fSumYSq - fSumY * fSumY ) );
83 double SAL_CALL LinearRegressionCurveCalculator::getCurveValue( double x )
84 throw (lang::IllegalArgumentException,
85 uno::RuntimeException)
87 double fResult;
88 ::rtl::math::setNan( & fResult );
90 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
91 ::rtl::math::isNan( m_fIntercept )))
93 fResult = m_fSlope * x + m_fIntercept;
96 return fResult;
99 uno::Sequence< geometry::RealPoint2D > SAL_CALL LinearRegressionCurveCalculator::getCurveValues(
100 double min, double max, ::sal_Int32 nPointCount,
101 const uno::Reference< chart2::XScaling >& xScalingX,
102 const uno::Reference< chart2::XScaling >& xScalingY,
103 ::sal_Bool bMaySkipPointsInCalculation )
104 throw (lang::IllegalArgumentException,
105 uno::RuntimeException)
107 if( bMaySkipPointsInCalculation &&
108 isLinearScaling( xScalingX ) &&
109 isLinearScaling( xScalingY ))
111 // optimize result
112 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
113 aResult[0].X = min;
114 aResult[0].Y = this->getCurveValue( min );
115 aResult[1].X = max;
116 aResult[1].Y = this->getCurveValue( max );
118 return aResult;
120 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
123 OUString LinearRegressionCurveCalculator::ImplGetRepresentation(
124 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
125 ::sal_Int32 nNumberFormatKey ) const
127 OUStringBuffer aBuf( "f(x) = ");
129 bool bHaveSlope = false;
131 if( m_fSlope != 0.0 )
133 if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
135 if( m_fSlope < 0 )
136 aBuf.append( UC_MINUS_SIGN );
138 else
139 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
140 aBuf.append( sal_Unicode( 'x' ));
141 bHaveSlope = true;
144 if( bHaveSlope )
146 if( m_fIntercept < 0.0 )
148 aBuf.append( UC_SPACE );
149 aBuf.append( UC_MINUS_SIGN );
150 aBuf.append( UC_SPACE );
151 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
153 else if( m_fIntercept > 0.0 )
155 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
156 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
159 else
161 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
164 return aBuf.makeStringAndClear();
167 } // namespace chart
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */