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 <PotentialRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
22 #include <SpecialCharacters.hxx>
25 #include <rtl/math.hxx>
26 #include <rtl/ustrbuf.hxx>
28 using namespace ::com::sun::star
;
33 PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator()
34 : m_fSlope(std::numeric_limits
<double>::quiet_NaN())
35 , m_fIntercept(std::numeric_limits
<double>::quiet_NaN())
40 PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
43 // ____ XRegressionCurveCalculator ____
44 void SAL_CALL
PotentialRegressionCurveCalculator::recalculateRegression(
45 const uno::Sequence
< double >& aXValues
,
46 const uno::Sequence
< double >& aYValues
)
48 RegressionCalculationHelper::tDoubleVectorPair
aValues(
49 RegressionCalculationHelper::cleanup(
51 RegressionCalculationHelper::isValidAndBothPositive()));
54 size_t nMax
= aValues
.first
.size();
55 if( nMax
<= 1 ) // at least 2 points
57 aValues
= RegressionCalculationHelper::cleanup(
59 RegressionCalculationHelper::isValidAndXPositiveAndYNegative());
60 nMax
= aValues
.first
.size();
63 m_fSlope
= std::numeric_limits
<double>::quiet_NaN();
64 m_fIntercept
= std::numeric_limits
<double>::quiet_NaN();
65 m_fCorrelationCoefficient
= std::numeric_limits
<double>::quiet_NaN();
71 double fAverageX
= 0.0, fAverageY
= 0.0;
73 for( i
= 0; i
< nMax
; ++i
)
75 fAverageX
+= log( aValues
.first
[i
] );
76 fAverageY
+= log( m_fSign
* aValues
.second
[i
] );
79 const double fN
= static_cast< double >( nMax
);
83 double fQx
= 0.0, fQy
= 0.0, fQxy
= 0.0;
84 for( i
= 0; i
< nMax
; ++i
)
86 double fDeltaX
= log( aValues
.first
[i
] ) - fAverageX
;
87 double fDeltaY
= log( m_fSign
* aValues
.second
[i
] ) - fAverageY
;
89 fQx
+= fDeltaX
* fDeltaX
;
90 fQy
+= fDeltaY
* fDeltaY
;
91 fQxy
+= fDeltaX
* fDeltaY
;
94 m_fSlope
= fQxy
/ fQx
;
95 m_fIntercept
= fAverageY
- m_fSlope
* fAverageX
;
96 m_fCorrelationCoefficient
= fQxy
/ sqrt( fQx
* fQy
);
98 m_fIntercept
= m_fSign
* exp( m_fIntercept
);
101 double SAL_CALL
PotentialRegressionCurveCalculator::getCurveValue( double x
)
103 if( ! ( std::isnan( m_fSlope
) ||
104 std::isnan( m_fIntercept
)))
106 return m_fIntercept
* pow( x
, m_fSlope
);
109 return std::numeric_limits
<double>::quiet_NaN();
112 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
PotentialRegressionCurveCalculator::getCurveValues(
113 double min
, double max
, ::sal_Int32 nPointCount
,
114 const uno::Reference
< chart2::XScaling
>& xScalingX
,
115 const uno::Reference
< chart2::XScaling
>& xScalingY
,
116 sal_Bool bMaySkipPointsInCalculation
)
118 if( bMaySkipPointsInCalculation
&&
119 isLogarithmicScaling( xScalingX
) &&
120 isLogarithmicScaling( xScalingY
))
123 uno::Sequence
< geometry::RealPoint2D
> aResult
{ { min
, getCurveValue( min
) },
124 { max
, getCurveValue( max
) } };
128 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
131 OUString
PotentialRegressionCurveCalculator::ImplGetRepresentation(
132 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
133 sal_Int32 nNumberFormatKey
, sal_Int32
* pFormulaMaxWidth
/* = nullptr */ ) const
135 bool bHasIntercept
= !rtl::math::approxEqual( fabs(m_fIntercept
), 1.0 );
136 OUStringBuffer
aBuf( mYName
+ " = " );
137 sal_Int32 nLineLength
= aBuf
.getLength();
138 sal_Int32 nValueLength
=0;
139 if ( pFormulaMaxWidth
&& *pFormulaMaxWidth
> 0 ) // count nValueLength
141 sal_Int32 nCharMin
= nLineLength
+ mXName
.getLength() + 3; // 3 = "^" + 2 extra characters
142 if ( m_fIntercept
!= 0.0 && m_fSlope
!= 0.0 )
144 if ( m_fIntercept
< 0.0 )
145 nCharMin
+= 2; // "- "
147 nValueLength
= (*pFormulaMaxWidth
- nCharMin
) / 2;
149 if ( nValueLength
== 0 ) // not yet calculated
150 nValueLength
= *pFormulaMaxWidth
- nCharMin
;
151 if ( nValueLength
<= 0 )
155 if( m_fIntercept
== 0.0 )
162 OUStringBuffer
aTmpBuf("");
163 // if nValueLength not calculated then nullptr
164 sal_Int32
* pValueLength
= nValueLength
? &nValueLength
: nullptr;
165 if ( m_fIntercept
< 0.0 ) // add intercept value
166 aTmpBuf
.append( OUStringChar(aMinusSign
) + " " );
169 OUString aValueString
= getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs(m_fIntercept
), pValueLength
);
170 if ( aValueString
!= "1" ) // aValueString may be rounded to 1 if nValueLength is small
172 aTmpBuf
.append( aValueString
+ " " );
175 if( m_fSlope
!= 0.0 ) // add slope value
177 aTmpBuf
.append( mXName
+ "^" +
178 getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
, pValueLength
));
180 addStringToEquation( aBuf
, nLineLength
, aTmpBuf
, pFormulaMaxWidth
);
183 return aBuf
.makeStringAndClear();
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */