merged tag ooo/OOO330_m14
[LibreOffice.git] / chart2 / source / tools / LogarithmicRegressionCurveCalculator.cxx
blobc7c2db60431c48fead5210cf15107a6ee18d9884
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 "LogarithmicRegressionCurveCalculator.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 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
46 m_fSlope( 0.0 ),
47 m_fIntercept( 0.0 )
49 ::rtl::math::setNan( & m_fSlope );
50 ::rtl::math::setNan( & m_fIntercept );
53 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
56 // ____ XRegressionCurve ____
57 void SAL_CALL LogarithmicRegressionCurveCalculator::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::isValidAndXPositive()));
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 += 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 = 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 );
104 double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
105 throw (lang::IllegalArgumentException,
106 uno::RuntimeException)
108 double fResult;
109 ::rtl::math::setNan( & fResult );
111 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
112 ::rtl::math::isNan( m_fIntercept )))
114 fResult = m_fSlope * log( x ) + m_fIntercept;
117 return fResult;
120 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
121 double min, double max, ::sal_Int32 nPointCount,
122 const uno::Reference< chart2::XScaling >& xScalingX,
123 const uno::Reference< chart2::XScaling >& xScalingY,
124 ::sal_Bool bMaySkipPointsInCalculation )
125 throw (lang::IllegalArgumentException,
126 uno::RuntimeException)
128 if( bMaySkipPointsInCalculation &&
129 isLogarithmicScaling( xScalingX ) &&
130 isLinearScaling( xScalingY ))
132 // optimize result
133 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
134 aResult[0].X = min;
135 aResult[0].Y = this->getCurveValue( min );
136 aResult[1].X = max;
137 aResult[1].Y = this->getCurveValue( max );
139 return aResult;
141 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
144 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
145 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
146 ::sal_Int32 nNumberFormatKey ) const
148 OUStringBuffer aBuf( C2U( "f(x) = " ));
150 bool bHaveSlope = false;
152 if( m_fSlope != 0.0 )
154 if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
156 if( m_fSlope < 0 )
157 aBuf.append( UC_MINUS_SIGN );
159 else
161 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
162 aBuf.append( UC_SPACE );
164 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
165 bHaveSlope = true;
168 if( bHaveSlope )
170 if( m_fIntercept < 0.0 )
172 aBuf.append( UC_SPACE );
173 aBuf.append( UC_MINUS_SIGN );
174 aBuf.append( UC_SPACE );
175 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
177 else if( m_fIntercept > 0.0 )
179 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
180 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
183 else
185 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
188 return aBuf.makeStringAndClear();
191 } // namespace chart