Skip to main content

mathverse_complex/
special_functions.rs

1//! Complex special functions: gamma, zeta, polylog, and related functions.
2
3use crate::Complex;
4
5/// Complex special functions.
6pub struct ComplexSpecialFunctions;
7
8impl ComplexSpecialFunctions {
9    /// Complex gamma function using Lanczos approximation.
10    pub fn gamma(z: Complex) -> Complex {
11        // Lanczos approximation coefficients
12        const P: [f64; 9] = [
13            0.99999999999980993,
14            676.5203681218851,
15            -1259.1392167224028,
16            771.32342877765313,
17            -176.61502916214059,
18            12.507343278686905,
19            -0.13857109526572012,
20            9.9843695780195716e-6,
21            1.5056327351493116e-7,
22        ];
23        
24        // Reflection formula for negative real part
25        if z.re < 0.5 {
26            let pi = Complex::new(std::f64::consts::PI, 0.0);
27            let sin_pi_z = (pi * z).sin();
28            let gamma_reflected = Self::gamma(Complex::one() - z);
29            return pi / (sin_pi_z * gamma_reflected);
30        }
31        
32        // Lanczos approximation
33        let z_minus_1 = z - Complex::one();
34        let mut x = Complex::real(P[0]);
35        
36        for i in 1..P.len() {
37            let coeff = Complex::real(P[i]);
38            let denominator = z_minus_1 + Complex::real(i as f64);
39            x = x + coeff / denominator;
40        }
41        
42        let sqrt_2pi = (2.0 * std::f64::consts::PI).sqrt();
43        let t = z_minus_1 + Complex::real((P.len() - 2) as f64) + Complex::real(0.5);
44        
45        let sqrt_2pi_t = Complex::real(sqrt_2pi) * t.pow(z - Complex::real(0.5));
46        let exp_t = (-t).exp();
47        
48        sqrt_2pi_t * exp_t * x
49    }
50
51    /// Complex digamma function (derivative of log gamma).
52    pub fn digamma(z: Complex) -> Complex {
53        // Use series expansion for large |z|
54        if z.norm() >= 10.0 {
55            let inv_z = Complex::one() / z;
56            let inv_z2 = inv_z * inv_z;
57            let inv_z4 = inv_z2 * inv_z2;
58            
59            // Asymptotic expansion: ψ(z) ~ ln(z) - 1/(2z) - 1/(12z²) + 1/(120z⁴) - 1/(252z⁶) + ...
60            let term2 = inv_z2 / Complex::real(12.0);
61            let term4 = inv_z4 / Complex::real(120.0);
62            let term6 = inv_z2 * inv_z4 / Complex::real(252.0);
63            
64            z.ln() - Complex::real(0.5) * inv_z - term2 + term4 - term6
65        } else {
66            // Use recurrence relation: ψ(z+1) = ψ(z) + 1/z
67            let mut n = 0;
68            let mut z_shifted = z;
69            
70            while z_shifted.norm() < 10.0 && n < 100 {
71                z_shifted = z_shifted + Complex::one();
72                n += 1;
73            }
74            
75            let psi_shifted = Self::digamma(z_shifted);
76            let mut result = psi_shifted;
77            
78            for k in 0..n {
79                result = result - Complex::one() / (z + Complex::real(k as f64));
80            }
81            
82            result
83        }
84    }
85
86    /// Riemann zeta function for complex arguments.
87    pub fn zeta(z: Complex, iterations: usize) -> Complex {
88        // For Re(z) < 0, use the functional equation:
89        // ζ(s) = 2^s · π^(s-1) · sin(πs/2) · Γ(1-s) · ζ(1-s)
90        // (terminates because Re(1-s) > 1)
91        if z.re < 0.0 {
92            let s = z;
93            let pi = Complex::new(std::f64::consts::PI, 0.0);
94            let factor = Complex::real(2.0).pow(s)
95                * pi.pow(s - Complex::one())
96                * (pi * s / Complex::real(2.0)).sin()
97                * Self::gamma(Complex::one() - s);
98            return factor * Self::zeta(Complex::one() - s, iterations);
99        }
100
101        // For Re(z) >= 0, use the Euler-Maclaurin formula (accurate for all s != 1):
102        // ζ(s) = Σ_{n<N} n^-s + N^(1-s)/(s-1) + N^-s/2
103        //        + Σ_{k=1..M} B_{2k}/(2k)! · s(s+1)···(s+2k-2) · N^(-s-2k+1)
104        Self::zeta_euler_maclaurin(z, iterations)
105    }
106
107    fn zeta_euler_maclaurin(s: Complex, n_terms: usize) -> Complex {
108        // Bernoulli numbers B2..B12
109        const BERNOULLI: [f64; 6] = [
110            1.0 / 6.0,
111            -1.0 / 30.0,
112            1.0 / 42.0,
113            -1.0 / 30.0,
114            5.0 / 66.0,
115            -691.0 / 2730.0,
116        ];
117
118        let n = n_terms.max(10) as f64;
119        let n_c = Complex::real(n);
120
121        let mut sum = Complex::zero();
122        for k in 1..n as usize {
123            sum = sum + Complex::real(k as f64).pow(-s);
124        }
125        sum = sum + n_c.pow(Complex::one() - s) / (s - Complex::one()) + n_c.pow(-s) / Complex::real(2.0);
126
127        for (i, &b) in BERNOULLI.iter().enumerate() {
128            let k2 = 2 * (i + 1);
129            let mut rising = s;
130            for j in 1..k2 - 1 {
131                rising = rising * (s + Complex::real(j as f64));
132            }
133            let mut factorial = 1.0;
134            for j in 2..=k2 {
135                factorial *= j as f64;
136            }
137            let term = Complex::real(b) * rising / Complex::real(factorial)
138                * n_c.pow(-s - Complex::real((k2 - 1) as f64));
139            sum = sum + term;
140        }
141
142        sum
143    }
144
145    /// Polylogarithm Li_s(z).
146    pub fn polylog(s: Complex, z: Complex, iterations: usize) -> Complex {
147        if z.norm() < 1.0 {
148            // Series expansion: Li_s(z) = Σ_{n=1}^∞ z^n / n^s
149            let mut sum = Complex::zero();
150            for n in 1..=iterations {
151                let n_complex = Complex::real(n as f64);
152                let term = z.pow(n_complex) / n_complex.pow(s);
153                sum = sum + term;
154            }
155            sum
156        } else if z.norm() > 1.0 {
157            // Use inversion formula
158            let log_minus_z = (Complex::one() - z).ln();
159            let mut sum = Complex::zero();
160            
161            for k in 0..=iterations {
162                let k_complex = Complex::real(k as f64);
163                let binomial = Self::binomial_coefficient_complex(s + k_complex - Complex::one(), k_complex);
164                let term = binomial * (log_minus_z).pow(k_complex) * Self::zeta(s + k_complex, 50);
165                
166                if k % 2 == 0 {
167                    sum = sum + term;
168                } else {
169                    sum = sum - term;
170                }
171            }
172            
173            sum
174        } else {
175            // Near |z| = 1, use analytic continuation
176            Complex::zero()
177        }
178    }
179
180    /// Complex binomial coefficient.
181    fn binomial_coefficient_complex(z: Complex, k: Complex) -> Complex {
182        // Γ(z+1) / (Γ(k+1) * Γ(z-k+1))
183        let numerator = Self::gamma(z + Complex::one());
184        let denominator = Self::gamma(k + Complex::one()) * Self::gamma(z - k + Complex::one());
185        numerator / denominator
186    }
187
188    /// Complex error function.
189    pub fn erf(z: Complex, iterations: usize) -> Complex {
190        let mut sum = Complex::zero();
191        let _z2 = z * z;
192        
193        for n in 0..=iterations {
194            let _n_complex = Complex::real(n as f64);
195            let sign = if n % 2 == 0 { Complex::one() } else { -Complex::one() };
196            
197            let factorial: f64 = (1..=n).map(|x| x as f64).product();
198            let denominator = Complex::real((2 * n + 1) as f64 * factorial);
199            
200            let term = sign * z.pow(Complex::real((2 * n + 1) as f64)) / denominator;
201            sum = sum + term;
202        }
203        
204        sum * Complex::new(2.0 / std::f64::consts::PI.sqrt(), 0.0)
205    }
206
207    /// Complex complementary error function.
208    pub fn erfc(z: Complex, iterations: usize) -> Complex {
209        Complex::one() - Self::erf(z, iterations)
210    }
211
212    /// Complex exponential integral Ei(z).
213    pub fn exponential_integral(z: Complex, iterations: usize) -> Complex {
214        if z.norm() < 20.0 {
215            // Series expansion (globally convergent, slow for large |z|):
216            // Ei(z) = γ + ln z + Σ_{n>=1} z^n/(n·n!)
217            let mut sum = Complex::real(0.57721566490153286060651209008240243104215933593992); // Euler-Mascheroni constant
218            sum = sum + z.ln();
219            
220            for n in 1..=iterations {
221                let n_complex = Complex::real(n as f64);
222                let factorial: f64 = (1..=n).map(|x| x as f64).product();
223                sum = sum + z.pow(n_complex) / (n_complex * Complex::real(factorial));
224            }
225            
226            sum
227        } else {
228            // Asymptotic expansion for large |z|: Ei(z) ~ e^z · Σ n!/z^(n+1).
229            // The series is divergent; truncate at the minimal term.
230            let inv_z = Complex::one() / z;
231            let mut sum = inv_z;
232            let mut prev_mag = sum.norm();
233            
234            for n in 1..=iterations {
235                let n_complex = Complex::real(n as f64);
236                let factorial: f64 = (1..=n).map(|x| x as f64).product();
237                let term = Complex::real(factorial) * inv_z.pow(n_complex + Complex::one());
238                let mag = term.norm();
239                if mag > prev_mag {
240                    break;
241                }
242                sum = sum + term;
243                prev_mag = mag;
244            }
245            
246            sum * z.exp()
247        }
248    }
249
250    /// Complex Fresnel integrals C(z) and S(z).
251    ///
252    /// Convention without π/2 factors: C(z) = Σ (-1)^n z^(4n+1)/((2n)! (4n+1)),
253    /// S(z) = Σ (-1)^n z^(4n+3)/((2n+1)! (4n+3)).
254    pub fn fresnel(z: Complex, iterations: usize) -> (Complex, Complex) {
255        let mut c = Complex::zero();
256        let mut s = Complex::zero();
257        
258        for n in 0..=iterations {
259            let _n_complex = Complex::real(n as f64);
260            let sign = if n % 2 == 0 { Complex::one() } else { -Complex::one() };
261            
262            let factorial_2n: f64 = (1..=2 * n).map(|x| x as f64).product();
263            let factorial_2n1: f64 = (1..=2 * n + 1).map(|x| x as f64).product();
264            
265            let term_c = sign * z.pow(Complex::real(4.0 * n as f64 + 1.0))
266                / Complex::real((4 * n + 1) as f64 * factorial_2n);
267            let term_s = sign * z.pow(Complex::real(4.0 * n as f64 + 3.0))
268                / Complex::real((4 * n + 3) as f64 * factorial_2n1);
269            
270            c = c + term_c;
271            s = s + term_s;
272        }
273        
274        (c, s)
275    }
276
277    /// Complex Airy functions Ai(z) and Bi(z).
278    ///
279    /// Ai(z) = c1·f(z) - c2·g(z), Bi(z) = √3·(c1·f(z) + c2·g(z)) with
280    /// f(z) = Σ z^(3k)/(9^k k! Γ(k+2/3)), g(z) = Σ z^(3k+1)/(9^k k! Γ(k+4/3)),
281    /// c1 = 3^(-2/3), c2 = 3^(-4/3).
282    pub fn airy(z: Complex, iterations: usize) -> (Complex, Complex) {
283        if z.norm() < 1e-15 {
284            // series gives f(0) = 1/Γ(2/3), g(0) = 0
285            let c1 = Complex::real(3.0_f64.powf(-2.0 / 3.0)) / Self::gamma(Complex::real(2.0 / 3.0));
286            let sqrt3 = Complex::real(3.0_f64.sqrt());
287            return (c1, sqrt3 * c1);
288        }
289        // Use series expansion for small |z|
290        if z.norm() < 2.0 {
291            let mut f = Complex::zero();
292            let mut g = Complex::zero();
293            
294            for n in 0..=iterations {
295                let n_complex = Complex::real(n as f64);
296                let factorial: f64 = (1..=n).map(|x| x as f64).product();
297                let nine_pow_n = 9.0_f64.powi(n as i32);
298                
299                let gamma_f = Self::gamma(n_complex + Complex::real(2.0 / 3.0));
300                let gamma_g = Self::gamma(n_complex + Complex::real(4.0 / 3.0));
301                
302                f = f + z.pow(Complex::real(3.0 * n as f64))
303                    / (Complex::real(nine_pow_n * factorial) * gamma_f);
304                g = g + z.pow(Complex::real(3.0 * n as f64 + 1.0))
305                    / (Complex::real(nine_pow_n * factorial) * gamma_g);
306            }
307            
308            let c1 = Complex::real(3.0_f64.powf(-2.0 / 3.0));
309            let c2 = Complex::real(3.0_f64.powf(-4.0 / 3.0));
310            let sqrt3 = Complex::real(3.0_f64.sqrt());
311            
312            let ai = c1 * f - c2 * g;
313            let bi = sqrt3 * (c1 * f + c2 * g);
314            (ai, bi)
315        } else {
316            // Asymptotic expansion for large |z|:
317            // Ai(z) ~ exp(-ζ) / (2√π z^(1/4)), Bi(z) ~ exp(ζ) / (√π z^(1/4)),
318            // ζ = (2/3) z^(3/2)
319            let t = Complex::real(2.0 / 3.0) * z.pow(Complex::real(1.5));
320            let z_pow_quarter = z.pow(Complex::real(0.25));
321            
322            let ai = (-t).exp() / (Complex::real(2.0) * Complex::real(std::f64::consts::PI.sqrt()) * z_pow_quarter);
323            let bi = t.exp() / (Complex::real(std::f64::consts::PI.sqrt()) * z_pow_quarter);
324            
325            (ai, bi)
326        }
327    }
328
329    /// Complex Bessel function of the first kind J_v(z).
330    pub fn bessel_j(v: Complex, z: Complex, iterations: usize) -> Complex {
331        if z.norm() < 1e-15 {
332            return if v == Complex::zero() { Complex::one() } else { Complex::zero() };
333        }
334        if z.norm() < 10.0 {
335            // Series expansion (DLMF 10.2.2), converges for all finite z
336            let mut sum = Complex::zero();
337            
338            for n in 0..=iterations {
339                let n_complex = Complex::real(n as f64);
340                let factorial_n: f64 = if n == 0 { 1.0 } else { (1..=n).map(|x| x as f64).product() };
341                let gamma_v_n = Self::gamma(v + n_complex + Complex::one());
342                
343                let term = (Complex::real((-1.0_f64).powi(n as i32)) * (z / Complex::real(2.0)).pow(v + Complex::real(2.0) * n_complex))
344                    / (Complex::real(factorial_n) * gamma_v_n);
345                
346                sum = sum + term;
347            }
348            
349            sum
350        } else {
351            // Asymptotic expansion for large |z|:
352            // J_v(z) ~ sqrt(2/(πz)) · cos(z - vπ/2 - π/4)
353            // (complex sqrt so the exponential decay/growth sectors come out right)
354            let sqrt_term = (Complex::real(2.0) / (Complex::real(std::f64::consts::PI) * z)).sqrt();
355            let phase = z - v * Complex::real(std::f64::consts::FRAC_PI_2) - Complex::real(std::f64::consts::FRAC_PI_4);
356            
357            sqrt_term * phase.cos()
358        }
359    }
360
361    /// Complex Bessel function of the second kind Y_v(z).
362    pub fn bessel_y(v: Complex, z: Complex, iterations: usize) -> Complex {
363        // Integer order: (J_v cos(vπ) - J_-v)/sin(vπ) is 0/0, use the
364        // limit series (DLMF 10.8.1) instead.
365        if v.im.abs() < 1e-15 && (v.re - v.re.round()).abs() < 1e-15 {
366            let n = v.re.round() as i64;
367            let n_abs = n.unsigned_abs() as usize;
368            let y = Self::bessel_y_integer(n_abs, z, iterations);
369            if n < 0 && n % 2 != 0 { -y } else { y }
370        } else {
371            let j_v = Self::bessel_j(v, z, iterations);
372            let j_minus_v = Self::bessel_j(-v, z, iterations);
373            
374            let cos_v_pi = (v * Complex::new(std::f64::consts::PI, 0.0)).cos();
375            
376            (j_v * cos_v_pi - j_minus_v) / (v * Complex::new(std::f64::consts::PI, 0.0)).sin()
377        }
378    }
379
380    /// Y_n(z) for integer order n >= 0:
381    /// (2/π) ln(z/2) J_n(z) - (1/π) Σ_{k<n} (n-k-1)!/k! (z/2)^(2k-n)
382    ///   - (1/π) Σ_k (-1)^k (ψ(k+1)+ψ(k+n+1))/(k!(k+n)!) (z/2)^(2k+n)
383    fn bessel_y_integer(n: usize, z: Complex, iterations: usize) -> Complex {
384        let pi = std::f64::consts::PI;
385        let z_over_2 = z / Complex::real(2.0);
386        let j_n = Self::bessel_j(Complex::real(n as f64), z, iterations);
387
388        let mut sum1 = Complex::zero();
389        for k in 0..n {
390            let fact_n_k_1: f64 = (1..=n - k - 1).map(|x| x as f64).product();
391            let fact_k: f64 = (1..=k).map(|x| x as f64).product();
392            sum1 = sum1 + Complex::real(fact_n_k_1 / fact_k)
393                * z_over_2.pow(Complex::real((2 * k) as f64 - n as f64));
394        }
395
396        let mut sum2 = Complex::zero();
397        for k in 0..=iterations {
398            let sign = if k % 2 == 0 { Complex::one() } else { -Complex::one() };
399            let fact_k: f64 = (1..=k).map(|x| x as f64).product();
400            let fact_k_n: f64 = (1..=k + n).map(|x| x as f64).product();
401            let psi = Self::digamma(Complex::real((k + 1) as f64))
402                + Self::digamma(Complex::real((k + n + 1) as f64));
403            sum2 = sum2 + sign * psi / Complex::real(fact_k * fact_k_n)
404                * z_over_2.pow(Complex::real((2 * k + n) as f64));
405        }
406
407        Complex::real(2.0 / pi) * z_over_2.ln() * j_n
408            - Complex::real(1.0 / pi) * (sum1 + sum2)
409    }
410}
411
412#[cfg(test)]
413mod tests {
414    use super::*;
415
416    #[test]
417    fn test_gamma() {
418        // Γ(1) = 1
419        let result = ComplexSpecialFunctions::gamma(Complex::one());
420        assert!((result.re - 1.0).abs() < 0.1);
421        
422        // Γ(2) = 1
423        let result = ComplexSpecialFunctions::gamma(Complex::real(2.0));
424        assert!((result.re - 1.0).abs() < 0.1);
425    }
426
427    #[test]
428    fn test_digamma() {
429        // ψ(1) should be -γ (Euler-Mascheroni constant)
430        let result = ComplexSpecialFunctions::digamma(Complex::one());
431        let gamma = 0.57721566490153286060651209008240243104215933593992;
432        assert!((result.re + gamma).abs() < 0.1);
433    }
434
435    #[test]
436    fn test_zeta() {
437        // ζ(2) = π²/6
438        let result = ComplexSpecialFunctions::zeta(Complex::real(2.0), 1000);
439        let expected = std::f64::consts::PI * std::f64::consts::PI / 6.0;
440        assert!((result.re - expected).abs() < 0.1);
441    }
442
443    #[test]
444    fn test_polylog() {
445        // Li_1(z) = -ln(1-z)
446        let z = Complex::real(0.5);
447        let result = ComplexSpecialFunctions::polylog(Complex::one(), z, 100);
448        let expected = -(Complex::one() - z).ln();
449        
450        assert!((result.re - expected.re).abs() < 0.1);
451        assert!((result.im - expected.im).abs() < 0.1);
452    }
453
454    #[test]
455    fn test_erf() {
456        // erf(0) = 0
457        let result = ComplexSpecialFunctions::erf(Complex::zero(), 50);
458        assert!(result.norm() < 0.1);
459    }
460
461    #[test]
462    fn test_bessel_j() {
463        // J_0(0) = 1
464        let result = ComplexSpecialFunctions::bessel_j(Complex::zero(), Complex::zero(), 50);
465        assert!((result.re - 1.0).abs() < 0.5);
466    }
467
468    #[test]
469    fn test_digamma_known_values() {
470        // ψ(1) = -γ
471        let result = ComplexSpecialFunctions::digamma(Complex::one());
472        let gamma = 0.57721566490153286060651209008240243104215933593992;
473        assert!((result.re + gamma).abs() < 1e-6);
474
475        // ψ(5) = -γ + H_4 = -γ + 25/12
476        let psi5 = ComplexSpecialFunctions::digamma(Complex::real(5.0));
477        let expected = -gamma + 25.0 / 12.0;
478        assert!((psi5.re - expected).abs() < 1e-8);
479    }
480
481    #[test]
482    fn test_zeta_critical_values() {
483        // ζ(0) = -1/2, ζ(-1) = -1/12, ζ(0.5) ≈ -1.46035 (regression: broken functional equation)
484        let z0 = ComplexSpecialFunctions::zeta(Complex::zero(), 100);
485        assert!((z0.re + 0.5).abs() < 1e-8);
486        assert!(z0.im.abs() < 1e-8);
487
488        let zm1 = ComplexSpecialFunctions::zeta(Complex::real(-1.0), 100);
489        assert!((zm1.re + 1.0 / 12.0).abs() < 1e-6);
490        assert!(zm1.im.abs() < 1e-6);
491
492        let zhalf = ComplexSpecialFunctions::zeta(Complex::real(0.5), 100);
493        assert!((zhalf.re + 1.4603545088).abs() < 1e-4);
494
495        // ζ(2) = π²/6 (existing)
496        let z2 = ComplexSpecialFunctions::zeta(Complex::real(2.0), 1000);
497        let expected = std::f64::consts::PI * std::f64::consts::PI / 6.0;
498        assert!((z2.re - expected).abs() < 0.1);
499
500        // ζ(1/2 + 14i) on the critical line (regression: used to recurse forever)
501        let critical = ComplexSpecialFunctions::zeta(Complex::new(0.5, 14.0), 100);
502        assert!(critical.is_finite());
503    }
504
505    #[test]
506    fn test_fresnel_small_z() {
507        // C(z) ≈ z, S(z) ≈ z³/3 for small z (regression: series had z in the denominator)
508        let (c, s) = ComplexSpecialFunctions::fresnel(Complex::real(0.1), 30);
509        assert!((c.re - 0.1).abs() < 1e-6);
510        assert!((s.re - 0.1f64.powi(3) / 3.0).abs() < 1e-6);
511    }
512
513    #[test]
514    fn test_fresnel_unit_values() {
515        // C(1) ≈ 0.904524, S(1) ≈ 0.310268 (no-π convention)
516        let (c, s) = ComplexSpecialFunctions::fresnel(Complex::one(), 30);
517        assert!((c.re - 0.9045242379).abs() < 1e-5);
518        assert!((s.re - 0.3102683017).abs() < 1e-5);
519    }
520
521    #[test]
522    fn test_airy_known_values() {
523        // Ai(0) ≈ 0.355028, Bi(0) ≈ 0.614927, Ai(1) ≈ 0.135292, Bi(1) ≈ 1.207424
524        let (ai0, bi0) = ComplexSpecialFunctions::airy(Complex::zero(), 30);
525        assert!((ai0.re - 0.3550280539).abs() < 1e-6);
526        assert!((bi0.re - 0.6149266274).abs() < 1e-6);
527
528        let (ai1, bi1) = ComplexSpecialFunctions::airy(Complex::one(), 30);
529        assert!((ai1.re - 0.1352924163).abs() < 1e-5);
530        assert!((bi1.re - 1.2074235949).abs() < 1e-4);
531    }
532
533    #[test]
534    fn test_bessel_j_known_values() {
535        // J_0(1) ≈ 0.765198, J_1(1) ≈ 0.440051
536        let j0 = ComplexSpecialFunctions::bessel_j(Complex::zero(), Complex::real(1.0), 30);
537        assert!((j0.re - 0.7651976866).abs() < 1e-6);
538
539        let j1 = ComplexSpecialFunctions::bessel_j(Complex::one(), Complex::real(1.0), 30);
540        assert!((j1.re - 0.4400505857).abs() < 1e-6);
541    }
542
543    #[test]
544    fn test_bessel_y_integer_order() {
545        // Y_0(1) ≈ 0.088257, Y_1(1) ≈ -0.781213 (regression: integer order gave NaN)
546        let y0 = ComplexSpecialFunctions::bessel_y(Complex::zero(), Complex::real(1.0), 40);
547        assert!(!y0.is_nan());
548        assert!((y0.re - 0.0882569642).abs() < 1e-4);
549
550        let y1 = ComplexSpecialFunctions::bessel_y(Complex::one(), Complex::real(1.0), 40);
551        assert!(!y1.is_nan());
552        assert!((y1.re + 0.7812128213).abs() < 1e-4);
553    }
554
555    #[test]
556    fn test_exponential_integral_known_values() {
557        // Ei(1) ≈ 1.89512 (series branch), Ei(5) ≈ 40.1853 (asymptotic branch)
558        let ei1 = ComplexSpecialFunctions::exponential_integral(Complex::one(), 40);
559        assert!((ei1.re - 1.8951178164).abs() < 1e-4);
560
561        let ei5 = ComplexSpecialFunctions::exponential_integral(Complex::real(5.0), 40);
562        assert!((ei5.re - 40.1852753558).abs() < 1e-2);
563    }
564}