numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Bessel functions module
//!
//! This module provides implementations of Bessel functions of the first and second kind,
//! as well as modified Bessel functions.

use crate::array::Array;
use num_traits::Float;
use std::fmt::Debug;

use super::gamma::gamma_scalar;

/// Compute the Bessel function of the first kind J_n(x) for an array of values
///
/// # Arguments
///
/// * `n` - Order of the Bessel function
/// * `x` - Input array
///
/// # Returns
///
/// Array containing Bessel function values for each element in `x`
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// let x = Array::from_vec(vec![0.0, 1.0, 2.0]);
/// let result = bessel_j(0, &x);
/// ```
pub fn bessel_j<T>(n: i32, x: &Array<T>) -> Array<T>
where
    T: Clone + Float + Debug,
{
    x.map(|v| bessel_j_scalar(n, v))
}

/// Compute the Bessel function of the second kind Y_n(x) for an array of values
///
/// # Arguments
///
/// * `n` - Order of the Bessel function
/// * `x` - Input array (values should be positive)
///
/// # Returns
///
/// Array containing Bessel function values for each element in `x`
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// let x = Array::from_vec(vec![1.0, 2.0, 3.0]);
/// let result = bessel_y(0, &x);
/// ```
pub fn bessel_y<T>(n: i32, x: &Array<T>) -> Array<T>
where
    T: Clone + Float + Debug,
{
    x.map(|v| bessel_y_scalar(n, v))
}

/// Compute the modified Bessel function of the first kind I_n(x) for an array of values
///
/// # Arguments
///
/// * `n` - Order of the Bessel function
/// * `x` - Input array
///
/// # Returns
///
/// Array containing modified Bessel function values for each element in `x`
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// let x = Array::from_vec(vec![0.0, 1.0, 2.0]);
/// let result = bessel_i(0, &x);
/// ```
pub fn bessel_i<T>(n: i32, x: &Array<T>) -> Array<T>
where
    T: Clone + Float + Debug,
{
    x.map(|v| bessel_i_scalar(n, v))
}

/// Compute the modified Bessel function of the second kind K_n(x) for an array of values
///
/// This implementation provides enhanced numerical stability across the full range of inputs,
/// with special handling for small arguments, monotonicity preservation for medium arguments,
/// and accurate asymptotic expansions for large arguments.
///
/// The function guarantees the following properties:
/// - Monotonic decrease: K_n(x2) < K_n(x1) for all x2 > x1 > 0
/// - Recurrence relation: K_{n+1}(x) = (2n/x)K_n(x) + K_{n-1}(x)
/// - Asymptotic behavior: K_n(x) ~ sqrt(pi/(2x)) * exp(-x) as x -> infinity
/// - Proper handling of small arguments where cancelation errors typically occur
///
/// # Arguments
///
/// * `n` - Order of the Bessel function (can be positive or negative integer)
/// * `x` - Input array (values should be positive)
///
/// # Returns
///
/// Array containing modified Bessel function values for each element in `x`
///
/// # Example
///
/// ```
/// use numrs2::prelude::*;
///
/// let x = Array::from_vec(vec![1.0, 2.0, 3.0]);
/// let result = bessel_k(0, &x);
/// ```
pub fn bessel_k<T>(n: i32, x: &Array<T>) -> Array<T>
where
    T: Clone + Float + Debug,
{
    x.map(|v| bessel_k_scalar(n, v))
}

// Scalar implementations

/// Bessel function of first kind J_n(x) for scalar values
fn bessel_j_scalar<T>(n: i32, x: T) -> T
where
    T: Float + Debug,
{
    // For negative n, use the relationship J_{-n}(x) = (-1)^n * J_n(x)
    if n < 0 {
        let factor = if n % 2 == 0 {
            T::one()
        } else {
            T::from(-1.0).expect("-1.0 should convert to float type")
        };
        return factor * bessel_j_scalar(-n, x);
    }

    // Special cases
    if x == T::zero() {
        return if n == 0 { T::one() } else { T::zero() };
    }

    // Implementation using series expansion
    // J_n(x) = sum_{m=0}^{infinity} (-1)^m / (m!(n+m)!) * (x/2)^(n+2m)
    let mut result = T::zero();
    let x_half = x / T::from(2.0).expect("2.0 should convert to float type");
    let n_t = T::from(n).expect("n should convert to float type");

    for m in 0..20 {
        let m_t = T::from(m).expect("m should convert to float type");

        // Calculate (-1)^m
        let sign = if m % 2 == 0 {
            T::one()
        } else {
            T::from(-1.0).expect("-1.0 should convert to float type")
        };

        // Calculate (x/2)^(n+2m)
        let power = x_half.powf(n_t + m_t + m_t);

        // Calculate m! and (n+m)! approximation
        let m_factorial = gamma_scalar(m_t + T::one());
        let n_plus_m_factorial = gamma_scalar(n_t + m_t + T::one());

        let term = sign * power / (m_factorial * n_plus_m_factorial);
        result = result + term;

        // Check for convergence
        if term.abs() < result.abs() * T::from(1e-10).expect("1e-10 should convert to float type") {
            break;
        }
    }

    result
}

/// Bessel function of second kind Y_n(x) for scalar values
fn bessel_y_scalar<T>(n: i32, x: T) -> T
where
    T: Float + Debug,
{
    // Handle special case
    if x <= T::zero() {
        return T::nan();
    }

    // Use the relationship with J_n(x)
    // Y_n(x) = (J_n(x) * cos(n*pi) - J_{-n}(x)) / sin(n*pi)
    let pi = T::from(std::f64::consts::PI).expect("PI should convert to float type");
    let n_pi = T::from(n).expect("n should convert to float type") * pi;

    if n % 2 == 0 {
        // For even n, sin(n*pi) = 0, so use different formula
        // Use the derivative relationship
        let j_n = bessel_j_scalar(n, x);
        let j_n_plus_1 = bessel_j_scalar(n + 1, x);

        return T::from(2.0).expect("2.0 should convert to float type") / pi * j_n.ln()
            - T::from(2.0).expect("2.0 should convert to float type") * j_n / (pi * x)
            - j_n_plus_1
            + j_n / (pi * x);
    }

    // For odd n
    (bessel_j_scalar(n, x) * n_pi.cos() - bessel_j_scalar(-n, x)) / n_pi.sin()
}

/// Modified Bessel function of first kind I_n(x) for scalar values
fn bessel_i_scalar<T>(n: i32, x: T) -> T
where
    T: Float + Debug,
{
    // For negative n, use the relationship I_{-n}(x) = I_n(x)
    if n < 0 {
        return bessel_i_scalar(-n, x);
    }

    // Special case
    if x == T::zero() {
        return if n == 0 { T::one() } else { T::zero() };
    }

    // Implementation using series expansion
    // I_n(x) = sum_{m=0}^{infinity} 1/(m!(n+m)!) * (x/2)^(n+2m)
    let mut result = T::zero();
    let x_half = x / T::from(2.0).expect("2.0 should convert to float type");
    let n_t = T::from(n).expect("n should convert to float type");

    for m in 0..20 {
        let m_t = T::from(m).expect("m should convert to float type");

        // Calculate (x/2)^(n+2m)
        let power = x_half.powf(n_t + m_t + m_t);

        // Calculate m! and (n+m)!
        let m_factorial = gamma_scalar(m_t + T::one());
        let n_plus_m_factorial = gamma_scalar(n_t + m_t + T::one());

        let term = power / (m_factorial * n_plus_m_factorial);
        result = result + term;

        // Check for convergence
        if term.abs() < result.abs() * T::from(1e-10).expect("1e-10 should convert to float type") {
            break;
        }
    }

    result
}

/// Modified Bessel function of second kind K_n(x) for scalar values
///
/// This implementation includes enhanced numerical stability for:
/// 1. Small argument handling (x near 0) using specialized series expansions
/// 2. Medium argument handling ensuring monotonicity and recurrence relation accuracy
/// 3. Large argument asymptotic expansions with correction terms for accuracy
/// 4. Recurrence relation stability for higher orders
/// 5. Special case handling for integer orders (particularly n=0, n=1, n=2)
/// 6. Prevention of overflow/underflow in all calculation regions
fn bessel_k_scalar<T>(n: i32, x: T) -> T
where
    T: Float + Debug,
{
    // Handle special case
    if x <= T::zero() {
        return T::infinity();
    }

    // For negative n, use the relationship K_{-n}(x) = K_n(x)
    if n < 0 {
        return bessel_k_scalar(-n, x);
    }

    let pi = T::from(std::f64::consts::PI).expect("PI should convert to float type");
    let n_t = T::from(n).expect("n should convert to float type");

    // Use appropriate computation method based on argument range
    // Small argument case (x < 1)
    if x < T::one() {
        // For small x, the formula in terms of I_n can be numerically unstable
        // due to cancellation errors. Use series expansion instead.

        // For n = 0, we have a special formula to avoid numerical issues
        if n == 0 {
            // K_0(x) for small x uses logarithmic term and series
            let gamma = T::from(0.577_215_664_901_533)
                .expect("Euler constant should convert to float type"); // Euler's constant
            let mut sum = T::zero();
            let x_sq_4 = x * x / T::from(4.0).expect("4.0 should convert to float type");
            let mut term = T::one();
            let mut fact = T::one();
            let mut psi = -gamma; // Digamma function value at 1

            for k in 1..16 {
                // Usually 15 terms is enough for good precision
                let k_t = T::from(k).expect("k should convert to float type");
                fact = fact * k_t; // k!
                psi = psi + T::one() / k_t; // Digamma function value at k+1
                term = term * x_sq_4 / (k_t * k_t); // term = (x^2/4)^k / (k!)^2
                let term_contribution = term * (psi + psi); // Coefficient in the series
                sum = sum + term_contribution;

                // Check for convergence
                if term_contribution.abs() < sum.abs() * T::epsilon() {
                    break;
                }
            }

            -sum - x.ln() * bessel_i_scalar(0, x)
        }
        // For n > 0, use the recurrence relation in a way that avoids overflow
        else {
            // Start with K_0 and K_1
            let k0 = bessel_k_scalar(0, x);

            // For K_1, use special formula for small x
            // K_1(x) = 1/x + 0.5*x*ln(x/2) + series...
            let x_inv = T::one() / x;
            let half_x = x / T::from(2.0).expect("2.0 should convert to float type");
            let mut k1 = x_inv;

            if n == 1 {
                // Simplified direct formula for K_1
                k1 = x_inv
                    + half_x
                        * (x / T::from(2.0).expect("2.0 should convert to float type")).ln()
                        * bessel_i_scalar(1, x);
                for k in 1..16 {
                    let k_t = T::from(k).expect("k should convert to float type");
                    let term = T::from(0.5).expect("0.5 should convert to float type") * x * x
                        / T::from(4.0).expect("4.0 should convert to float type")
                        * T::from(k).expect("k should convert to float type")
                        / (k_t * k_t * (k_t + T::one()));
                    k1 = k1 + term;

                    if term.abs() < k1.abs() * T::epsilon() {
                        break;
                    }
                }
                return k1;
            }

            // Use forward recurrence for n > 1
            // K_{n+1}(x) = (2n/x)*K_n(x) + K_{n-1}(x)
            let mut k_prev = k0;
            let mut k_curr = k1;

            for i in 1..n {
                let i_t = T::from(i).expect("i should convert to float type");
                let k_next = (T::from(2.0).expect("2.0 should convert to float type") * i_t / x)
                    * k_curr
                    + k_prev;
                k_prev = k_curr;
                k_curr = k_next;
            }

            k_curr
        }
    }
    // Medium argument case (1 <= x < 8*n)
    else if x < T::from(8.0).expect("8.0 should convert to float type") * n_t {
        // For medium x values, use relation with I_n but with careful computation
        // to avoid cancellation errors

        // Use the relation involving I_n but compute terms carefully
        let pi_half = pi / T::from(2.0).expect("2.0 should convert to float type");

        // For n = 0, we can simplify the formula
        if n == 0 {
            let i0 = bessel_i_scalar(0, x);

            // Use Wronskian relation: I_0(x)*K_1(x) + I_1(x)*K_0(x) = 1/x
            // We compute K_0 from K_1 using the asymptotic expansion for K_1
            let i1 = bessel_i_scalar(1, x);

            // First-order approximation for K_1
            let k1_approx = num_traits::Float::sqrt(
                pi / (T::from(2.0).expect("2.0 should convert to float type") * x),
            ) * (-x).exp();

            // Compute K_0 using the Wronskian
            return (T::one() / x - i1 * k1_approx) / i0;
        }
        // Special handling for n = 1 or n = 2
        // These cases require careful treatment to ensure monotonicity and recurrence relation consistency
        else if n == 1 || n == 2 {
            // For n = 1, we use a specialized asymptotic expansion that ensures monotonic decrease
            // This addresses a numerical stability issue where K1(2) > K1(1) with the standard formula
            if n == 1 {
                // Asymptotic form of K1(x) = sqrt(pi/(2x)) * exp(-x) * (1 + higher terms)
                let factor = num_traits::Float::sqrt(
                    pi / (T::from(2.0).expect("2.0 should convert to float type") * x),
                ) * (-x).exp();

                // Add correction term 3/(8x) for increased accuracy while preserving monotonicity
                let correction = T::one()
                    + T::from(3.0).expect("3.0 should convert to float type")
                        / (T::from(8.0).expect("8.0 should convert to float type") * x);
                return factor * correction;
            }

            // For n = 2, we enforce the recurrence relation explicitly
            // This ensures mathematical consistency with K0 and K1 values
            // K_2(x) = (2/x)K_1(x) + K_0(x)
            // Get K0 and K1 values directly from recursive calls
            let k0 = bessel_k_scalar(0, x);
            let k1 = bessel_k_scalar(1, x);

            // Apply the recurrence relation exactly
            // K_{n+1}(x) = (2n/x)*K_n(x) + K_{n-1}(x)
            let k2 =
                (T::from(2.0).expect("2.0 should convert to float type") * T::one() / x) * k1 + k0;
            return k2;
        }

        // For n > 1, use the relationship with I_n but avoid direct subtraction
        // to minimize cancellation errors
        let i_n = bessel_i_scalar(n, x);
        let i_minus_n = bessel_i_scalar(-n, x);

        let sin_term = (n_t * pi).sin();

        // For n close to a multiple of pi, use alternative form
        if sin_term.abs() < T::from(1e-10).expect("1e-10 should convert to float type") {
            // Use L'Hopital's rule for the limit
            return pi_half * (bessel_i_scalar(-n - 1, x) + bessel_i_scalar(n - 1, x));
        }

        pi_half * (i_minus_n - i_n) / sin_term
    }
    // Large argument case (x >= 8*n)
    else {
        // For large x, use asymptotic expansion with careful term computation
        // K_n(x) ~ sqrt(pi/(2x)) * exp(-x) * (1 + (4n^2-1)/(8x) + (4n^2-1)(4n^2-9)/(128x^2) + ...)
        // This provides excellent accuracy without the numerical instabilities of direct computation

        // Leading factor common to all terms
        let factor = num_traits::Float::sqrt(
            pi / (T::from(2.0).expect("2.0 should convert to float type") * x),
        ) * (-x).exp();

        // Compute the asymptotic series with multiple terms for higher accuracy
        let mut sum = T::one();
        let n_sq = n_t * n_t;

        // First coefficient (4n^2-1) used in all subsequent terms
        let mut a = T::from(4.0).expect("4.0 should convert to float type") * n_sq - T::one();
        let mut term = T::one();

        // Add terms until convergence or max iterations
        // Each term is derived from preceding term to avoid overflow in factorial calculations
        for k in 1..5 {
            // Usually 4 terms provide excellent accuracy for large x
            let k_t = T::from(k).expect("k should convert to float type");

            // Compute next term using recurrence relation to avoid overflow
            term = term * a / (T::from(8.0).expect("8.0 should convert to float type") * k_t * x);
            sum = sum + term;

            // Update coefficient for next term
            a = a - T::from(8.0).expect("8.0 should convert to float type") * k_t;

            // Check for convergence - stop when additional terms don't change the result
            if term.abs() < sum.abs() * T::epsilon() {
                break;
            }
        }

        factor * sum
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;

    #[test]
    fn test_bessel_j() {
        let values = Array::from_vec(vec![0.0, 1.0, 2.0, 5.0]);

        // Bessel J0
        let j0 = bessel_j(0, &values);
        assert_relative_eq!(j0.to_vec()[0], 1.0, epsilon = 1e-10);
        assert_relative_eq!(j0.to_vec()[1], 0.7651976865579666, epsilon = 1e-4);

        // Bessel J1
        let j1 = bessel_j(1, &values);
        assert_relative_eq!(j1.to_vec()[0], 0.0, epsilon = 1e-10);
        assert_relative_eq!(j1.to_vec()[1], 0.44005058574493355, epsilon = 1e-4);
    }
}