Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "MeanValueRegressionCurveCalculator.hxx"
21 : : #include "macros.hxx"
22 : :
23 : : #include <rtl/math.hxx>
24 : : #include <rtl/ustrbuf.hxx>
25 : :
26 : : using namespace ::com::sun::star;
27 : :
28 : : using ::rtl::OUString;
29 : : using ::rtl::OUStringBuffer;
30 : :
31 : : namespace chart
32 : : {
33 : :
34 : 76 : MeanValueRegressionCurveCalculator::MeanValueRegressionCurveCalculator() :
35 : 76 : m_fMeanValue( 0.0 )
36 : : {
37 : 76 : ::rtl::math::setNan( & m_fMeanValue );
38 : 76 : }
39 : :
40 : 76 : MeanValueRegressionCurveCalculator::~MeanValueRegressionCurveCalculator()
41 [ - + ]: 152 : {}
42 : :
43 : : // ____ XRegressionCurveCalculator ____
44 : 76 : void SAL_CALL MeanValueRegressionCurveCalculator::recalculateRegression(
45 : : const uno::Sequence< double >& /*aXValues*/,
46 : : const uno::Sequence< double >& aYValues )
47 : : throw (uno::RuntimeException)
48 : : {
49 : 76 : const sal_Int32 nDataLength = aYValues.getLength();
50 : 76 : sal_Int32 nMax = nDataLength;
51 : 76 : double fSumY = 0.0;
52 : 76 : const double * pY = aYValues.getConstArray();
53 : :
54 [ + + ]: 974 : for( sal_Int32 i = 0; i < nDataLength; ++i )
55 : : {
56 [ + - - + ]: 1796 : if( ::rtl::math::isNan( pY[i] ) ||
[ - + ]
57 : 898 : ::rtl::math::isInf( pY[i] ))
58 : 0 : --nMax;
59 : : else
60 : 898 : fSumY += pY[i];
61 : : }
62 : :
63 : 76 : m_fCorrelationCoeffitient = 0.0;
64 : :
65 [ - + ]: 76 : if( nMax == 0 )
66 : : {
67 : 0 : ::rtl::math::setNan( & m_fMeanValue );
68 : : }
69 : : else
70 : : {
71 : 76 : m_fMeanValue = fSumY / static_cast< double >( nMax );
72 : :
73 : : // correlation coefficient: standard deviation
74 [ + - ]: 76 : if( nMax > 1 )
75 : : {
76 : 76 : double fErrorSum = 0.0;
77 [ + + ]: 974 : for( sal_Int32 i = 0; i < nDataLength; ++i )
78 : : {
79 [ + - + - ]: 1796 : if( !::rtl::math::isNan( pY[i] ) &&
[ + - ]
80 : 898 : !::rtl::math::isInf( pY[i] ))
81 : : {
82 : 898 : double v = m_fMeanValue - pY[i];
83 : 898 : fErrorSum += (v*v);
84 : : }
85 : : }
86 : : OSL_ASSERT( fErrorSum >= 0.0 );
87 : 76 : m_fCorrelationCoeffitient = sqrt( fErrorSum / (nMax - 1 ));
88 : : }
89 : : }
90 : 76 : }
91 : :
92 : 0 : double SAL_CALL MeanValueRegressionCurveCalculator::getCurveValue( double /*x*/ )
93 : : throw (lang::IllegalArgumentException,
94 : : uno::RuntimeException)
95 : : {
96 : 0 : return m_fMeanValue;
97 : : }
98 : :
99 : :
100 : 76 : uno::Sequence< geometry::RealPoint2D > SAL_CALL MeanValueRegressionCurveCalculator::getCurveValues(
101 : : double min, double max, ::sal_Int32 nPointCount,
102 : : const uno::Reference< chart2::XScaling >& xScalingX,
103 : : const uno::Reference< chart2::XScaling >& xScalingY,
104 : : ::sal_Bool bMaySkipPointsInCalculation )
105 : : throw (lang::IllegalArgumentException,
106 : : uno::RuntimeException)
107 : : {
108 [ + - ]: 76 : if( bMaySkipPointsInCalculation )
109 : : {
110 : : // optimize result
111 [ + - ]: 76 : uno::Sequence< geometry::RealPoint2D > aResult( 2 );
112 [ + - ]: 76 : aResult[0].X = min;
113 [ + - ]: 76 : aResult[0].Y = m_fMeanValue;
114 [ + - ]: 76 : aResult[1].X = max;
115 [ + - ]: 76 : aResult[1].Y = m_fMeanValue;
116 : :
117 [ + - ][ + - ]: 76 : return aResult;
118 : : }
119 : 76 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
120 : : }
121 : :
122 : 0 : OUString MeanValueRegressionCurveCalculator::ImplGetRepresentation(
123 : : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
124 : : ::sal_Int32 nNumberFormatKey ) const
125 : : {
126 [ # # ][ # # ]: 0 : OUStringBuffer aBuf( C2U( "f(x) = " ));
127 : :
128 [ # # ][ # # ]: 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fMeanValue ));
129 : :
130 [ # # ]: 0 : return aBuf.makeStringAndClear();
131 : : }
132 : :
133 : : } // namespace chart
134 : :
135 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|