Coverage Report

Created: 2026-04-09 11:41

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