1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
23 #include <string_view>
25 #include <ExponentialRegressionCurveCalculator.hxx>
26 #include <RegressionCalculationHelper.hxx>
27 #include <SpecialCharacters.hxx>
29 #include <rtl/math.hxx>
30 #include <rtl/ustrbuf.hxx>
32 using namespace ::com::sun::star
;
37 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator()
38 : m_fLogSlope(std::numeric_limits
<double>::quiet_NaN())
39 , m_fLogIntercept(std::numeric_limits
<double>::quiet_NaN())
44 ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
47 // ____ XRegressionCurveCalculator ____
48 void SAL_CALL
ExponentialRegressionCurveCalculator::recalculateRegression(
49 const uno::Sequence
< double >& aXValues
,
50 const uno::Sequence
< double >& aYValues
)
52 RegressionCalculationHelper::tDoubleVectorPair
aValues(
53 RegressionCalculationHelper::cleanup(
55 RegressionCalculationHelper::isValidAndYPositive()));
58 size_t nMax
= aValues
.first
.size();
59 if( nMax
<= 1 ) // at least 2 points
61 aValues
= RegressionCalculationHelper::cleanup(
63 RegressionCalculationHelper::isValidAndYNegative());
64 nMax
= aValues
.first
.size();
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
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);
81 for( i
= 0; i
< nMax
; ++i
)
83 double yValue
= log( m_fSign
*aValues
.second
[i
] );
86 yValue
-= fLogIntercept
;
90 fAverageX
+= aValues
.first
[i
];
96 const double fN
= static_cast< double >( nMax
);
100 double fQx
= 0.0, fQy
= 0.0, fQxy
= 0.0;
101 for( i
= 0; i
< nMax
; ++i
)
103 double fDeltaX
= aValues
.first
[i
] - fAverageX
;
104 double fDeltaY
= yVector
[i
] - fAverageY
;
106 fQx
+= fDeltaX
* fDeltaX
;
107 fQy
+= fDeltaY
* fDeltaY
;
108 fQxy
+= fDeltaX
* fDeltaY
;
111 m_fLogSlope
= fQxy
/ fQx
;
112 m_fLogIntercept
= mForceIntercept
? fLogIntercept
: fAverageY
- m_fLogSlope
* fAverageX
;
113 m_fCorrelationCoefficient
= fQxy
/ sqrt( fQx
* fQy
);
116 double SAL_CALL
ExponentialRegressionCurveCalculator::getCurveValue( double x
)
118 if( ! ( std::isnan( m_fLogSlope
) ||
119 std::isnan( m_fLogIntercept
)))
121 return m_fSign
* exp(m_fLogIntercept
+ x
* m_fLogSlope
);
124 return std::numeric_limits
<double>::quiet_NaN();
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
))
138 uno::Sequence
< geometry::RealPoint2D
> aResult
{ { min
, getCurveValue( min
) },
139 { max
, getCurveValue( max
) } };
144 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
147 OUString
ExponentialRegressionCurveCalculator::ImplGetRepresentation(
148 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
149 sal_Int32 nNumberFormatKey
, sal_Int32
* pFormulaMaxWidth
/* = nullptr */ ) const
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;
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
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 ) ) &&
168 nValueLength
= ( *pFormulaMaxWidth
- nCharMin
) / 2;
170 nValueLength
= *pFormulaMaxWidth
- nCharMin
;
171 if ( nValueLength
<= 0 )
175 OUStringBuffer
aTmpBuf("");
176 // if nValueLength not calculated then nullptr
177 sal_Int32
* pValueLength
= nValueLength
? &nValueLength
: nullptr;
179 aTmpBuf
.append( OUStringChar(aMinusSign
) + " " );
182 OUString aValueString
= getFormattedString( xNumFormatter
, nNumberFormatKey
, fIntercept
, pValueLength
);
183 if ( aValueString
!= "1" ) // aValueString may be rounded to 1 if nValueLength is small
185 aTmpBuf
.append( aValueString
+ " " );
186 addStringToEquation( aBuf
, nLineLength
, aTmpBuf
, pFormulaMaxWidth
);
190 aTmpBuf
.append( "exp( " );
191 if ( !bHasIntercept
)
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
199 aTmpBuf
.append( aValueString
).append( (m_fLogSlope
< 0.0) ? std::u16string_view(u
" ") : std::u16string_view(u
" + ") );
203 if ( m_fLogSlope
< 0.0 )
204 aTmpBuf
.append( OUStringChar(aMinusSign
) + " " );
207 OUString aValueString
= getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs(m_fLogSlope
), pValueLength
);
208 if ( aValueString
!= "1" ) // aValueString may be rounded to 1 if nValueLength is small
210 aTmpBuf
.append( aValueString
+ " " );
213 aTmpBuf
.append( mXName
+ " )");
214 addStringToEquation( aBuf
, nLineLength
, aTmpBuf
, pFormulaMaxWidth
);
216 return aBuf
.makeStringAndClear();
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */