LibreOffice Module scaddins (master) 1
bessel.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
20#include "bessel.hxx"
21#include <cmath>
22#include <rtl/math.hxx>
23
24#include <com/sun/star/lang/IllegalArgumentException.hpp>
25#include <com/sun/star/sheet/NoConvergenceException.hpp>
26
27using ::com::sun::star::lang::IllegalArgumentException;
28using ::com::sun::star::sheet::NoConvergenceException;
29
30namespace sca::analysis {
31
32// BESSEL J
33
34
35/* The BESSEL function, first kind, unmodified:
36 The algorithm follows
37 http://www.reference-global.com/isbn/978-3-11-020354-7
38 Numerical Mathematics 1 / Numerische Mathematik 1,
39 An algorithm-based introduction / Eine algorithmisch orientierte Einfuehrung
40 Deuflhard, Peter; Hohmann, Andreas
41 Berlin, New York (Walter de Gruyter) 2008
42 4. ueberarb. u. erw. Aufl. 2008
43 eBook ISBN: 978-3-11-020355-4
44 Chapter 6.3.2 , algorithm 6.24
45 The source is in German.
46 The BesselJ-function is a special case of the adjoint summation with
47 a_k = 2*(k-1)/x for k=1,...
48 b_k = -1, for all k, directly substituted
49 m_0=1, m_k=2 for k even, and m_k=0 for k odd, calculated on the fly
50 alpha_k=1 for k=N and alpha_k=0 otherwise
51*/
52
53double BesselJ( double x, sal_Int32 N )
54
55{
56 if( N < 0 )
57 throw IllegalArgumentException();
58 if (x==0.0)
59 return (N==0) ? 1.0 : 0.0;
60
61 /* The algorithm works only for x>0, therefore remember sign. BesselJ
62 with integer order N is an even function for even N (means J(-x)=J(x))
63 and an odd function for odd N (means J(-x)=-J(x)).*/
64 double fSign = (N % 2 == 1 && x < 0) ? -1.0 : 1.0;
65 double fX = fabs(x);
66
67 const double fMaxIteration = 9000000.0; //experimental, for to return in < 3 seconds
68 double fEstimateIteration = fX * 1.5 + N;
69 bool bAsymptoticPossible = pow(fX,0.4) > N;
70 if (fEstimateIteration > fMaxIteration)
71 {
72 if (!bAsymptoticPossible)
73 throw NoConvergenceException();
74 return fSign * sqrt(M_2_PI/fX)* cos(fX-N*M_PI_2-M_PI_4);
75 }
76
77 double const epsilon = 1.0e-15; // relative error
78 bool bHasfound = false;
79 double k= 0.0;
80 // e_{-1} = 0; e_0 = alpha_0 / b_2
81 double u ; // u_0 = e_0/f_0 = alpha_0/m_0 = alpha_0
82
83 // first used with k=1
84 double m_bar; // m_bar_k = m_k * f_bar_{k-1}
85 double g_bar; // g_bar_k = m_bar_k - a_{k+1} + g_{k-1}
86 double g_bar_delta_u; // g_bar_delta_u_k = f_bar_{k-1} * alpha_k
87 // - g_{k-1} * delta_u_{k-1} - m_bar_k * u_{k-1}
88 // f_{-1} = 0.0; f_0 = m_0 / b_2 = 1/(-1) = -1
89 double g = 0.0; // g_0= f_{-1} / f_0 = 0/(-1) = 0
90 double delta_u = 0.0; // dummy initialize, first used with * 0
91 double f_bar = -1.0; // f_bar_k = 1/f_k, but only used for k=0
92
93 if (N==0)
94 {
95 //k=0; alpha_0 = 1.0
96 u = 1.0; // u_0 = alpha_0
97 // k = 1.0; at least one step is necessary
98 // m_bar_k = m_k * f_bar_{k-1} ==> m_bar_1 = 0.0
99 g_bar_delta_u = 0.0; // alpha_k = 0.0, m_bar = 0.0; g= 0.0
100 g_bar = - 2.0/fX; // k = 1.0, g = 0.0
101 delta_u = g_bar_delta_u / g_bar;
102 u = u + delta_u ; // u_k = u_{k-1} + delta_u_k
103 g = -1.0 / g_bar; // g_k=b_{k+2}/g_bar_k
104 f_bar = f_bar * g; // f_bar_k = f_bar_{k-1}* g_k
105 k = 2.0;
106 // From now on all alpha_k = 0.0 and k > N+1
107 }
108 else
109 { // N >= 1 and alpha_k = 0.0 for k<N
110 u=0.0; // u_0 = alpha_0
111 for (k =1.0; k<= N-1; k = k + 1.0)
112 {
113 m_bar=2.0 * fmod(k-1.0, 2.0) * f_bar;
114 g_bar_delta_u = - g * delta_u - m_bar * u; // alpha_k = 0.0
115 g_bar = m_bar - 2.0*k/fX + g;
116 delta_u = g_bar_delta_u / g_bar;
117 u = u + delta_u;
118 g = -1.0/g_bar;
119 f_bar=f_bar * g;
120 }
121 // Step alpha_N = 1.0
122 m_bar=2.0 * fmod(k-1.0, 2.0) * f_bar;
123 g_bar_delta_u = f_bar - g * delta_u - m_bar * u; // alpha_k = 1.0
124 g_bar = m_bar - 2.0*k/fX + g;
125 delta_u = g_bar_delta_u / g_bar;
126 u = u + delta_u;
127 g = -1.0/g_bar;
128 f_bar = f_bar * g;
129 k = k + 1.0;
130 }
131 // Loop until desired accuracy, always alpha_k = 0.0
132 do
133 {
134 m_bar = 2.0 * fmod(k-1.0, 2.0) * f_bar;
135 g_bar_delta_u = - g * delta_u - m_bar * u;
136 g_bar = m_bar - 2.0*k/fX + g;
137 delta_u = g_bar_delta_u / g_bar;
138 u = u + delta_u;
139 g = -1.0/g_bar;
140 f_bar = f_bar * g;
141 bHasfound = (fabs(delta_u)<=fabs(u)*epsilon);
142 k = k + 1.0;
143 }
144 while (!bHasfound && k <= fMaxIteration);
145 if (!bHasfound)
146 throw NoConvergenceException(); // unlikely to happen
147
148 return u * fSign;
149}
150
151
152// BESSEL I
153
154
155/* The BESSEL function, first kind, modified:
156
157 inf (x/2)^(n+2k)
158 I_n(x) = SUM TERM(n,k) with TERM(n,k) := --------------
159 k=0 k! (n+k)!
160
161 No asymptotic approximation used, see issue 43040.
162 */
163
164double BesselI( double x, sal_Int32 n )
165{
166 const sal_Int32 nMaxIteration = 2000;
167 const double fXHalf = x / 2.0;
168 if( n < 0 )
169 throw IllegalArgumentException();
170
171 double fResult = 0.0;
172
173 /* Start the iteration without TERM(n,0), which is set here.
174
175 TERM(n,0) = (x/2)^n / n!
176 */
177 sal_Int32 nK = 0;
178 double fTerm = 1.0;
179 // avoid overflow in Fak(n)
180 for( nK = 1; nK <= n; ++nK )
181 {
182 fTerm = fTerm / static_cast< double >( nK ) * fXHalf;
183 }
184 fResult = fTerm; // Start result with TERM(n,0).
185 if( fTerm != 0.0 )
186 {
187 nK = 1;
188 const double fEpsilon = 1.0E-15;
189 do
190 {
191 /* Calculation of TERM(n,k) from TERM(n,k-1):
192
193 (x/2)^(n+2k)
194 TERM(n,k) = --------------
195 k! (n+k)!
196
197 (x/2)^2 (x/2)^(n+2(k-1))
198 = --------------------------
199 k (k-1)! (n+k) (n+k-1)!
200
201 (x/2)^2 (x/2)^(n+2(k-1))
202 = --------- * ------------------
203 k(n+k) (k-1)! (n+k-1)!
204
205 x^2/4
206 = -------- TERM(n,k-1)
207 k(n+k)
208 */
209 fTerm = fTerm * fXHalf / static_cast<double>(nK) * fXHalf / static_cast<double>(nK+n);
210 fResult += fTerm;
211 nK++;
212 }
213 while( (fabs( fTerm ) > fabs(fResult) * fEpsilon) && (nK < nMaxIteration) );
214
215 }
216 return fResult;
217}
218
221static double Besselk0( double fNum )
222{
223 double fRet;
224
225 if( fNum <= 2.0 )
226 {
227 double fNum2 = fNum * 0.5;
228 double y = fNum2 * fNum2;
229
230 fRet = -log( fNum2 ) * BesselI( fNum, 0 ) +
231 ( -0.57721566 + y * ( 0.42278420 + y * ( 0.23069756 + y * ( 0.3488590e-1 +
232 y * ( 0.262698e-2 + y * ( 0.10750e-3 + y * 0.74e-5 ) ) ) ) ) );
233 }
234 else
235 {
236 double y = 2.0 / fNum;
237
238 fRet = exp( -fNum ) / sqrt( fNum ) * ( 1.25331414 + y * ( -0.7832358e-1 +
239 y * ( 0.2189568e-1 + y * ( -0.1062446e-1 + y * ( 0.587872e-2 +
240 y * ( -0.251540e-2 + y * 0.53208e-3 ) ) ) ) ) );
241 }
242
243 return fRet;
244}
245
248static double Besselk1( double fNum )
249{
250 double fRet;
251
252 if( fNum <= 2.0 )
253 {
254 double fNum2 = fNum * 0.5;
255 double y = fNum2 * fNum2;
256
257 fRet = log( fNum2 ) * BesselI( fNum, 1 ) +
258 ( 1.0 + y * ( 0.15443144 + y * ( -0.67278579 + y * ( -0.18156897 + y * ( -0.1919402e-1 +
259 y * ( -0.110404e-2 + y * -0.4686e-4 ) ) ) ) ) )
260 / fNum;
261 }
262 else
263 {
264 double y = 2.0 / fNum;
265
266 fRet = exp( -fNum ) / sqrt( fNum ) * ( 1.25331414 + y * ( 0.23498619 +
267 y * ( -0.3655620e-1 + y * ( 0.1504268e-1 + y * ( -0.780353e-2 +
268 y * ( 0.325614e-2 + y * -0.68245e-3 ) ) ) ) ) );
269 }
270
271 return fRet;
272}
273
274
275double BesselK( double fNum, sal_Int32 nOrder )
276{
277 switch( nOrder )
278 {
279 case 0: return Besselk0( fNum );
280 case 1: return Besselk1( fNum );
281 default:
282 {
283 double fTox = 2.0 / fNum;
284 double fBkm = Besselk0( fNum );
285 double fBk = Besselk1( fNum );
286
287 for( sal_Int32 n = 1 ; n < nOrder ; n++ )
288 {
289 const double fBkp = fBkm + double( n ) * fTox * fBk;
290 fBkm = fBk;
291 fBk = fBkp;
292 }
293
294 return fBk;
295 }
296 }
297}
298
299
300// BESSEL Y
301
302
303/* The BESSEL function, second kind, unmodified:
304 The algorithm for order 0 and for order 1 follows
305 http://www.reference-global.com/isbn/978-3-11-020354-7
306 Numerical Mathematics 1 / Numerische Mathematik 1,
307 An algorithm-based introduction / Eine algorithmisch orientierte Einfuehrung
308 Deuflhard, Peter; Hohmann, Andreas
309 Berlin, New York (Walter de Gruyter) 2008
310 4. ueberarb. u. erw. Aufl. 2008
311 eBook ISBN: 978-3-11-020355-4
312 Chapter 6.3.2 , algorithm 6.24
313 The source is in German.
314 See #i31656# for a commented version of the implementation, attachment #desc6
315 https://bz.apache.org/ooo/attachment.cgi?id=63609
316*/
317
320static double Bessely0( double fX )
321{
322 // If fX > 2^64 then sin and cos fail
323 if (fX <= 0 || !rtl::math::isValidArcArg(fX))
324 throw IllegalArgumentException();
325 const double fMaxIteration = 9000000.0; // should not be reached
326 if (fX > 5.0e+6) // iteration is not considerable better then approximation
327 return sqrt(1/M_PI/fX)
328 *(std::sin(fX)-std::cos(fX));
329 const double epsilon = 1.0e-15;
330 const double EulerGamma = 0.57721566490153286060;
331 double alpha = log(fX/2.0)+EulerGamma;
332 double u = alpha;
333
334 double k = 1.0;
335 double g_bar_delta_u = 0.0;
336 double g_bar = -2.0 / fX;
337 double delta_u = g_bar_delta_u / g_bar;
338 double g = -1.0/g_bar;
339 double f_bar = -1 * g;
340
341 double sign_alpha = 1.0;
342 bool bHasFound = false;
343 k = k + 1;
344 do
345 {
346 double km1mod2 = fmod(k-1.0, 2.0);
347 double m_bar = (2.0*km1mod2) * f_bar;
348 if (km1mod2 == 0.0)
349 alpha = 0.0;
350 else
351 {
352 alpha = sign_alpha * (4.0/k);
353 sign_alpha = -sign_alpha;
354 }
355 g_bar_delta_u = f_bar * alpha - g * delta_u - m_bar * u;
356 g_bar = m_bar - (2.0*k)/fX + g;
357 delta_u = g_bar_delta_u / g_bar;
358 u = u+delta_u;
359 g = -1.0 / g_bar;
360 f_bar = f_bar*g;
361 bHasFound = (fabs(delta_u)<=fabs(u)*epsilon);
362 k=k+1;
363 }
364 while (!bHasFound && k<fMaxIteration);
365 if (!bHasFound)
366 throw NoConvergenceException(); // not likely to happen
367 return u*M_2_PI;
368}
369
370// See #i31656# for a commented version of this implementation, attachment #desc6
371// https://bz.apache.org/ooo/attachment.cgi?id=63609
374static double Bessely1( double fX )
375{
376 // If fX > 2^64 then sin and cos fail
377 if (fX <= 0 || !rtl::math::isValidArcArg(fX))
378 throw IllegalArgumentException();
379 const double fMaxIteration = 9000000.0; // should not be reached
380 if (fX > 5.0e+6) // iteration is not considerable better then approximation
381 return - sqrt(1/M_PI/fX)
382 *(std::sin(fX)+std::cos(fX));
383 const double epsilon = 1.0e-15;
384 const double EulerGamma = 0.57721566490153286060;
385 double alpha = 1.0/fX;
386 double f_bar = -1.0;
387 double u = alpha;
388 double k = 1.0;
389 alpha = 1.0 - EulerGamma - log(fX/2.0);
390 double g_bar_delta_u = -alpha;
391 double g_bar = -2.0 / fX;
392 double delta_u = g_bar_delta_u / g_bar;
393 u = u + delta_u;
394 double g = -1.0/g_bar;
395 f_bar = f_bar * g;
396 double sign_alpha = -1.0;
397 bool bHasFound = false;
398 k = k + 1.0;
399 do
400 {
401 double km1mod2 = fmod(k-1.0,2.0);
402 double m_bar = (2.0*km1mod2) * f_bar;
403 double q = (k-1.0)/2.0;
404 if (km1mod2 == 0.0) // k is odd
405 {
406 alpha = sign_alpha * (1.0/q + 1.0/(q+1.0));
407 sign_alpha = -sign_alpha;
408 }
409 else
410 alpha = 0.0;
411 g_bar_delta_u = f_bar * alpha - g * delta_u - m_bar * u;
412 g_bar = m_bar - (2.0*k)/fX + g;
413 delta_u = g_bar_delta_u / g_bar;
414 u = u+delta_u;
415 g = -1.0 / g_bar;
416 f_bar = f_bar*g;
417 bHasFound = (fabs(delta_u)<=fabs(u)*epsilon);
418 k=k+1;
419 }
420 while (!bHasFound && k<fMaxIteration);
421 if (!bHasFound)
422 throw NoConvergenceException();
423 return -u*2.0/M_PI;
424}
425
426double BesselY( double fNum, sal_Int32 nOrder )
427{
428 switch( nOrder )
429 {
430 case 0: return Bessely0( fNum );
431 case 1: return Bessely1( fNum );
432 default:
433 {
434 double fTox = 2.0 / fNum;
435 double fBym = Bessely0( fNum );
436 double fBy = Bessely1( fNum );
437
438 for( sal_Int32 n = 1 ; n < nOrder ; n++ )
439 {
440 const double fByp = double( n ) * fTox * fBy - fBym;
441 fBym = fBy;
442 fBy = fByp;
443 }
444
445 return fBy;
446 }
447 }
448}
449
450} // namespace sca::analysis
451
452/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
float u
float y
float x
sal_Int64 n
constexpr double alpha[nDetails]
log
static double Besselk0(double fNum)
Definition: bessel.cxx:221
static double Bessely0(double fX)
Definition: bessel.cxx:320
double BesselI(double x, sal_Int32 n)
Returns the result for the modified BESSEL function of first kind (I), n-th order,...
Definition: bessel.cxx:164
double BesselK(double fNum, sal_Int32 nOrder)
Returns the result for the modified BESSEL function of second kind (K), n-th order,...
Definition: bessel.cxx:275
double BesselY(double fNum, sal_Int32 nOrder)
Returns the result for the unmodified BESSEL function of second kind (Y), n-th order,...
Definition: bessel.cxx:426
static double Besselk1(double fNum)
Definition: bessel.cxx:248
double BesselJ(double x, sal_Int32 N)
Returns the result for the unmodified BESSEL function of first kind (J), n-th order,...
Definition: bessel.cxx:53
static double Bessely1(double fX)
Definition: bessel.cxx:374
#define N