Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / tools / MeanValueRegressionCurveCalculator.cxx
blobaefd3f8f05aa4dd83c7eeb6049efe4f5d6ec6f0c
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>
24 #include <cmath>
25 #include <limits>
27 using namespace ::com::sun::star;
29 namespace chart
32 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
33 m_fMeanValue( std::numeric_limits<double>::quiet_NaN() )
37 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
40 // ____ XRegressionCurveCalculator ____
41 void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
42 const uno::Sequence< double >& /*aXValues*/,
43 const uno::Sequence< double >& aYValues )
45 const sal_Int32 nDataLength = aYValues.getLength();
46 sal_Int32 nMax = nDataLength;
47 double fSumY = 0.0;
48 const double * pY = aYValues.getConstArray();
50 for( sal_Int32 i = 0; i < nDataLength; ++i )
52 if( std::isnan( pY[i] ) ||
53 std::isinf( pY[i] ))
54 --nMax;
55 else
56 fSumY += pY[i];
59 m_fCorrelationCoefficient = 0.0;
61 if( nMax == 0 )
63 m_fMeanValue = std::numeric_limits<double>::quiet_NaN();
65 else
67 m_fMeanValue = fSumY / static_cast< double >( nMax );
69 // correlation coefficient: standard deviation
70 if( nMax > 1 )
72 double fErrorSum = 0.0;
73 for( sal_Int32 i = 0; i < nDataLength; ++i )
75 if( !std::isnan( pY[i] ) &&
76 !std::isinf( pY[i] ))
78 double v = m_fMeanValue - pY[i];
79 fErrorSum += (v*v);
82 OSL_ASSERT( fErrorSum >= 0.0 );
83 m_fCorrelationCoefficient = sqrt( fErrorSum / (nMax - 1 ));
88 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
90 return m_fMeanValue;
93 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
94 double min, double max, ::sal_Int32 nPointCount,
95 const uno::Reference< chart2::XScaling >& xScalingX,
96 const uno::Reference< chart2::XScaling >& xScalingY,
97 sal_Bool bMaySkipPointsInCalculation )
99 if( bMaySkipPointsInCalculation )
101 // optimize result
102 uno::Sequence< geometry::RealPoint2D > aResult{ { min, m_fMeanValue },
103 { max, m_fMeanValue } };
105 return aResult;
107 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
110 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
111 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
112 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaLength /* = nullptr */ ) const
114 OUString aBuf(mYName + " = ");
115 if ( pFormulaLength )
117 *pFormulaLength -= aBuf.getLength();
118 if ( *pFormulaLength <= 0 )
119 return "###";
121 return ( aBuf + getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue, pFormulaLength ) );
124 } // namespace chart
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */