1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "MeanValueRegressionCurveCalculator.hxx"
31 #include "macros.hxx"
32 
33 #include <rtl/math.hxx>
34 #include <rtl/ustrbuf.hxx>
35 
36 using namespace ::com::sun::star;
37 
38 using ::rtl::OUString;
39 using ::rtl::OUStringBuffer;
40 
41 namespace chart
42 {
43 
44 MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
45         m_fMeanValue( 0.0 )
46 {
47     ::rtl::math::setNan( & m_fMeanValue );
48 }
49 
50 MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
51 {}
52 
53 // ____ XRegressionCurveCalculator ____
54 void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
55     const uno::Sequence< double >& /*aXValues*/,
56     const uno::Sequence< double >& aYValues )
57     throw (uno::RuntimeException)
58 {
59     const sal_Int32 nDataLength = aYValues.getLength();
60     sal_Int32 nMax = nDataLength;
61     double fSumY = 0.0;
62     const double * pY = aYValues.getConstArray();
63 
64     for( sal_Int32 i = 0; i < nDataLength; ++i )
65     {
66         if( ::rtl::math::isNan( pY[i] ) ||
67             ::rtl::math::isInf( pY[i] ))
68             --nMax;
69         else
70             fSumY += pY[i];
71     }
72 
73     m_fCorrelationCoeffitient = 0.0;
74 
75     if( nMax == 0 )
76     {
77         ::rtl::math::setNan( & m_fMeanValue );
78     }
79     else
80     {
81         m_fMeanValue = fSumY / static_cast< double >( nMax );
82 
83         // correlation coefficient: standard deviation
84         if( nMax > 1 )
85         {
86             double fErrorSum = 0.0;
87             for( sal_Int32 i = 0; i < nDataLength; ++i )
88             {
89                 if( !::rtl::math::isNan( pY[i] ) &&
90                     !::rtl::math::isInf( pY[i] ))
91                 {
92                     double v = m_fMeanValue - pY[i];
93                     fErrorSum += (v*v);
94                 }
95             }
96             OSL_ASSERT( fErrorSum >= 0.0 );
97             m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
98         }
99     }
100 }
101 
102 double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
103     throw (lang::IllegalArgumentException,
104            uno::RuntimeException)
105 {
106     return m_fMeanValue;
107 }
108 
109 
110 uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
111     double min, double max, ::sal_Int32 nPointCount,
112     const uno::Reference< chart2::XScaling >& xScalingX,
113     const uno::Reference< chart2::XScaling >& xScalingY,
114     ::sal_Bool bMaySkipPointsInCalculation )
115     throw (lang::IllegalArgumentException,
116            uno::RuntimeException)
117 {
118     if( bMaySkipPointsInCalculation )
119     {
120         // optimize result
121         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
122         aResult[0].X = min;
123         aResult[0].Y = m_fMeanValue;
124         aResult[1].X = max;
125         aResult[1].Y = m_fMeanValue;
126 
127         return aResult;
128     }
129     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
130 }
131 
132 OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
133     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
134     ::sal_Int32 nNumberFormatKey ) const
135 {
136     OUStringBuffer aBuf( C2U( "f(x) = " ));
137 
138     aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue ));
139 
140     return aBuf.makeStringAndClear();
141 }
142 
143 } //  namespace chart
144