1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ExponentialRegressionCurveCalculator.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "ExponentialRegressionCurveCalculator.hxx"
35 #include "RegressionCalculationHelper.hxx"
37 #include <rtl/math.hxx>
38 #include <rtl/ustrbuf.hxx>
40 using namespace ::com::sun::star
;
42 using ::rtl::OUString
;
43 using ::rtl::OUStringBuffer
;
48 ExponentialRegressionCurveCalculator::ExponentialRegressionCurveCalculator() :
52 ::rtl::math::setNan( & m_fSlope
);
53 ::rtl::math::setNan( & m_fIntercept
);
56 ExponentialRegressionCurveCalculator::~ExponentialRegressionCurveCalculator()
59 // ____ XRegressionCurveCalculator ____
60 void SAL_CALL
ExponentialRegressionCurveCalculator::recalculateRegression(
61 const uno::Sequence
< double >& aXValues
,
62 const uno::Sequence
< double >& aYValues
)
63 throw (uno::RuntimeException
)
65 RegressionCalculationHelper::tDoubleVectorPair
aValues(
66 RegressionCalculationHelper::cleanup(
68 RegressionCalculationHelper::isValidAndYPositive()));
70 const size_t nMax
= aValues
.first
.size();
73 ::rtl::math::setNan( & m_fSlope
);
74 ::rtl::math::setNan( & m_fIntercept
);
75 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
79 double fAverageX
= 0.0, fAverageY
= 0.0;
81 for( i
= 0; i
< nMax
; ++i
)
83 fAverageX
+= aValues
.first
[i
];
84 fAverageY
+= log( aValues
.second
[i
] );
87 const double fN
= static_cast< double >( nMax
);
91 double fQx
= 0.0, fQy
= 0.0, fQxy
= 0.0;
92 for( i
= 0; i
< nMax
; ++i
)
94 double fDeltaX
= aValues
.first
[i
] - fAverageX
;
95 double fDeltaY
= log( aValues
.second
[i
] ) - fAverageY
;
97 fQx
+= fDeltaX
* fDeltaX
;
98 fQy
+= fDeltaY
* fDeltaY
;
99 fQxy
+= fDeltaX
* fDeltaY
;
102 m_fSlope
= fQxy
/ fQx
;
103 m_fIntercept
= fAverageY
- m_fSlope
* fAverageX
;
104 m_fCorrelationCoeffitient
= fQxy
/ sqrt( fQx
* fQy
);
106 m_fSlope
= exp( m_fSlope
);
107 m_fIntercept
= exp( m_fIntercept
);
110 double SAL_CALL
ExponentialRegressionCurveCalculator::getCurveValue( double x
)
111 throw (lang::IllegalArgumentException
,
112 uno::RuntimeException
)
115 ::rtl::math::setNan( & fResult
);
117 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
118 ::rtl::math::isNan( m_fIntercept
)))
120 fResult
= m_fIntercept
* pow( m_fSlope
, x
);
126 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
ExponentialRegressionCurveCalculator::getCurveValues(
127 double min
, double max
, ::sal_Int32 nPointCount
,
128 const uno::Reference
< chart2::XScaling
>& xScalingX
,
129 const uno::Reference
< chart2::XScaling
>& xScalingY
,
130 ::sal_Bool bMaySkipPointsInCalculation
)
131 throw (lang::IllegalArgumentException
,
132 uno::RuntimeException
)
134 if( bMaySkipPointsInCalculation
&&
135 isLinearScaling( xScalingX
) &&
136 isLogarithmicScaling( xScalingY
))
139 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
141 aResult
[0].Y
= this->getCurveValue( min
);
143 aResult
[1].Y
= this->getCurveValue( max
);
148 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
152 OUString
ExponentialRegressionCurveCalculator::ImplGetRepresentation(
153 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
154 ::sal_Int32 nNumberFormatKey
) const
156 OUStringBuffer
aBuf( C2U( "f(x) = " ));
158 if( m_fIntercept
== 0.0 ||
161 aBuf
.append( sal_Unicode( '0' ));
163 else if( rtl::math::approxEqual( m_fSlope
, 1.0 ) )
165 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
169 if( ! rtl::math::approxEqual( m_fIntercept
, 1.0 ) )
171 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
172 aBuf
.append( sal_Unicode( 0x00b7 ));
176 aBuf
.append( sal_Unicode( '(' ));
177 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
));
179 aBuf
.append( sal_Unicode( ')' ));
180 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "^x" ));
183 return aBuf
.makeStringAndClear();