xref: /AOO41X/main/chart2/source/tools/MeanValueRegressionCurveCalculator.cxx (revision cde9e8dc2218e857da4894ecba5c903312256674)
1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "MeanValueRegressionCurveCalculator.hxx"
27cdf0e10cSrcweir #include "macros.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <rtl/math.hxx>
30cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using ::rtl::OUString;
35cdf0e10cSrcweir using ::rtl::OUStringBuffer;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace chart
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 
MeanValueRegressionCurveCalculator()40cdf0e10cSrcweir MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
41cdf0e10cSrcweir         m_fMeanValue( 0.0 )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     ::rtl::math::setNan( & m_fMeanValue );
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
~MeanValueRegressionCurveCalculator()46cdf0e10cSrcweir MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
47cdf0e10cSrcweir {}
48cdf0e10cSrcweir 
49cdf0e10cSrcweir // ____ XRegressionCurveCalculator ____
recalculateRegression(const uno::Sequence<double> &,const uno::Sequence<double> & aYValues)50cdf0e10cSrcweir void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
51cdf0e10cSrcweir     const uno::Sequence< double >& /*aXValues*/,
52cdf0e10cSrcweir     const uno::Sequence< double >& aYValues )
53cdf0e10cSrcweir     throw (uno::RuntimeException)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     const sal_Int32 nDataLength = aYValues.getLength();
56cdf0e10cSrcweir     sal_Int32 nMax = nDataLength;
57cdf0e10cSrcweir     double fSumY = 0.0;
58cdf0e10cSrcweir     const double * pY = aYValues.getConstArray();
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nDataLength; ++i )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         if( ::rtl::math::isNan( pY[i] ) ||
63cdf0e10cSrcweir             ::rtl::math::isInf( pY[i] ))
64cdf0e10cSrcweir             --nMax;
65cdf0e10cSrcweir         else
66cdf0e10cSrcweir             fSumY += pY[i];
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     m_fCorrelationCoeffitient = 0.0;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     if( nMax == 0 )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         ::rtl::math::setNan( & m_fMeanValue );
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir     else
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         m_fMeanValue = fSumY / static_cast< double >( nMax );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         // correlation coefficient: standard deviation
80cdf0e10cSrcweir         if( nMax > 1 )
81cdf0e10cSrcweir         {
82cdf0e10cSrcweir             double fErrorSum = 0.0;
83cdf0e10cSrcweir             for( sal_Int32 i = 0; i < nDataLength; ++i )
84cdf0e10cSrcweir             {
85cdf0e10cSrcweir                 if( !::rtl::math::isNan( pY[i] ) &&
86cdf0e10cSrcweir                     !::rtl::math::isInf( pY[i] ))
87cdf0e10cSrcweir                 {
88cdf0e10cSrcweir                     double v = m_fMeanValue - pY[i];
89cdf0e10cSrcweir                     fErrorSum += (v*v);
90cdf0e10cSrcweir                 }
91cdf0e10cSrcweir             }
92cdf0e10cSrcweir             OSL_ASSERT( fErrorSum >= 0.0 );
93cdf0e10cSrcweir             m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
getCurveValue(double)98cdf0e10cSrcweir double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
99cdf0e10cSrcweir     throw (lang::IllegalArgumentException,
100cdf0e10cSrcweir            uno::RuntimeException)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     return m_fMeanValue;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)106cdf0e10cSrcweir uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
107cdf0e10cSrcweir     double min, double max, ::sal_Int32 nPointCount,
108cdf0e10cSrcweir     const uno::Reference< chart2::XScaling >& xScalingX,
109cdf0e10cSrcweir     const uno::Reference< chart2::XScaling >& xScalingY,
110cdf0e10cSrcweir     ::sal_Bool bMaySkipPointsInCalculation )
111cdf0e10cSrcweir     throw (lang::IllegalArgumentException,
112cdf0e10cSrcweir            uno::RuntimeException)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     if( bMaySkipPointsInCalculation )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         // optimize result
117cdf0e10cSrcweir         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
118cdf0e10cSrcweir         aResult[0].X = min;
119cdf0e10cSrcweir         aResult[0].Y = m_fMeanValue;
120cdf0e10cSrcweir         aResult[1].X = max;
121cdf0e10cSrcweir         aResult[1].Y = m_fMeanValue;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         return aResult;
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const128cdf0e10cSrcweir OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
129cdf0e10cSrcweir     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
130cdf0e10cSrcweir     ::sal_Int32 nNumberFormatKey ) const
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     OUStringBuffer aBuf( C2U( "f(x) = " ));
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue ));
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     return aBuf.makeStringAndClear();
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir } //  namespace chart
140