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 0 : PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
36 : m_fSlope( 0.0 ),
37 0 : m_fIntercept( 0.0 )
38 : {
39 0 : ::rtl::math::setNan( & m_fSlope );
40 0 : ::rtl::math::setNan( & m_fIntercept );
41 0 : }
42 :
43 0 : PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
44 0 : {}
45 :
46 : // ____ XRegressionCurveCalculator ____
47 0 : 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 0 : RegressionCalculationHelper::isValidAndBothPositive()));
56 :
57 0 : const size_t nMax = aValues.first.size();
58 0 : 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 0 : return;
64 : }
65 :
66 0 : double fAverageX = 0.0, fAverageY = 0.0;
67 0 : size_t i = 0;
68 0 : for( i = 0; i < nMax; ++i )
69 : {
70 0 : fAverageX += log( aValues.first[i] );
71 0 : fAverageY += log( aValues.second[i] );
72 : }
73 :
74 0 : const double fN = static_cast< double >( nMax );
75 0 : fAverageX /= fN;
76 0 : fAverageY /= fN;
77 :
78 0 : double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
79 0 : for( i = 0; i < nMax; ++i )
80 : {
81 0 : double fDeltaX = log( aValues.first[i] ) - fAverageX;
82 0 : double fDeltaY = log( aValues.second[i] ) - fAverageY;
83 :
84 0 : fQx += fDeltaX * fDeltaX;
85 0 : fQy += fDeltaY * fDeltaY;
86 0 : fQxy += fDeltaX * fDeltaY;
87 : }
88 :
89 0 : m_fSlope = fQxy / fQx;
90 0 : m_fIntercept = fAverageY - m_fSlope * fAverageX;
91 0 : m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
92 :
93 0 : m_fIntercept = exp( m_fIntercept );
94 : }
95 :
96 0 : double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
97 : throw (lang::IllegalArgumentException,
98 : uno::RuntimeException)
99 : {
100 : double fResult;
101 0 : ::rtl::math::setNan( & fResult );
102 :
103 0 : if( ! ( ::rtl::math::isNan( m_fSlope ) ||
104 0 : ::rtl::math::isNan( m_fIntercept )))
105 : {
106 0 : fResult = m_fIntercept * pow( x, m_fSlope );
107 : }
108 :
109 0 : return fResult;
110 : }
111 :
112 0 : 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 0 : if( bMaySkipPointsInCalculation &&
121 0 : 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 0 : 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: */
|