bump product version to 5.0.4.1
[LibreOffice.git] / chart2 / source / tools / PotentialRegressionCurveCalculator.cxx
blob7495c912b6eafe0f3a9be762822194b7158d758e
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 "PotentialRegressionCurveCalculator.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;
29 namespace chart
32 PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
33 m_fSlope( 0.0 ),
34 m_fIntercept( 0.0 )
36 ::rtl::math::setNan( & m_fSlope );
37 ::rtl::math::setNan( & m_fIntercept );
40 PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
43 // ____ XRegressionCurveCalculator ____
44 void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression(
45 const uno::Sequence< double >& aXValues,
46 const uno::Sequence< double >& aYValues )
47 throw (uno::RuntimeException, std::exception)
49 RegressionCalculationHelper::tDoubleVectorPair aValues(
50 RegressionCalculationHelper::cleanup(
51 aXValues, aYValues,
52 RegressionCalculationHelper::isValidAndBothPositive()));
54 const size_t nMax = aValues.first.size();
55 if( nMax == 0 )
57 ::rtl::math::setNan( & m_fSlope );
58 ::rtl::math::setNan( & m_fIntercept );
59 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
60 return;
63 double fAverageX = 0.0, fAverageY = 0.0;
64 size_t i = 0;
65 for( i = 0; i < nMax; ++i )
67 fAverageX += log( aValues.first[i] );
68 fAverageY += log( aValues.second[i] );
71 const double fN = static_cast< double >( nMax );
72 fAverageX /= fN;
73 fAverageY /= fN;
75 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
76 for( i = 0; i < nMax; ++i )
78 double fDeltaX = log( aValues.first[i] ) - fAverageX;
79 double fDeltaY = log( aValues.second[i] ) - fAverageY;
81 fQx += fDeltaX * fDeltaX;
82 fQy += fDeltaY * fDeltaY;
83 fQxy += fDeltaX * fDeltaY;
86 m_fSlope = fQxy / fQx;
87 m_fIntercept = fAverageY - m_fSlope * fAverageX;
88 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
90 m_fIntercept = exp( m_fIntercept );
93 double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
94 throw (lang::IllegalArgumentException,
95 uno::RuntimeException, std::exception)
97 double fResult;
98 ::rtl::math::setNan( & fResult );
100 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
101 ::rtl::math::isNan( m_fIntercept )))
103 fResult = m_fIntercept * pow( x, m_fSlope );
106 return fResult;
109 uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues(
110 double min, double max, ::sal_Int32 nPointCount,
111 const uno::Reference< chart2::XScaling >& xScalingX,
112 const uno::Reference< chart2::XScaling >& xScalingY,
113 sal_Bool bMaySkipPointsInCalculation )
114 throw (lang::IllegalArgumentException,
115 uno::RuntimeException, std::exception)
117 if( bMaySkipPointsInCalculation &&
118 isLogarithmicScaling( xScalingX ) &&
119 isLogarithmicScaling( xScalingY ))
121 // optimize result
122 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
123 aResult[0].X = min;
124 aResult[0].Y = this->getCurveValue( min );
125 aResult[1].X = max;
126 aResult[1].Y = this->getCurveValue( max );
128 return aResult;
130 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
133 OUString PotentialRegressionCurveCalculator::ImplGetRepresentation(
134 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
135 ::sal_Int32 nNumberFormatKey ) const
137 OUStringBuffer aBuf( "f(x) = ");
139 if( m_fIntercept == 0.0 )
141 aBuf.append( '0');
143 else if( m_fSlope == 0.0 )
145 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
147 else
149 if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
151 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
152 aBuf.append( ' ');
154 if( m_fSlope != 0.0 )
156 aBuf.append( "x^" );
157 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
161 return aBuf.makeStringAndClear();
164 } // namespace chart
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */