scirs2-special 0.3.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
#![allow(clippy::excessive_precision)]
#![allow(clippy::redundant_closure)]
#![allow(clippy::legacy_numeric_constants)]
#![allow(clippy::needless_borrows_for_generic_args)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::manual_slice_size_calculation)]
#![allow(clippy::useless_format)]
#![allow(clippy::manual_div_ceil)]
#![allow(clippy::redundant_pattern_matching)]
#![allow(clippy::needless_return)]
#![allow(clippy::let_and_return)]
#![allow(clippy::derivable_impls)]
#![allow(clippy::cast_abs_to_unsigned)]
//! # SciRS2 Special - Special Mathematical Functions
//!
//! **scirs2-special** provides production-ready special mathematical functions modeled after SciPy's
//! `special` module, offering gamma, Bessel, error functions, elliptic integrals, hypergeometric functions,
//! and more, with enhanced numerical stability, GPU acceleration, and arbitrary precision support.
//!
//! ## 🎯 Key Features
//!
//! - **SciPy Compatibility**: Drop-in replacement for `scipy.special` functions
//! - **100+ Functions**: Gamma, Bessel, error, elliptic, hypergeometric, orthogonal polynomials
//! - **Numerical Stability**: Carefully implemented algorithms avoiding overflow/underflow
//! - **GPU Acceleration**: CUDA/ROCm support for array operations
//! - **Arbitrary Precision**: High-precision computations with `rug` backend
//! - **Memory Efficient**: Chunked processing for large arrays
//! - **SIMD & Parallel**: Vectorized and multi-threaded execution
//!
//! ## 📦 Module Overview
//!
//! | SciRS2 Module | SciPy Equivalent | Description |
//! |---------------|------------------|-------------|
//! | `gamma` | `scipy.special.gamma` | Gamma and related functions |
//! | `bessel` | `scipy.special.jv`, `yv` | Bessel functions (J, Y, I, K) |
//! | `erf` | `scipy.special.erf`, `erfc` | Error and complementary error functions |
//! | `elliptic` | `scipy.special.ellipk` | Elliptic integrals and functions |
//! | `hypergeometric` | `scipy.special.hyp2f1` | Hypergeometric functions |
//! | `combinatorial` | `scipy.special.factorial` | Factorials, binomial coefficients |
//! | `orthogonal` | `scipy.special.eval_legendre` | Orthogonal polynomials (Legendre, Chebyshev, etc.) |
//! | `zeta` | `scipy.special.zeta` | Riemann zeta and related functions |
//!
//! ## 🚀 Quick Start
//!
//! ```toml
//! [dependencies]
//! scirs2-special = "0.1.5"
//! ```
//!
//!
//! ```rust
//! use scirs2_special::{gamma, bessel, erf};
//!
//! // Gamma function: Γ(5) = 4! = 24
//! let g = gamma(5.0f64);
//! assert!((g - 24.0).abs() < 1e-10);
//!
//! // Bessel function of the first kind: J₀(x)
//! let j0 = bessel::j0(2.0);
//!
//! // Error function
//! let erf_val = erf::erf(1.0);
//! ```
//!
//! ## 🏗️ Architecture
//!
//! ```text
//! scirs2-special
//! ├── Gamma Functions (gamma, lgamma, digamma, polygamma, beta)
//! ├── Bessel Functions (J, Y, I, K, modified, spherical)
//! ├── Error Functions (erf, erfc, erfcx, erfi, dawson)
//! ├── Elliptic Integrals (complete, incomplete, Jacobi)
//! ├── Hypergeometric (2F1, 1F1, 0F1, generalized)
//! ├── Combinatorial (factorial, binomial, multinomial, Stirling)
//! ├── Orthogonal Polynomials (Legendre, Chebyshev, Hermite, Laguerre)
//! ├── Statistical (logistic, softmax, sinc, logsumexp)
//! ├── Special Integrals (exponential, logarithmic, Fresnel)
//! ├── Zeta & Related (Riemann zeta, Hurwitz zeta, polylog)
//! ├── Lambert W & Wright Omega
//! ├── Airy Functions (Ai, Bi, derivatives)
//! ├── Performance Features
//! │   ├── GPU acceleration (gamma, Bessel, erf)
//! │   ├── Chunked processing (memory-efficient)
//! │   ├── SIMD vectorization
//! │   └── Parallel execution
//! └── Arbitrary Precision (via rug, optional)
//! ```
//!
//! ## 📊 Performance
//!
//! | Function | Array Size | CPU | GPU | Speedup |
//! |----------|------------|-----|-----|---------|
//! | Gamma | 10⁶ | 120ms | 6ms | 20× |
//! | Bessel J0 | 10⁶ | 180ms | 8ms | 22.5× |
//! | Erf | 10⁶ | 85ms | 4ms | 21× |
//!
//! ## 🔒 Version Information
//!
//! - **Version**: 0.1.5
//! - **Release Date**: January 15, 2026
//! - **Repository**: [github.com/cool-japan/scirs](https://github.com/cool-japan/scirs)

