update dev300-m58
[ooovba.git] / chart2 / source / tools / LogarithmicRegressionCurveCalculator.cxx
blobe1c5af73ebb390e3c8545cf6dd14d70d1df2c77e
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: LogarithmicRegressionCurveCalculator.cxx,v $
10 * $Revision: 1.9 $
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 "LogarithmicRegressionCurveCalculator.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 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
49 m_fSlope( 0.0 ),
50 m_fIntercept( 0.0 )
52 ::rtl::math::setNan( & m_fSlope );
53 ::rtl::math::setNan( & m_fIntercept );
56 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
59 // ____ XRegressionCurve ____
60 void SAL_CALL LogarithmicRegressionCurveCalculator::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::isValidAndXPositive()));
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 += 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 = 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 );
107 double SAL_CALL LogarithmicRegressionCurveCalculator::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_fSlope * log( x ) + m_fIntercept;
120 return fResult;
123 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::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 isLogarithmicScaling( xScalingX ) &&
133 isLinearScaling( 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;
144 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
147 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
148 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
149 ::sal_Int32 nNumberFormatKey ) const
151 OUStringBuffer aBuf( C2U( "f(x) = " ));
153 bool bHaveSlope = false;
155 if( m_fSlope != 0.0 )
157 if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
159 if( m_fSlope < 0 )
160 aBuf.append( UC_MINUS_SIGN );
162 else
164 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
165 aBuf.append( UC_SPACE );
167 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
168 bHaveSlope = true;
171 if( bHaveSlope )
173 if( m_fIntercept < 0.0 )
175 aBuf.append( UC_SPACE );
176 aBuf.append( UC_MINUS_SIGN );
177 aBuf.append( UC_SPACE );
178 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
180 else if( m_fIntercept > 0.0 )
182 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
183 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
186 else
188 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
191 return aBuf.makeStringAndClear();
194 } // namespace chart