merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / PotentialRegressionCurveCalculator.cxx
blobb1d10424b9a1f28ce6a4e26411ed33643e848880
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 "PotentialRegressionCurveCalculator.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 PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
46 m_fSlope( 0.0 ),
47 m_fIntercept( 0.0 )
49 ::rtl::math::setNan( & m_fSlope );
50 ::rtl::math::setNan( & m_fIntercept );
53 PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
56 // ____ XRegressionCurveCalculator ____
57 void SAL_CALL PotentialRegressionCurveCalculator::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::isValidAndBothPositive()));
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 += log( 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 = log( 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_fIntercept = exp( m_fIntercept );
106 double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
107 throw (lang::IllegalArgumentException,
108 uno::RuntimeException)
110 double fResult;
111 ::rtl::math::setNan( & fResult );
113 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
114 ::rtl::math::isNan( m_fIntercept )))
116 fResult = m_fIntercept * pow( x, m_fSlope );
119 return fResult;
122 uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues(
123 double min, double max, ::sal_Int32 nPointCount,
124 const uno::Reference< chart2::XScaling >& xScalingX,
125 const uno::Reference< chart2::XScaling >& xScalingY,
126 ::sal_Bool bMaySkipPointsInCalculation )
127 throw (lang::IllegalArgumentException,
128 uno::RuntimeException)
130 if( bMaySkipPointsInCalculation &&
131 isLogarithmicScaling( xScalingX ) &&
132 isLogarithmicScaling( xScalingY ))
134 // optimize result
135 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
136 aResult[0].X = min;
137 aResult[0].Y = this->getCurveValue( min );
138 aResult[1].X = max;
139 aResult[1].Y = this->getCurveValue( max );
141 return aResult;
143 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
146 OUString PotentialRegressionCurveCalculator::ImplGetRepresentation(
147 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
148 ::sal_Int32 nNumberFormatKey ) const
150 OUStringBuffer aBuf( C2U( "f(x) = " ));
152 if( m_fIntercept == 0.0 )
154 aBuf.append( sal_Unicode( '0' ));
156 else if( m_fSlope == 0.0 )
158 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
160 else
162 if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
164 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
165 aBuf.append( sal_Unicode( ' ' ));
167 if( m_fSlope != 0.0 )
169 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "x^" ));
170 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
174 return aBuf.makeStringAndClear();
177 } // namespace chart