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_chart2.hxx" 26 #include "LogarithmicRegressionCurveCalculator.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 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() : 42 m_fSlope( 0.0 ), 43 m_fIntercept( 0.0 ) 44 { 45 ::rtl::math::setNan( & m_fSlope ); 46 ::rtl::math::setNan( & m_fIntercept ); 47 } 48 49 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator() 50 {} 51 52 // ____ XRegressionCurve ____ 53 void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression( 54 const uno::Sequence< double >& aXValues, 55 const uno::Sequence< double >& aYValues ) 56 throw (uno::RuntimeException) 57 { 58 RegressionCalculationHelper::tDoubleVectorPair aValues( 59 RegressionCalculationHelper::cleanup( 60 aXValues, aYValues, 61 RegressionCalculationHelper::isValidAndXPositive())); 62 63 const size_t nMax = aValues.first.size(); 64 if( nMax == 0 ) 65 { 66 ::rtl::math::setNan( & m_fSlope ); 67 ::rtl::math::setNan( & m_fIntercept ); 68 ::rtl::math::setNan( & m_fCorrelationCoeffitient ); 69 return; 70 } 71 72 double fAverageX = 0.0, fAverageY = 0.0; 73 size_t i = 0; 74 for( i = 0; i < nMax; ++i ) 75 { 76 fAverageX += log( aValues.first[i] ); 77 fAverageY += aValues.second[i]; 78 } 79 80 const double fN = static_cast< double >( nMax ); 81 fAverageX /= fN; 82 fAverageY /= fN; 83 84 double fQx = 0.0, fQy = 0.0, fQxy = 0.0; 85 for( i = 0; i < nMax; ++i ) 86 { 87 double fDeltaX = log( aValues.first[i] ) - fAverageX; 88 double fDeltaY = aValues.second[i] - fAverageY; 89 90 fQx += fDeltaX * fDeltaX; 91 fQy += fDeltaY * fDeltaY; 92 fQxy += fDeltaX * fDeltaY; 93 } 94 95 m_fSlope = fQxy / fQx; 96 m_fIntercept = fAverageY - m_fSlope * fAverageX; 97 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy ); 98 } 99 100 double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x ) 101 throw (lang::IllegalArgumentException, 102 uno::RuntimeException) 103 { 104 double fResult; 105 ::rtl::math::setNan( & fResult ); 106 107 if( ! ( ::rtl::math::isNan( m_fSlope ) || 108 ::rtl::math::isNan( m_fIntercept ))) 109 { 110 fResult = m_fSlope * log( x ) + m_fIntercept; 111 } 112 113 return fResult; 114 } 115 116 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues( 117 double min, double max, ::sal_Int32 nPointCount, 118 const uno::Reference< chart2::XScaling >& xScalingX, 119 const uno::Reference< chart2::XScaling >& xScalingY, 120 ::sal_Bool bMaySkipPointsInCalculation ) 121 throw (lang::IllegalArgumentException, 122 uno::RuntimeException) 123 { 124 if( bMaySkipPointsInCalculation && 125 isLogarithmicScaling( xScalingX ) && 126 isLinearScaling( xScalingY )) 127 { 128 // optimize result 129 uno::Sequence< geometry::RealPoint2D > aResult( 2 ); 130 aResult[0].X = min; 131 aResult[0].Y = this->getCurveValue( min ); 132 aResult[1].X = max; 133 aResult[1].Y = this->getCurveValue( max ); 134 135 return aResult; 136 } 137 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation ); 138 } 139 140 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation( 141 const uno::Reference< util::XNumberFormatter >& xNumFormatter, 142 ::sal_Int32 nNumberFormatKey ) const 143 { 144 OUStringBuffer aBuf( C2U( "f(x) = " )); 145 146 bool bHaveSlope = false; 147 148 if( m_fSlope != 0.0 ) 149 { 150 if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 )) 151 { 152 if( m_fSlope < 0 ) 153 aBuf.append( UC_MINUS_SIGN ); 154 } 155 else 156 { 157 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope )); 158 aBuf.append( UC_SPACE ); 159 } 160 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" )); 161 bHaveSlope = true; 162 } 163 164 if( bHaveSlope ) 165 { 166 if( m_fIntercept < 0.0 ) 167 { 168 aBuf.append( UC_SPACE ); 169 aBuf.append( UC_MINUS_SIGN ); 170 aBuf.append( UC_SPACE ); 171 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept ))); 172 } 173 else if( m_fIntercept > 0.0 ) 174 { 175 aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " )); 176 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); 177 } 178 } 179 else 180 { 181 aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept )); 182 } 183 184 return aBuf.makeStringAndClear(); 185 } 186 187 } // namespace chart 188