LCOV - code coverage report
Current view: top level - libreoffice/chart2/source/tools - ExponentialRegressionCurveCalculator.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 75 0.0 %
Date: 2012-12-27 Functions: 0 7 0.0 %
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 "ExponentialRegressionCurveCalculator.hxx"
      21             : #include "macros.hxx"
      22             : #include "RegressionCalculationHelper.hxx"
      23             : 
      24             : #include <rtl/math.hxx>
      25             : #include <rtl/ustrbuf.hxx>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : 
      29             : using ::rtl::OUString;
      30             : using ::rtl::OUStringBuffer;
      31             : 
      32             : namespace chart
      33             : {
      34             : 
      35           0 : ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() :
      36             :         m_fLogSlope( 0.0 ),
      37           0 :         m_fLogIntercept( 0.0 )
      38             : {
      39           0 :     ::rtl::math::setNan( & m_fLogSlope );
      40           0 :     ::rtl::math::setNan( & m_fLogIntercept );
      41           0 : }
      42             : 
      43           0 : ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
      44           0 : {}
      45             : 
      46             : // ____ XRegressionCurveCalculator ____
      47           0 : void SAL_CALL ExponentialRegressionCurveCalculator::recalculateRegression(
      48             :     const uno::Sequence< double >& aXValues,
      49             :     const uno::Sequence< double >& aYValues )
      50             :     throw (uno::RuntimeException)
      51             : {
      52             :     RegressionCalculationHelper::tDoubleVectorPair aValues(
      53             :         RegressionCalculationHelper::cleanup(
      54             :             aXValues, aYValues,
      55           0 :             RegressionCalculationHelper::isValidAndYPositive()));
      56             : 
      57           0 :     const size_t nMax = aValues.first.size();
      58           0 :     if( nMax == 0 )
      59             :     {
      60           0 :         ::rtl::math::setNan( & m_fLogSlope );
      61           0 :         ::rtl::math::setNan( & m_fLogIntercept );
      62           0 :         ::rtl::math::setNan( & m_fCorrelationCoeffitient );// actual it is coefficient of determination
      63           0 :         return;
      64             :     }
      65             : 
      66           0 :     double fAverageX = 0.0, fAverageY = 0.0;
      67           0 :     size_t i = 0;
      68           0 :     for( i = 0; i < nMax; ++i )
      69             :     {
      70           0 :         fAverageX += aValues.first[i];
      71           0 :         fAverageY += log( aValues.second[i] );
      72             :     }
      73             : 
      74           0 :     const double fN = static_cast< double >( nMax );
      75           0 :     fAverageX /= fN;
      76           0 :     fAverageY /= fN;
      77             : 
      78           0 :     double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
      79           0 :     for( i = 0; i < nMax; ++i )
      80             :     {
      81           0 :         double fDeltaX = aValues.first[i] - fAverageX;
      82           0 :         double fDeltaY = log( aValues.second[i] ) - fAverageY;
      83             : 
      84           0 :         fQx  += fDeltaX * fDeltaX;
      85           0 :         fQy  += fDeltaY * fDeltaY;
      86           0 :         fQxy += fDeltaX * fDeltaY;
      87             :     }
      88             : 
      89           0 :     m_fLogSlope = fQxy / fQx;
      90           0 :     m_fLogIntercept = fAverageY - m_fLogSlope * fAverageX;
      91           0 :     m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
      92             : 
      93             : }
      94             : 
      95           0 : double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x )
      96             :     throw (lang::IllegalArgumentException,
      97             :            uno::RuntimeException)
      98             : {
      99             :     double fResult;
     100           0 :     ::rtl::math::setNan( & fResult );
     101             : 
     102           0 :     if( ! ( ::rtl::math::isNan( m_fLogSlope ) ||
     103           0 :             ::rtl::math::isNan( m_fLogIntercept )))
     104             :     {
     105           0 :         fResult = exp(m_fLogIntercept + x * m_fLogSlope);
     106             :     }
     107             : 
     108           0 :     return fResult;
     109             : }
     110             : 
     111           0 : uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalculator::getCurveValues(
     112             :     double min, double max, ::sal_Int32 nPointCount,
     113             :     const uno::Reference< chart2::XScaling >& xScalingX,
     114             :     const uno::Reference< chart2::XScaling >& xScalingY,
     115             :     ::sal_Bool bMaySkipPointsInCalculation )
     116             :     throw (lang::IllegalArgumentException,
     117             :            uno::RuntimeException)
     118             : {
     119           0 :     if( bMaySkipPointsInCalculation &&
     120           0 :         isLinearScaling( xScalingX ) &&
     121           0 :         isLogarithmicScaling( xScalingY ))
     122             :     {
     123             :         // optimize result
     124           0 :         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
     125           0 :         aResult[0].X = min;
     126           0 :         aResult[0].Y = this->getCurveValue( min );
     127           0 :         aResult[1].X = max;
     128           0 :         aResult[1].Y = this->getCurveValue( max );
     129             : 
     130           0 :         return aResult;
     131             :     }
     132             : 
     133           0 :     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
     134             : }
     135             : 
     136             : 
     137           0 : OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
     138             :     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
     139             :     ::sal_Int32 nNumberFormatKey ) const
     140             : {
     141           0 :     double fIntercept = exp(m_fLogIntercept);
     142           0 :     double fSlope = exp(m_fLogSlope);
     143           0 :     bool bHasSlope = !rtl::math::approxEqual( fSlope, 1.0 );
     144           0 :     bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 );
     145             : 
     146           0 :     OUStringBuffer aBuf( C2U( "f(x) = " ));
     147             : 
     148           0 :     if ( fIntercept == 0.0)
     149             :     {
     150             :         // underflow, a true zero is impossible
     151           0 :         aBuf.append( C2U( "exp( " ));
     152           0 :         aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
     153           0 :         aBuf.append( (m_fLogSlope < 0.0) ? C2U( " - " ) : C2U( " + " ));
     154           0 :         aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
     155           0 :         aBuf.append( C2U( " x )" ));
     156             :     }
     157             :     else
     158             :     {
     159           0 :         if (bHasIntercept)
     160             :         {
     161           0 :             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept) );
     162           0 :             aBuf.append( C2U( " exp( " ));
     163           0 :             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
     164           0 :             aBuf.append( C2U( " x )" ));
     165             :         }
     166             :         else
     167             :         {
     168             :             // show logarithmic output, if intercept and slope both are near one
     169             :             // otherwise drop output of intercept, which is 1 here
     170           0 :             aBuf.append( C2U( " exp( " ));
     171           0 :             if (!bHasSlope)
     172             :             {
     173           0 :                 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept) );
     174           0 :                 aBuf.append( (m_fLogSlope < 0.0) ? C2U( " - " ) : C2U( " + " ));
     175           0 :                 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope)) );
     176             :             }
     177             :             else
     178             :             {
     179           0 :                 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogSlope) );
     180             :             }
     181           0 :             aBuf.append( C2U( " x )" ));
     182             :         }
     183             :     }
     184             : 
     185           0 :     return aBuf.makeStringAndClear();
     186             : }
     187             : 
     188             : } //  namespace chart
     189             : 
     190             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10