merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / tools / PotentialRegressionCurveCalculator.cxx
blob20471ab338704252fc3093191f264275c0056d9a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PotentialRegressionCurveCalculator.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "PotentialRegressionCurveCalculator.hxx"
34 #include "macros.hxx"
35 #include "RegressionCalculationHelper.hxx"
37 #include <rtl/math.hxx>
38 #include <rtl/ustrbuf.hxx>
40 using namespace ::com::sun::star;
42 using ::rtl::OUString;
43 using ::rtl::OUStringBuffer;
45 namespace chart
48 PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
49 m_fSlope( 0.0 ),
50 m_fIntercept( 0.0 )
52 ::rtl::math::setNan( & m_fSlope );
53 ::rtl::math::setNan( & m_fIntercept );
56 PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
59 // ____ XRegressionCurveCalculator ____
60 void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression(
61 const uno::Sequence< double >& aXValues,
62 const uno::Sequence< double >& aYValues )
63 throw (uno::RuntimeException)
65 RegressionCalculationHelper::tDoubleVectorPair aValues(
66 RegressionCalculationHelper::cleanup(
67 aXValues, aYValues,
68 RegressionCalculationHelper::isValidAndBothPositive()));
70 const size_t nMax = aValues.first.size();
71 if( nMax == 0 )
73 ::rtl::math::setNan( & m_fSlope );
74 ::rtl::math::setNan( & m_fIntercept );
75 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
76 return;
79 double fAverageX = 0.0, fAverageY = 0.0;
80 size_t i = 0;
81 for( i = 0; i < nMax; ++i )
83 fAverageX += log( aValues.first[i] );
84 fAverageY += log( aValues.second[i] );
87 const double fN = static_cast< double >( nMax );
88 fAverageX /= fN;
89 fAverageY /= fN;
91 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
92 for( i = 0; i < nMax; ++i )
94 double fDeltaX = log( aValues.first[i] ) - fAverageX;
95 double fDeltaY = log( aValues.second[i] ) - fAverageY;
97 fQx += fDeltaX * fDeltaX;
98 fQy += fDeltaY * fDeltaY;
99 fQxy += fDeltaX * fDeltaY;
102 m_fSlope = fQxy / fQx;
103 m_fIntercept = fAverageY - m_fSlope * fAverageX;
104 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
106 m_fIntercept = exp( m_fIntercept );
109 double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
110 throw (lang::IllegalArgumentException,
111 uno::RuntimeException)
113 double fResult;
114 ::rtl::math::setNan( & fResult );
116 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
117 ::rtl::math::isNan( m_fIntercept )))
119 fResult = m_fIntercept * pow( x, m_fSlope );
122 return fResult;
125 uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues(
126 double min, double max, ::sal_Int32 nPointCount,
127 const uno::Reference< chart2::XScaling >& xScalingX,
128 const uno::Reference< chart2::XScaling >& xScalingY,
129 ::sal_Bool bMaySkipPointsInCalculation )
130 throw (lang::IllegalArgumentException,
131 uno::RuntimeException)
133 if( bMaySkipPointsInCalculation &&
134 isLogarithmicScaling( xScalingX ) &&
135 isLogarithmicScaling( xScalingY ))
137 // optimize result
138 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
139 aResult[0].X = min;
140 aResult[0].Y = this->getCurveValue( min );
141 aResult[1].X = max;
142 aResult[1].Y = this->getCurveValue( max );
144 return aResult;
146 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
149 OUString PotentialRegressionCurveCalculator::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 )
157 aBuf.append( sal_Unicode( '0' ));
159 else if( m_fSlope == 0.0 )
161 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
163 else
165 if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
167 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
168 aBuf.append( sal_Unicode( ' ' ));
170 if( m_fSlope != 0.0 )
172 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "x^" ));
173 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
177 return aBuf.makeStringAndClear();
180 } // namespace chart