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 <PolynomialRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
22 
23 #include <cmath>
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
26 
27 #include <SpecialCharacters.hxx>
28 
29 using namespace com::sun::star;
30 
31 namespace chart
32 {
33 
lcl_GetDotProduct(std::vector<double> & aVec1,std::vector<double> & aVec2)34 static double lcl_GetDotProduct(std::vector<double>& aVec1, std::vector<double>& aVec2)
35 {
36     double fResult = 0.0;
37     assert(aVec1.size() == aVec2.size());
38     for (size_t i = 0; i < aVec1.size(); ++i)
39         fResult += aVec1[i] * aVec2[i];
40     return fResult;
41 }
42 
PolynomialRegressionCurveCalculator()43 PolynomialRegressionCurveCalculator::PolynomialRegressionCurveCalculator()
44 {}
45 
~PolynomialRegressionCurveCalculator()46 PolynomialRegressionCurveCalculator::~PolynomialRegressionCurveCalculator()
47 {}
48 
computeCorrelationCoefficient(RegressionCalculationHelper::tDoubleVectorPair & rValues,const sal_Int32 aNoValues,double yAverage)49 void PolynomialRegressionCurveCalculator::computeCorrelationCoefficient(
50     RegressionCalculationHelper::tDoubleVectorPair& rValues,
51     const sal_Int32 aNoValues,
52     double yAverage )
53 {
54     double aSumError = 0.0;
55     double aSumTotal = 0.0;
56     double aSumYpred2 = 0.0;
57 
58     for( sal_Int32 i = 0; i < aNoValues; i++ )
59     {
60         double xValue = rValues.first[i];
61         double yActual = rValues.second[i];
62         double yPredicted = getCurveValue( xValue );
63         aSumTotal += (yActual - yAverage) * (yActual - yAverage);
64         aSumError += (yActual - yPredicted) * (yActual - yPredicted);
65         if(mForceIntercept)
66             aSumYpred2 += (yPredicted - mInterceptValue) * (yPredicted - mInterceptValue);
67     }
68 
69     double aRSquared = 0.0;
70     if(mForceIntercept)
71     {
72         if (auto const div = aSumError + aSumYpred2)
73         {
74             aRSquared = aSumYpred2 / div;
75         }
76     }
77     else if (aSumTotal != 0.0)
78     {
79         aRSquared = 1.0 - (aSumError / aSumTotal);
80     }
81 
82     if (aRSquared > 0.0)
83         m_fCorrelationCoeffitient = std::sqrt(aRSquared);
84     else
85         m_fCorrelationCoeffitient = 0.0;
86 }
87 
88 // ____ XRegressionCurveCalculator ____
recalculateRegression(const uno::Sequence<double> & aXValues,const uno::Sequence<double> & aYValues)89 void SAL_CALL PolynomialRegressionCurveCalculator::recalculateRegression(
90     const uno::Sequence< double >& aXValues,
91     const uno::Sequence< double >& aYValues )
92 {
93     rtl::math::setNan(&m_fCorrelationCoeffitient);
94 
95     RegressionCalculationHelper::tDoubleVectorPair aValues(
96         RegressionCalculationHelper::cleanup( aXValues, aYValues, RegressionCalculationHelper::isValid()));
97 
98     const sal_Int32 aNoValues = aValues.first.size();
99 
100     const sal_Int32 aNoPowers = mForceIntercept ? mDegree : mDegree + 1;
101 
102     mCoefficients.clear();
103     mCoefficients.resize(aNoPowers, 0.0);
104 
105     double yAverage = 0.0;
106 
107     std::vector<double> yVector;
108     yVector.resize(aNoValues, 0.0);
109 
110     for(sal_Int32 i = 0; i < aNoValues; i++)
111     {
112         double yValue = aValues.second[i];
113         if (mForceIntercept)
114             yValue -= mInterceptValue;
115         yVector[i] = yValue;
116         yAverage += yValue;
117     }
118     if (aNoValues != 0)
119     {
120         yAverage /= aNoValues;
121     }
122 
123     // Special case for single variable regression like in LINEST
124     // implementation in Calc.
125     if (mDegree == 1)
126     {
127         std::vector<double> xVector;
128         xVector.resize(aNoValues, 0.0);
129         double xAverage = 0.0;
130 
131         for(sal_Int32 i = 0; i < aNoValues; ++i)
132         {
133             double xValue = aValues.first[i];
134             xVector[i] = xValue;
135             xAverage += xValue;
136         }
137         if (aNoValues != 0)
138         {
139             xAverage /= aNoValues;
140         }
141 
142         if (!mForceIntercept)
143         {
144             for (sal_Int32 i = 0; i < aNoValues; ++i)
145             {
146                 xVector[i] -= xAverage;
147                 yVector[i] -= yAverage;
148             }
149         }
150         double fSumXY = lcl_GetDotProduct(xVector, yVector);
151         double fSumX2 = lcl_GetDotProduct(xVector, xVector);
152 
153         double fSlope = fSumXY / fSumX2;
154 
155         if (!mForceIntercept)
156         {
157             mInterceptValue = ::rtl::math::approxSub(yAverage, fSlope * xAverage);
158             mCoefficients[0] = mInterceptValue;
159             mCoefficients[1] = fSlope;
160         }
161         else
162         {
163             mCoefficients[0] = fSlope;
164             mCoefficients.insert(mCoefficients.begin(), mInterceptValue);
165         }
166 
167         computeCorrelationCoefficient(aValues, aNoValues, yAverage);
168         return;
169     }
170 
171     std::vector<double> aQRTransposed;
172     aQRTransposed.resize(aNoValues * aNoPowers, 0.0);
173 
174     for(sal_Int32 j = 0; j < aNoPowers; j++)
175     {
176         sal_Int32 aPower = mForceIntercept ? j+1 : j;
177         sal_Int32 aColumnIndex = j * aNoValues;
178         for(sal_Int32 i = 0; i < aNoValues; i++)
179         {
180             double xValue = aValues.first[i];
181             aQRTransposed[i + aColumnIndex] = std::pow(xValue, static_cast<int>(aPower));
182         }
183     }
184 
185     // QR decomposition - based on org.apache.commons.math.linear.QRDecomposition from apache commons math (ASF)
186     sal_Int32 aMinorSize = std::min(aNoValues, aNoPowers);
187 
188     std::vector<double> aDiagonal;
189     aDiagonal.resize(aMinorSize, 0.0);
190 
191     // Calculate Householder reflectors
192     for (sal_Int32 aMinor = 0; aMinor < aMinorSize; aMinor++)
193     {
194         double aNormSqr = 0.0;
195         for (sal_Int32 x = aMinor; x < aNoValues; x++)
196         {
197             double c = aQRTransposed[x + aMinor * aNoValues];
198             aNormSqr += c * c;
199         }
200 
201         double a;
202 
203         if (aQRTransposed[aMinor + aMinor * aNoValues] > 0.0)
204             a = -std::sqrt(aNormSqr);
205         else
206             a = std::sqrt(aNormSqr);
207 
208         aDiagonal[aMinor] = a;
209 
210         if (a != 0.0)
211         {
212             aQRTransposed[aMinor + aMinor * aNoValues] -= a;
213 
214             for (sal_Int32 aColumn = aMinor + 1; aColumn < aNoPowers; aColumn++)
215             {
216                 double alpha = 0.0;
217                 for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
218                 {
219                     alpha -= aQRTransposed[aRow + aColumn * aNoValues] * aQRTransposed[aRow + aMinor * aNoValues];
220                 }
221                 alpha /= a * aQRTransposed[aMinor + aMinor * aNoValues];
222 
223                 for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
224                 {
225                     aQRTransposed[aRow + aColumn * aNoValues] -= alpha * aQRTransposed[aRow + aMinor * aNoValues];
226                 }
227             }
228         }
229     }
230 
231     // Solve the linear equation
232     for (sal_Int32 aMinor = 0; aMinor < aMinorSize; aMinor++)
233     {
234         double aDotProduct = 0;
235 
236         for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
237         {
238             aDotProduct += yVector[aRow] * aQRTransposed[aRow + aMinor * aNoValues];
239         }
240         aDotProduct /= aDiagonal[aMinor] * aQRTransposed[aMinor + aMinor * aNoValues];
241 
242         for (sal_Int32 aRow = aMinor; aRow < aNoValues; aRow++)
243         {
244             yVector[aRow] += aDotProduct * aQRTransposed[aRow + aMinor * aNoValues];
245         }
246 
247     }
248 
249     for (sal_Int32 aRow = aDiagonal.size() - 1; aRow >= 0; aRow--)
250     {
251         yVector[aRow] /= aDiagonal[aRow];
252         double yRow = yVector[aRow];
253         mCoefficients[aRow] = yRow;
254 
255         for (sal_Int32 i = 0; i < aRow; i++)
256         {
257             yVector[i] -= yRow * aQRTransposed[i + aRow * aNoValues];
258         }
259     }
260 
261     if(mForceIntercept)
262     {
263         mCoefficients.insert(mCoefficients.begin(), mInterceptValue);
264     }
265 
266     // Calculate correlation coefficient
267     computeCorrelationCoefficient(aValues, aNoValues, yAverage);
268 }
269 
getCurveValue(double x)270 double SAL_CALL PolynomialRegressionCurveCalculator::getCurveValue( double x )
271 {
272     double fResult;
273     rtl::math::setNan(&fResult);
274 
275     if (mCoefficients.empty())
276     {
277         return fResult;
278     }
279 
280     sal_Int32 aNoCoefficients = static_cast<sal_Int32>(mCoefficients.size());
281 
282     // Horner's method
283     fResult = 0.0;
284     for (sal_Int32 i = aNoCoefficients - 1; i >= 0; i--)
285     {
286         fResult = mCoefficients[i] + (x * fResult);
287     }
288     return fResult;
289 }
290 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,sal_Int32 nNumberFormatKey,sal_Int32 * pFormulaMaxWidth) const291 OUString PolynomialRegressionCurveCalculator::ImplGetRepresentation(
292     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
293     sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
294 {
295     OUStringBuffer aBuf( mYName + " = " );
296 
297     sal_Int32 nValueLength=0;
298     sal_Int32 aLastIndex = mCoefficients.size() - 1;
299 
300     if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 )
301     {
302         sal_Int32 nCharMin = aBuf.getLength(); // count characters different from coefficients
303         double nCoefficients = aLastIndex + 1.0; // number of coefficients
304         for (sal_Int32 i = aLastIndex; i >= 0; i--)
305         {
306             double aValue = mCoefficients[i];
307             if ( aValue == 0.0 )
308             { // do not count coeffitient if it is 0
309                 nCoefficients --;
310                 continue;
311             }
312             if ( rtl::math::approxEqual( fabs( aValue ) , 1.0 ) )
313             { // do not count coeffitient if it is 1
314                 nCoefficients --;
315                 if ( i == 0 ) // intercept = 1
316                     nCharMin ++;
317             }
318             if ( i != aLastIndex )
319                 nCharMin += 3; // " + "
320             if ( i > 0 )
321             {
322                 nCharMin += mXName.getLength() + 1; // " x"
323                 if ( i > 1 )
324                     nCharMin +=1; // "^i"
325                 if ( i >= 10 )
326                     nCharMin ++; // 2 digits for i
327             }
328         }
329         nValueLength = ( *pFormulaMaxWidth - nCharMin ) / nCoefficients;
330         if ( nValueLength <= 0 )
331             nValueLength = 1;
332     }
333 
334     bool bFindValue = false;
335     sal_Int32 nLineLength = aBuf.getLength();
336     for (sal_Int32 i = aLastIndex; i >= 0; i--)
337     {
338         double aValue = mCoefficients[i];
339         OUStringBuffer aTmpBuf(""); // temporary buffer
340         if (aValue == 0.0)
341         {
342             continue;
343         }
344         else if (aValue < 0.0)
345         {
346             if ( bFindValue ) // if it is not the first aValue
347                 aTmpBuf.append( " " );
348             aTmpBuf.append( OUStringChar(aMinusSign) ).append(" ");
349             aValue = - aValue;
350         }
351         else
352         {
353             if ( bFindValue ) // if it is not the first aValue
354                 aTmpBuf.append( " + " );
355         }
356         bFindValue = true;
357 
358         // if nValueLength not calculated then nullptr
359         sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
360         OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, aValue, pValueLength );
361         if ( i == 0 || aValueString != "1" )  // aValueString may be rounded to 1 if nValueLength is small
362         {
363             aTmpBuf.append( aValueString );
364             if ( i > 0 ) // insert blank between coefficient and x
365                 aTmpBuf.append( " " );
366         }
367 
368         if(i > 0)
369         {
370             aTmpBuf.append( mXName );
371             if (i > 1)
372             {
373                 if (i < 10) // simple case if only one digit
374                     aTmpBuf.append( aSuperscriptFigures[ i ] );
375                 else
376                 {
377                     OUString aValueOfi = OUString::number( i );
378                     for ( sal_Int32 n = 0; n < aValueOfi.getLength() ; n++ )
379                     {
380                         sal_Int32 nIndex = aValueOfi[n] - u'0';
381                         aTmpBuf.append( aSuperscriptFigures[ nIndex ] );
382                     }
383                 }
384             }
385         }
386         addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
387     }
388     if ( aBuf.toString() == ( mYName + " = ") )
389         aBuf.append( "0" );
390 
391     return aBuf.makeStringAndClear();
392 }
393 
394 } //  namespace chart
395 
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
397