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 : namespace chart
30 : {
31 :
32 0 : LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
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 : LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
41 0 : {}
42 :
43 : // ____ XRegressionCurve ____
44 0 : void SAL_CALL LogarithmicRegressionCurveCalculator::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::isValidAndXPositive()));
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 += 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 = 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 :
91 0 : double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
92 : throw (lang::IllegalArgumentException,
93 : uno::RuntimeException, std::exception)
94 : {
95 : double fResult;
96 0 : ::rtl::math::setNan( & fResult );
97 :
98 0 : if( ! ( ::rtl::math::isNan( m_fSlope ) ||
99 0 : ::rtl::math::isNan( m_fIntercept )))
100 : {
101 0 : fResult = m_fSlope * log( x ) + m_fIntercept;
102 : }
103 :
104 0 : return fResult;
105 : }
106 :
107 0 : uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
108 : double min, double max, ::sal_Int32 nPointCount,
109 : const uno::Reference< chart2::XScaling >& xScalingX,
110 : const uno::Reference< chart2::XScaling >& xScalingY,
111 : sal_Bool bMaySkipPointsInCalculation )
112 : throw (lang::IllegalArgumentException,
113 : uno::RuntimeException, std::exception)
114 : {
115 0 : if( bMaySkipPointsInCalculation &&
116 0 : isLogarithmicScaling( xScalingX ) &&
117 0 : isLinearScaling( xScalingY ))
118 : {
119 : // optimize result
120 0 : uno::Sequence< geometry::RealPoint2D > aResult( 2 );
121 0 : aResult[0].X = min;
122 0 : aResult[0].Y = this->getCurveValue( min );
123 0 : aResult[1].X = max;
124 0 : aResult[1].Y = this->getCurveValue( max );
125 :
126 0 : return aResult;
127 : }
128 0 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
129 : }
130 :
131 0 : OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
132 : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
133 : ::sal_Int32 nNumberFormatKey ) const
134 : {
135 0 : OUStringBuffer aBuf( "f(x) = ");
136 :
137 0 : bool bHaveSlope = false;
138 :
139 0 : if( m_fSlope != 0.0 )
140 : {
141 0 : if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
142 : {
143 0 : if( m_fSlope < 0 )
144 0 : aBuf.append( UC_MINUS_SIGN );
145 : }
146 : else
147 : {
148 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
149 0 : aBuf.append( UC_SPACE );
150 : }
151 0 : aBuf.append( "ln(x)" );
152 0 : bHaveSlope = true;
153 : }
154 :
155 0 : if( bHaveSlope )
156 : {
157 0 : if( m_fIntercept < 0.0 )
158 : {
159 0 : aBuf.append( UC_SPACE );
160 0 : aBuf.append( UC_MINUS_SIGN );
161 0 : aBuf.append( UC_SPACE );
162 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
163 : }
164 0 : else if( m_fIntercept > 0.0 )
165 : {
166 0 : aBuf.append( " + " );
167 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
168 : }
169 : }
170 : else
171 : {
172 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
173 : }
174 :
175 0 : return aBuf.makeStringAndClear();
176 : }
177 :
178 : } // namespace chart
179 :
180 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|