/src/libreoffice/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx
Line | Count | Source |
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 | | |
25 | | #include <ExponentialRegressionCurveCalculator.hxx> |
26 | | #include <RegressionCalculationHelper.hxx> |
27 | | #include <SpecialCharacters.hxx> |
28 | | |
29 | | #include <rtl/math.hxx> |
30 | | #include <rtl/ustrbuf.hxx> |
31 | | |
32 | | using namespace ::com::sun::star; |
33 | | |
34 | | namespace chart |
35 | | { |
36 | | |
37 | | ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() |
38 | 0 | : m_fLogSlope(std::numeric_limits<double>::quiet_NaN()) |
39 | 0 | , m_fLogIntercept(std::numeric_limits<double>::quiet_NaN()) |
40 | 0 | , m_fSign(1.0) |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator() |
45 | 0 | {} |
46 | | |
47 | | // ____ XRegressionCurveCalculator ____ |
48 | | void SAL_CALL ExponentialRegressionCurveCalculator::recalculateRegression( |
49 | | const uno::Sequence< double >& aXValues, |
50 | | const uno::Sequence< double >& aYValues ) |
51 | 0 | { |
52 | 0 | RegressionCalculationHelper::tDoubleVectorPair aValues( |
53 | 0 | RegressionCalculationHelper::cleanup( |
54 | 0 | aXValues, aYValues, |
55 | 0 | RegressionCalculationHelper::isValidAndYPositive())); |
56 | 0 | m_fSign = 1.0; |
57 | |
|
58 | 0 | size_t nMax = aValues.first.size(); |
59 | 0 | if( nMax <= 1 ) // at least 2 points |
60 | 0 | { |
61 | 0 | aValues = RegressionCalculationHelper::cleanup( |
62 | 0 | aXValues, aYValues, |
63 | 0 | RegressionCalculationHelper::isValidAndYNegative()); |
64 | 0 | nMax = aValues.first.size(); |
65 | 0 | if( nMax <= 1 ) |
66 | 0 | { |
67 | 0 | m_fLogSlope = std::numeric_limits<double>::quiet_NaN(); |
68 | 0 | m_fLogIntercept = std::numeric_limits<double>::quiet_NaN(); |
69 | 0 | m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();// actual it is coefficient of determination |
70 | 0 | return; |
71 | 0 | } |
72 | 0 | m_fSign = -1.0; |
73 | 0 | } |
74 | | |
75 | 0 | double fAverageX = 0.0, fAverageY = 0.0; |
76 | 0 | double fLogIntercept = ( mForceIntercept && (m_fSign * mInterceptValue)>0 ) ? log(m_fSign * mInterceptValue) : 0.0; |
77 | 0 | std::vector<double> yVector; |
78 | 0 | yVector.resize(nMax, 0.0); |
79 | |
|
80 | 0 | size_t i = 0; |
81 | 0 | for( i = 0; i < nMax; ++i ) |
82 | 0 | { |
83 | 0 | double yValue = log( m_fSign *aValues.second[i] ); |
84 | 0 | if (mForceIntercept) |
85 | 0 | { |
86 | 0 | yValue -= fLogIntercept; |
87 | 0 | } |
88 | 0 | else |
89 | 0 | { |
90 | 0 | fAverageX += aValues.first[i]; |
91 | 0 | fAverageY += yValue; |
92 | 0 | } |
93 | 0 | yVector[i] = yValue; |
94 | 0 | } |
95 | |
|
96 | 0 | const double fN = static_cast< double >( nMax ); |
97 | 0 | fAverageX /= fN; |
98 | 0 | fAverageY /= fN; |
99 | |
|
100 | 0 | double fQx = 0.0, fQy = 0.0, fQxy = 0.0; |
101 | 0 | for( i = 0; i < nMax; ++i ) |
102 | 0 | { |
103 | 0 | double fDeltaX = aValues.first[i] - fAverageX; |
104 | 0 | double fDeltaY = yVector[i] - fAverageY; |
105 | |
|
106 | 0 | fQx += fDeltaX * fDeltaX; |
107 | 0 | fQy += fDeltaY * fDeltaY; |
108 | 0 | fQxy += fDeltaX * fDeltaY; |
109 | 0 | } |
110 | |
|
111 | 0 | m_fLogSlope = fQxy / fQx; |
112 | 0 | m_fLogIntercept = mForceIntercept ? fLogIntercept : fAverageY - m_fLogSlope * fAverageX; |
113 | 0 | m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy ); |
114 | 0 | } |
115 | | |
116 | | double SAL_CALL ExponentialRegressionCurveCalculator::getCurveValue( double x ) |
117 | 0 | { |
118 | 0 | if( ! ( std::isnan( m_fLogSlope ) || |
119 | 0 | std::isnan( m_fLogIntercept ))) |
120 | 0 | { |
121 | 0 | return m_fSign * exp(m_fLogIntercept + x * m_fLogSlope); |
122 | 0 | } |
123 | | |
124 | 0 | return std::numeric_limits<double>::quiet_NaN(); |
125 | 0 | } |
126 | | |
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 ) |
132 | 0 | { |
133 | 0 | if( bMaySkipPointsInCalculation && |
134 | 0 | isLinearScaling( xScalingX ) && |
135 | 0 | isLogarithmicScaling( xScalingY )) |
136 | 0 | { |
137 | | // optimize result |
138 | 0 | uno::Sequence< geometry::RealPoint2D > aResult{ { min, getCurveValue( min ) }, |
139 | 0 | { max, getCurveValue( max ) } }; |
140 | |
|
141 | 0 | return aResult; |
142 | 0 | } |
143 | | |
144 | 0 | return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation ); |
145 | 0 | } |
146 | | |
147 | | OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation( |
148 | | const uno::Reference< util::XNumberFormatter >& xNumFormatter, |
149 | | sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const |
150 | 0 | { |
151 | 0 | double fIntercept = exp(m_fLogIntercept); |
152 | 0 | bool bHasSlope = !rtl::math::approxEqual( exp(m_fLogSlope), 1.0 ); |
153 | 0 | bool bHasLogSlope = !rtl::math::approxEqual( fabs(m_fLogSlope), 1.0 ); |
154 | 0 | bool bHasIntercept = !rtl::math::approxEqual( fIntercept, 1.0 ) && fIntercept != 0.0; |
155 | |
|
156 | 0 | OUStringBuffer aBuf( mYName + " = " ); |
157 | 0 | sal_Int32 nLineLength = aBuf.getLength(); |
158 | 0 | sal_Int32 nValueLength=0; |
159 | 0 | if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) |
160 | 0 | { // count characters different from coefficients |
161 | 0 | sal_Int32 nCharMin = nLineLength + 10 + mXName.getLength(); // 10 = "exp( ", " x )" + 2 extra characters |
162 | 0 | if ( m_fSign < 0.0 ) |
163 | 0 | nCharMin += 2; |
164 | 0 | if ( fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) ) |
165 | 0 | nCharMin += 3; // " + " special case where equation is written exp( a + b x ) |
166 | 0 | if ( ( bHasIntercept || fIntercept == 0.0 || ( !bHasSlope && m_fLogIntercept != 0.0 ) ) && |
167 | 0 | bHasLogSlope ) |
168 | 0 | nValueLength = ( *pFormulaMaxWidth - nCharMin ) / 2; |
169 | 0 | else |
170 | 0 | nValueLength = *pFormulaMaxWidth - nCharMin; |
171 | 0 | if ( nValueLength <= 0 ) |
172 | 0 | nValueLength = 1; |
173 | 0 | } |
174 | | // temporary buffer |
175 | 0 | OUStringBuffer aTmpBuf(""); |
176 | | // if nValueLength not calculated then nullptr |
177 | 0 | sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; |
178 | 0 | if ( m_fSign < 0.0 ) |
179 | 0 | aTmpBuf.append( OUStringChar(aMinusSign) + " " ); |
180 | 0 | if ( bHasIntercept ) |
181 | 0 | { |
182 | 0 | OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept, pValueLength ); |
183 | 0 | if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small |
184 | 0 | { |
185 | 0 | aTmpBuf.append( aValueString + " " ); |
186 | 0 | addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); |
187 | 0 | aTmpBuf.truncate(); |
188 | 0 | } |
189 | 0 | } |
190 | 0 | aTmpBuf.append( "exp( " ); |
191 | 0 | if ( !bHasIntercept ) |
192 | 0 | { |
193 | 0 | if ( fIntercept == 0.0 || // underflow, a true zero is impossible |
194 | 0 | ( !bHasSlope && m_fLogIntercept != 0.0 ) ) // show logarithmic output, if intercept and slope both are near one |
195 | 0 | { // otherwise drop output of intercept, which is 1 here |
196 | 0 | OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, m_fLogIntercept, pValueLength ); |
197 | 0 | if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small |
198 | 0 | { |
199 | 0 | aTmpBuf.append( aValueString ).append( (m_fLogSlope < 0.0) ? std::u16string_view(u" ") : std::u16string_view(u" + ") ); |
200 | 0 | } |
201 | 0 | } |
202 | 0 | } |
203 | 0 | if ( m_fLogSlope < 0.0 ) |
204 | 0 | aTmpBuf.append( OUStringChar(aMinusSign) + " " ); |
205 | 0 | if ( bHasLogSlope ) |
206 | 0 | { |
207 | 0 | OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope), pValueLength ); |
208 | 0 | if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small |
209 | 0 | { |
210 | 0 | aTmpBuf.append( aValueString + " " ); |
211 | 0 | } |
212 | 0 | } |
213 | 0 | aTmpBuf.append( mXName + " )"); |
214 | 0 | addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); |
215 | |
|
216 | 0 | return aBuf.makeStringAndClear(); |
217 | 0 | } |
218 | | |
219 | | } // namespace chart |
220 | | |
221 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |