Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / ExponentialRegressionCurveCalculator.cxx
blob2c2b81359355d4310b5d241f1454fc63134a8c32
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <ExponentialRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
22 #include <SpecialCharacters.hxx>
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star;
29 namespace chart
32 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator()
33 : m_fLogSlope(0.0)
34 , m_fLogIntercept(0.0)
35 , m_fSign(1.0)
37 ::rtl::math::setNan( & m_fLogSlope );
38 ::rtl::math::setNan( & m_fLogIntercept );
41 ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
44 // ____ XRegressionCurveCalculator ____
45 void SAL_CALL ExponentialRegressionCurveCalculator::recalculateRegression(
46 const uno::Sequence< double >& aXValues,
47 const uno::Sequence< double >& aYValues )
49 RegressionCalculationHelper::tDoubleVectorPair aValues(
50 RegressionCalculationHelper::cleanup(
51 aXValues, aYValues,
52 RegressionCalculationHelper::isValidAndYPositive()));
53 m_fSign = 1.0;
55 size_t nMax = aValues.first.size();
56 if( nMax <= 1 ) // at least 2 points
58 aValues = RegressionCalculationHelper::cleanup(
59 aXValues, aYValues,
60 RegressionCalculationHelper::isValidAndYNegative());
61 nMax = aValues.first.size();
62 if( nMax <= 1 )
64 ::rtl::math::setNan( & m_fLogSlope );
65 ::rtl::math::setNan( & m_fLogIntercept );
66 ::rtl::math::setNan( & m_fCorrelationCoeffitient );// actual it is coefficient of determination
67 return;
69 m_fSign = -1.0;
72 double fAverageX = 0.0, fAverageY = 0.0;
73 double fLogIntercept = ( mForceIntercept && (m_fSign * mInterceptValue)>0 ) ? log(m_fSign * mInterceptValue) : 0.0;
74 std::vector<double> yVector;
75 yVector.resize(nMax, 0.0);
77 size_t i = 0;
78 for( i = 0; i < nMax; ++i )
80 double yValue = log( m_fSign *aValues.second[i] );
81 if (mForceIntercept)
83 yValue -= fLogIntercept;
85 else
87 fAverageX += aValues.first[i];
88 fAverageY += yValue;
90 yVector[i] = yValue;
93 const double fN = static_cast< double >( nMax );
94 fAverageX /= fN;
95 fAverageY /= fN;
97 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
98 for( i = 0; i < nMax; ++i )
100 double fDeltaX = aValues.first[i] - fAverageX;
101 double fDeltaY = yVector[i] - fAverageY;
103 fQx += fDeltaX * fDeltaX;
104 fQy += fDeltaY * fDeltaY;
105 fQxy += fDeltaX * fDeltaY;
108 m_fLogSlope = fQxy / fQx;
109 m_fLogIntercept = mForceIntercept ? fLogIntercept : fAverageY - m_fLogSlope * fAverageX;
110 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
113 double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x )
115 double fResult;
116 ::rtl::math::setNan( & fResult );
118 if( ! ( ::rtl::math::isNan( m_fLogSlope ) ||
119 ::rtl::math::isNan( m_fLogIntercept )))
121 fResult = m_fSign * exp(m_fLogIntercept + x * m_fLogSlope);
124 return fResult;
127 uno::Sequence< geometry::RealPoint2D > SAL_CALL ExponentialRegressionCurveCalculator::getCurveValues(
128 double min, double max, ::sal_Int32 nPointCount,
129 const uno::Reference< chart2::XScaling >& xScalingX,
130 const uno::Reference< chart2::XScaling >& xScalingY,
131 sal_Bool bMaySkipPointsInCalculation )
133 if( bMaySkipPointsInCalculation &&
134 isLinearScaling( xScalingX ) &&
135 isLogarithmicScaling( xScalingY ))
137 // optimize result
138 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
139 aResult[0].X = min;
140 aResult[0].Y = getCurveValue( min );
141 aResult[1].X = max;
142 aResult[1].Y = getCurveValue( max );
144 return aResult;
147 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
150 OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation(
151 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
152 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
154 double fIntercept = exp(m_fLogIntercept);
155 bool bHasSlope = !rtl::math::approxEqual( exp(m_fLogSlope), 1.0 );
156 bool bHasLogSlope = !rtl::math::approxEqual( fabs(m_fLogSlope), 1.0 );
157 bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 ) && fIntercept != 0.0;
159 OUStringBuffer aBuf( mYName + " = " );
160 sal_Int32 nLineLength = aBuf.getLength();
161 sal_Int32 nValueLength=0;
162 if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 )
163 { // count characters different from coefficients
164 sal_Int32 nCharMin = nLineLength + 10 + mXName.getLength(); // 10 = "exp( ", " x )" + 2 extra characters
165 if ( m_fSign < 0.0 )
166 nCharMin += 2;
167 if ( fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) )
168 nCharMin += 3; // " + " special case where equation is written exp( a + b x )
169 if ( ( bHasIntercept || fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) ) &&
170 bHasLogSlope )
171 nValueLength = ( *pFormulaMaxWidth - nCharMin ) / 2;
172 else
173 nValueLength = *pFormulaMaxWidth - nCharMin;
174 if ( nValueLength <= 0 )
175 nValueLength = 1;
177 // temporary buffer
178 OUStringBuffer aTmpBuf("");
179 // if nValueLength not calculated then nullptr
180 sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
181 if ( m_fSign < 0.0 )
182 aTmpBuf.append( OUStringChar(aMinusSign) ).append( " " );
183 if ( bHasIntercept )
185 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept, pValueLength );
186 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
188 aTmpBuf.append( aValueString ).append( " " );
189 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
190 aTmpBuf.truncate();
193 aTmpBuf.append( "exp( " );
194 if ( !bHasIntercept )
196 if ( fIntercept == 0.0 || // underflow, a true zero is impossible
197 ( !bHasSlope && m_fLogIntercept != 0.0 ) ) // show logarithmic output, if intercept and slope both are near one
198 { // otherwise drop output of intercept, which is 1 here
199 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept, pValueLength );
200 if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small
202 aTmpBuf.append( aValueString ).append( (m_fLogSlope < 0.0) ? OUStringLiteral(" ") : OUStringLiteral(" + ") );
206 if ( m_fLogSlope < 0.0 )
207 aTmpBuf.append( OUStringChar(aMinusSign) ).append( " " );
208 if ( bHasLogSlope )
210 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope), pValueLength );
211 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
213 aTmpBuf.append( aValueString ).append( " " );
216 aTmpBuf.append( mXName ).append(" )");
217 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
219 return aBuf.makeStringAndClear();
222 } // namespace chart
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */