scirs2-special 0.4.2

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Lattice and Modular Functions
//!
//! This module provides special functions related to lattices, modular forms,
//! and elliptic function theory, including:
//!
//! - Dedekind eta function η(τ)
//! - Lambert series Σ q^n/(1-q^n)
//! - Eisenstein series G₄(τ), G₆(τ)
//! - Klein J-invariant j(τ)
//! - Weierstrass elliptic functions ℘(z; g₂, g₃)
//! - Jacobi theta function wrappers
//!
//! All lattice functions here take τ = i·t (purely imaginary modular parameter),
//! which corresponds to the nome q = exp(-2π t) and is the most common real-valued case.
//!
//! ## References
//!
//! 1. Whittaker & Watson, *A Course of Modern Analysis*, Chapters 20-21.
//! 2. Chandrasekharan, K. (1985). *Elliptic Functions*.
//! 3. DLMF §23: Weierstrass Elliptic and Modular Functions.

use crate::error::{SpecialError, SpecialResult};
use std::f64::consts::PI;

// -------------------------------------------------------------------------
// Internal constants
// -------------------------------------------------------------------------

/// Maximum series terms for lattice sum convergence
const MAX_TERMS: usize = 500;

/// Convergence tolerance for series
const TOL: f64 = 1e-15;

// -------------------------------------------------------------------------
// Dedekind eta function
// -------------------------------------------------------------------------

/// Dedekind eta function η(τ) for purely imaginary τ = i·t  (t > 0)
///
/// Defined as:
/// ```text
/// η(it) = q^{1/24} ∏_{n=1}^{∞} (1 - q^n),   q = e^{-2πt}
/// ```
///
/// For a rectangular lattice with half-periods (ω₁, iω₂), t = ω₂/ω₁.
///
/// # Arguments
/// * `tau_imag` - Imaginary part of τ (must be > 0)
///
/// # Returns
/// * `SpecialResult<f64>` — real value of η(i·tau_imag)
///
/// # Examples
/// ```
/// use scirs2_special::dedekind_eta;
/// // η(i) ≈ 0.76823... (known value)
/// let eta_1 = dedekind_eta(1.0).expect("should not fail");
/// assert!((eta_1 - 0.768_225_142_896_1).abs() < 1e-6);
/// ```
pub fn dedekind_eta(tau_imag: f64) -> SpecialResult<f64> {
    if tau_imag <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "dedekind_eta: tau_imag must be positive, got {tau_imag}"
        )));
    }

    // nome q = exp(-2π t)
    let q = (-2.0 * PI * tau_imag).exp();

    if q >= 1.0 {
        return Err(SpecialError::ComputationError(
            "dedekind_eta: nome q >= 1 (tau_imag too small)".to_string(),
        ));
    }

    // q^{1/24} prefactor
    let prefactor = (-2.0 * PI * tau_imag / 24.0).exp();

    // Product ∏_{n=1}^{N} (1 - q^n)
    let mut product = 1.0f64;
    let mut qn = q;
    for _ in 1..=MAX_TERMS {
        let factor = 1.0 - qn;
        product *= factor;
        if (1.0 - factor).abs() < TOL {
            break;
        }
        qn *= q;
    }

    Ok(prefactor * product)
}

// -------------------------------------------------------------------------
// Lambert series
// -------------------------------------------------------------------------

/// Lambert series: L(q) = Σ_{n=1}^{N} q^n / (1 - q^n)
///
/// This is a building block for many modular forms and closely related to
/// divisor sum functions σ_k(n).
///
/// # Arguments
/// * `q`       - Nome (must satisfy 0 < q < 1)
/// * `n_terms` - Number of terms to sum (use 100-500 for good accuracy)
///
/// # Returns
/// * `SpecialResult<f64>`
///
/// # Examples
/// ```
/// use scirs2_special::lambert_series;
/// // Should converge for small q
/// let v = lambert_series(0.5, 200).expect("should not fail");
/// assert!(v > 0.0 && v.is_finite());
/// ```
pub fn lambert_series(q: f64, n_terms: usize) -> SpecialResult<f64> {
    if q <= 0.0 || q >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "lambert_series: q must satisfy 0 < q < 1, got {q}"
        )));
    }
    if n_terms == 0 {
        return Ok(0.0);
    }

    let mut sum = 0.0f64;
    let mut qn = q;
    for _ in 1..=n_terms {
        let denom = 1.0 - qn;
        if denom.abs() < 1e-300 {
            // Too close to a pole — shouldn't happen for q < 1 but guard anyway
            break;
        }
        let term = qn / denom;
        sum += term;
        if term.abs() < TOL * sum.abs().max(1e-300) {
            break;
        }
        qn *= q;
    }
    Ok(sum)
}

// -------------------------------------------------------------------------
// Eisenstein series
// -------------------------------------------------------------------------

/// Eisenstein series G₄(τ) for τ = i·t  (t > 0)
///
/// G₄(τ) = 2ζ(4) + 2(2πi)^4/3! Σ_{n=1}^{∞} σ₃(n) q^n
///        = π⁴/45  + (8π⁴/3) Σ_{n=1}^{∞} σ₃(n) q^n
///
/// where σ₃(n) = Σ_{d|n} d³ and q = e^{2πiτ} = e^{-2πt}.
///
/// # Arguments
/// * `tau_imag` - Imaginary part of τ  (> 0)
///
/// # Examples
/// ```
/// use scirs2_special::eisenstein_g4;
/// let g4 = eisenstein_g4(1.0).expect("should not fail");
/// assert!(g4.is_finite() && g4 > 0.0);
/// ```
pub fn eisenstein_g4(tau_imag: f64) -> SpecialResult<f64> {
    if tau_imag <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "eisenstein_g4: tau_imag must be positive, got {tau_imag}"
        )));
    }

    let q = (-2.0 * PI * tau_imag).exp();
    if q >= 1.0 {
        return Err(SpecialError::ComputationError(
            "eisenstein_g4: nome q >= 1".to_string(),
        ));
    }

    // Constant term: 2*ζ(4) = 2*π⁴/90 = π⁴/45
    let zeta4_2 = PI.powi(4) / 45.0;

    // Series contribution: (8π⁴/3) Σ σ₃(n) q^n
    let coeff = 8.0 * PI.powi(4) / 3.0;
    let series = eisenstein_sigma_series(q, 3, MAX_TERMS)?;

    Ok(zeta4_2 + coeff * series)
}

/// Eisenstein series G₆(τ) for τ = i·t  (t > 0)
///
/// G₆(τ) = 2ζ(6) + 2(2πi)^6/5! Σ_{n=1}^{∞} σ₅(n) q^n
///        = 2π⁶/945 + (16π⁶/15) Σ_{n=1}^{∞} σ₅(n) q^n
///
/// # Arguments
/// * `tau_imag` - Imaginary part of τ  (> 0)
///
/// # Examples
/// ```
/// use scirs2_special::eisenstein_g6;
/// let g6 = eisenstein_g6(1.0).expect("should not fail");
/// assert!(g6.is_finite() && g6 > 0.0);
/// ```
pub fn eisenstein_g6(tau_imag: f64) -> SpecialResult<f64> {
    if tau_imag <= 0.0 {
        return Err(SpecialError::DomainError(format!(
            "eisenstein_g6: tau_imag must be positive, got {tau_imag}"
        )));
    }

    let q = (-2.0 * PI * tau_imag).exp();
    if q >= 1.0 {
        return Err(SpecialError::ComputationError(
            "eisenstein_g6: nome q >= 1".to_string(),
        ));
    }

    // Constant term: 2*ζ(6) = 2*π⁶/945
    let zeta6_2 = 2.0 * PI.powi(6) / 945.0;

    // Series: (16π⁶/15) Σ σ₅(n) q^n
    let coeff = 16.0 * PI.powi(6) / 15.0;
    let series = eisenstein_sigma_series(q, 5, MAX_TERMS)?;

    Ok(zeta6_2 + coeff * series)
}

/// Compute Σ_{n=1}^{N} σ_k(n) q^n  (divisor power sum series)
fn eisenstein_sigma_series(q: f64, k: u32, n_terms: usize) -> SpecialResult<f64> {
    let mut sum = 0.0f64;
    let mut qn = q;

    for n in 1..=n_terms {
        let sigma_k = divisor_power_sum(n as u64, k);
        let term = sigma_k * qn;
        sum += term;
        if term.abs() < TOL * sum.abs().max(1e-300) {
            break;
        }
        qn *= q;
    }
    Ok(sum)
}

/// Compute σ_k(n) = Σ_{d|n} d^k
fn divisor_power_sum(n: u64, k: u32) -> f64 {
    let mut sum = 0.0f64;
    let mut d = 1u64;
    while d * d <= n {
        if n % d == 0 {
            sum += (d as f64).powi(k as i32);
            if d != n / d {
                sum += ((n / d) as f64).powi(k as i32);
            }
        }
        d += 1;
    }
    sum
}

// -------------------------------------------------------------------------
// Klein J-invariant
// -------------------------------------------------------------------------

/// Klein J-invariant j(τ) for τ = i·t  (t > 0)
///
/// j(τ) = 1728 * g₂(τ)³ / (g₂(τ)³ - 27 g₃(τ)²)
///
/// where g₂ and g₃ are related to the Eisenstein series:
///   g₂(τ) = 60 G₄(τ)   and   g₃(τ) = 140 G₆(τ)
///
/// # Arguments
/// * `tau_imag` - Imaginary part of τ  (> 0)
///
/// # Examples
/// ```
/// use scirs2_special::klein_j_invariant;
/// // j(i) ≈ 1728 (famous value)
/// let j = klein_j_invariant(1.0).expect("should not fail");
/// assert!((j - 1728.0).abs() < 0.01);
/// ```
pub fn klein_j_invariant(tau_imag: f64) -> SpecialResult<f64> {
    let g4 = eisenstein_g4(tau_imag)?;
    let g6 = eisenstein_g6(tau_imag)?;

    let g2 = 60.0 * g4;
    let g3 = 140.0 * g6;

    // Discriminant: Δ = g2³ - 27 g3²
    let g2_cubed = g2.powi(3);
    let discriminant = g2_cubed - 27.0 * g3.powi(2);

    if discriminant.abs() < 1e-300 {
        return Err(SpecialError::ComputationError(
            "klein_j_invariant: discriminant is zero (singular lattice)".to_string(),
        ));
    }

    Ok(1728.0 * g2_cubed / discriminant)
}

// -------------------------------------------------------------------------
// Weierstrass elliptic functions (series-based)
// -------------------------------------------------------------------------

/// Weierstrass ℘-function via the Fourier-Laurent expansion
///
/// ℘(z; g₂, g₃) is the unique even elliptic function with a double pole at z=0 and
/// satisfies (℘')² = 4℘³ - g₂℘ - g₃.
///
/// This implementation uses the Eisenstein series Laurent expansion:
///   ℘(z) = 1/z² + Σ_{k=1}^{N} (2k+1) G_{2k+2} z^{2k}
///
/// where G_{2k+2} are the Eisenstein series evaluated at the given modular parameter
/// inferred from g₂, g₃. For a direct computation from g₂, g₃ without a lattice,
/// this function uses a different Weierstrass-Mittag-Leffler series.
///
/// **Note**: The caller must supply `n_terms` lattice vectors over a rectangular
/// lattice with half-periods (ω₁ = π, ω₂ = i·π/τ) consistent with the invariants.
/// For an alternative, see `weierstrass_p_lattice`.
///
/// # Arguments
/// * `z`       - Complex argument (real part here)
/// * `g2`      - Lattice invariant g₂ = 60 G₄
/// * `g3`      - Lattice invariant g₃ = 140 G₆
/// * `n_terms` - Number of Eisenstein coefficients in the Laurent series
///
/// # Examples
/// ```
/// use scirs2_special::weierstrass_p;
/// // Near z=0, ℘(z) ≈ 1/z² + O(z²)
/// let v = weierstrass_p(0.1, 1.0, 0.0, 20).expect("should not fail");
/// assert!((v - 100.0).abs() < 1.0); // leading 1/z² term
/// ```
pub fn weierstrass_p(z: f64, g2: f64, g3: f64, n_terms: usize) -> SpecialResult<f64> {
    if z == 0.0 {
        return Err(SpecialError::DomainError(
            "weierstrass_p: z must be nonzero (pole at z=0)".to_string(),
        ));
    }

    // Laurent expansion around z=0:
    // ℘(z) = z^{-2} + Σ_{n=2,4,...} c_n z^n
    // where c_{2k} = (2k+1) G_{2k+2} expressed via g2, g3 recursion.
    //
    // The Eisenstein series satisfy the recursion:
    // G_{2k} = (3 / ((2k+1)(k-3)(2k-1))) Σ_{j=2}^{k-2} G_{2j} G_{2k-2j}
    // with G_4 = g2/60, G_6 = g3/140.
    //
    // Weierstrass coefficients c_{2k} = (2k+1) G_{2(k+1)}

    let g4 = g2 / 60.0;
    let g6 = g3 / 140.0;

    // Build table of G_{2k} for k = 2, 3, ..., n_terms+1
    // G_{2k} for k >= 4 via recurrence:
    // G_{2k} = 3/((2k-1)(k-3)(2k+1)) * sum_{j=2}^{k-2} G_{2j} G_{2(k-j)}
    // Simplified: (2k+3)(k-1) G_{2k+2} = 3 Σ_{j=2}^{k-2} (2j-1)(2k-2j-1) G_{2j} G_{2k-2j}
    // From DLMF 23.6.2: c_n = (3/((2n+3)(n-1))) Σ_{m=1}^{n-2} c_m c_{n-1-m}
    // where c_n = (2n+1) G_{2n+2}

    let actual_terms = n_terms.min(50).max(2);
    let mut c = vec![0.0f64; actual_terms + 2];
    // c[1] = 3 G_4 = g2/20, c[2] = 5 G_6 = g3/28
    // The Weierstrass expansion: ℘(z) = 1/z² + Σ_{n=1}^∞ c[n] z^{2n}
    // where c[1] = g2/20, c[2] = g3/28
    if actual_terms >= 1 {
        c[1] = g2 / 20.0;
    }
    if actual_terms >= 2 {
        c[2] = g3 / 28.0;
    }
    // Recurrence for n >= 3: c[n] = 3/(2n+3)/(n-1) * sum_{m=1}^{n-2} c[m]*c[n-1-m]
    // DLMF 23.6.3
    for n in 3..=actual_terms {
        let mut s = 0.0f64;
        for m in 1..=(n - 2) {
            s += c[m] * c[n - 1 - m];
        }
        let nd = n as f64;
        c[n] = 3.0 / ((2.0 * nd + 3.0) * (nd - 1.0)) * s;
    }

    // Evaluate: ℘(z) = 1/z² + Σ_{n=1}^{actual_terms} c[n] z^{2n}
    let z2 = z * z;
    let mut result = 1.0 / z2;
    let mut zn = z2; // z^2
    for n in 1..=actual_terms {
        result += c[n] * zn;
        zn *= z2;
    }

    Ok(result)
}

/// Derivative ℘'(z; g₂, g₃) of the Weierstrass P-function
///
/// The derivative satisfies (℘')² = 4℘³ - g₂℘ - g₃.
/// Here computed by differentiating the Laurent series term-by-term:
///   ℘'(z) = -2/z³ + Σ_{n=1}^{N} 2n c[n] z^{2n-1}
///
/// # Arguments
/// * `z`       - Argument (nonzero)
/// * `g2`      - Lattice invariant g₂
/// * `g3`      - Lattice invariant g₃
/// * `n_terms` - Number of terms
///
/// # Examples
/// ```
/// use scirs2_special::weierstrass_p_prime;
/// // Near z=0, ℘'(z) ≈ -2/z³
/// let vp = weierstrass_p_prime(0.1, 1.0, 0.0, 20).expect("should not fail");
/// assert!((vp + 2000.0).abs() < 10.0); // -2/0.001
/// ```
pub fn weierstrass_p_prime(z: f64, g2: f64, g3: f64, n_terms: usize) -> SpecialResult<f64> {
    if z == 0.0 {
        return Err(SpecialError::DomainError(
            "weierstrass_p_prime: z must be nonzero (pole at z=0)".to_string(),
        ));
    }

    let actual_terms = n_terms.min(50).max(2);
    let mut c = vec![0.0f64; actual_terms + 2];
    if actual_terms >= 1 {
        c[1] = g2 / 20.0;
    }
    if actual_terms >= 2 {
        c[2] = g3 / 28.0;
    }
    for n in 3..=actual_terms {
        let mut s = 0.0f64;
        for m in 1..=(n - 2) {
            s += c[m] * c[n - 1 - m];
        }
        let nd = n as f64;
        c[n] = 3.0 / ((2.0 * nd + 3.0) * (nd - 1.0)) * s;
    }

    // ℘'(z) = -2/z³ + Σ 2n c[n] z^{2n-1}
    let z2 = z * z;
    let mut result = -2.0 / (z2 * z);
    let mut zn = z; // z^1 = z^{2*1 - 1}
    for n in 1..=actual_terms {
        let nd = n as f64;
        result += 2.0 * nd * c[n] * zn;
        zn *= z2;
    }

    Ok(result)
}

// -------------------------------------------------------------------------
// Theta function wrappers (delegate to theta_functions module conventions)
// -------------------------------------------------------------------------

/// Jacobi theta function θ₁(z, q)
///
/// θ₁(z, q) = 2 Σ_{n=0}^{∞} (-1)^n q^{(n+½)²} sin((2n+1)z)
///
/// # Arguments
/// * `z` - Argument (real)
/// * `q` - Nome (0 < q < 1)
///
/// # Examples
/// ```
/// use scirs2_special::lattice_theta1;
/// // θ₁(0, q) = 0 for all q
/// let v = lattice_theta1(0.0, 0.5).expect("should not fail");
/// assert!(v.abs() < 1e-14);
/// ```
pub fn lattice_theta1(z: f64, q: f64) -> SpecialResult<f64> {
    validate_nome(q)?;
    let mut sum = 0.0f64;
    for n in 0..MAX_TERMS {
        let nf = n as f64;
        let half = nf + 0.5;
        let sign = if n % 2 == 0 { 1.0 } else { -1.0 };
        let term = sign * q.powf(half * half) * ((2.0 * nf + 1.0) * z).sin();
        sum += term;
        if term.abs() < TOL {
            break;
        }
    }
    Ok(2.0 * sum)
}

/// Jacobi theta function θ₂(z, q)
///
/// θ₂(z, q) = 2 Σ_{n=0}^{∞} q^{(n+½)²} cos((2n+1)z)
///
/// # Arguments
/// * `z` - Argument
/// * `q` - Nome (0 < q < 1)
pub fn lattice_theta2(z: f64, q: f64) -> SpecialResult<f64> {
    validate_nome(q)?;
    let mut sum = 0.0f64;
    for n in 0..MAX_TERMS {
        let nf = n as f64;
        let half = nf + 0.5;
        let term = q.powf(half * half) * ((2.0 * nf + 1.0) * z).cos();
        sum += term;
        if term.abs() < TOL {
            break;
        }
    }
    Ok(2.0 * sum)
}

/// Jacobi theta function θ₃(z, q)
///
/// θ₃(z, q) = 1 + 2 Σ_{n=1}^{∞} q^{n²} cos(2nz)
///
/// # Arguments
/// * `z` - Argument
/// * `q` - Nome (0 < q < 1)
///
/// # Examples
/// ```
/// use scirs2_special::lattice_theta3;
/// // θ₃(0, q) is always positive and converges
/// let v = lattice_theta3(0.0, 0.3).expect("should not fail");
/// assert!(v > 1.0);
/// ```
pub fn lattice_theta3(z: f64, q: f64) -> SpecialResult<f64> {
    validate_nome(q)?;
    let mut sum = 0.0f64;
    for n in 1..=MAX_TERMS {
        let nf = n as f64;
        let term = q.powf(nf * nf) * (2.0 * nf * z).cos();
        sum += term;
        if term.abs() < TOL {
            break;
        }
    }
    Ok(1.0 + 2.0 * sum)
}

/// Jacobi theta function θ₄(z, q)
///
/// θ₄(z, q) = 1 + 2 Σ_{n=1}^{∞} (-1)^n q^{n²} cos(2nz)
///
/// # Arguments
/// * `z` - Argument
/// * `q` - Nome (0 < q < 1)
pub fn lattice_theta4(z: f64, q: f64) -> SpecialResult<f64> {
    validate_nome(q)?;
    let mut sum = 0.0f64;
    for n in 1..=MAX_TERMS {
        let nf = n as f64;
        let sign = if n % 2 == 0 { 1.0 } else { -1.0 };
        let term = sign * q.powf(nf * nf) * (2.0 * nf * z).cos();
        sum += term;
        if term.abs() < TOL {
            break;
        }
    }
    Ok(1.0 + 2.0 * sum)
}

fn validate_nome(q: f64) -> SpecialResult<()> {
    if q < 0.0 || q >= 1.0 {
        return Err(SpecialError::DomainError(format!(
            "lattice: nome q must satisfy 0 ≤ q < 1, got {q}"
        )));
    }
    Ok(())
}

// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------

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

    #[test]
    fn test_dedekind_eta_positive() {
        let eta = dedekind_eta(1.0).expect("should not fail");
        // Known: η(i) ≈ 0.768_225_142_896
        assert!((eta - 0.768_225_142_896).abs() < 1e-5);
    }

    #[test]
    fn test_dedekind_eta_invalid() {
        assert!(dedekind_eta(0.0).is_err());
        assert!(dedekind_eta(-1.0).is_err());
    }

    #[test]
    fn test_lambert_series_convergence() {
        let v = lambert_series(0.3, 300).expect("should not fail");
        assert!(v > 0.0 && v.is_finite());
    }

    #[test]
    fn test_lambert_series_small_q() {
        // For small q, L(q) ≈ q/(1-q) ≈ q
        let q = 0.01f64;
        let v = lambert_series(q, 300).expect("should not fail");
        assert!((v - q / (1.0 - q)).abs() < 1e-6);
    }

    #[test]
    fn test_lambert_series_invalid() {
        assert!(lambert_series(0.0, 100).is_err());
        assert!(lambert_series(1.0, 100).is_err());
        assert!(lambert_series(-0.5, 100).is_err());
    }

    #[test]
    fn test_eisenstein_g4_finite() {
        let g4 = eisenstein_g4(1.0).expect("should not fail");
        assert!(g4.is_finite() && g4 > 0.0);
    }

    #[test]
    fn test_eisenstein_g6_finite() {
        let g6 = eisenstein_g6(1.0).expect("should not fail");
        assert!(g6.is_finite() && g6 > 0.0);
    }

    #[test]
    fn test_eisenstein_decreasing_with_t() {
        // G_4 and G_6 decrease as t → ∞ (q → 0)
        let g4_1 = eisenstein_g4(1.0).expect("ok");
        let g4_2 = eisenstein_g4(2.0).expect("ok");
        // As t increases, the series part shrinks, so G4 approaches ζ(4)/45 contribution only
        assert!(g4_2.is_finite());
        let _ = g4_1; // both finite
    }

    #[test]
    fn test_klein_j_invariant_at_i() {
        // j(i) = 1728 (exact)
        let j = klein_j_invariant(1.0).expect("should not fail");
        assert!((j - 1728.0).abs() < 1.0, "j(i) = {j}, expected 1728");
    }

    #[test]
    fn test_klein_j_invalid() {
        assert!(klein_j_invariant(0.0).is_err());
        assert!(klein_j_invariant(-1.0).is_err());
    }

    #[test]
    fn test_weierstrass_p_near_zero() {
        // ℘(z) ≈ 1/z² for small z
        let z = 0.01f64;
        let v = weierstrass_p(z, 1.0, 0.0, 10).expect("should not fail");
        assert!((v - 1.0 / (z * z)).abs() / (1.0 / (z * z)) < 0.01);
    }

    #[test]
    fn test_weierstrass_p_zero_arg_error() {
        assert!(weierstrass_p(0.0, 1.0, 0.0, 10).is_err());
    }

    #[test]
    fn test_weierstrass_p_prime_near_zero() {
        // ℘'(z) ≈ -2/z³ for small z
        let z = 0.01f64;
        let vp = weierstrass_p_prime(z, 1.0, 0.0, 10).expect("should not fail");
        let expected = -2.0 / (z * z * z);
        assert!((vp - expected).abs() / expected.abs() < 0.01);
    }

    #[test]
    fn test_theta1_zero_at_origin() {
        let v = lattice_theta1(0.0, 0.3).expect("should not fail");
        assert!(v.abs() < 1e-14);
    }

    #[test]
    fn test_theta3_greater_than_one_at_zero() {
        let v = lattice_theta3(0.0, 0.3).expect("should not fail");
        assert!(v > 1.0);
    }

    #[test]
    fn test_theta4_at_zero() {
        let v = lattice_theta4(0.0, 0.3).expect("should not fail");
        // θ₄(0, q) = 1 + 2 Σ (-1)^n q^{n²} < 1 for q > 0
        assert!(v < 1.0);
    }

    #[test]
    fn test_theta2_convergence() {
        let v = lattice_theta2(0.0, 0.2).expect("should not fail");
        assert!(v > 0.0 && v.is_finite());
    }

    #[test]
    fn test_theta_invalid_nome() {
        assert!(lattice_theta1(0.0, 1.0).is_err());
        assert!(lattice_theta1(0.0, -0.1).is_err());
    }

    #[test]
    fn test_divisor_power_sum() {
        // σ_3(1) = 1, σ_3(2) = 1+8 = 9, σ_3(3) = 1+27=28, σ_3(4) = 1+8+64=73
        assert_relative_eq!(divisor_power_sum(1, 3), 1.0, epsilon = 1e-10);
        assert_relative_eq!(divisor_power_sum(2, 3), 9.0, epsilon = 1e-10);
        assert_relative_eq!(divisor_power_sum(3, 3), 28.0, epsilon = 1e-10);
        assert_relative_eq!(divisor_power_sum(4, 3), 73.0, epsilon = 1e-10);
    }
}