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