update credits
[LibreOffice.git] / chart2 / source / tools / MeanValueRegressionCurveCalculator.cxx
blob10bebac3c0776ca680ea31a8d7b76f9891317640
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "MeanValueRegressionCurveCalculator.hxx"
21 #include "macros.hxx"
23 #include <rtl/math.hxx>
24 #include <rtl/ustrbuf.hxx>
26 using namespace ::com::sun::star;
29 namespace chart
32 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
33 m_fMeanValue( 0.0 )
35 ::rtl::math::setNan( & m_fMeanValue );
38 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
41 // ____ XRegressionCurveCalculator ____
42 void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
43 const uno::Sequence< double >& /*aXValues*/,
44 const uno::Sequence< double >& aYValues )
45 throw (uno::RuntimeException)
47 const sal_Int32 nDataLength = aYValues.getLength();
48 sal_Int32 nMax = nDataLength;
49 double fSumY = 0.0;
50 const double * pY = aYValues.getConstArray();
52 for( sal_Int32 i = 0; i < nDataLength; ++i )
54 if( ::rtl::math::isNan( pY[i] ) ||
55 ::rtl::math::isInf( pY[i] ))
56 --nMax;
57 else
58 fSumY += pY[i];
61 m_fCorrelationCoeffitient = 0.0;
63 if( nMax == 0 )
65 ::rtl::math::setNan( & m_fMeanValue );
67 else
69 m_fMeanValue = fSumY / static_cast< double >( nMax );
71 // correlation coefficient: standard deviation
72 if( nMax > 1 )
74 double fErrorSum = 0.0;
75 for( sal_Int32 i = 0; i < nDataLength; ++i )
77 if( !::rtl::math::isNan( pY[i] ) &&
78 !::rtl::math::isInf( pY[i] ))
80 double v = m_fMeanValue - pY[i];
81 fErrorSum += (v*v);
84 OSL_ASSERT( fErrorSum >= 0.0 );
85 m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
90 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
91 throw (lang::IllegalArgumentException,
92 uno::RuntimeException)
94 return m_fMeanValue;
98 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
99 double min, double max, ::sal_Int32 nPointCount,
100 const uno::Reference< chart2::XScaling >& xScalingX,
101 const uno::Reference< chart2::XScaling >& xScalingY,
102 ::sal_Bool bMaySkipPointsInCalculation )
103 throw (lang::IllegalArgumentException,
104 uno::RuntimeException)
106 if( bMaySkipPointsInCalculation )
108 // optimize result
109 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
110 aResult[0].X = min;
111 aResult[0].Y = m_fMeanValue;
112 aResult[1].X = max;
113 aResult[1].Y = m_fMeanValue;
115 return aResult;
117 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
120 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
121 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
122 ::sal_Int32 nNumberFormatKey ) const
124 OUString aBuf = "f(x) = " +
125 getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue );
127 return aBuf;
130 } // namespace chart
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */