LibreOffice Module chart2 (master) 1
PotentialRegressionCurveCalculator.cxx
Go to the documentation of this file.
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
22#include <SpecialCharacters.hxx>
23
24#include <limits>
25#include <rtl/math.hxx>
26#include <rtl/ustrbuf.hxx>
27
28using namespace ::com::sun::star;
29
30namespace chart
31{
32
34 : m_fSlope(std::numeric_limits<double>::quiet_NaN())
35 , m_fIntercept(std::numeric_limits<double>::quiet_NaN())
36 , m_fSign(1.0)
37{
38}
39
41{}
42
43// ____ XRegressionCurveCalculator ____
45 const uno::Sequence< double >& aXValues,
46 const uno::Sequence< double >& aYValues )
47{
50 aXValues, aYValues,
52 m_fSign = 1.0;
53
54 size_t nMax = aValues.first.size();
55 if( nMax <= 1 ) // at least 2 points
56 {
58 aXValues, aYValues,
60 nMax = aValues.first.size();
61 if( nMax <= 1 )
62 {
63 m_fSlope = std::numeric_limits<double>::quiet_NaN();
64 m_fIntercept = std::numeric_limits<double>::quiet_NaN();
65 m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN();
66 return;
67 }
68 m_fSign = -1.0;
69 }
70
71 double fAverageX = 0.0, fAverageY = 0.0;
72 size_t i = 0;
73 for( i = 0; i < nMax; ++i )
74 {
75 fAverageX += log( aValues.first[i] );
76 fAverageY += log( m_fSign * aValues.second[i] );
77 }
78
79 const double fN = static_cast< double >( nMax );
80 fAverageX /= fN;
81 fAverageY /= fN;
82
83 double fQx = 0.0, fQy = 0.0, fQxy = 0.0;
84 for( i = 0; i < nMax; ++i )
85 {
86 double fDeltaX = log( aValues.first[i] ) - fAverageX;
87 double fDeltaY = log( m_fSign * aValues.second[i] ) - fAverageY;
88
89 fQx += fDeltaX * fDeltaX;
90 fQy += fDeltaY * fDeltaY;
91 fQxy += fDeltaX * fDeltaY;
92 }
93
94 m_fSlope = fQxy / fQx;
95 m_fIntercept = fAverageY - m_fSlope * fAverageX;
96 m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy );
97
99}
100
102{
103 if( ! ( std::isnan( m_fSlope ) ||
104 std::isnan( m_fIntercept )))
105 {
106 return m_fIntercept * pow( x, m_fSlope );
107 }
108
109 return std::numeric_limits<double>::quiet_NaN();
110}
111
113 double min, double max, ::sal_Int32 nPointCount,
114 const uno::Reference< chart2::XScaling >& xScalingX,
115 const uno::Reference< chart2::XScaling >& xScalingY,
116 sal_Bool bMaySkipPointsInCalculation )
117{
118 if( bMaySkipPointsInCalculation &&
119 isLogarithmicScaling( xScalingX ) &&
120 isLogarithmicScaling( xScalingY ))
121 {
122 // optimize result
124 { max, getCurveValue( max ) } };
125
126 return aResult;
127 }
128 return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation );
129}
130
132 const uno::Reference< util::XNumberFormatter >& xNumFormatter,
133 sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const
134{
135 bool bHasIntercept = !rtl::math::approxEqual( fabs(m_fIntercept), 1.0 );
136 OUStringBuffer aBuf( mYName + " = " );
137 sal_Int32 nLineLength = aBuf.getLength();
138 sal_Int32 nValueLength=0;
139 if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength
140 {
141 sal_Int32 nCharMin = nLineLength + mXName.getLength() + 3; // 3 = "^" + 2 extra characters
142 if ( m_fIntercept != 0.0 && m_fSlope != 0.0 )
143 {
144 if ( m_fIntercept < 0.0 )
145 nCharMin += 2; // "- "
146 if ( bHasIntercept )
147 nValueLength = (*pFormulaMaxWidth - nCharMin) / 2;
148 }
149 if ( nValueLength == 0 ) // not yet calculated
150 nValueLength = *pFormulaMaxWidth - nCharMin;
151 if ( nValueLength <= 0 )
152 nValueLength = 1;
153 }
154
155 if( m_fIntercept == 0.0 )
156 {
157 aBuf.append( '0' );
158 }
159 else
160 {
161 // temporary buffer
162 OUStringBuffer aTmpBuf("");
163 // if nValueLength not calculated then nullptr
164 sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr;
165 if ( m_fIntercept < 0.0 ) // add intercept value
166 aTmpBuf.append( OUStringChar(aMinusSign) + " " );
167 if( bHasIntercept )
168 {
169 OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength );
170 if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small
171 {
172 aTmpBuf.append( aValueString + " " );
173 }
174 }
175 if( m_fSlope != 0.0 ) // add slope value
176 {
177 aTmpBuf.append( mXName + "^" +
178 getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope, pValueLength ));
179 }
180 addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth );
181 }
182
183 return aBuf.makeStringAndClear();
184}
185
186} // namespace chart
187
188/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_Unicode aMinusSign
virtual double SAL_CALL getCurveValue(double x) override
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
virtual OUString ImplGetRepresentation(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, sal_Int32 *pFormulaLength=nullptr) const override
virtual void SAL_CALL recalculateRegression(const css::uno::Sequence< double > &aXValues, const css::uno::Sequence< double > &aYValues) override
static bool isLogarithmicScaling(const css::uno::Reference< css::chart2::XScaling > &xScaling)
static void addStringToEquation(OUStringBuffer &aStrEquation, sal_Int32 &nLineLength, OUStringBuffer const &aAddString, const sal_Int32 *pMaxLength)
static OUString getFormattedString(const css::uno::Reference< css::util::XNumberFormatter > &xNumFormatter, sal_Int32 nNumberFormatKey, double fNumber, const sal_Int32 *pStringLength)
virtual css::uno::Sequence< css::geometry::RealPoint2D > SAL_CALL getCurveValues(double min, double max, sal_Int32 nPointCount, const css::uno::Reference< css::chart2::XScaling > &xScalingX, const css::uno::Reference< css::chart2::XScaling > &xScalingY, sal_Bool bMaySkipPointsInCalculation) override
float x
#define max(a, b)
aBuf
std::pair< std::vector< double >, std::vector< double > > tDoubleVectorPair
tDoubleVectorPair cleanup(const css::uno::Sequence< double > &rXValues, const css::uno::Sequence< double > &rYValues, Pred aPred)
takes the given x- and y-values and copies them into the resulting pair, which contains x-values in t...
int i
log
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
unsigned char sal_Bool