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