/src/libreoffice/chart2/source/tools/PotentialRegressionCurveCalculator.cxx
Line | Count | Source |
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 | | |
20 | | #include <PotentialRegressionCurveCalculator.hxx> |
21 | | #include <RegressionCalculationHelper.hxx> |
22 | | #include <SpecialCharacters.hxx> |
23 | | |
24 | | #include <limits> |
25 | | #include <rtl/math.hxx> |
26 | | #include <rtl/ustrbuf.hxx> |
27 | | |
28 | | using namespace ::com::sun::star; |
29 | | |
30 | | namespace chart |
31 | | { |
32 | | |
33 | | PotentialRegressionCurveCalculator::PotentialRegressionCurveCalculator() |
34 | 0 | : m_fSlope(std::numeric_limits<double>::quiet_NaN()) |
35 | 0 | , m_fIntercept(std::numeric_limits<double>::quiet_NaN()) |
36 | 0 | , m_fSign(1.0) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | PotentialRegressionCurveCalculator::~PotentialRegressionCurveCalculator() |
41 | 0 | {} |
42 | | |
43 | | // ____ XRegressionCurveCalculator ____ |
44 | | void SAL_CALL PotentialRegressionCurveCalculator::recalculateRegression( |
45 | | const uno::Sequence< double >& aXValues, |
46 | | const uno::Sequence< double >& aYValues ) |
47 | 0 | { |
48 | 0 | RegressionCalculationHelper::tDoubleVectorPair aValues( |
49 | 0 | RegressionCalculationHelper::cleanup( |
50 | 0 | aXValues, aYValues, |
51 | 0 | RegressionCalculationHelper::isValidAndBothPositive())); |
52 | 0 | m_fSign = 1.0; |
53 | |
|
54 | 0 | size_t nMax = aValues.first.size(); |
55 | 0 | if( nMax <= 1 ) // at least 2 points |
56 | 0 | { |
57 | 0 | aValues = RegressionCalculationHelper::cleanup( |
58 | 0 | aXValues, aYValues, |
59 | 0 | RegressionCalculationHelper::isValidAndXPositiveAndYNegative()); |
60 | 0 | nMax = aValues.first.size(); |
61 | 0 | if( nMax <= 1 ) |
62 | 0 | { |
63 | 0 | m_fSlope = std::numeric_limits<double>::quiet_NaN(); |
64 | 0 | m_fIntercept = std::numeric_limits<double>::quiet_NaN(); |
65 | 0 | m_fCorrelationCoefficient = std::numeric_limits<double>::quiet_NaN(); |
66 | 0 | return; |
67 | 0 | } |
68 | 0 | m_fSign = -1.0; |
69 | 0 | } |
70 | | |
71 | 0 | double fAverageX = 0.0, fAverageY = 0.0; |
72 | 0 | size_t i = 0; |
73 | 0 | for( i = 0; i < nMax; ++i ) |
74 | 0 | { |
75 | 0 | fAverageX += log( aValues.first[i] ); |
76 | 0 | fAverageY += log( m_fSign * aValues.second[i] ); |
77 | 0 | } |
78 | |
|
79 | 0 | const double fN = static_cast< double >( nMax ); |
80 | 0 | fAverageX /= fN; |
81 | 0 | fAverageY /= fN; |
82 | |
|
83 | 0 | double fQx = 0.0, fQy = 0.0, fQxy = 0.0; |
84 | 0 | for( i = 0; i < nMax; ++i ) |
85 | 0 | { |
86 | 0 | double fDeltaX = log( aValues.first[i] ) - fAverageX; |
87 | 0 | double fDeltaY = log( m_fSign * aValues.second[i] ) - fAverageY; |
88 | |
|
89 | 0 | fQx += fDeltaX * fDeltaX; |
90 | 0 | fQy += fDeltaY * fDeltaY; |
91 | 0 | fQxy += fDeltaX * fDeltaY; |
92 | 0 | } |
93 | |
|
94 | 0 | m_fSlope = fQxy / fQx; |
95 | 0 | m_fIntercept = fAverageY - m_fSlope * fAverageX; |
96 | 0 | m_fCorrelationCoefficient = fQxy / sqrt( fQx * fQy ); |
97 | |
|
98 | 0 | m_fIntercept = m_fSign * exp( m_fIntercept ); |
99 | 0 | } |
100 | | |
101 | | double SAL_CALL PotentialRegressionCurveCalculator::getCurveValue( double x ) |
102 | 0 | { |
103 | 0 | if( ! ( std::isnan( m_fSlope ) || |
104 | 0 | std::isnan( m_fIntercept ))) |
105 | 0 | { |
106 | 0 | return m_fIntercept * pow( x, m_fSlope ); |
107 | 0 | } |
108 | | |
109 | 0 | return std::numeric_limits<double>::quiet_NaN(); |
110 | 0 | } |
111 | | |
112 | | uno::Sequence< geometry::RealPoint2D > SAL_CALL PotentialRegressionCurveCalculator::getCurveValues( |
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 | 0 | { |
118 | 0 | if( bMaySkipPointsInCalculation && |
119 | 0 | isLogarithmicScaling( xScalingX ) && |
120 | 0 | isLogarithmicScaling( xScalingY )) |
121 | 0 | { |
122 | | // optimize result |
123 | 0 | uno::Sequence< geometry::RealPoint2D > aResult{ { min, getCurveValue( min ) }, |
124 | 0 | { max, getCurveValue( max ) } }; |
125 | |
|
126 | 0 | return aResult; |
127 | 0 | } |
128 | 0 | return RegressionCurveCalculator::getCurveValues( min, max, nPointCount, xScalingX, xScalingY, bMaySkipPointsInCalculation ); |
129 | 0 | } |
130 | | |
131 | | OUString PotentialRegressionCurveCalculator::ImplGetRepresentation( |
132 | | const uno::Reference< util::XNumberFormatter >& xNumFormatter, |
133 | | sal_Int32 nNumberFormatKey, sal_Int32* pFormulaMaxWidth /* = nullptr */ ) const |
134 | 0 | { |
135 | 0 | bool bHasIntercept = !rtl::math::approxEqual( fabs(m_fIntercept), 1.0 ); |
136 | 0 | OUStringBuffer aBuf( mYName + " = " ); |
137 | 0 | sal_Int32 nLineLength = aBuf.getLength(); |
138 | 0 | sal_Int32 nValueLength=0; |
139 | 0 | if ( pFormulaMaxWidth && *pFormulaMaxWidth > 0 ) // count nValueLength |
140 | 0 | { |
141 | 0 | sal_Int32 nCharMin = nLineLength + mXName.getLength() + 3; // 3 = "^" + 2 extra characters |
142 | 0 | if ( m_fIntercept != 0.0 && m_fSlope != 0.0 ) |
143 | 0 | { |
144 | 0 | if ( m_fIntercept < 0.0 ) |
145 | 0 | nCharMin += 2; // "- " |
146 | 0 | if ( bHasIntercept ) |
147 | 0 | nValueLength = (*pFormulaMaxWidth - nCharMin) / 2; |
148 | 0 | } |
149 | 0 | if ( nValueLength == 0 ) // not yet calculated |
150 | 0 | nValueLength = *pFormulaMaxWidth - nCharMin; |
151 | 0 | if ( nValueLength <= 0 ) |
152 | 0 | nValueLength = 1; |
153 | 0 | } |
154 | |
|
155 | 0 | if( m_fIntercept == 0.0 ) |
156 | 0 | { |
157 | 0 | aBuf.append( '0' ); |
158 | 0 | } |
159 | 0 | else |
160 | 0 | { |
161 | | // temporary buffer |
162 | 0 | OUStringBuffer aTmpBuf(""); |
163 | | // if nValueLength not calculated then nullptr |
164 | 0 | sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; |
165 | 0 | if ( m_fIntercept < 0.0 ) // add intercept value |
166 | 0 | aTmpBuf.append( OUStringChar(aMinusSign) + " " ); |
167 | 0 | if( bHasIntercept ) |
168 | 0 | { |
169 | 0 | OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength ); |
170 | 0 | if ( aValueString != "1" ) // aValueString may be rounded to 1 if nValueLength is small |
171 | 0 | { |
172 | 0 | aTmpBuf.append( aValueString + " " ); |
173 | 0 | } |
174 | 0 | } |
175 | 0 | if( m_fSlope != 0.0 ) // add slope value |
176 | 0 | { |
177 | 0 | aTmpBuf.append( mXName + "^" + |
178 | 0 | getFormattedString( xNumFormatter, nNumberFormatKey, m_fSlope, pValueLength )); |
179 | 0 | } |
180 | 0 | addStringToEquation( aBuf, nLineLength, aTmpBuf, pFormulaMaxWidth ); |
181 | 0 | } |
182 | |
|
183 | 0 | return aBuf.makeStringAndClear(); |
184 | 0 | } |
185 | | |
186 | | } // namespace chart |
187 | | |
188 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |