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