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 "LogarithmicRegressionCurveCalculator.hxx"
22 #include "RegressionCalculationHelper.hxx"
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star
;
33 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
37 ::rtl::math::setNan( & m_fSlope
);
38 ::rtl::math::setNan( & m_fIntercept
);
41 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
44 // ____ XRegressionCurve ____
45 void SAL_CALL
LogarithmicRegressionCurveCalculator::recalculateRegression(
46 const uno::Sequence
< double >& aXValues
,
47 const uno::Sequence
< double >& aYValues
)
48 throw (uno::RuntimeException
)
50 RegressionCalculationHelper::tDoubleVectorPair
aValues(
51 RegressionCalculationHelper::cleanup(
53 RegressionCalculationHelper::isValidAndXPositive()));
55 const size_t nMax
= aValues
.first
.size();
58 ::rtl::math::setNan( & m_fSlope
);
59 ::rtl::math::setNan( & m_fIntercept
);
60 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
64 double fAverageX
= 0.0, fAverageY
= 0.0;
66 for( i
= 0; i
< nMax
; ++i
)
68 fAverageX
+= log( aValues
.first
[i
] );
69 fAverageY
+= aValues
.second
[i
];
72 const double fN
= static_cast< double >( nMax
);
76 double fQx
= 0.0, fQy
= 0.0, fQxy
= 0.0;
77 for( i
= 0; i
< nMax
; ++i
)
79 double fDeltaX
= log( aValues
.first
[i
] ) - fAverageX
;
80 double fDeltaY
= aValues
.second
[i
] - fAverageY
;
82 fQx
+= fDeltaX
* fDeltaX
;
83 fQy
+= fDeltaY
* fDeltaY
;
84 fQxy
+= fDeltaX
* fDeltaY
;
87 m_fSlope
= fQxy
/ fQx
;
88 m_fIntercept
= fAverageY
- m_fSlope
* fAverageX
;
89 m_fCorrelationCoeffitient
= fQxy
/ sqrt( fQx
* fQy
);
92 double SAL_CALL
LogarithmicRegressionCurveCalculator::getCurveValue( double x
)
93 throw (lang::IllegalArgumentException
,
94 uno::RuntimeException
)
97 ::rtl::math::setNan( & fResult
);
99 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
100 ::rtl::math::isNan( m_fIntercept
)))
102 fResult
= m_fSlope
* log( x
) + m_fIntercept
;
108 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
LogarithmicRegressionCurveCalculator::getCurveValues(
109 double min
, double max
, ::sal_Int32 nPointCount
,
110 const uno::Reference
< chart2::XScaling
>& xScalingX
,
111 const uno::Reference
< chart2::XScaling
>& xScalingY
,
112 ::sal_Bool bMaySkipPointsInCalculation
)
113 throw (lang::IllegalArgumentException
,
114 uno::RuntimeException
)
116 if( bMaySkipPointsInCalculation
&&
117 isLogarithmicScaling( xScalingX
) &&
118 isLinearScaling( xScalingY
))
121 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
123 aResult
[0].Y
= this->getCurveValue( min
);
125 aResult
[1].Y
= this->getCurveValue( max
);
129 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
132 OUString
LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
133 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
134 ::sal_Int32 nNumberFormatKey
) const
136 OUStringBuffer
aBuf( "f(x) = ");
138 bool bHaveSlope
= false;
140 if( m_fSlope
!= 0.0 )
142 if( ::rtl::math::approxEqual( fabs( m_fSlope
), 1.0 ))
145 aBuf
.append( UC_MINUS_SIGN
);
149 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
));
150 aBuf
.append( UC_SPACE
);
152 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
158 if( m_fIntercept
< 0.0 )
160 aBuf
.append( UC_SPACE
);
161 aBuf
.append( UC_MINUS_SIGN
);
162 aBuf
.append( UC_SPACE
);
163 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs( m_fIntercept
)));
165 else if( m_fIntercept
> 0.0 )
167 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
168 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
173 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
176 return aBuf
.makeStringAndClear();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */