SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
k13.c
Go to the documentation of this file.
1/**
2 * @file k13.c
3 * @brief Computes the Modified Bessel Function of the Second Kind K_{1/3}(z).
4 *
5 * This file provides the implementation of k13(), which computes the
6 * Modified Bessel Function of the Second Kind K_{1/3}(z) for a given input.
7 * It uses a series expansion for small arguments and an asymptotic expansion for
8 * larger arguments, referencing Abramowitz & Stegun and derived from Roger Dejus's k13.f code.
9 *
10 * @copyright
11 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
12 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
13 *
14 * @license
15 * This file is distributed under the terms of the Software License Agreement
16 * found in the file LICENSE included with this distribution.
17 *
18 * @author R. Dejus, H. Shang, R. Soliday
19 */
20
21#include "mdb.h"
22#include "constants.h"
23#include "math.h"
24
25#define A_LIM 10.1
26#define EPS1 1.0e-12
27#define EPS2 1.0e-8
28#define GAMMA_OF_NY 2.678938534707747898
29#define NY 1.0 / 3.0
30
31/**
32 * @brief Compute the Modified Bessel Function of the Second Kind K_{1/3}(z).
33 *
34 * This function calculates K_{1/3}(z) for inputs in the approximate range 0.0 < z < 60.0.
35 * For smaller z (z < A_LIM), it uses a series expansion, while for larger z,
36 * it employs an asymptotic expansion. The method and constants are adapted from
37 * the original Fortran implementation by Roger Dejus and mathematical formulas
38 * from Abramowitz & Stegun.
39 *
40 * @param z The input value for which K_{1/3}(z) is evaluated.
41 * @return The computed value of the Modified Bessel Function K_{1/3}(z).
42 */
43double k13(double z) {
44 double c1, c2, zs, ny_fac, neg_ny_fac, zm, zp, pm, pp, term, sum, ze, za, mu, pa, e2;
45 long k;
46
47 if (z < A_LIM) {
48 /*series expansion */
49 c1 = PI / 2.0 / sin(PI * NY);
50 c2 = 2.0 * c1;
51 zs = z * z / 4.0;
52 ny_fac = NY * GAMMA_OF_NY; /*Factorial of (+ny)*/
53 neg_ny_fac = c2 / GAMMA_OF_NY; /* Factorial of (-ny) */
54 e2 = pow(z / 2.0, NY);
55 zm = 1 / e2 / neg_ny_fac;
56 zp = e2 / ny_fac;
57 /*first term */
58 pm = pp = 1.0;
59 sum = term = c1 * (pm * zm - pp * zp);
60 /* additional term */
61 k = 0;
62 while (fabs(term) > EPS1 * sum) {
63 k = k + 1;
64 pm = pm * zs / (k * (k - NY));
65 pp = pp * zs / (k * (k + NY));
66 term = c1 * (pm * zm - pp * zp);
67 sum += term;
68 }
69 } else {
70 /* Use asymptotic expansion for large arguments */
71 ze = sqrt(PI / 2.0 / z) * exp(-z);
72 za = 1.0 / (8.0 * z);
73 mu = 4.0 * NY * NY;
74 /* first term */
75 pa = 1.0;
76 sum = term = pa * ze;
77 /*Additional terms */
78 k = 0;
79 while (fabs(term) > EPS2 * sum) {
80 k = k + 1;
81 pa = pa * za * (mu - (2 * k - 1) * (2 * k - 1)) / k;
82 term = pa * ze;
83 sum += term;
84 }
85 }
86 return sum;
87}
double k13(double z)
Compute the Modified Bessel Function of the Second Kind K_{1/3}(z).
Definition k13.c:43