// Export error types

pub mod error;
pub mod error_context;
pub mod error_wrappers;
pub use error::{SpecialError, SpecialResult};

// Modules
pub mod advanced_performance_enhancement;
mod airy;
mod anger_weber;
#[cfg(feature = "high-precision")]
pub mod arbitrary_precision;
#[cfg(feature = "gpu")]
pub mod array_ops;
pub mod bessel;
mod bessel_zeros;
mod boxcox;
mod carlson;
mod combinatorial;
mod constants;
pub mod convenience;
mod coulomb;
pub mod cross_validation;
pub mod distributions;
pub mod edge_case_tests;
mod ellipsoidal;
mod elliptic;
pub mod erf;
#[cfg(test)]
mod extended_property_tests;
pub mod extended_scipy_validation;
mod fresnel;
pub mod gamma;
#[cfg(feature = "gpu")]
pub mod gpu_context_manager;
#[cfg(feature = "gpu")]
pub mod gpu_ops;
mod hypergeometric;
mod hypergeometric_enhanced;
pub mod incomplete_gamma;
pub mod information_theory;
mod kelvin;
mod lambert;
mod logint;
mod mathieu;
pub mod memory_efficient;
pub mod optimizations;
mod orthogonal;
mod parabolic;
pub mod performance_benchmarks;
pub mod physics_engineering;
pub mod precision;
mod property_tests;
pub mod python_interop;
#[cfg(test)]
mod quickcheck_tests;
pub mod simd_ops;
mod spherical_harmonics;
mod spheroidal;
pub mod stability_analysis;
mod statistical;
mod struve;
pub mod utility;
mod validation;
#[cfg(feature = "plotting")]
pub mod visualization;
mod voigt;
mod wright;
mod wright_bessel;
mod wright_simplified;
mod zeta;
mod zeta_ext;

// New SciPy parity modules
mod clausen;
mod debye;
mod scipy_compat;
mod spherical_bessel_extended;

