1use crate::Complex;
4
5pub struct ComplexSpecialFunctions;
7
8impl ComplexSpecialFunctions {
9 pub fn gamma(z: Complex) -> Complex {
11 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 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 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 pub fn digamma(z: Complex) -> Complex {
53 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 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 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 pub fn zeta(z: Complex, iterations: usize) -> Complex {
88 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 Self::zeta_euler_maclaurin(z, iterations)
105 }
106
107 fn zeta_euler_maclaurin(s: Complex, n_terms: usize) -> Complex {
108 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 pub fn polylog(s: Complex, z: Complex, iterations: usize) -> Complex {
147 if z.norm() < 1.0 {
148 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 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 Complex::zero()
177 }
178 }
179
180 fn binomial_coefficient_complex(z: Complex, k: Complex) -> Complex {
182 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 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 pub fn erfc(z: Complex, iterations: usize) -> Complex {
209 Complex::one() - Self::erf(z, iterations)
210 }
211
212 pub fn exponential_integral(z: Complex, iterations: usize) -> Complex {
214 if z.norm() < 20.0 {
215 let mut sum = Complex::real(0.57721566490153286060651209008240243104215933593992); 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 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 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 pub fn airy(z: Complex, iterations: usize) -> (Complex, Complex) {
283 if z.norm() < 1e-15 {
284 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 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 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 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 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 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 pub fn bessel_y(v: Complex, z: Complex, iterations: usize) -> Complex {
363 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 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 let result = ComplexSpecialFunctions::gamma(Complex::one());
420 assert!((result.re - 1.0).abs() < 0.1);
421
422 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 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 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 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 let result = ComplexSpecialFunctions::erf(Complex::zero(), 50);
458 assert!(result.norm() < 0.1);
459 }
460
461 #[test]
462 fn test_bessel_j() {
463 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 let result = ComplexSpecialFunctions::digamma(Complex::one());
472 let gamma = 0.57721566490153286060651209008240243104215933593992;
473 assert!((result.re + gamma).abs() < 1e-6);
474
475 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 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 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 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 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 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 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 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 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 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}