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: LogarithmicRegressionCurveCalculator.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 "LogarithmicRegressionCurveCalculator.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 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
52 ::rtl::math::setNan( & m_fSlope
);
53 ::rtl::math::setNan( & m_fIntercept
);
56 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
59 // ____ XRegressionCurve ____
60 void SAL_CALL
LogarithmicRegressionCurveCalculator::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::isValidAndXPositive()));
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
+= log( aValues
.first
[i
] );
84 fAverageY
+= 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
= log( aValues
.first
[i
] ) - fAverageX
;
95 double fDeltaY
= 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
);
107 double SAL_CALL
LogarithmicRegressionCurveCalculator::getCurveValue( double x
)
108 throw (lang::IllegalArgumentException
,
109 uno::RuntimeException
)
112 ::rtl::math::setNan( & fResult
);
114 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
115 ::rtl::math::isNan( m_fIntercept
)))
117 fResult
= m_fSlope
* log( x
) + m_fIntercept
;
123 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
LogarithmicRegressionCurveCalculator::getCurveValues(
124 double min
, double max
, ::sal_Int32 nPointCount
,
125 const uno::Reference
< chart2::XScaling
>& xScalingX
,
126 const uno::Reference
< chart2::XScaling
>& xScalingY
,
127 ::sal_Bool bMaySkipPointsInCalculation
)
128 throw (lang::IllegalArgumentException
,
129 uno::RuntimeException
)
131 if( bMaySkipPointsInCalculation
&&
132 isLogarithmicScaling( xScalingX
) &&
133 isLinearScaling( xScalingY
))
136 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
138 aResult
[0].Y
= this->getCurveValue( min
);
140 aResult
[1].Y
= this->getCurveValue( max
);
144 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
147 OUString
LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
148 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
149 ::sal_Int32 nNumberFormatKey
) const
151 OUStringBuffer
aBuf( C2U( "f(x) = " ));
153 bool bHaveSlope
= false;
155 if( m_fSlope
!= 0.0 )
157 if( ::rtl::math::approxEqual( fabs( m_fSlope
), 1.0 ))
160 aBuf
.append( UC_MINUS_SIGN
);
164 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
));
165 aBuf
.append( UC_SPACE
);
167 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ln(x)" ));
173 if( m_fIntercept
< 0.0 )
175 aBuf
.append( UC_SPACE
);
176 aBuf
.append( UC_MINUS_SIGN
);
177 aBuf
.append( UC_SPACE
);
178 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs( m_fIntercept
)));
180 else if( m_fIntercept
> 0.0 )
182 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
183 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
188 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
191 return aBuf
.makeStringAndClear();