1*cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cde9e8dcSAndrew Rist  * distributed with this work for additional information
6*cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9*cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cde9e8dcSAndrew Rist  *
11*cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cde9e8dcSAndrew Rist  *
13*cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15*cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18*cde9e8dcSAndrew Rist  * under the License.
19*cde9e8dcSAndrew Rist  *
20*cde9e8dcSAndrew Rist  *************************************************************/
21*cde9e8dcSAndrew Rist 
22*cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "PotentialRegressionCurveCalculator.hxx"
27cdf0e10cSrcweir #include "macros.hxx"
28cdf0e10cSrcweir #include "RegressionCalculationHelper.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/math.hxx>
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace ::com::sun::star;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using ::rtl::OUString;
36cdf0e10cSrcweir using ::rtl::OUStringBuffer;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace chart
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
PotentialRegressionCurveCalculator()41cdf0e10cSrcweir PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() :
42cdf0e10cSrcweir         m_fSlope( 0.0 ),
43cdf0e10cSrcweir         m_fIntercept( 0.0 )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     ::rtl::math::setNan( & m_fSlope );
46cdf0e10cSrcweir     ::rtl::math::setNan( & m_fIntercept );
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~PotentialRegressionCurveCalculator()49cdf0e10cSrcweir PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator()
50cdf0e10cSrcweir {}
51cdf0e10cSrcweir 
52cdf0e10cSrcweir // ____ XRegressionCurveCalculator ____
recalculateRegression(const uno::Sequence<double> & aXValues,const uno::Sequence<double> & aYValues)53cdf0e10cSrcweir void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression(
54cdf0e10cSrcweir     const uno::Sequence< double >& aXValues,
55cdf0e10cSrcweir     const uno::Sequence< double >& aYValues )
56cdf0e10cSrcweir     throw (uno::RuntimeException)
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     RegressionCalculationHelper::tDoubleVectorPair aValues(
59cdf0e10cSrcweir         RegressionCalculationHelper::cleanup(
60cdf0e10cSrcweir             aXValues, aYValues,
61cdf0e10cSrcweir             RegressionCalculationHelper::isValidAndBothPositive()));
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     const size_t nMax = aValues.first.size();
64cdf0e10cSrcweir     if( nMax == 0 )
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir         ::rtl::math::setNan( & m_fSlope );
67cdf0e10cSrcweir         ::rtl::math::setNan( & m_fIntercept );
68cdf0e10cSrcweir         ::rtl::math::setNan( & m_fCorrelationCoeffitient );
69cdf0e10cSrcweir         return;
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     double fAverageX = 0.0, fAverageY = 0.0;
73cdf0e10cSrcweir     size_t i = 0;
74cdf0e10cSrcweir     for( i = 0; i < nMax; ++i )
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         fAverageX += log( aValues.first[i] );
77cdf0e10cSrcweir         fAverageY += log( aValues.second[i] );
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     const double fN = static_cast< double >( nMax );
81cdf0e10cSrcweir     fAverageX /= fN;
82cdf0e10cSrcweir     fAverageY /= fN;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
85cdf0e10cSrcweir     for( i = 0; i < nMax; ++i )
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         double fDeltaX = log( aValues.first[i] ) - fAverageX;
88cdf0e10cSrcweir         double fDeltaY = log( aValues.second[i] ) - fAverageY;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         fQx  += fDeltaX * fDeltaX;
91cdf0e10cSrcweir         fQy  += fDeltaY * fDeltaY;
92cdf0e10cSrcweir         fQxy += fDeltaX * fDeltaY;
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     m_fSlope = fQxy / fQx;
96cdf0e10cSrcweir     m_fIntercept = fAverageY - m_fSlope * fAverageX;
97cdf0e10cSrcweir     m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     m_fIntercept = exp( m_fIntercept );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
getCurveValue(double x)102cdf0e10cSrcweir double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x )
103cdf0e10cSrcweir     throw (lang::IllegalArgumentException,
104cdf0e10cSrcweir            uno::RuntimeException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     double fResult;
107cdf0e10cSrcweir     ::rtl::math::setNan( & fResult );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     if( ! ( ::rtl::math::isNan( m_fSlope ) ||
110cdf0e10cSrcweir             ::rtl::math::isNan( m_fIntercept )))
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         fResult = m_fIntercept * pow( x, m_fSlope );
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     return fResult;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getCurveValues(double min,double max,::sal_Int32 nPointCount,const uno::Reference<chart2::XScaling> & xScalingX,const uno::Reference<chart2::XScaling> & xScalingY,::sal_Bool bMaySkipPointsInCalculation)118cdf0e10cSrcweir uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues(
119cdf0e10cSrcweir     double min, double max, ::sal_Int32 nPointCount,
120cdf0e10cSrcweir     const uno::Reference< chart2::XScaling >& xScalingX,
121cdf0e10cSrcweir     const uno::Reference< chart2::XScaling >& xScalingY,
122cdf0e10cSrcweir     ::sal_Bool bMaySkipPointsInCalculation )
123cdf0e10cSrcweir     throw (lang::IllegalArgumentException,
124cdf0e10cSrcweir            uno::RuntimeException)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     if( bMaySkipPointsInCalculation &&
127cdf0e10cSrcweir         isLogarithmicScaling( xScalingX ) &&
128cdf0e10cSrcweir         isLogarithmicScaling( xScalingY ))
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         // optimize result
131cdf0e10cSrcweir         uno::Sequence< geometry::RealPoint2D > aResult( 2 );
132cdf0e10cSrcweir         aResult[0].X = min;
133cdf0e10cSrcweir         aResult[0].Y = this->getCurveValue( min );
134cdf0e10cSrcweir         aResult[1].X = max;
135cdf0e10cSrcweir         aResult[1].Y = this->getCurveValue( max );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         return aResult;
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir     return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
ImplGetRepresentation(const uno::Reference<util::XNumberFormatter> & xNumFormatter,::sal_Int32 nNumberFormatKey) const142cdf0e10cSrcweir OUString PotentialRegressionCurveCalculator::ImplGetRepresentation(
143cdf0e10cSrcweir     const uno::Reference< util::XNumberFormatter >& xNumFormatter,
144cdf0e10cSrcweir     ::sal_Int32 nNumberFormatKey ) const
145cdf0e10cSrcweir {
146cdf0e10cSrcweir     OUStringBuffer aBuf( C2U( "f(x) = " ));
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if( m_fIntercept == 0.0 )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         aBuf.append( sal_Unicode( '0' ));
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir     else if( m_fSlope == 0.0 )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     else
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         if( ! rtl::math::approxEqual( m_fIntercept, 1.0 ) )
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fIntercept ));
161cdf0e10cSrcweir             aBuf.append( sal_Unicode( ' ' ));
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         if( m_fSlope != 0.0 )
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             aBuf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "x^" ));
166cdf0e10cSrcweir             aBuf.append( getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope ));
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir     return aBuf.makeStringAndClear();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir } //  namespace chart
174