Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / MeanValueRegressionCurveCalculator.cxx
blob5d98e8c563c11cc69154596a2355efba77dd7dd9
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>
22 #include <osl/diagnose.h>
23 #include <rtl/math.hxx>
25 using namespace ::com::sun::star;
27 namespace chart
30 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
31 m_fMeanValue( 0.0 )
33 ::rtl::math::setNan( & m_fMeanValue );
36 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
39 // ____ XRegressionCurveCalculator ____
40 void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
41 const uno::Sequence< double >& /*aXValues*/,
42 const uno::Sequence< double >& aYValues )
44 const sal_Int32 nDataLength = aYValues.getLength();
45 sal_Int32 nMax = nDataLength;
46 double fSumY = 0.0;
47 const double * pY = aYValues.getConstArray();
49 for( sal_Int32 i = 0; i < nDataLength; ++i )
51 if( ::rtl::math::isNan( pY[i] ) ||
52 ::rtl::math::isInf( pY[i] ))
53 --nMax;
54 else
55 fSumY += pY[i];
58 m_fCorrelationCoeffitient = 0.0;
60 if( nMax == 0 )
62 ::rtl::math::setNan( & m_fMeanValue );
64 else
66 m_fMeanValue = fSumY / static_cast< double >( nMax );
68 // correlation coefficient: standard deviation
69 if( nMax > 1 )
71 double fErrorSum = 0.0;
72 for( sal_Int32 i = 0; i < nDataLength; ++i )
74 if( !::rtl::math::isNan( pY[i] ) &&
75 !::rtl::math::isInf( pY[i] ))
77 double v = m_fMeanValue - pY[i];
78 fErrorSum += (v*v);
81 OSL_ASSERT( fErrorSum >= 0.0 );
82 m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
87 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
89 return m_fMeanValue;
92 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
93 double min, double max, ::sal_Int32 nPointCount,
94 const uno::Reference< chart2::XScaling >& xScalingX,
95 const uno::Reference< chart2::XScaling >& xScalingY,
96 sal_Bool bMaySkipPointsInCalculation )
98 if( bMaySkipPointsInCalculation )
100 // optimize result
101 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
102 aResult[0].X = min;
103 aResult[0].Y = m_fMeanValue;
104 aResult[1].X = max;
105 aResult[1].Y = m_fMeanValue;
107 return aResult;
109 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
112 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
113 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
114 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaLength /* = nullptr */ ) const
116 OUString aBuf(mYName + " = ");
117 if ( pFormulaLength )
119 *pFormulaLength -= aBuf.getLength();
120 if ( *pFormulaLength <= 0 )
121 return "###";
123 return ( aBuf + getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue, pFormulaLength ) );
126 } // namespace chart
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */