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: MeanValueRegressionCurveCalculator.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 "MeanValueRegressionCurveCalculator.hxx"
36 #include <rtl/math.hxx>
37 #include <rtl/ustrbuf.hxx>
39 using namespace ::com::sun::star
;
41 using ::rtl::OUString
;
42 using ::rtl::OUStringBuffer
;
47 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
50 ::rtl::math::setNan( & m_fMeanValue
);
53 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
56 // ____ XRegressionCurveCalculator ____
57 void SAL_CALL
MeanValueRegressionCurveCalculator::recalculateRegression(
58 const uno::Sequence
< double >& /*aXValues*/,
59 const uno::Sequence
< double >& aYValues
)
60 throw (uno::RuntimeException
)
62 const sal_Int32 nDataLength
= aYValues
.getLength();
63 sal_Int32 nMax
= nDataLength
;
65 const double * pY
= aYValues
.getConstArray();
67 for( sal_Int32 i
= 0; i
< nDataLength
; ++i
)
69 if( ::rtl::math::isNan( pY
[i
] ) ||
70 ::rtl::math::isInf( pY
[i
] ))
76 m_fCorrelationCoeffitient
= 0.0;
80 ::rtl::math::setNan( & m_fMeanValue
);
84 m_fMeanValue
= fSumY
/ static_cast< double >( nMax
);
86 // correlation coefficient: standard deviation
89 double fErrorSum
= 0.0;
90 for( sal_Int32 i
= 0; i
< nDataLength
; ++i
)
92 if( !::rtl::math::isNan( pY
[i
] ) &&
93 !::rtl::math::isInf( pY
[i
] ))
95 double v
= m_fMeanValue
- pY
[i
];
99 OSL_ASSERT( fErrorSum
>= 0.0 );
100 m_fCorrelationCoeffitient
= sqrt( fErrorSum
/ (nMax
- 1 ));
105 double SAL_CALL
MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
106 throw (lang::IllegalArgumentException
,
107 uno::RuntimeException
)
113 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
MeanValueRegressionCurveCalculator::getCurveValues(
114 double min
, double max
, ::sal_Int32 nPointCount
,
115 const uno::Reference
< chart2::XScaling
>& xScalingX
,
116 const uno::Reference
< chart2::XScaling
>& xScalingY
,
117 ::sal_Bool bMaySkipPointsInCalculation
)
118 throw (lang::IllegalArgumentException
,
119 uno::RuntimeException
)
121 if( bMaySkipPointsInCalculation
)
124 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
126 aResult
[0].Y
= m_fMeanValue
;
128 aResult
[1].Y
= m_fMeanValue
;
132 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
135 OUString
MeanValueRegressionCurveCalculator::ImplGetRepresentation(
136 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
137 ::sal_Int32 nNumberFormatKey
) const
139 OUStringBuffer
aBuf( C2U( "f(x) = " ));
141 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fMeanValue
));
143 return aBuf
.makeStringAndClear();