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 "PotentialRegressionCurveCalculator.hxx"
21 : : #include "macros.hxx"
22 : : #include "RegressionCalculationHelper.hxx"
23 : :
24 : : #include <rtl/math.hxx>
25 : : #include <rtl/ustrbuf.hxx>
26 : :
27 : : using namespace ::com::sun::star;
28 : :
29 : : using ::rtl::OUString;
30 : : using ::rtl::OUStringBuffer;
31 : :
32 : : namespace chart
33 : : {
34 : :
35 : 32 : PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
36 : : m_fSlope( 0.0 ),
37 : 32 : m_fIntercept( 0.0 )
38 : : {
39 : 32 : ::rtl::math::setNan( & m_fSlope );
40 : 32 : ::rtl::math::setNan( & m_fIntercept );
41 : 32 : }
42 : :
43 : 32 : PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
44 [ - + ]: 64 : {}
45 : :
46 : : // ____ XRegressionCurveCalculator ____
47 : 32 : void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression(
48 : : const uno::Sequence< double >& aXValues,
49 : : const uno::Sequence< double >& aYValues )
50 : : throw (uno::RuntimeException)
51 : : {
52 : : RegressionCalculationHelper::tDoubleVectorPair aValues(
53 : : RegressionCalculationHelper::cleanup(
54 : : aXValues, aYValues,
55 [ + - ]: 32 : RegressionCalculationHelper::isValidAndBothPositive()));
56 : :
57 : 32 : const size_t nMax = aValues.first.size();
58 [ - + ]: 32 : if( nMax == 0 )
59 : : {
60 : 0 : ::rtl::math::setNan( & m_fSlope );
61 : 0 : ::rtl::math::setNan( & m_fIntercept );
62 : 0 : ::rtl::math::setNan( & m_fCorrelationCoeffitient );
63 : 32 : return;
64 : : }
65 : :
66 : 32 : double fAverageX = 0.0, fAverageY = 0.0;
67 : 32 : size_t i = 0;
68 [ + + ]: 332 : for( i = 0; i < nMax; ++i )
69 : : {
70 [ + - ]: 300 : fAverageX += log( aValues.first[i] );
71 [ + - ]: 300 : fAverageY += log( aValues.second[i] );
72 : : }
73 : :
74 : 32 : const double fN = static_cast< double >( nMax );
75 : 32 : fAverageX /= fN;
76 : 32 : fAverageY /= fN;
77 : :
78 : 32 : double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
79 [ + + ]: 332 : for( i = 0; i < nMax; ++i )
80 : : {
81 [ + - ]: 300 : double fDeltaX = log( aValues.first[i] ) - fAverageX;
82 [ + - ]: 300 : double fDeltaY = log( aValues.second[i] ) - fAverageY;
83 : :
84 : 300 : fQx += fDeltaX * fDeltaX;
85 : 300 : fQy += fDeltaY * fDeltaY;
86 : 300 : fQxy += fDeltaX * fDeltaY;
87 : : }
88 : :
89 : 32 : m_fSlope = fQxy / fQx;
90 : 32 : m_fIntercept = fAverageY - m_fSlope * fAverageX;
91 : 32 : m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
92 : :
93 [ + - ]: 32 : m_fIntercept = exp( m_fIntercept );
94 : : }
95 : :
96 : 1600 : double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
97 : : throw (lang::IllegalArgumentException,
98 : : uno::RuntimeException)
99 : : {
100 : : double fResult;
101 : 1600 : ::rtl::math::setNan( & fResult );
102 : :
103 [ + - ]: 3200 : if( ! ( ::rtl::math::isNan( m_fSlope ) ||
104 [ + - ][ + - ]: 1600 : ::rtl::math::isNan( m_fIntercept )))
105 : : {
106 : 1600 : fResult = m_fIntercept * pow( x, m_fSlope );
107 : : }
108 : :
109 : 1600 : return fResult;
110 : : }
111 : :
112 : 32 : uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues(
113 : : double min, double max, ::sal_Int32 nPointCount,
114 : : const uno::Reference< chart2::XScaling >& xScalingX,
115 : : const uno::Reference< chart2::XScaling >& xScalingY,
116 : : ::sal_Bool bMaySkipPointsInCalculation )
117 : : throw (lang::IllegalArgumentException,
118 : : uno::RuntimeException)
119 : : {
120 [ + - - + : 64 : if( bMaySkipPointsInCalculation &&
# # ][ - + ]
121 : 32 : isLogarithmicScaling( xScalingX ) &&
122 : 0 : isLogarithmicScaling( xScalingY ))
123 : : {
124 : : // optimize result
125 [ # # ]: 0 : uno::Sequence< geometry::RealPoint2D > aResult( 2 );
126 [ # # ]: 0 : aResult[0].X = min;
127 [ # # ][ # # ]: 0 : aResult[0].Y = this->getCurveValue( min );
128 [ # # ]: 0 : aResult[1].X = max;
129 [ # # ][ # # ]: 0 : aResult[1].Y = this->getCurveValue( max );
130 : :
131 [ # # ][ # # ]: 0 : return aResult;
132 : : }
133 : 32 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
134 : : }
135 : :
136 : 0 : OUString PotentialRegressionCurveCalculator::ImplGetRepresentation(
137 : : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
138 : : ::sal_Int32 nNumberFormatKey ) const
139 : : {
140 [ # # ][ # # ]: 0 : OUStringBuffer aBuf( C2U( "f(x) = " ));
141 : :
142 [ # # ]: 0 : if( m_fIntercept == 0.0 )
143 : : {
144 [ # # ]: 0 : aBuf.append( sal_Unicode( '0' ));
145 : : }
146 [ # # ]: 0 : else if( m_fSlope == 0.0 )
147 : : {
148 [ # # ][ # # ]: 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
149 : : }
150 : : else
151 : : {
152 [ # # ]: 0 : if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
153 : : {
154 [ # # ][ # # ]: 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
155 [ # # ]: 0 : aBuf.append( sal_Unicode( ' ' ));
156 : : }
157 [ # # ]: 0 : if( m_fSlope != 0.0 )
158 : : {
159 [ # # ]: 0 : aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "x^" ));
160 [ # # ][ # # ]: 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
161 : : }
162 : : }
163 : :
164 [ # # ]: 0 : return aBuf.makeStringAndClear();
165 : : }
166 : :
167 : : } // namespace chart
168 : :
169 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|