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 <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
;
32 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator()
34 , m_fLogIntercept(0.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(
52 RegressionCalculationHelper::isValidAndYPositive()));
55 size_t nMax
= aValues
.first
.size();
56 if( nMax
<= 1 ) // at least 2 points
58 aValues
= RegressionCalculationHelper::cleanup(
60 RegressionCalculationHelper::isValidAndYNegative());
61 nMax
= aValues
.first
.size();
64 ::rtl::math::setNan( & m_fLogSlope
);
65 ::rtl::math::setNan( & m_fLogIntercept
);
66 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);// actual it is coefficient of determination
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);
78 for( i
= 0; i
< nMax
; ++i
)
80 double yValue
= log( m_fSign
*aValues
.second
[i
] );
83 yValue
-= fLogIntercept
;
87 fAverageX
+= aValues
.first
[i
];
93 const double fN
= static_cast< double >( nMax
);
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
)
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
);
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( 2 );
140 aResult
[0].Y
= getCurveValue( min
);
142 aResult
[1].Y
= getCurveValue( max
);
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
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 ) ) &&
171 nValueLength
= ( *pFormulaMaxWidth
- nCharMin
) / 2;
173 nValueLength
= *pFormulaMaxWidth
- nCharMin
;
174 if ( nValueLength
<= 0 )
178 OUStringBuffer
aTmpBuf("");
179 // if nValueLength not calculated then nullptr
180 sal_Int32
* pValueLength
= nValueLength
? &nValueLength
: nullptr;
182 aTmpBuf
.append( OUStringChar(aMinusSign
) ).append( " " );
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
);
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( " " );
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();
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */