merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / ExponentialRegressionCurveCalculator.cxx
blobd7bb861743233b8e7a1d3e59eb27acebd47d58c4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "ExponentialRegressionCurveCalculator.hxx"
31 #include "macros.hxx"
32 #include "RegressionCalculationHelper.hxx"
34 #include <rtl/math.hxx>
35 #include <rtl/ustrbuf.hxx>
37 using namespace ::com::sun::star;
39 using ::rtl::OUString;
40 using ::rtl::OUStringBuffer;
42 namespace chart
45 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() :
46 m_fSlope( 0.0 ),
47 m_fIntercept( 0.0 )
49 ::rtl::math::setNan( & m_fSlope );
50 ::rtl::math::setNan( & m_fIntercept );
53 ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
56 // ____ XRegressionCurveCalculator ____
57 void SAL_CALL ExponentialRegressionCurveCalculator::recalculateRegression(
58 const uno::Sequence< double >& aXValues,
59 const uno::Sequence< double >& aYValues )
60 throw (uno::RuntimeException)
62 RegressionCalculationHelper::tDoubleVectorPair aValues(
63 RegressionCalculationHelper::cleanup(
64 aXValues, aYValues,
65 RegressionCalculationHelper::isValidAndYPositive()));
67 const size_t nMax = aValues.first.size();
68 if( nMax == 0 )
70 ::rtl::math::setNan( & m_fSlope );
71 ::rtl::math::setNan( & m_fIntercept );
72 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
73 return;
76 double fAverageX = 0.0, fAverageY = 0.0;
77 size_t i = 0;
78 for( i = 0; i < nMax; ++i )
80 fAverageX += aValues.first[i];
81 fAverageY += log( aValues.second[i] );
84 const double fN = static_cast< double >( nMax );
85 fAverageX /= fN;
86 fAverageY /= fN;
88 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
89 for( i = 0; i < nMax; ++i )
91 double fDeltaX = aValues.first[i] - fAverageX;
92 double fDeltaY = log( aValues.second[i] ) - fAverageY;
94 fQx += fDeltaX * fDeltaX;
95 fQy += fDeltaY * fDeltaY;
96 fQxy += fDeltaX * fDeltaY;
99 m_fSlope = fQxy / fQx;
100 m_fIntercept = fAverageY - m_fSlope * fAverageX;
101 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
103 m_fSlope = exp( m_fSlope );
104 m_fIntercept = exp( m_fIntercept );
107 double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x )
108 throw (lang::IllegalArgumentException,
109 uno::RuntimeException)
111 double fResult;
112 ::rtl::math::setNan( & fResult );
114 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
115 ::rtl::math::isNan( m_fIntercept )))
117 fResult = m_fIntercept * pow( m_fSlope, x );
120 return fResult;
123 uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalculator::getCurveValues(
124 double min, double max, ::sal_Int32 nPointCount,
125 const uno::Reference< chart2::XScaling >& xScalingX,
126 const uno::Reference< chart2::XScaling >& xScalingY,
127 ::sal_Bool bMaySkipPointsInCalculation )
128 throw (lang::IllegalArgumentException,
129 uno::RuntimeException)
131 if( bMaySkipPointsInCalculation &&
132 isLinearScaling( xScalingX ) &&
133 isLogarithmicScaling( xScalingY ))
135 // optimize result
136 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
137 aResult[0].X = min;
138 aResult[0].Y = this->getCurveValue( min );
139 aResult[1].X = max;
140 aResult[1].Y = this->getCurveValue( max );
142 return aResult;
145 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
149 OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
150 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
151 ::sal_Int32 nNumberFormatKey ) const
153 OUStringBuffer aBuf( C2U( "f(x) = " ));
155 if( m_fIntercept == 0.0 ||
156 m_fSlope == 0.0 )
158 aBuf.append( sal_Unicode( '0' ));
160 else if( rtl::math::approxEqual( m_fSlope, 1.0 ) )
162 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
164 else
166 if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
168 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
169 aBuf.append( sal_Unicode( 0x00b7 ));
172 if( m_fSlope < 0.0 )
173 aBuf.append( sal_Unicode( '(' ));
174 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
175 if( m_fSlope < 0.0 )
176 aBuf.append( sal_Unicode( ')' ));
177 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "^x" ));
180 return aBuf.makeStringAndClear();
183 } // namespace chart