2003-12-17 07:29:31 -06:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* Copyright 2008 by Sun Microsystems, Inc.
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* $RCSfile: PotentialRegressionCurveCalculator.cxx,v $
|
|
|
|
* $Revision: 1.7 $
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* This file is part of OpenOffice.org.
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* 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.
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* 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).
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
2008-04-10 16:16:57 -05:00
|
|
|
* 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.
|
2003-12-17 07:29:31 -06:00
|
|
|
*
|
|
|
|
************************************************************************/
|
2006-09-17 07:28:18 -05:00
|
|
|
|
|
|
|
// MARKER(update_precomp.py): autogen include statement, do not remove
|
|
|
|
#include "precompiled_chart2.hxx"
|
2003-12-17 07:29:31 -06:00
|
|
|
#include "PotentialRegressionCurveCalculator.hxx"
|
|
|
|
#include "macros.hxx"
|
|
|
|
#include "RegressionCalculationHelper.hxx"
|
|
|
|
|
2007-11-23 05:06:40 -06:00
|
|
|
#include <rtl/math.hxx>
|
2003-12-17 07:29:31 -06:00
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
|
|
|
|
|
|
|
using ::rtl::OUString;
|
|
|
|
using ::rtl::OUStringBuffer;
|
|
|
|
|
|
|
|
namespace chart
|
|
|
|
{
|
|
|
|
|
|
|
|
PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
|
|
|
|
m_fSlope( 0.0 ),
|
2007-11-23 05:06:40 -06:00
|
|
|
m_fIntercept( 0.0 )
|
2003-12-17 07:29:31 -06:00
|
|
|
{
|
|
|
|
::rtl::math::setNan( & m_fSlope );
|
|
|
|
::rtl::math::setNan( & m_fIntercept );
|
|
|
|
}
|
|
|
|
|
|
|
|
PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
|
|
|
|
{}
|
|
|
|
|
|
|
|
// ____ XRegressionCurveCalculator ____
|
|
|
|
void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression(
|
|
|
|
const uno::Sequence< double >& aXValues,
|
|
|
|
const uno::Sequence< double >& aYValues )
|
|
|
|
throw (uno::RuntimeException)
|
|
|
|
{
|
|
|
|
RegressionCalculationHelper::tDoubleVectorPair aValues(
|
|
|
|
RegressionCalculationHelper::cleanup(
|
|
|
|
aXValues, aYValues,
|
|
|
|
RegressionCalculationHelper::isValidAndBothPositive()));
|
|
|
|
|
|
|
|
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;
|
2004-03-08 09:03:51 -06:00
|
|
|
size_t i = 0;
|
|
|
|
for( i = 0; i < nMax; ++i )
|
2003-12-17 07:29:31 -06:00
|
|
|
{
|
|
|
|
fAverageX += log( aValues.first[i] );
|
|
|
|
fAverageY += log( 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 = log( 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 );
|
|
|
|
|
|
|
|
m_fIntercept = exp( m_fIntercept );
|
|
|
|
}
|
|
|
|
|
|
|
|
double SAL_CALL PotentialRegressionCurveCalculator::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_fIntercept * pow( x, m_fSlope );
|
|
|
|
}
|
|
|
|
|
|
|
|
return fResult;
|
|
|
|
}
|
|
|
|
|
2007-11-23 05:06:40 -06:00
|
|
|
uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::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)
|
2003-12-17 07:29:31 -06:00
|
|
|
{
|
2007-11-23 05:06:40 -06:00
|
|
|
if( bMaySkipPointsInCalculation &&
|
|
|
|
isLogarithmicScaling( xScalingX ) &&
|
|
|
|
isLogarithmicScaling( 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 );
|
2003-12-17 07:29:31 -06:00
|
|
|
}
|
|
|
|
|
2007-11-23 05:06:40 -06:00
|
|
|
OUString PotentialRegressionCurveCalculator::ImplGetRepresentation(
|
|
|
|
const uno::Reference< util::XNumberFormatter >& xNumFormatter,
|
|
|
|
::sal_Int32 nNumberFormatKey ) const
|
2003-12-17 07:29:31 -06:00
|
|
|
{
|
|
|
|
OUStringBuffer aBuf( C2U( "f(x) = " ));
|
|
|
|
|
|
|
|
if( m_fIntercept == 0.0 )
|
|
|
|
{
|
|
|
|
aBuf.append( sal_Unicode( '0' ));
|
|
|
|
}
|
|
|
|
else if( m_fSlope == 0.0 )
|
|
|
|
{
|
2007-11-23 05:06:40 -06:00
|
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
|
2003-12-17 07:29:31 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
|
|
|
|
{
|
2007-11-23 05:06:40 -06:00
|
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
|
2003-12-17 07:29:31 -06:00
|
|
|
aBuf.append( sal_Unicode( ' ' ));
|
|
|
|
}
|
|
|
|
if( m_fSlope != 0.0 )
|
|
|
|
{
|
|
|
|
aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "x^" ));
|
2007-11-23 05:06:40 -06:00
|
|
|
aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
|
2003-12-17 07:29:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return aBuf.makeStringAndClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chart
|