// Re-export common functions
// Note: These functions require various trait bounds in their implementation,
// including Float, FromPrimitive, Debug, AddAssign, etc.
pub use advanced_performance_enhancement::{
    benchmark_function, erf_advancedfast, gamma_advancedfast, gamma_array_advancedfast,
    j0_advancedfast, PerformanceConfig, PerformanceMetrics,
};
pub use airy::{ai, ai_zeros, aie, aip, airye, bi, bi_zeros, bie, bip, itairy};
pub use anger_weber::{anger_j, anger_weber, lommel_s1, lommel_s2, weber_e};
// Complex Airy functions
pub use airy::complex::{ai_complex, aip_complex, bi_complex, bip_complex};
pub use bessel::{
    h1vp,
    h2vp,
    // Hankel functions
    hankel1,
    hankel1e,
    hankel2,
    hankel2e,
    // Regular Bessel functions
    i0,
    // Derivatives of modified Bessel functions
    i0_prime,
    i0e,
    i1,
    i1_prime,
    i1e,
    iv,
    iv_prime,
    ive,
    ivp,
    j0,
    // Derivatives of Bessel functions
    j0_prime,
    j0e,
    j1,
    j1_prime,
    j1e,
    jn,
    jn_prime,
    jne,
    jv,
    jv_prime,
    jve,
    // SciPy-compatible derivative interfaces
    jvp,
    k0,
    k0_prime,
    k0e,
    k1,
    k1_prime,
    k1e,
    kv,
    kv_prime,
    kve,
    kvp,
    // Spherical Bessel functions
    spherical_jn,
    spherical_yn,
    y0,
    y0_prime,
    y0e,
    y1,
    y1_prime,
    y1e,
    yn,
    yn_prime,
    yne,
    yvp,
};
pub use bessel_zeros::{
    besselpoly,
    // Bessel utilities
    itj0y0,
    // Zeros of Bessel functions
    j0_zeros,
    j1_zeros,
    jn_zeros,
    jnjnp_zeros,
    jnp_zeros,
    jnyn_zeros,
    y0_zeros,
    y1_zeros,
    yn_zeros,
};
pub use boxcox::{
    boxcox, boxcox1p, boxcox1p_array, boxcox_array, inv_boxcox, inv_boxcox1p, inv_boxcox1p_array,
    inv_boxcox_array,
};
pub use carlson::{elliprc, elliprd, elliprf, elliprf_array, elliprg, elliprj};
pub use combinatorial::{
    bell_number, bernoulli_number, binomial, comb, double_factorial, euler_number, factorial,
    factorial2, factorialk, perm, permutations, stirling2, stirling_first, stirling_second,
};
pub use coulomb::{coulomb_f, coulomb_g, coulomb_h_plus, coulomb_hminus, coulomb_phase_shift};
pub use distributions::{
    // Binomial distribution
    bdtr,
    bdtr_array,
    bdtrc,
    bdtri,
    bdtrik,
    bdtrin,
    // Beta distribution inverse functions
    btdtria,
    btdtrib,
    // Chi-square distribution
    chdtr,
    chdtrc,
    chdtri,
    // F distribution
    fdtr,
    fdtrc,
    fdtridfd,
    // Gamma distribution
    gdtr,
    gdtrc,
    gdtria,
    gdtrib,
    gdtrix,
    kolmogi,
    // Kolmogorov-Smirnov distribution
    kolmogorov,
    log_ndtr,
    // Negative binomial distribution
    nbdtr,
    nbdtrc,
    nbdtri,
    // Normal distribution
    ndtr,
    ndtr_array,
    ndtri,
    ndtri_exp,
    // Poisson distribution
    pdtr,
    pdtrc,
    pdtri,
    pdtrik,
    // Student's t distribution
    stdtr,
};
pub use ellipsoidal::{
    ellip_harm, ellip_harm_2, ellip_harm_array, ellip_harm_coefficients, ellip_harm_complex,
    ellip_normal,
};
pub use elliptic::{
    complete_elliptic_pi, ellipe, ellipeinc, ellipj, ellipk, ellipkinc, ellipkm1, elliptic_e,
    elliptic_e_inc, elliptic_f, elliptic_k, elliptic_nome, elliptic_pi, jacobi_cn, jacobi_dn,
    jacobi_sn, weierstrass_p, weierstrass_p_prime, weierstrass_sigma, weierstrass_zeta,
};
pub use fresnel::{
    fresnel, fresnel_complex, fresnelc, fresnels, mod_fresnel_plus, mod_fresnelminus,
};
pub use gamma::{
    beta,
    // Safe versions with error handling
    beta_safe,
    betainc,
    betainc_regularized,
    betaincinv,
    betaln,
    digamma,
    digamma_safe,
    gamma,
    gamma_safe,
    gammaln,
    loggamma,
    polygamma,
};
pub use incomplete_gamma::{
    gammainc, gammainc_lower, gammainc_upper, gammaincc, gammainccinv, gammaincinv, gammasgn,
    gammastar,
};
// Complex gamma functions
pub use gamma::complex::{beta_complex, digamma_complex, gamma_complex, loggamma_complex};
// Complex Bessel functions
pub use bessel::complex::{i0_complex, j0_complex, j1_complex, jn_complex, jv_complex, k0_complex};
// Complex error functions
pub use erf::complex::{erf_complex, erfc_complex, erfcx_complex, faddeeva_complex};
pub use hypergeometric::{hyp0f1, hyp1f1, hyp2f1, hyperu, ln_pochhammer, pochhammer};
pub use hypergeometric_enhanced::{
    hyp0f1_enhanced, hyp1f1_enhanced, hyp1f1_regularized, hyp2f1_enhanced, hyp2f1_regularized,
    whittaker_m, whittaker_w,
};
pub use information_theory::{
    binary_entropy, cross_entropy, entr, entr_array, entropy, huber, huber_loss, kl_div,
    kl_divergence, pseudo_huber, rel_entr,
};
pub use kelvin::{bei, beip, ber, berp, kei, keip, kelvin, ker, kerp};
pub use lambert::{lambert_w, lambert_w_real};
pub use logint::{chi, ci, e1, expint, li, li_complex, polylog, shi, shichi, si, sici, spence};
pub use mathieu::{
    mathieu_a, mathieu_b, mathieu_cem, mathieu_even_coef, mathieu_odd_coef, mathieu_sem,
};
pub use orthogonal::{
    chebyshev, gegenbauer, hermite, hermite_prob, jacobi, laguerre, laguerre_generalized, legendre,
    legendre_assoc,
};
pub use parabolic::{pbdv, pbdv_seq, pbvv, pbvv_seq, pbwa};
pub use spherical_harmonics::{
    solid_harmonic_irregular, solid_harmonic_regular, sph_harm, sph_harm_complex,
    sph_harm_cos_angle, sph_harm_normalization,
};
pub use spheroidal::{
    obl_ang1, obl_cv, obl_cv_seq, obl_rad1, obl_rad2, pro_ang1, pro_cv, pro_cv_seq, pro_rad1,
    pro_rad2,
};
pub use statistical::{
    expm1_array, log1p_array, log_abs_gamma, log_softmax, logistic, logistic_derivative, logsumexp,
    sinc, sinc_array, softmax,
};
pub use struve::{it2_struve0, it_mod_struve0, it_struve0, mod_struve, struve};

// New SciPy parity exports
pub use clausen::clausen;
pub use debye::{debye1, debye2, debye3, debye4, debye5};
pub use scipy_compat::{
    acosdg, asindg, atandg, bernoulli_poly, euler_poly, exp1, expn, loggamma_sign, multinomial,
};
pub use spherical_bessel_extended::{
    riccati_jn, riccati_yn, spherical_in, spherical_in_derivative, spherical_kn,
    spherical_kn_derivative,
};
pub use utility::{
    agm,
    // Basic functions
    cbrt,
    // Array operations
    cbrt_array,
    cosdg,
    // Accurate computations
    cosm1,
    cotdg,
    // Special functions
    diric,
    exp10,
    exp10_array,
    exp2,
    // Statistical functions (SciPy compatibility)
    expit,
    expit_array,
    expm1_array_utility,
    exprel,
    gradient,
    log1p_array_utility,
    log_expit,
    logit,
    logit_array,
    owens_t,
    powm1,
    // Trigonometric in degrees
    radian,
    round,
    round_array,
    sindg,
    softplus,
    spherical_distance,
    tandg,
    xlog1py,
    // Additional convenience functions for SciPy parity
    xlog1py_scalar,
    xlogy,
};
pub use voigt::{
    pseudo_voigt, voigt_profile, voigt_profile_array, voigt_profile_fwhm, voigt_profile_fwhm_array,
    voigt_profile_normalized,
};
pub use wright::{wright_omega_optimized, wright_omega_real_optimized};
pub use wright_bessel::{
    log_wright_bessel, wright_bessel, wright_bessel_complex, wright_bessel_zeros,
};
pub use wright_simplified::{wright_omega, wright_omega_real};
pub use zeta::{hurwitz_zeta, zeta, zetac};
pub use zeta_ext::{dirichlet_eta, lerch_phi, riemann_zeta, riemann_zeta_complex};

