Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / chart2 / source / tools / LogarithmicRegressionCurveCalculator.cxx
blobd164918970ba22b9c22aa6289c9013fd86901d8a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <LogarithmicRegressionCurveCalculator.hxx>
21 #include <RegressionCalculationHelper.hxx>
22 #include <SpecialCharacters.hxx>
24 #include <rtl/math.hxx>
25 #include <rtl/ustrbuf.hxx>
27 using namespace ::com::sun::star;
29 namespace chart
32 LogarithmicRegressionCurveCalculator::LogarithmicRegressionCurveCalculator() :
33 m_fSlope( 0.0 ),
34 m_fIntercept( 0.0 )
36 ::rtl::math::setNan( & m_fSlope );
37 ::rtl::math::setNan( & m_fIntercept );
40 LogarithmicRegressionCurveCalculator::~LogarithmicRegressionCurveCalculator()
43 // ____ XRegressionCurve ____
44 void SAL_CALL LogarithmicRegressionCurveCalculator::recalculateRegression(
45 const uno::Sequence< double >& aXValues,
46 const uno::Sequence< double >& aYValues )
48 RegressionCalculationHelper::tDoubleVectorPair aValues(
49 RegressionCalculationHelper::cleanup(
50 aXValues, aYValues,
51 RegressionCalculationHelper::isValidAndXPositive()));
53 const size_t nMax = aValues.first.size();
54 if( nMax <= 1 ) // at least 2 points
56 ::rtl::math::setNan( & m_fSlope );
57 ::rtl::math::setNan( & m_fIntercept );
58 ::rtl::math::setNan( & m_fCorrelationCoeffitient );
59 return;
62 double fAverageX = 0.0, fAverageY = 0.0;
63 size_t i = 0;
64 for( i = 0; i < nMax; ++i )
66 fAverageX += log( aValues.first[i] );
67 fAverageY += aValues.second[i];
70 const double fN = static_cast< double >( nMax );
71 fAverageX /= fN;
72 fAverageY /= fN;
74 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
75 for( i = 0; i < nMax; ++i )
77 double fDeltaX = log( aValues.first[i] ) - fAverageX;
78 double fDeltaY = aValues.second[i] - fAverageY;
80 fQx += fDeltaX * fDeltaX;
81 fQy += fDeltaY * fDeltaY;
82 fQxy += fDeltaX * fDeltaY;
85 m_fSlope = fQxy / fQx;
86 m_fIntercept = fAverageY - m_fSlope * fAverageX;
87 m_fCorrelationCoeffitient = fQxy / sqrt( fQx * fQy );
90 double SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValue( double x )
92 double fResult;
93 ::rtl::math::setNan( & fResult );
95 if( ! ( ::rtl::math::isNan( m_fSlope ) ||
96 ::rtl::math::isNan( m_fIntercept )))
98 fResult = m_fSlope * log( x ) + m_fIntercept;
101 return fResult;
104 uno::Sequence< geometry::RealPoint2D > SAL_CALL LogarithmicRegressionCurveCalculator::getCurveValues(
105 double min, double max, ::sal_Int32 nPointCount,
106 const uno::Reference< chart2::XScaling >& xScalingX,
107 const uno::Reference< chart2::XScaling >& xScalingY,
108 sal_Bool bMaySkipPointsInCalculation )
110 if( bMaySkipPointsInCalculation &&
111 isLogarithmicScaling( xScalingX ) &&
112 isLinearScaling( xScalingY ))
114 // optimize result
115 uno::Sequence< geometry::RealPoint2D > aResult( 2 );
116 aResult[0].X = min;
117 aResult[0].Y = getCurveValue( min );
118 aResult[1].X = max;
119 aResult[1].Y = getCurveValue( max );
121 return aResult;
123 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
126 OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation(
127 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
128 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
130 bool bHasSlope = !rtl::math::approxEqual( fabs( m_fSlope ), 1.0 );
131 OUStringBuffer aBuf( mYName + " = " );
132 sal_Int32 nLineLength = aBuf.getLength();
133 sal_Int32 nValueLength=0;
134 if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength
136 sal_Int32 nCharMin = nLineLength + 6 + mXName.getLength(); // 6 = "ln(x)" + 2 extra characters
137 if( m_fSlope < 0.0 )
138 nCharMin += 2; // "- "
139 if( m_fSlope != 0.0 && m_fIntercept != 0.0 )
141 nCharMin += 3; // " + "
142 if ( bHasSlope )
143 nValueLength = (*pFormulaMaxWidth - nCharMin) / 2;
145 if ( nValueLength == 0 ) // not yet calculated
146 nValueLength = *pFormulaMaxWidth - nCharMin;
147 if ( nValueLength <= 0 )
148 nValueLength = 1;
151 // temporary buffer
152 OUStringBuffer aTmpBuf("");
153 // if nValueLength not calculated then nullptr
154 sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
155 if( m_fSlope != 0.0 ) // add slope value
157 if( m_fSlope < 0.0 )
159 aTmpBuf.append( OUStringChar(aMinusSign) ).append( " " );
161 if( bHasSlope )
163 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fSlope), pValueLength );
164 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
166 aTmpBuf.append( aValueString ).append( " " );
169 aTmpBuf.append( "ln(" ).append( mXName ).append( ") " );
170 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
171 aTmpBuf.truncate();
173 if( m_fIntercept > 0.0 )
174 aTmpBuf.append( "+ " );
176 // add intercept value
177 if( m_fIntercept < 0.0 )
178 aTmpBuf.append( OUStringChar(aMinusSign) ).append( " " );
179 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength );
180 if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small
182 aTmpBuf.append( aValueString );
183 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
186 if ( aBuf.toString() == (mYName + " = ") )
187 aBuf.append( "0" );
189 return aBuf.makeStringAndClear();
192 } // namespace chart
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */