192 lines
5.9 KiB
C++
192 lines
5.9 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*************************************************************************
|
|
*
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
*
|
|
* Copyright 2000, 2010 Oracle and/or its affiliates.
|
|
*
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
|
*
|
|
* This file is part of OpenOffice.org.
|
|
*
|
|
* OpenOffice.org is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License version 3
|
|
* only, as published by the Free Software Foundation.
|
|
*
|
|
* OpenOffice.org is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License version 3 for more details
|
|
* (a copy is included in the LICENSE file that accompanied this code).
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* version 3 along with OpenOffice.org. If not, see
|
|
* <http://www.openoffice.org/license.html>
|
|
* for a copy of the LGPLv3 License.
|
|
*
|
|
************************************************************************/
|
|
|
|
#include "LogarithmicRegressionCurveCalculator.hxx"
|
|
#include "macros.hxx"
|
|
#include "RegressionCalculationHelper.hxx"
|
|
|
|
#include <rtl/math.hxx>
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
using ::rtl::OUString;
|
|
using ::rtl::OUStringBuffer;
|
|
|
|
namespace chart
|
|
{
|
|
|
|
LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
|
|
m_fSlope( 0.0 ),
|
|
m_fIntercept( 0.0 )
|
|
{
|
|
::rtl::math::setNan( & m_fSlope );
|
|
::rtl::math::setNan( & m_fIntercept );
|
|
}
|
|
|
|
LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
|
|
{}
|
|
|
|
// ____ XRegressionCurve ____
|
|
void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression(
|
|
const uno::Sequence< double >& aXValues,
|
|
const uno::Sequence< double >& aYValues )
|
|
throw (uno::RuntimeException)
|
|
{
|
|
RegressionCalculationHelper::tDoubleVectorPair aValues(
|
|
RegressionCalculationHelper::cleanup(
|
|
aXValues, aYValues,
|
|
RegressionCalculationHelper::isValidAndXPositive()));
|
|
|
|
const size_t nMax = aValues.first.size();
|
|
if( nMax == 0 )
|
|
{
|
|
::rtl::math::setNan( & m_fSlope );
|
|
::rtl::math::setNan( & m_fIntercept );
|
|
::rtl::math::setNan( & m_fCorrelationCoeffitient );
|
|
return;
|
|
}
|
|
|
|
double fAverageX = 0.0, fAverageY = 0.0;
|
|
size_t i = 0;
|
|
for( i = 0; i < nMax; ++i )
|
|
{
|
|
fAverageX += log( aValues.first[i] );
|
|
fAverageY += aValues.second[i];
|
|
}
|
|
|
|
const double fN = static_cast< double >( nMax );
|
|
fAverageX /= fN;
|
|
fAverageY /= fN;
|
|
|
|
double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
|
|
for( i = 0; i < nMax; ++i )
|
|
{
|
|
double fDeltaX = log( aValues.first[i] ) - fAverageX;
|
|
double fDeltaY = aValues.second[i] - fAverageY;
|
|
|
|
fQx += fDeltaX * fDeltaX;
|
|
fQy += fDeltaY * fDeltaY;
|
|
fQxy += fDeltaX * fDeltaY;
|
|
}
|
|
|
|
m_fSlope = fQxy / fQx;
|
|
m_fIntercept = fAverageY - m_fSlope * fAverageX;
|
|
m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
|
|
}
|
|
|
|
double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
|
|
throw (lang::IllegalArgumentException,
|
|
uno::RuntimeException)
|
|
{
|
|
double fResult;
|
|
::rtl::math::setNan( & fResult );
|
|
|
|
if( ! ( ::rtl::math::isNan( m_fSlope ) ||
|
|
::rtl::math::isNan( m_fIntercept )))
|
|
{
|
|
fResult = m_fSlope * log( x ) + m_fIntercept;
|
|
}
|
|
|
|
return fResult;
|
|
}
|
|
|
|
uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
|
|
double min, double max, ::sal_Int32 nPointCount,
|
|
const uno::Reference< chart2::XScaling >& xScalingX,
|
|
const uno::Reference< chart2::XScaling >& xScalingY,
|
|
::sal_Bool bMaySkipPointsInCalculation )
|
|
throw (lang::IllegalArgumentException,
|
|
uno::RuntimeException)
|
|
{
|
|
if( bMaySkipPointsInCalculation &&
|
|
isLogarithmicScaling( xScalingX ) &&
|
|
isLinearScaling( xScalingY ))
|
|
{
|
|
// optimize result
|
|
uno::Sequence< geometry::RealPoint2D > aResult( 2 );
|
|
aResult[0].X = min;
|
|
aResult[0].Y = this->getCurveValue( min );
|
|
aResult[1].X = max;
|
|
aResult[1].Y = this->getCurveValue( max );
|
|
|
|
return aResult;
|
|
}
|
|
return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
|
|
}
|
|
|
|
OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
|
|
const uno::Reference< util::XNumberFormatter >& xNumFormatter,
|
|
::sal_Int32 nNumberFormatKey ) const
|
|
{
|
|
OUStringBuffer aBuf( C2U( "f(x) = " ));
|
|
|
|
bool bHaveSlope = false;
|
|
|
|
if( m_fSlope != 0.0 )
|
|
{
|
|
if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
|
|
{
|
|
if( m_fSlope < 0 )
|
|
aBuf.append( UC_MINUS_SIGN );
|
|
}
|
|
else
|
|
{
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
|
|
aBuf.append( UC_SPACE );
|
|
}
|
|
aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
|
|
bHaveSlope = true;
|
|
}
|
|
|
|
if( bHaveSlope )
|
|
{
|
|
if( m_fIntercept < 0.0 )
|
|
{
|
|
aBuf.append( UC_SPACE );
|
|
aBuf.append( UC_MINUS_SIGN );
|
|
aBuf.append( UC_SPACE );
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
|
|
}
|
|
else if( m_fIntercept > 0.0 )
|
|
{
|
|
aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
|
|
}
|
|
|
|
return aBuf.makeStringAndClear();
|
|
}
|
|
|
|
} // namespace chart
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|