// SIMD operations (when enabled)
#[cfg(feature = "simd")]
pub use simd_ops::{
    benchmark_simd_performance, erf_f32_simd, exp_f32_simd, gamma_f32_simd, gamma_f64_simd,
    j0_f32_simd, vectorized_special_ops,
};

// SIMD-accelerated batch operations (Phase 3.4)
#[cfg(feature = "simd")]
pub use simd_ops::{
    batch_bessel_j0_f32, batch_bessel_j0_f64, batch_bessel_j1_f32, batch_bessel_j1_f64,
    batch_bessel_y0_f32, batch_bessel_y0_f64, batch_bessel_y1_f32, batch_bessel_y1_f64,
    batch_beta_f32, batch_beta_f64, batch_digamma_f32, batch_digamma_f64, batch_erf_f32,
    batch_erf_f64, batch_erfc_f32, batch_erfc_f64, batch_gamma_f32, batch_gamma_f64,
    batch_lgamma_f32, batch_lgamma_f64,
};

// Parallel operations (when enabled)
#[cfg(feature = "parallel")]
pub use simd_ops::{
    adaptive_gamma_processing, benchmark_parallel_performance, gamma_f64_parallel, j0_f64_parallel,
};

// Combined SIMD+Parallel operations (when both enabled)
#[cfg(all(feature = "simd", feature = "parallel"))]
pub use simd_ops::gamma_f32_simd_parallel;

// Error function and related functions
pub use erf::{dawsn, erf, erfc, erfcinv, erfcx, erfi, erfinv, wofz};

// Arbitrary precision functions (when enabled)
#[cfg(feature = "high-precision")]
pub use arbitrary_precision::{
    bessel::{bessel_j_ap, bessel_j_mp, bessel_y_ap, bessel_y_mp},
    cleanup_cache,
    error_function::{erf_ap, erf_mp, erfc_ap, erfc_mp},
    gamma::{gamma_ap, gamma_mp, log_gamma_ap, log_gamma_mp},
    to_complex64, to_f64, PrecisionContext,
};

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

    #[test]
    fn test_gamma_function() {
        // Test integer values
        assert_relative_eq!(gamma(1.0), 1.0, epsilon = 1e-10);
        assert_relative_eq!(gamma(2.0), 1.0, epsilon = 1e-10);
        assert_relative_eq!(gamma(3.0), 2.0, epsilon = 1e-10);
        assert_relative_eq!(gamma(4.0), 6.0, epsilon = 1e-10);
        assert_relative_eq!(gamma(5.0), 24.0, epsilon = 1e-10);
    }

    #[test]
    fn test_lambert_w() {
        // Test principal branch (k=0)
        let w = lambert_w(Complex64::new(1.0, 0.0), 0, 1e-8).expect("Operation failed");
        let expected = Complex64::new(0.567_143_290_409_783_8, 0.0);
        assert!((w - expected).norm() < 1e-10);

        // Test w * exp(w) = z
        let z = Complex64::new(1.0, 0.0);
        let w_exp_w = w * w.exp();
        assert!((w_exp_w - z).norm() < 1e-10);

        // Test branch k = 1
        let w_b1 = lambert_w(Complex64::new(1.0, 0.0), 1, 1e-8).expect("Operation failed");
        assert!(w_b1.im > 0.0);

        // Test branch k = -1
        let w_bm1 = lambert_w(Complex64::new(1.0, 0.0), -1, 1e-8).expect("Operation failed");
        assert!(w_bm1.im < 0.0);

        // Test real function
        let w_real = lambert_w_real(1.0, 1e-8).expect("Operation failed");
        assert!((w_real - 0.567_143_290_409_783_8).abs() < 1e-10);
    }
}