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