LibreOffice Module chart2 (master) 1
ExponentialRegressionCurveCalculator.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <limits>
23#include <string_view>
24
27#include <SpecialCharacters.hxx>
28
29#include <rtl/math.hxx>
30#include <rtl/ustrbuf.hxx>
31
32using namespace ::com::sun::star;
33
34namespace chart
35{
36
38 : m_fLogSlope(std::numeric_limits<double>::quiet_NaN())
39 , m_fLogIntercept(std::numeric_limits<double>::quiet_NaN())
40 , m_fSign(1.0)
41{
42}
43
45{}
46
47// ____ XRegressionCurveCalculator ____
49 const uno::Sequence< double >& aXValues,
50 const uno::Sequence< double >& aYValues )
51{
54 aXValues, aYValues,
56 m_fSign = 1.0;
57
58 size_t nMax = aValues.first.size();
59 if( nMax <= 1 ) // at least 2 points
60 {
62 aXValues, aYValues,
64 nMax = aValues.first.size();
65 if( nMax <= 1 )
66 {
67 m_fLogSlope = std::numeric_limits<double>::quiet_NaN();
68 m_fLogIntercept = std::numeric_limits<double>::quiet_NaN();
69 m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();// actual it is coefficient of determination
70 return;
71 }
72 m_fSign = -1.0;
73 }
74
75 double fAverageX = 0.0, fAverageY = 0.0;
76 double fLogIntercept = ( mForceIntercept && (m_fSign * mInterceptValue)>0 ) ? log(m_fSign * mInterceptValue) : 0.0;
77 std::vector<double> yVector;
78 yVector.resize(nMax, 0.0);
79
80 size_t i = 0;
81 for( i = 0; i < nMax; ++i )
82 {
83 double yValue = log( m_fSign *aValues.second[i] );
85 {
86 yValue -= fLogIntercept;
87 }
88 else
89 {
90 fAverageX += aValues.first[i];
91 fAverageY += yValue;
92 }
93 yVector[i] = yValue;
94 }
95
96 const double fN = static_cast< double >( nMax );
97 fAverageX /= fN;
98 fAverageY /= fN;
99
100 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
101 for( i = 0; i < nMax; ++i )
102 {
103 double fDeltaX = aValues.first[i] - fAverageX;
104 double fDeltaY = yVector[i] - fAverageY;
105
106 fQx += fDeltaX * fDeltaX;
107 fQy += fDeltaY * fDeltaY;
108 fQxy += fDeltaX * fDeltaY;
109 }
110
111 m_fLogSlope = fQxy / fQx;
112 m_fLogIntercept = mForceIntercept ? fLogIntercept : fAverageY - m_fLogSlope * fAverageX;
113 m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy );
114}
115
117{
118 if( ! ( std::isnan( m_fLogSlope ) ||
119 std::isnan( m_fLogIntercept )))
120 {
121 return m_fSign * exp(m_fLogIntercept + x * m_fLogSlope);
122 }
123
124 return std::numeric_limits<double>::quiet_NaN();
125}
126
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 )
132{
133 if( bMaySkipPointsInCalculation &&
134 isLinearScaling( xScalingX ) &&
135 isLogarithmicScaling( xScalingY ))
136 {
137 // optimize result
139 { max, getCurveValue( max ) } };
140
141 return aResult;
142 }
143
144 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
145}
146
148 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
149 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
150{
151 double fIntercept = exp(m_fLogIntercept);
152 bool bHasSlope = !rtl::math::approxEqual( exp(m_fLogSlope), 1.0 );
153 bool bHasLogSlope = !rtl::math::approxEqual( fabs(m_fLogSlope), 1.0 );
154 bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 ) && fIntercept != 0.0;
155
156 OUStringBuffer aBuf( mYName + " = " );
157 sal_Int32 nLineLength = aBuf.getLength();
158 sal_Int32 nValueLength=0;
159 if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 )
160 { // count characters different from coefficients
161 sal_Int32 nCharMin = nLineLength + 10 + mXName.getLength(); // 10 = "exp( ", " x )" + 2 extra characters
162 if ( m_fSign < 0.0 )
163 nCharMin += 2;
164 if ( fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) )
165 nCharMin += 3; // " + " special case where equation is written exp( a + b x )
166 if ( ( bHasIntercept || fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) ) &&
167 bHasLogSlope )
168 nValueLength = ( *pFormulaMaxWidth - nCharMin ) / 2;
169 else
170 nValueLength = *pFormulaMaxWidth - nCharMin;
171 if ( nValueLength <= 0 )
172 nValueLength = 1;
173 }
174 // temporary buffer
175 OUStringBuffer aTmpBuf("");
176 // if nValueLength not calculated then nullptr
177 sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
178 if ( m_fSign < 0.0 )
179 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
180 if ( bHasIntercept )
181 {
182 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept, pValueLength );
183 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
184 {
185 aTmpBuf.append( aValueString + " " );
186 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
187 aTmpBuf.truncate();
188 }
189 }
190 aTmpBuf.append( "exp( " );
191 if ( !bHasIntercept )
192 {
193 if ( fIntercept == 0.0 || // underflow, a true zero is impossible
194 ( !bHasSlope && m_fLogIntercept != 0.0 ) ) // show logarithmic output, if intercept and slope both are near one
195 { // otherwise drop output of intercept, which is 1 here
196 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept, pValueLength );
197 if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small
198 {
199 aTmpBuf.append( aValueString ).append( (m_fLogSlope < 0.0) ? std::u16string_view(u" ") : std::u16string_view(u" + ") );
200 }
201 }
202 }
203 if ( m_fLogSlope < 0.0 )
204 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
205 if ( bHasLogSlope )
206 {
207 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope), pValueLength );
208 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
209 {
210 aTmpBuf.append( aValueString + " " );
211 }
212 }
213 aTmpBuf.append( mXName + " )");
214 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
215
216 return aBuf.makeStringAndClear();
217}
218
219} // namespace chart
220
221/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Unicode aMinusSign
virtual double SAL_CALL getCurveValue(double x) override
virtual OUString ImplGetRepresentation(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, sal_Int32 *pFormulaLength=nullptr) const override
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
virtual void SAL_CALL recalculateRegression(const css::uno::Sequence< double > &aXValues, const css::uno::Sequence< double > &aYValues) override
static bool isLogarithmicScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
static void addStringToEquation(OUStringBuffer &aStrEquation, sal_Int32 &nLineLength, OUStringBuffer const &aAddString, const sal_Int32 *pMaxLength)
static OUString getFormattedString(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, double fNumber, const sal_Int32 *pStringLength)
static bool isLinearScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
float u
float x
#define max(a, b)
aBuf
std::pair< std::vector< double >, std::vector< double > > tDoubleVectorPair
tDoubleVectorPair cleanup(const css::uno::Sequence< double > &rXValues, const css::uno::Sequence< double > &rYValues, Pred aPred)
takes the given x- and y-values and copies them into the resulting pair, which contains x-values in t...
int i
log
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
unsigned char sal_Bool