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 "LinearRegressionCurveCalculator.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 : LinearRegressionCurveCalculator::LinearRegressionCurveCalculator() :
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 : LinearRegressionCurveCalculator::~LinearRegressionCurveCalculator()
44 0 : {}
45 :
46 : // ____ XRegressionCurveCalculator ____
47 0 : void SAL_CALL LinearRegressionCurveCalculator::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::isValid()));
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 : const double fN = static_cast< double >( nMax );
67 0 : double fSumX = 0.0, fSumY = 0.0, fSumXSq = 0.0, fSumYSq = 0.0, fSumXY = 0.0;
68 0 : for( size_t i = 0; i < nMax; ++i )
69 : {
70 0 : fSumX += aValues.first[i];
71 0 : fSumY += aValues.second[i];
72 0 : fSumXSq += aValues.first[i] * aValues.first[i];
73 0 : fSumYSq += aValues.second[i] * aValues.second[i];
74 0 : fSumXY += aValues.first[i] * aValues.second[i];
75 : }
76 :
77 0 : m_fSlope = (fN * fSumXY - fSumX * fSumY) / ( fN * fSumXSq - fSumX * fSumX );
78 0 : m_fIntercept = (fSumY - m_fSlope * fSumX) / fN;
79 :
80 : m_fCorrelationCoeffitient = ( fN * fSumXY - fSumX * fSumY ) /
81 : sqrt( ( fN * fSumXSq - fSumX * fSumX ) *
82 0 : ( fN * fSumYSq - fSumY * fSumY ) );
83 : }
84 :
85 0 : double SAL_CALL LinearRegressionCurveCalculator::getCurveValue( double x )
86 : throw (lang::IllegalArgumentException,
87 : uno::RuntimeException)
88 : {
89 : double fResult;
90 0 : ::rtl::math::setNan( & fResult );
91 :
92 0 : if( ! ( ::rtl::math::isNan( m_fSlope ) ||
93 0 : ::rtl::math::isNan( m_fIntercept )))
94 : {
95 0 : fResult = m_fSlope * x + m_fIntercept;
96 : }
97 :
98 0 : return fResult;
99 : }
100 :
101 0 : uno::Sequence< geometry::RealPoint2D > SAL_CALL LinearRegressionCurveCalculator::getCurveValues(
102 : double min, double max, ::sal_Int32 nPointCount,
103 : const uno::Reference< chart2::XScaling >& xScalingX,
104 : const uno::Reference< chart2::XScaling >& xScalingY,
105 : ::sal_Bool bMaySkipPointsInCalculation )
106 : throw (lang::IllegalArgumentException,
107 : uno::RuntimeException)
108 : {
109 0 : if( bMaySkipPointsInCalculation &&
110 0 : isLinearScaling( xScalingX ) &&
111 0 : isLinearScaling( xScalingY ))
112 : {
113 : // optimize result
114 0 : uno::Sequence< geometry::RealPoint2D > aResult( 2 );
115 0 : aResult[0].X = min;
116 0 : aResult[0].Y = this->getCurveValue( min );
117 0 : aResult[1].X = max;
118 0 : aResult[1].Y = this->getCurveValue( max );
119 :
120 0 : return aResult;
121 : }
122 0 : return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
123 : }
124 :
125 0 : OUString LinearRegressionCurveCalculator::ImplGetRepresentation(
126 : const uno::Reference< util::XNumberFormatter >& xNumFormatter,
127 : ::sal_Int32 nNumberFormatKey ) const
128 : {
129 0 : OUStringBuffer aBuf( C2U( "f(x) = " ));
130 :
131 0 : bool bHaveSlope = false;
132 :
133 0 : if( m_fSlope != 0.0 )
134 : {
135 0 : if( ::rtl::math::approxEqual( fabs( m_fSlope ), 1.0 ))
136 : {
137 0 : if( m_fSlope < 0 )
138 0 : aBuf.append( UC_MINUS_SIGN );
139 : }
140 : else
141 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
142 0 : aBuf.append( sal_Unicode( 'x' ));
143 0 : bHaveSlope = true;
144 : }
145 :
146 0 : if( bHaveSlope )
147 : {
148 0 : if( m_fIntercept < 0.0 )
149 : {
150 0 : aBuf.append( UC_SPACE );
151 0 : aBuf.append( UC_MINUS_SIGN );
152 0 : aBuf.append( UC_SPACE );
153 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, fabs( m_fIntercept )));
154 : }
155 0 : else if( m_fIntercept > 0.0 )
156 : {
157 0 : aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
158 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
159 : }
160 : }
161 : else
162 : {
163 0 : aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
164 : }
165 :
166 0 : return aBuf.makeStringAndClear();
167 : }
168 :
169 : } // namespace chart
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|