bump product version to 4.1.6.2
[LibreOffice.git] / chart2 / source / tools / ExponentialRegressionCurveCalculator.cxx
blobc76138ac809f76a33aba1c59b0a7ca8a6731402d
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 "ExponentialRegressionCurveCalculator.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 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() :
34 m_fLogSlope( 0.0 ),
35 m_fLogIntercept( 0.0 )
37 ::rtl::math::setNan( & m_fLogSlope );
38 ::rtl::math::setNan( & m_fLogIntercept );
41 ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
44 // ____ XRegressionCurveCalculator ____
45 void SAL_CALL ExponentialRegressionCurveCalculator::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::isValidAndYPositive()));
55 const size_t nMax = aValues.first.size();
56 if( nMax == 0 )
58 ::rtl::math::setNan( & m_fLogSlope );
59 ::rtl::math::setNan( & m_fLogIntercept );
60 ::rtl::math::setNan( & m_fCorrelationCoeffitient );// actual it is coefficient of determination
61 return;
64 double fAverageX = 0.0, fAverageY = 0.0;
65 size_t i = 0;
66 for( i = 0; i < nMax; ++i )
68 fAverageX += aValues.first[i];
69 fAverageY += log( aValues.second[i] );
72 const double fN = static_cast< double >( nMax );
73 fAverageX /= fN;
74 fAverageY /= fN;
76 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
77 for( i = 0; i < nMax; ++i )
79 double fDeltaX = aValues.first[i] - fAverageX;
80 double fDeltaY = log( aValues.second[i] ) - fAverageY;
82 fQx += fDeltaX * fDeltaX;
83 fQy += fDeltaY * fDeltaY;
84 fQxy += fDeltaX * fDeltaY;
87 m_fLogSlope = fQxy / fQx;
88 m_fLogIntercept = fAverageY - m_fLogSlope * fAverageX;
89 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
93 double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x )
94 throw (lang::IllegalArgumentException,
95 uno::RuntimeException)
97 double fResult;
98 ::rtl::math::setNan( & fResult );
100 if( ! ( ::rtl::math::isNan( m_fLogSlope ) ||
101 ::rtl::math::isNan( m_fLogIntercept )))
103 fResult = exp(m_fLogIntercept + x * m_fLogSlope);
106 return fResult;
109 uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalculator::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)
117 if( bMaySkipPointsInCalculation &&
118 isLinearScaling( 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;
131 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
135 OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
136 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
137 ::sal_Int32 nNumberFormatKey ) const
139 double fIntercept = exp(m_fLogIntercept);
140 double fSlope = exp(m_fLogSlope);
141 bool bHasSlope = !rtl::math::approxEqual( fSlope, 1.0 );
142 bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 );
144 OUStringBuffer aBuf( "f(x) = ");
146 if ( fIntercept == 0.0)
148 // underflow, a true zero is impossible
149 aBuf.append( "exp( ");
150 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
151 aBuf.append( (m_fLogSlope < 0.0) ? " - " : " + ");
152 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
153 aBuf.append( " x )");
155 else
157 if (bHasIntercept)
159 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept) );
160 aBuf.append( " exp( ");
161 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
162 aBuf.append( " x )");
164 else
166 // show logarithmic output, if intercept and slope both are near one
167 // otherwise drop output of intercept, which is 1 here
168 aBuf.append( " exp( ");
169 if (!bHasSlope)
171 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
172 aBuf.append( (m_fLogSlope < 0.0) ? " - " : " + ");
173 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
175 else
177 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
179 aBuf.append( " x )");
183 return aBuf.makeStringAndClear();
186 } // namespace chart
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */