scirs2-special 0.4.1

Special functions module for SciRS2 (scirs2-special)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Spherical Bessel functions
//!
//! This module provides implementations of spherical Bessel functions
//! with enhanced numerical stability for both small and large arguments.
//!
//! Spherical Bessel functions are solutions to the differential equation:
//! x² d²y/dx² + 2x dy/dx + [x² - n(n+1)]y = 0
//!
//! Functions included in this module:
//! - spherical_jn(n, x): Spherical Bessel function of the first kind
//! - spherical_yn(n, x): Spherical Bessel function of the second kind
//! - spherical_jn_scaled(n, x): Scaled spherical Bessel function of the first kind (for large arguments)
//! - spherical_yn_scaled(n, x): Scaled spherical Bessel function of the second kind (for large arguments)

use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::Debug;

/// Helper function for small argument series expansion of spherical Bessel functions
///
/// This function implements the series expansion of j_n(x) for small x using:
/// j_n(x) = (x^n)/(2n+1)!! * (1 - x^2/(2(2n+3)) + x^4/(2*4*(2n+3)(2n+5)) - ...)
///
/// The function computes the first few terms for small x to avoid precision loss.
#[allow(dead_code)]
fn small_arg_series_jn<F: Float + FromPrimitive + Debug>(n: i32, x: F) -> F {
    let _n_f = F::from(n).expect("Failed to convert to float");
    let x_sq = x * x;

    // Compute the factorial denominator (2n+1)!!
    let mut factorial = F::one();
    for i in 1..=n {
        factorial = factorial * F::from(2 * i + 1).expect("Failed to convert to float");
    }

    // First term in the series
    let mut term = F::from(1.0).expect("Failed to convert constant to float");
    let mut series = term;

    // Add more terms for better precision
    let terms_to_compute = if n < 5 { 4 } else { 3 };

    // Terms in the alternating series
    for i in 1..=terms_to_compute {
        let denom = F::from(2.0 * i as f64).expect("Failed to convert to float")
            * F::from((2 * n + 1 + 2 * i) as f64).expect("Failed to convert to float");
        term = term * x_sq.neg() / denom;
        series = series + term;

        // Break early if term is insignificant
        if term.abs() < F::from(1e-15).expect("Failed to convert constant to float") * series.abs()
        {
            break;
        }
    }

    // Compute x^n/(2n+1)!!
    let mut x_pow_n = F::one();
    for _ in 0..n {
        x_pow_n = x_pow_n * x;
    }

    x_pow_n / factorial * series
}

/// Spherical Bessel function of the first kind with enhanced stability.
///
/// These functions are related to the ordinary Bessel functions by:
/// j_n(x) = sqrt(π/(2x)) J_{n+1/2}(x)
///
/// This implementation uses:
/// - Series expansions for small arguments
/// - Recurrence relations for intermediate values
/// - Asymptotic formulas for large arguments
///
/// # Arguments
///
/// * `n` - Order (non-negative integer)
/// * `x` - Input value
///
/// # Returns
///
/// * j_n(x) Spherical Bessel function value
///
/// # Examples
///
/// ```
/// use scirs2_special::bessel::spherical::spherical_jn;
///
/// // j₀(x) = sin(x)/x
/// let x = 1.5f64;
/// let j0_exact = x.sin() / x;
/// assert!((spherical_jn(0, x) - j0_exact).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn spherical_jn<F: Float + FromPrimitive + Debug>(n: i32, x: F) -> F {
    if n < 0 {
        panic!("Order n must be non-negative");
    }

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

    // Direct formulas for n=0 and n=1 to avoid unnecessary recursion
    if n == 0 {
        // For j0(x), use a more accurate implementation for small x
        if x.abs() < F::from(0.01).expect("Failed to convert constant to float") {
            // Series expansion: j0(x) = 1 - x²/6 + x⁴/120 - ...
            let x2 = x * x;
            return F::one() - x2 / F::from(6.0).expect("Failed to convert constant to float")
                + x2 * x2 / F::from(120.0).expect("Failed to convert constant to float");
        } else {
            return x.sin() / x;
        }
    } else if n == 1 {
        // For j1(x), use a more accurate implementation for small x
        if x.abs() < F::from(0.01).expect("Failed to convert constant to float") {
            // Series expansion: j1(x) = x/3 - x³/30 + ...
            let x2 = x * x;
            return x / F::from(3.0).expect("Failed to convert constant to float")
                - x * x2 / F::from(30.0).expect("Failed to convert constant to float");
        } else {
            return (x.sin() / x - x.cos()) / x;
        }
    }

    // Use series expansion for very small arguments to avoid cancellation errors
    if x.abs()
        < F::from(0.1).expect("Failed to convert constant to float")
            * (F::from(n).expect("Failed to convert to float") + F::one())
    {
        return small_arg_series_jn(n, x);
    }

    // For large arguments, use the scaled version with appropriate scaling
    if x > F::from(n).expect("Failed to convert to float")
        * F::from(10.0).expect("Failed to convert constant to float")
    {
        let scaling = x.sin();
        return spherical_jn_scaled(n, x) * scaling / x;
    }

    // Limit the maximum recursion for stability
    let max_n = n.min(1000); // Protect against stack overflow

    // For higher orders, use a recurrence relation
    // j_{n+1} = (2n+1)/x * j_n - j_{n-1}
    // Initialize j_0 with special case handling for small arguments
    let mut j_nminus_2 = if x.abs() < F::from(0.01).expect("Failed to convert constant to float") {
        // Series expansion: j0(x) = 1 - x²/6 + x⁴/120 - ...
        let x2 = x * x;
        F::one() - x2 / F::from(6.0).expect("Failed to convert constant to float")
            + x2 * x2 / F::from(120.0).expect("Failed to convert constant to float")
    } else {
        x.sin() / x
    }; // j_0
       // Initialize j_1 with special case handling for small arguments
    let mut j_nminus_1 = if x.abs() < F::from(0.01).expect("Failed to convert constant to float") {
        // Series expansion: j1(x) = x/3 - x³/30 + ...
        let x2 = x * x;
        x / F::from(3.0).expect("Failed to convert constant to float")
            - x * x2 / F::from(30.0).expect("Failed to convert constant to float")
    } else {
        (x.sin() / x - x.cos()) / x
    }; // j_1

    for k in 2..=max_n {
        let j_n = F::from(2.0 * k as f64 - 1.0).expect("Failed to convert to float") / x
            * j_nminus_1
            - j_nminus_2;
        j_nminus_2 = j_nminus_1;
        j_nminus_1 = j_n;
    }

    j_nminus_1
}

/// Spherical Bessel function of the second kind with enhanced stability.
///
/// These functions are related to the ordinary Bessel functions by:
/// y_n(x) = sqrt(π/(2x)) Y_{n+1/2}(x)
///
/// This implementation uses:
/// - Closed-form formulas for n=0 and n=1
/// - Recurrence relations for higher orders
/// - Asymptotic behavior for large arguments
///
/// # Arguments
///
/// * `n` - Order (non-negative integer)
/// * `x` - Input value (must be positive)
///
/// # Returns
///
/// * y_n(x) Spherical Bessel function value
///
/// # Examples
///
/// ```
/// use scirs2_special::bessel::spherical::spherical_yn;
///
/// // y₀(x) = -cos(x)/x
/// let x = 1.5f64;
/// let y0_exact = -x.cos() / x;
/// assert!((spherical_yn(0, x) - y0_exact).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn spherical_yn<F: Float + FromPrimitive + Debug>(n: i32, x: F) -> F {
    if n < 0 {
        panic!("Order n must be non-negative");
    }

    // Special case x = 0 or negative
    if x <= F::zero() {
        return F::neg_infinity();
    }

    // Direct formulas for n=0 and n=1 to avoid unnecessary recursion
    if n == 0 {
        return -x.cos() / x;
    } else if n == 1 {
        return -(x.cos() / x + x.sin()) / x;
    }

    // For large arguments, use the scaled version with appropriate scaling
    if x > F::from(n).expect("Failed to convert to float")
        * F::from(10.0).expect("Failed to convert constant to float")
    {
        let scaling = -x.cos();
        return spherical_yn_scaled(n, x) * scaling / x;
    }

    // Limit the maximum recursion for stability
    let max_n = n.min(1000); // Protect against stack overflow

    // For higher orders, use a recurrence relation
    // y_{n+1} = (2n+1)/x * y_n - y_{n-1}
    let mut y_nminus_2 = -x.cos() / x; // y_0
    let mut y_nminus_1 = -(x.cos() / x + x.sin()) / x; // y_1

    for k in 2..=max_n {
        let y_n = F::from(2.0 * k as f64 - 1.0).expect("Failed to convert to float") / x
            * y_nminus_1
            - y_nminus_2;
        y_nminus_2 = y_nminus_1;
        y_nminus_1 = y_n;
    }

    y_nminus_1
}

/// Scaled spherical Bessel function of the first kind.
///
/// This function computes j̃_n(x) = j_n(x) * x / sin(x) for improved accuracy
/// with large arguments. This removes the oscillation and normalization factors
/// that can cause loss of precision.
///
/// # Arguments
///
/// * `n` - Order (non-negative integer)
/// * `x` - Input value (should be large, typically x > 10*n)
///
/// # Returns
///
/// * Scaled spherical Bessel function value j̃_n(x)
#[allow(dead_code)]
pub fn spherical_jn_scaled<F: Float + FromPrimitive + Debug>(n: i32, x: F) -> F {
    if n < 0 {
        panic!("Order n must be non-negative");
    }

    // Special case n=0, for which we know the scaled function approaches 1
    if n == 0 {
        if x == F::zero() {
            return F::one();
        }

        if x > F::from(10.0).expect("Failed to convert constant to float") {
            // For large x, j0_scaled approaches 1
            let x_sq = x * x;
            return F::one()
                - F::one() / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);
        } else {
            // For smaller x, compute directly
            // This should simplify to 1, but use explicit value to avoid precision issues
            return F::one();
        }
    }

    // Special case n=1
    if n == 1 {
        if x == F::zero() {
            return F::zero();
        }

        if x > F::from(10.0).expect("Failed to convert constant to float") {
            // For large x, j1_scaled approaches asymptotics
            let x_sq = x * x;
            return F::one()
                - F::from(3.0).expect("Failed to convert constant to float")
                    / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);
        } else {
            // Compute more accurately for small x
            // This is equivalent to (1 - x.cos()/x.sin()) but with better precision
            let x2 = x * x;
            return F::one()
                - F::from(2.0).expect("Failed to convert constant to float")
                    / F::from(3.0).expect("Failed to convert constant to float")
                    * x2;
        }
    }

    // For higher orders with small arguments
    if x < F::from(5.0).expect("Failed to convert constant to float") {
        if x == F::zero() {
            return F::zero();
        }

        // Use the direct formula with the unscaled function
        let j_n = spherical_jn(n, x);
        return j_n * x / x.sin();
    }

    // For large arguments, use asymptotic expansion
    if x > F::from(n * n).expect("Failed to convert to float")
        || x > F::from(1000.0).expect("Failed to convert constant to float")
    {
        let x_sq = x * x;
        let n_f = F::from(n).expect("Failed to convert to float");

        // Asymptotic expansion
        let mut factor = F::one();

        // First order correction
        factor = factor
            - n_f * (n_f + F::one())
                / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);

        // Second order correction for very large x
        if x > F::from(100.0).expect("Failed to convert constant to float") {
            let term2 = n_f
                * (n_f + F::one())
                * (n_f * (n_f + F::one())
                    - F::from(2.0).expect("Failed to convert constant to float"))
                / (F::from(8.0).expect("Failed to convert constant to float") * x_sq * x_sq);
            factor = factor + term2;
        }

        return factor;
    }

    // Limit n to avoid stack overflows
    let safe_n = n.min(50);

    // For intermediate orders and arguments, use a safely truncated recurrence relation
    // We use Miller's algorithm with downward recurrence for stability

    // Start with a higher order than needed (with limit to prevent overflows)
    let nmax = (n * 2).min(100);

    // Initialize with arbitrary values (will be rescaled later)
    let mut j_n_plus_1 = F::from(1e-100).expect("Failed to convert constant to float");
    let mut j_n = F::from(1e-100).expect("Failed to convert constant to float");

    // Apply recurrence relation backward for better numerical stability
    for k in (0..=nmax).rev() {
        let j_nminus_1 = F::from(2.0 * k as f64 + 1.0).expect("Failed to convert to float") / x
            * j_n
            - j_n_plus_1;
        j_n_plus_1 = j_n;
        j_n = j_nminus_1;

        // Normalize occasionally to avoid overflow/underflow
        if j_n.abs() > F::from(1e50).expect("Failed to convert constant to float") {
            let scale = F::from(1e-50).expect("Failed to convert constant to float");
            j_n = j_n * scale;
            j_n_plus_1 = j_n_plus_1 * scale;
        }
    }

    // Rescale using the known asymptotic behavior of j0_scaled
    let j_0_scaled = F::one(); // The scaled j0 approaches 1 as x increases

    // Normalize the sequence
    let scale = j_0_scaled / j_n;

    // Fix the scale and recalculate j_n for n=0
    j_n = j_0_scaled;
    j_n_plus_1 = j_n_plus_1 * scale;

    // Now apply recurrence forward to get j_safe_n_scaled
    for k in 0..safe_n {
        let j_n_plus_1_new = F::from(2.0 * k as f64 + 1.0).expect("Failed to convert to float") / x
            * j_n
            - j_n_plus_1;
        j_n_plus_1 = j_n;
        j_n = j_n_plus_1_new;
    }

    j_n
}

/// Scaled spherical Bessel function of the second kind.
///
/// This function computes ỹ_n(x) = y_n(x) * x / (-cos(x)) for improved accuracy
/// with large arguments. This removes the oscillation and normalization factors
/// that can cause loss of precision.
///
/// # Arguments
///
/// * `n` - Order (non-negative integer)
/// * `x` - Input value (should be large, typically x > 10*n)
///
/// # Returns
///
/// * Scaled spherical Bessel function value ỹ_n(x)
#[allow(dead_code)]
pub fn spherical_yn_scaled<F: Float + FromPrimitive + Debug>(n: i32, x: F) -> F {
    if n < 0 {
        panic!("Order n must be non-negative");
    }

    if x <= F::zero() {
        return F::neg_infinity();
    }

    // Special case n=0
    if n == 0 {
        if x > F::from(10.0).expect("Failed to convert constant to float") {
            // For large x, y0_scaled approaches 1
            let x_sq = x * x;
            return F::one()
                - F::one() / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);
        } else {
            // For smaller x, compute directly
            return -x.cos() / x * x / (-x.cos()); // Simplifies to 1
        }
    }

    // Special case n=1
    if n == 1 {
        if x > F::from(10.0).expect("Failed to convert constant to float") {
            // For large x, y1_scaled approaches asymptotics
            let x_sq = x * x;
            return -F::one()
                + F::from(3.0).expect("Failed to convert constant to float")
                    / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);
        } else {
            // Compute directly for small x
            return -(x.cos() / x + x.sin()) / x * x / (-x.cos());
        }
    }

    // For higher orders with small arguments
    if x < F::from(5.0).expect("Failed to convert constant to float") {
        // Use the direct formula with the unscaled function
        let y_n = spherical_yn(n, x);
        return y_n * x / (-x.cos());
    }

    // For large arguments, use asymptotic expansion
    if x > F::from(n * n).expect("Failed to convert to float")
        || x > F::from(1000.0).expect("Failed to convert constant to float")
    {
        let x_sq = x * x;
        let n_f = F::from(n).expect("Failed to convert to float");

        // Asymptotic expansion
        let sign = if n % 2 == 0 { F::one() } else { F::one().neg() };
        let mut factor = sign;

        // First order correction
        factor = factor
            - sign * n_f * (n_f + F::one())
                / (F::from(2.0).expect("Failed to convert constant to float") * x_sq);

        // Second order correction for very large x
        if x > F::from(100.0).expect("Failed to convert constant to float") {
            let term2 = sign
                * n_f
                * (n_f + F::one())
                * (n_f * (n_f + F::one())
                    - F::from(2.0).expect("Failed to convert constant to float"))
                / (F::from(8.0).expect("Failed to convert constant to float") * x_sq * x_sq);
            factor = factor + term2;
        }

        return factor;
    }

    // Fall back to direct computation for intermediate orders
    // With our improved spherical_yn, this should avoid stack overflows
    spherical_yn(n, x) * x / (-x.cos())
}

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

    #[test]
    fn test_spherical_jn_special_cases() {
        // j₀(x) = sin(x)/x
        let x = 1.5f64;
        let j0_exact = x.sin() / x;
        let j0 = spherical_jn(0, x);
        assert_relative_eq!(j0, j0_exact, epsilon = 1e-10);
    }

    #[test]
    fn test_spherical_j1_special_cases() {
        // j₁(x) = sin(x)/(x²) - cos(x)/x
        let x = 1.5f64;
        let j1_exact = (x.sin() / x - x.cos()) / x;
        let j1 = spherical_jn(1, x);
        assert_relative_eq!(j1, j1_exact, epsilon = 1e-10);
    }

    #[test]
    fn test_spherical_y0_special_cases() {
        // y₀(x) = -cos(x)/x
        let x = 1.5f64;
        let y0_exact = -x.cos() / x;
        let y0 = spherical_yn(0, x);
        assert_relative_eq!(y0, y0_exact, epsilon = 1e-10);
    }

    #[test]
    fn test_spherical_y1_special_cases() {
        // y₁(x) = -cos(x)/(x²) - sin(x)/x
        let x = 1.5f64;
        let y1_exact = -(x.cos() / x + x.sin()) / x;
        let y1 = spherical_yn(1, x);
        assert_relative_eq!(y1, y1_exact, epsilon = 1e-10);
    }

    #[test]
    fn test_spherical_jn_small_arguments() {
        // Test series expansion for j₀(x) with very small x
        let x = 1e-6;
        // For small x, j₀(x) ≈ 1 - x²/6
        let j0_series = 1.0 - x * x / 6.0;
        let j0 = spherical_jn(0, x);
        assert_relative_eq!(j0, j0_series, epsilon = 1e-12);
    }

    #[test]
    fn test_spherical_j1_small_arguments() {
        // Test j₁(x) with small x
        // For small x, j₁(x) ≈ x/3
        let x = 1e-6;
        let j1_series = x / 3.0;
        let j1 = spherical_jn(1, x);
        // Use a slightly larger epsilon due to differences in series truncation
        assert_relative_eq!(j1, j1_series, epsilon = 2e-6);
    }
}