LCOV - code coverage report
Current view: top level - chart2/source/tools - MeanValueRegressionCurveCalculator.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 35 44 79.5 %
Date: 2014-04-11 Functions: 5 7 71.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "MeanValueRegressionCurveCalculator.hxx"
      21             : #include "macros.hxx"
      22             : 
      23             : #include <rtl/math.hxx>
      24             : #include <rtl/ustrbuf.hxx>
      25             : 
      26             : using namespace ::com::sun::star;
      27             : 
      28             : namespace chart
      29             : {
      30             : 
      31          32 : MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
      32          32 :         m_fMeanValue( 0.0 )
      33             : {
      34          32 :     ::rtl::math::setNan( & m_fMeanValue );
      35          32 : }
      36             : 
      37          64 : MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
      38          64 : {}
      39             : 
      40             : // ____ XRegressionCurveCalculator ____
      41          32 : void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
      42             :     const uno::Sequence< double >& /*aXValues*/,
      43             :     const uno::Sequence< double >& aYValues )
      44             :     throw (uno::RuntimeException, std::exception)
      45             : {
      46          32 :     const sal_Int32 nDataLength = aYValues.getLength();
      47          32 :     sal_Int32 nMax = nDataLength;
      48          32 :     double fSumY = 0.0;
      49          32 :     const double * pY = aYValues.getConstArray();
      50             : 
      51         421 :     for( sal_Int32 i = 0; i < nDataLength; ++i )
      52             :     {
      53         778 :         if( ::rtl::math::isNan( pY[i] ) ||
      54         389 :             ::rtl::math::isInf( pY[i] ))
      55           0 :             --nMax;
      56             :         else
      57         389 :             fSumY += pY[i];
      58             :     }
      59             : 
      60          32 :     m_fCorrelationCoeffitient = 0.0;
      61             : 
      62          32 :     if( nMax == 0 )
      63             :     {
      64           0 :         ::rtl::math::setNan( & m_fMeanValue );
      65             :     }
      66             :     else
      67             :     {
      68          32 :         m_fMeanValue = fSumY / static_cast< double >( nMax );
      69             : 
      70             :         // correlation coefficient: standard deviation
      71          32 :         if( nMax > 1 )
      72             :         {
      73          32 :             double fErrorSum = 0.0;
      74         421 :             for( sal_Int32 i = 0; i < nDataLength; ++i )
      75             :             {
      76         778 :                 if( !::rtl::math::isNan( pY[i] ) &&
      77         389 :                     !::rtl::math::isInf( pY[i] ))
      78             :                 {
      79         389 :                     double v = m_fMeanValue - pY[i];
      80         389 :                     fErrorSum += (v*v);
      81             :                 }
      82             :             }
      83             :             OSL_ASSERT( fErrorSum >= 0.0 );
      84          32 :             m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
      85             :         }
      86             :     }
      87          32 : }
      88             : 
      89           0 : double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
      90             :     throw (lang::IllegalArgumentException,
      91             :            uno::RuntimeException, std::exception)
      92             : {
      93           0 :     return m_fMeanValue;
      94             : }
      95             : 
      96          32 : uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
      97             :     double min, double max, ::sal_Int32 nPointCount,
      98             :     const uno::Reference< chart2::XScaling >& xScalingX,
      99             :     const uno::Reference< chart2::XScaling >& xScalingY,
     100             :     sal_Bool bMaySkipPointsInCalculation )
     101             :     throw (lang::IllegalArgumentException,
     102             :            uno::RuntimeException, std::exception)
     103             : {
     104          32 :     if( bMaySkipPointsInCalculation )
     105             :     {
     106             :         // optimize result
     107          32 :         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
     108          32 :         aResult[0].X = min;
     109          32 :         aResult[0].Y = m_fMeanValue;
     110          32 :         aResult[1].X = max;
     111          32 :         aResult[1].Y = m_fMeanValue;
     112             : 
     113          32 :         return aResult;
     114             :     }
     115           0 :     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
     116             : }
     117             : 
     118           0 : OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
     119             :     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
     120             :     ::sal_Int32 nNumberFormatKey ) const
     121             : {
     122           0 :     OUString aBuf = "f(x) = " +
     123           0 :                     getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue );
     124             : 
     125           0 :     return aBuf;
     126             : }
     127             : 
     128             : } //  namespace chart
     129             : 
     130             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10