1*cde9e8dcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*cde9e8dcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cde9e8dcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cde9e8dcSAndrew Rist * distributed with this work for additional information 6*cde9e8dcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cde9e8dcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cde9e8dcSAndrew Rist * "License"); you may not use this file except in compliance 9*cde9e8dcSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*cde9e8dcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*cde9e8dcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cde9e8dcSAndrew Rist * software distributed under the License is distributed on an 15*cde9e8dcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cde9e8dcSAndrew Rist * KIND, either express or implied. See the License for the 17*cde9e8dcSAndrew Rist * specific language governing permissions and limitations 18*cde9e8dcSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*cde9e8dcSAndrew Rist *************************************************************/ 21*cde9e8dcSAndrew Rist 22*cde9e8dcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_chart2.hxx" 26cdf0e10cSrcweir #include "LogarithmicRegressionCurveCalculator.hxx" 27cdf0e10cSrcweir #include "macros.hxx" 28cdf0e10cSrcweir #include "RegressionCalculationHelper.hxx" 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include <rtl/math.hxx> 31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 32cdf0e10cSrcweir 33cdf0e10cSrcweir using namespace ::com::sun::star; 34cdf0e10cSrcweir 35cdf0e10cSrcweir using ::rtl::OUString; 36cdf0e10cSrcweir using ::rtl::OUStringBuffer; 37cdf0e10cSrcweir 38cdf0e10cSrcweir namespace chart 39cdf0e10cSrcweir { 40cdf0e10cSrcweir 41cdf0e10cSrcweir LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() : 42cdf0e10cSrcweir m_fSlope( 0.0 ), 43cdf0e10cSrcweir m_fIntercept( 0.0 ) 44cdf0e10cSrcweir { 45cdf0e10cSrcweir ::rtl::math::setNan( & m_fSlope ); 46cdf0e10cSrcweir ::rtl::math::setNan( & m_fIntercept ); 47cdf0e10cSrcweir } 48cdf0e10cSrcweir 49cdf0e10cSrcweir LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator() 50cdf0e10cSrcweir {} 51cdf0e10cSrcweir 52cdf0e10cSrcweir // ____ XRegressionCurve ____ 53cdf0e10cSrcweir void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression( 54cdf0e10cSrcweir const uno::Sequence< double >& aXValues, 55cdf0e10cSrcweir const uno::Sequence< double >& aYValues ) 56cdf0e10cSrcweir throw (uno::RuntimeException) 57cdf0e10cSrcweir { 58cdf0e10cSrcweir RegressionCalculationHelper::tDoubleVectorPair aValues( 59cdf0e10cSrcweir RegressionCalculationHelper::cleanup( 60cdf0e10cSrcweir aXValues, aYValues, 61cdf0e10cSrcweir RegressionCalculationHelper::isValidAndXPositive())); 62cdf0e10cSrcweir 63cdf0e10cSrcweir const size_t nMax = aValues.first.size(); 64cdf0e10cSrcweir if( nMax == 0 ) 65cdf0e10cSrcweir { 66cdf0e10cSrcweir ::rtl::math::setNan( & m_fSlope ); 67cdf0e10cSrcweir ::rtl::math::setNan( & m_fIntercept ); 68cdf0e10cSrcweir ::rtl::math::setNan( & m_fCorrelationCoeffitient ); 69cdf0e10cSrcweir return; 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir double fAverageX = 0.0, fAverageY = 0.0; 73cdf0e10cSrcweir size_t i = 0; 74cdf0e10cSrcweir for( i = 0; i < nMax; ++i ) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir fAverageX += log( aValues.first[i] ); 77cdf0e10cSrcweir fAverageY += aValues.second[i]; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir const double fN = static_cast< double >( nMax ); 81cdf0e10cSrcweir fAverageX /= fN; 82cdf0e10cSrcweir fAverageY /= fN; 83cdf0e10cSrcweir 84cdf0e10cSrcweir double fQx = 0.0, fQy = 0.0, fQxy = 0.0; 85cdf0e10cSrcweir for( i = 0; i < nMax; ++i ) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir double fDeltaX = log( aValues.first[i] ) - fAverageX; 88cdf0e10cSrcweir double fDeltaY = aValues.second[i] - fAverageY; 89cdf0e10cSrcweir 90cdf0e10cSrcweir fQx += fDeltaX * fDeltaX; 91cdf0e10cSrcweir fQy += fDeltaY * fDeltaY; 92cdf0e10cSrcweir fQxy += fDeltaX * fDeltaY; 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir m_fSlope = fQxy / fQx; 96cdf0e10cSrcweir m_fIntercept = fAverageY - m_fSlope * fAverageX; 97cdf0e10cSrcweir m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x ) 101cdf0e10cSrcweir throw (lang::IllegalArgumentException, 102cdf0e10cSrcweir uno::RuntimeException) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir double fResult; 105cdf0e10cSrcweir ::rtl::math::setNan( & fResult ); 106cdf0e10cSrcweir 107cdf0e10cSrcweir if( ! ( ::rtl::math::isNan( m_fSlope ) || 108cdf0e10cSrcweir ::rtl::math::isNan( m_fIntercept ))) 109cdf0e10cSrcweir { 110cdf0e10cSrcweir fResult = m_fSlope * log( x ) + m_fIntercept; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir return fResult; 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues( 117cdf0e10cSrcweir double min, double max, ::sal_Int32 nPointCount, 118cdf0e10cSrcweir const uno::Reference< chart2::XScaling >& xScalingX, 119cdf0e10cSrcweir const uno::Reference< chart2::XScaling >& xScalingY, 120cdf0e10cSrcweir ::sal_Bool bMaySkipPointsInCalculation ) 121cdf0e10cSrcweir throw (lang::IllegalArgumentException, 122cdf0e10cSrcweir uno::RuntimeException) 123cdf0e10cSrcweir { 124cdf0e10cSrcweir if( bMaySkipPointsInCalculation && 125cdf0e10cSrcweir isLogarithmicScaling( xScalingX ) && 126cdf0e10cSrcweir isLinearScaling( xScalingY )) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir // optimize result 129cdf0e10cSrcweir uno::Sequence< geometry::RealPoint2D > aResult( 2 ); 130cdf0e10cSrcweir aResult[0].X = min; 131cdf0e10cSrcweir aResult[0].Y = this->getCurveValue( min ); 132cdf0e10cSrcweir aResult[1].X = max; 133cdf0e10cSrcweir aResult[1].Y = this->getCurveValue( max ); 134cdf0e10cSrcweir 135cdf0e10cSrcweir return aResult; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation ); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation( 141cdf0e10cSrcweir const uno::Reference< util::XNumberFormatter >& xNumFormatter, 142cdf0e10cSrcweir ::sal_Int32 nNumberFormatKey ) const 143cdf0e10cSrcweir { 144cdf0e10cSrcweir OUStringBuffer aBuf( C2U( "f(x) = " )); 145cdf0e10cSrcweir 146cdf0e10cSrcweir bool bHaveSlope = false; 147cdf0e10cSrcweir 148cdf0e10cSrcweir if( m_fSlope != 0.0 ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 )) 151cdf0e10cSrcweir { 152cdf0e10cSrcweir if( m_fSlope < 0 ) 153cdf0e10cSrcweir aBuf.append( UC_MINUS_SIGN ); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir else 156cdf0e10cSrcweir { 157cdf0e10cSrcweir aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope )); 158cdf0e10cSrcweir aBuf.append( UC_SPACE ); 159cdf0e10cSrcweir } 160cdf0e10cSrcweir aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" )); 161cdf0e10cSrcweir bHaveSlope = true; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir if( bHaveSlope ) 165cdf0e10cSrcweir { 166cdf0e10cSrcweir if( m_fIntercept < 0.0 ) 167cdf0e10cSrcweir { 168cdf0e10cSrcweir aBuf.append( UC_SPACE ); 169cdf0e10cSrcweir aBuf.append( UC_MINUS_SIGN ); 170cdf0e10cSrcweir aBuf.append( UC_SPACE ); 171cdf0e10cSrcweir aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept ))); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir else if( m_fIntercept > 0.0 ) 174cdf0e10cSrcweir { 175cdf0e10cSrcweir aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " )); 176cdf0e10cSrcweir aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir } 179cdf0e10cSrcweir else 180cdf0e10cSrcweir { 181cdf0e10cSrcweir aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); 182cdf0e10cSrcweir } 183cdf0e10cSrcweir 184cdf0e10cSrcweir return aBuf.makeStringAndClear(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir 187cdf0e10cSrcweir } // namespace chart 188