1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: LinearRegressionCurveCalculator.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
33 #include "LinearRegressionCurveCalculator.hxx"
35 #include "RegressionCalculationHelper.hxx"
37 #include <rtl/math.hxx>
38 #include <rtl/ustrbuf.hxx>
40 using namespace ::com::sun::star
;
42 using ::rtl::OUString
;
43 using ::rtl::OUStringBuffer
;
48 LinearRegressionCurveCalculator::LinearRegressionCurveCalculator() :
52 ::rtl::math::setNan( & m_fSlope
);
53 ::rtl::math::setNan( & m_fIntercept
);
56 LinearRegressionCurveCalculator::~LinearRegressionCurveCalculator()
59 // ____ XRegressionCurveCalculator ____
60 void SAL_CALL
LinearRegressionCurveCalculator::recalculateRegression(
61 const uno::Sequence
< double >& aXValues
,
62 const uno::Sequence
< double >& aYValues
)
63 throw (uno::RuntimeException
)
65 RegressionCalculationHelper::tDoubleVectorPair
aValues(
66 RegressionCalculationHelper::cleanup(
68 RegressionCalculationHelper::isValid()));
70 const size_t nMax
= aValues
.first
.size();
73 ::rtl::math::setNan( & m_fSlope
);
74 ::rtl::math::setNan( & m_fIntercept
);
75 ::rtl::math::setNan( & m_fCorrelationCoeffitient
);
79 const double fN
= static_cast< double >( nMax
);
80 double fSumX
= 0.0, fSumY
= 0.0, fSumXSq
= 0.0, fSumYSq
= 0.0, fSumXY
= 0.0;
81 for( size_t i
= 0; i
< nMax
; ++i
)
83 fSumX
+= aValues
.first
[i
];
84 fSumY
+= aValues
.second
[i
];
85 fSumXSq
+= aValues
.first
[i
] * aValues
.first
[i
];
86 fSumYSq
+= aValues
.second
[i
] * aValues
.second
[i
];
87 fSumXY
+= aValues
.first
[i
] * aValues
.second
[i
];
90 m_fSlope
= (fN
* fSumXY
- fSumX
* fSumY
) / ( fN
* fSumXSq
- fSumX
* fSumX
);
91 m_fIntercept
= (fSumY
- m_fSlope
* fSumX
) / fN
;
93 m_fCorrelationCoeffitient
= ( fN
* fSumXY
- fSumX
* fSumY
) /
94 sqrt( ( fN
* fSumXSq
- fSumX
* fSumX
) *
95 ( fN
* fSumYSq
- fSumY
* fSumY
) );
98 double SAL_CALL
LinearRegressionCurveCalculator::getCurveValue( double x
)
99 throw (lang::IllegalArgumentException
,
100 uno::RuntimeException
)
103 ::rtl::math::setNan( & fResult
);
105 if( ! ( ::rtl::math::isNan( m_fSlope
) ||
106 ::rtl::math::isNan( m_fIntercept
)))
108 fResult
= m_fSlope
* x
+ m_fIntercept
;
114 uno::Sequence
< geometry::RealPoint2D
> SAL_CALL
LinearRegressionCurveCalculator::getCurveValues(
115 double min
, double max
, ::sal_Int32 nPointCount
,
116 const uno::Reference
< chart2::XScaling
>& xScalingX
,
117 const uno::Reference
< chart2::XScaling
>& xScalingY
,
118 ::sal_Bool bMaySkipPointsInCalculation
)
119 throw (lang::IllegalArgumentException
,
120 uno::RuntimeException
)
122 if( bMaySkipPointsInCalculation
&&
123 isLinearScaling( xScalingX
) &&
124 isLinearScaling( xScalingY
))
127 uno::Sequence
< geometry::RealPoint2D
> aResult( 2 );
129 aResult
[0].Y
= this->getCurveValue( min
);
131 aResult
[1].Y
= this->getCurveValue( max
);
135 return RegressionCurveCalculator::getCurveValues( min
, max
, nPointCount
, xScalingX
, xScalingY
, bMaySkipPointsInCalculation
);
138 OUString
LinearRegressionCurveCalculator::ImplGetRepresentation(
139 const uno::Reference
< util::XNumberFormatter
>& xNumFormatter
,
140 ::sal_Int32 nNumberFormatKey
) const
142 OUStringBuffer
aBuf( C2U( "f(x) = " ));
144 bool bHaveSlope
= false;
146 if( m_fSlope
!= 0.0 )
148 if( ::rtl::math::approxEqual( fabs( m_fSlope
), 1.0 ))
151 aBuf
.append( UC_MINUS_SIGN
);
154 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fSlope
));
155 aBuf
.append( sal_Unicode( 'x' ));
161 if( m_fIntercept
< 0.0 )
163 aBuf
.append( UC_SPACE
);
164 aBuf
.append( UC_MINUS_SIGN
);
165 aBuf
.append( UC_SPACE
);
166 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, fabs( m_fIntercept
)));
168 else if( m_fIntercept
> 0.0 )
170 aBuf
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " + " ));
171 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
176 aBuf
.append( getFormattedString( xNumFormatter
, nNumberFormatKey
, m_fIntercept
));
179 return aBuf
.makeStringAndClear();