1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "LinearRegressionCurveCalculator.hxx"
22 #include "RegressionCalculationHelper.hxx"
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star
;
33 LinearRegressionCurveCalculator::LinearRegressionCurveCalculator() :
37 ::rtl::math::setNan( & m_fSlope
);
38 ::rtl::math::setNan( & m_fIntercept
);
41 LinearRegressionCurveCalculator::~LinearRegressionCurveCalculator()
44 // ____ XRegressionCurveCalculator ____
45 void SAL_CALL
LinearRegressionCurveCalculator::recalculateRegression(
46 const uno::Sequence
< double >& aXValues
,
47 const uno::Sequence
< double >& aYValues
)
48 throw (uno::RuntimeException
)
50 RegressionCalculationHelper::tDoubleVectorPair
aValues(
51 RegressionCalculationHelper::cleanup(
53 RegressionCalculationHelper::isValid()));
55 const size_t nMax
= aValues
.first
.size();
58 ::rtl::math::setNan( & m_fSlope
);
59 ::rtl::math::setNan( & m_fIntercept
);
60 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
64 const double fN
= static_cast< double >( nMax
);
65 double fSumX
= 0.0, fSumY
= 0.0, fSumXSq
= 0.0, fSumYSq
= 0.0, fSumXY
= 0.0;
66 for( size_t i
= 0; i
< nMax
; ++i
)
68 fSumX
+= aValues
.first
[i
];
69 fSumY
+= aValues
.second
[i
];
70 fSumXSq
+= aValues
.first
[i
] * aValues
.first
[i
];
71 fSumYSq
+= aValues
.second
[i
] * aValues
.second
[i
];
72 fSumXY
+= aValues
.first
[i
] * aValues
.second
[i
];
75 m_fSlope
= (fN
* fSumXY
- fSumX
* fSumY
) / ( fN
* fSumXSq
- fSumX
* fSumX
);
76 m_fIntercept
= (fSumY
- m_fSlope
* fSumX
) / fN
;
78 m_fCorrelationCoeffitient
= ( fN
* fSumXY
- fSumX
* fSumY
) /
79 sqrt( ( fN
* fSumXSq
- fSumX
* fSumX
) *
80 ( fN
* fSumYSq
- fSumY
* fSumY
) );
83 double SAL_CALL
LinearRegressionCurveCalculator::getCurveValue( double x
)
84 throw (lang::IllegalArgumentException
,
85 uno::RuntimeException
)
88 ::rtl::math::setNan( & fResult
);
90 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
91 ::rtl::math::isNan( m_fIntercept
)))
93 fResult
= m_fSlope
* x
+ m_fIntercept
;
99 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
LinearRegressionCurveCalculator::getCurveValues(
100 double min
, double max
, ::sal_Int32 nPointCount
,
101 const uno::Reference
< chart2::XScaling
>& xScalingX
,
102 const uno::Reference
< chart2::XScaling
>& xScalingY
,
103 ::sal_Bool bMaySkipPointsInCalculation
)
104 throw (lang::IllegalArgumentException
,
105 uno::RuntimeException
)
107 if( bMaySkipPointsInCalculation
&&
108 isLinearScaling( xScalingX
) &&
109 isLinearScaling( xScalingY
))
112 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
114 aResult
[0].Y
= this->getCurveValue( min
);
116 aResult
[1].Y
= this->getCurveValue( max
);
120 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
123 OUString
LinearRegressionCurveCalculator::ImplGetRepresentation(
124 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
125 ::sal_Int32 nNumberFormatKey
) const
127 OUStringBuffer
aBuf( "f(x) = ");
129 bool bHaveSlope
= false;
131 if( m_fSlope
!= 0.0 )
133 if( ::rtl::math::approxEqual( fabs( m_fSlope
), 1.0 ))
136 aBuf
.append( UC_MINUS_SIGN
);
139 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
));
140 aBuf
.append( sal_Unicode( 'x' ));
146 if( m_fIntercept
< 0.0 )
148 aBuf
.append( UC_SPACE
);
149 aBuf
.append( UC_MINUS_SIGN
);
150 aBuf
.append( UC_SPACE
);
151 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs( m_fIntercept
)));
153 else if( m_fIntercept
> 0.0 )
155 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
156 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
161 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
164 return aBuf
.makeStringAndClear();
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */