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>
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star
;
32 PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator()
37 ::rtl::math::setNan( & m_fSlope
);
38 ::rtl::math::setNan( & m_fIntercept
);
41 PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
44 // ____ XRegressionCurveCalculator ____
45 void SAL_CALL
PotentialRegressionCurveCalculator::recalculateRegression(
46 const uno::Sequence
< double >& aXValues
,
47 const uno::Sequence
< double >& aYValues
)
49 RegressionCalculationHelper::tDoubleVectorPair
aValues(
50 RegressionCalculationHelper::cleanup(
52 RegressionCalculationHelper::isValidAndBothPositive()));
55 size_t nMax
= aValues
.first
.size();
56 if( nMax
<= 1 ) // at least 2 points
58 aValues
= RegressionCalculationHelper::cleanup(
60 RegressionCalculationHelper::isValidAndXPositiveAndYNegative());
61 nMax
= aValues
.first
.size();
64 ::rtl::math::setNan( & m_fSlope
);
65 ::rtl::math::setNan( & m_fIntercept
);
66 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
72 double fAverageX
= 0.0, fAverageY
= 0.0;
74 for( i
= 0; i
< nMax
; ++i
)
76 fAverageX
+= log( aValues
.first
[i
] );
77 fAverageY
+= log( m_fSign
* aValues
.second
[i
] );
80 const double fN
= static_cast< double >( nMax
);
84 double fQx
= 0.0, fQy
= 0.0, fQxy
= 0.0;
85 for( i
= 0; i
< nMax
; ++i
)
87 double fDeltaX
= log( aValues
.first
[i
] ) - fAverageX
;
88 double fDeltaY
= log( m_fSign
* aValues
.second
[i
] ) - fAverageY
;
90 fQx
+= fDeltaX
* fDeltaX
;
91 fQy
+= fDeltaY
* fDeltaY
;
92 fQxy
+= fDeltaX
* fDeltaY
;
95 m_fSlope
= fQxy
/ fQx
;
96 m_fIntercept
= fAverageY
- m_fSlope
* fAverageX
;
97 m_fCorrelationCoeffitient
= fQxy
/ sqrt( fQx
* fQy
);
99 m_fIntercept
= m_fSign
* exp( m_fIntercept
);
102 double SAL_CALL
PotentialRegressionCurveCalculator::getCurveValue( double x
)
105 ::rtl::math::setNan( & fResult
);
107 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
108 ::rtl::math::isNan( m_fIntercept
)))
110 fResult
= m_fIntercept
* pow( x
, m_fSlope
);
116 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
PotentialRegressionCurveCalculator::getCurveValues(
117 double min
, double max
, ::sal_Int32 nPointCount
,
118 const uno::Reference
< chart2::XScaling
>& xScalingX
,
119 const uno::Reference
< chart2::XScaling
>& xScalingY
,
120 sal_Bool bMaySkipPointsInCalculation
)
122 if( bMaySkipPointsInCalculation
&&
123 isLogarithmicScaling( xScalingX
) &&
124 isLogarithmicScaling( xScalingY
))
127 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
129 aResult
[0].Y
= getCurveValue( min
);
131 aResult
[1].Y
= getCurveValue( max
);
135 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
138 OUString
PotentialRegressionCurveCalculator::ImplGetRepresentation(
139 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
140 sal_Int32 nNumberFormatKey
, sal_Int32
* pFormulaMaxWidth
/* = nullptr */ ) const
142 bool bHasIntercept
= !rtl::math::approxEqual( fabs(m_fIntercept
), 1.0 );
143 OUStringBuffer
aBuf( mYName
+ " = " );
144 sal_Int32 nLineLength
= aBuf
.getLength();
145 sal_Int32 nValueLength
=0;
146 if ( pFormulaMaxWidth
&& *pFormulaMaxWidth
> 0 ) // count nValueLength
148 sal_Int32 nCharMin
= nLineLength
+ mXName
.getLength() + 3; // 3 = "^" + 2 extra characters
149 if ( m_fIntercept
!= 0.0 && m_fSlope
!= 0.0 )
151 if ( m_fIntercept
< 0.0 )
152 nCharMin
+= 2; // "- "
154 nValueLength
= (*pFormulaMaxWidth
- nCharMin
) / 2;
156 if ( nValueLength
== 0 ) // not yet calculated
157 nValueLength
= *pFormulaMaxWidth
- nCharMin
;
158 if ( nValueLength
<= 0 )
162 if( m_fIntercept
== 0.0 )
169 OUStringBuffer
aTmpBuf("");
170 // if nValueLength not calculated then nullptr
171 sal_Int32
* pValueLength
= nValueLength
? &nValueLength
: nullptr;
172 if ( m_fIntercept
< 0.0 ) // add intercept value
173 aTmpBuf
.append( OUStringChar(aMinusSign
) ).append( " " );
176 OUString aValueString
= getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs(m_fIntercept
), pValueLength
);
177 if ( aValueString
!= "1" ) // aValueString may be rounded to 1 if nValueLength is small
179 aTmpBuf
.append( aValueString
).append( " " );
182 if( m_fSlope
!= 0.0 ) // add slope value
184 aTmpBuf
.append( mXName
).append( "^" );
185 aTmpBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
, pValueLength
));
187 addStringToEquation( aBuf
, nLineLength
, aTmpBuf
, pFormulaMaxWidth
);
190 return aBuf
.makeStringAndClear();
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */