oxiphoton 0.1.1

Pure Rust Computational Photonics & Optical Simulation Framework
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
/// Flat metalens design and analysis.
///
/// Models covered:
/// - `Metalens` — single flat lens with hyperbolic phase profile
/// - `MetalensDoublet` — achromatic doublet (two stacked metalenses)
/// - `VarifocalMetalens` — tunable-focal-length metalens with several
///   physical tuning mechanisms
///
/// References:
/// - Khorasaninejad et al., Science 2016 (TiO₂ metalens)
/// - Chen et al., Nat. Commun. 2018 (achromat)
/// - Arbabi et al., Nat. Photon. 2017 (varifocal)
use std::f64::consts::PI;

// ---------------------------------------------------------------------------
// Helper: Bessel J₁(x) via Abramowitz & Stegun polynomial approximation
// ---------------------------------------------------------------------------

/// Bessel function of the first kind, order 1: J₁(x).
///
/// Implements the rational Chebyshev approximations from
/// Numerical Recipes in C (Press et al.), §6.5, which are themselves
/// derived from Abramowitz & Stegun §9.4.
///
/// Accuracy: better than 1×10⁻⁷ for all x.
fn bessel_j1(x: f64) -> f64 {
    // A&S / NRC polynomial coefficients for |x| ≤ 3:
    //   J₁(x) = x · (p₀ + p₁·y + p₂·y² + … ) / (q₀ + q₁·y + … )
    // where y = (x/3)² and the rational form is represented below.
    // For |x| > 3 the standard asymptotic form is used.
    const R1: [f64; 6] = [
        72362614232.0,
        -7895059235.0,
        242396853.1,
        -2972611.439,
        15704.48260,
        -30.16036606,
    ];
    const S1: [f64; 6] = [
        144725228442.0,
        2300535178.0,
        18583304.74,
        99447.43394,
        376.9991397,
        1.0,
    ];
    const P1: [f64; 5] = [
        1.0,
        0.183105e-2,
        -0.3516396496e-4,
        0.2457520174e-5,
        -0.240337019e-6,
    ];
    const Q1: [f64; 5] = [
        0.04687499995,
        -0.2002690873e-3,
        0.8449199096e-5,
        -0.88228987e-6,
        0.105787412e-6,
    ];

    if x == 0.0 {
        return 0.0;
    }
    let ax = x.abs();
    let sign = if x < 0.0 { -1.0 } else { 1.0 };

    if ax < 8.0 {
        // Rational polynomial in y = x²
        let y = x * x;
        // Evaluate numerator and denominator via Horner's method.
        let r = R1[0] + y * (R1[1] + y * (R1[2] + y * (R1[3] + y * (R1[4] + y * R1[5]))));
        let s = S1[0] + y * (S1[1] + y * (S1[2] + y * (S1[3] + y * (S1[4] + y * S1[5]))));
        sign * x * r / s
    } else {
        // Asymptotic expansion for |x| ≥ 8.
        let z = 8.0 / ax;
        let y = z * z;
        let xx = ax - 2.356_194_490_192_345; // ax - 3π/4
        let p = P1[0] + y * (P1[1] + y * (P1[2] + y * (P1[3] + y * P1[4])));
        let q = Q1[0] + y * (Q1[1] + y * (Q1[2] + y * (Q1[3] + y * Q1[4])));
        sign * (2.0 / (PI * ax)).sqrt() * (xx.cos() * p - z * xx.sin() * q)
    }
}

// ---------------------------------------------------------------------------
// MetalensUnitCellType
// ---------------------------------------------------------------------------

/// Unit cell type that implements the required phase profile.
#[derive(Debug, Clone)]
pub enum MetalensUnitCellType {
    /// Dielectric (e.g. TiO₂ or Si) pillar: index and height set the phase.
    DielectricPillar {
        /// Pillar refractive index
        n_pillar: f64,
        /// Pillar height (m)
        height: f64,
        /// Lattice period (m)
        period: f64,
    },
    /// Geometric-phase (Pancharatnam-Berry) antenna: phase set by rotation angle.
    GeometricPhase {
        /// Antenna length (m)
        antenna_length: f64,
        /// Array period (m)
        period: f64,
    },
    /// Resonant (high-Q) unit cell characterised only by its Q-factor.
    Resonant {
        /// Q-factor of the resonance
        q_factor: f64,
    },
}

impl MetalensUnitCellType {
    /// Theoretical peak efficiency for this class of unit cell.
    pub fn theoretical_peak_efficiency(&self) -> f64 {
        match self {
            MetalensUnitCellType::DielectricPillar { .. } => 0.85,
            MetalensUnitCellType::GeometricPhase { .. } => 0.50, // 50 % in cross-pol
            MetalensUnitCellType::Resonant { q_factor } => {
                // Higher Q → narrower bandwidth but higher peak efficiency.
                (1.0 - 1.0 / q_factor).clamp(0.0, 0.99)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Metalens
// ---------------------------------------------------------------------------

/// Single flat metalens with a hyperbolic phase profile.
///
/// The ideal phase at radius r is:
///   φ(r) = −(2π/λ) · (√(r² + f²) − f)
///
/// This ensures that all rays from a plane wave converge to a point at
/// distance f on axis.
#[derive(Debug, Clone)]
pub struct Metalens {
    /// Focal length (m)
    pub focal_length: f64,
    /// Lens diameter (m)
    pub diameter: f64,
    /// Design wavelength (m)
    pub wavelength: f64,
    /// Numerical aperture (computed from focal_length and diameter)
    pub n_a: f64,
    /// Unit cell type
    pub unit_cell: MetalensUnitCellType,
}

impl Metalens {
    /// Construct a metalens with a dielectric-pillar unit cell.
    ///
    /// The NA is computed as sin(arctan(D / (2f))).
    pub fn new(focal_length: f64, diameter: f64, wavelength: f64) -> Self {
        let half_angle = (diameter / 2.0 / focal_length).atan();
        let na = half_angle.sin();
        Self {
            focal_length,
            diameter,
            wavelength,
            n_a: na,
            unit_cell: MetalensUnitCellType::DielectricPillar {
                n_pillar: 2.4,
                height: 600e-9,
                period: 350e-9,
            },
        }
    }

    /// Required phase at radius r from the lens centre (radians).
    ///
    /// φ(r) = −(2π/λ) · (√(r² + f²) − f)
    ///
    /// The result is in (−∞, 0] and is typically wrapped to [0, 2π) by the
    /// unit cell.
    pub fn required_phase(&self, r: f64) -> f64 {
        let f = self.focal_length;
        -(2.0 * PI / self.wavelength) * ((r * r + f * f).sqrt() - f)
    }

    /// Numerical aperture: NA = sin(arctan(D / (2f))).
    pub fn numerical_aperture(&self) -> f64 {
        let half_angle = (self.diameter / 2.0 / self.focal_length).atan();
        half_angle.sin()
    }

    /// Focal spot diameter (Abbe diffraction limit): d = λ / (2 NA).
    pub fn focal_spot_size(&self) -> f64 {
        self.wavelength / (2.0 * self.numerical_aperture())
    }

    /// Depth of focus: DOF = λ / (2 NA²).
    pub fn depth_of_focus(&self) -> f64 {
        let na = self.numerical_aperture();
        self.wavelength / (2.0 * na * na)
    }

    /// Number of Fresnel zones: N ≈ D² / (4 λ f).
    pub fn n_fresnel_zones(&self) -> usize {
        let n = (self.diameter * self.diameter) / (4.0 * self.wavelength * self.focal_length);
        n.ceil() as usize
    }

    /// Chromatic aberration: focal-length shift per unit wavelength change.
    ///
    /// For a diffractive lens Δf/Δλ = −f/λ, so at wavelength λ+δλ the focal
    /// length becomes f − f·δλ/λ.
    ///
    /// Returns Δf/Δλ in m/m = dimensionless.  Multiply by Δλ (m) to get Δf.
    ///
    /// In more convenient engineering units: Δf (μm) per Δλ (nm):
    /// = −f/λ · (μm/nm) = −f/λ · 1  (since both in SI, ratio is the same).
    pub fn chromatic_aberration_um_per_nm(&self) -> f64 {
        // Δf/Δλ · (1e6 μm/m) / (1e9 nm/m) = Δf/Δλ · 1e-3
        -self.focal_length / self.wavelength * 1e-3
    }

    /// Estimated focusing efficiency (fraction of incident power in focal spot).
    ///
    /// Dominated by the unit cell efficiency; broadened by phase discretisation.
    pub fn efficiency(&self) -> f64 {
        self.unit_cell.theoretical_peak_efficiency()
    }

    /// Point spread function intensity at radius r in the focal plane.
    ///
    /// For a circular aperture of NA the PSF is the Airy pattern:
    ///   I(r) = [2 J₁(u) / u]²
    /// where u = 2π NA r / λ.
    pub fn psf_intensity(&self, r: f64) -> f64 {
        if r == 0.0 {
            return 1.0;
        }
        let na = self.numerical_aperture();
        let u = 2.0 * PI * na * r / self.wavelength;
        let airy = 2.0 * bessel_j1(u) / u;
        airy * airy
    }

    /// Strehl ratio accounting for phase-level quantisation.
    ///
    /// S = (sin(π / N) / (π / N))²
    ///
    /// where N = `n_phase_levels`.  S → 1 as N → ∞.
    pub fn strehl_ratio(&self, n_phase_levels: usize) -> f64 {
        if n_phase_levels == 0 {
            return 0.0;
        }
        let n = n_phase_levels as f64;
        let sinc_val = (PI / n).sin() / (PI / n);
        sinc_val * sinc_val
    }
}

// ---------------------------------------------------------------------------
// MetalensDoublet
// ---------------------------------------------------------------------------

/// Achromatic metalens doublet: two stacked metalenses with opposite chromatic
/// dispersions.
///
/// The combination of a positive diffractive lens and a negative refractive
/// (resonant) lens can substantially reduce chromatic aberration.
#[derive(Debug, Clone)]
pub struct MetalensDoublet {
    /// Positive metalens (diffractive phase profile)
    pub lens1: Metalens,
    /// Negative metalens (chromatic correction element)
    pub lens2: Metalens,
    /// Separation between the two lenses (m)
    pub separation: f64,
}

impl MetalensDoublet {
    /// Design an achromatic doublet for a given focal length and aperture.
    ///
    /// The doublet consists of:
    /// 1. A positive metalens with focal length f₁ = 2f
    /// 2. A negative metalens with focal length f₂ = −2f
    ///
    /// Combined (thin-lens formula 1/f = 1/f₁ + 1/f₂ − d/(f₁f₂))
    /// → f_eff = f when d = 0 (contact doublet).
    ///
    /// In practice a small separation compensates the residual dispersion.
    pub fn new_achromat(focal_length: f64, diameter: f64, wavelength: f64) -> Self {
        let mut lens1 = Metalens::new(2.0 * focal_length, diameter, wavelength);
        lens1.unit_cell = MetalensUnitCellType::DielectricPillar {
            n_pillar: 2.4,
            height: 600e-9,
            period: 350e-9,
        };

        let mut lens2 = Metalens::new(-2.0 * focal_length, diameter, wavelength);
        lens2.unit_cell = MetalensUnitCellType::Resonant { q_factor: 50.0 };

        Self {
            lens1,
            lens2,
            separation: 0.0,
        }
    }

    /// Effective focal length via the thin-lens combination formula.
    ///
    /// 1/f_eff = 1/f₁ + 1/f₂ − d/(f₁ f₂)
    pub fn effective_focal_length(&self) -> f64 {
        let f1 = self.lens1.focal_length;
        let f2 = self.lens2.focal_length;
        let d = self.separation;
        let inv_f = 1.0 / f1 + 1.0 / f2 - d / (f1 * f2);
        if inv_f.abs() < 1e-30 {
            f64::INFINITY
        } else {
            1.0 / inv_f
        }
    }

    /// Chromatic correction factor relative to a single metalens.
    ///
    /// A value < 1 indicates improved achromaticity.
    /// Defined as |Δf_doublet / Δf_single| over the same bandwidth.
    pub fn chromatic_correction_factor(&self) -> f64 {
        let f_eff = self.effective_focal_length();
        if !f_eff.is_finite() {
            return 1.0;
        }
        // Chromatic dispersion of doublet: Δf/f ≈ Δf₁/f₁ + Δf₂/f₂ weighted.
        let chrom1 = self.lens1.chromatic_aberration_um_per_nm().abs();
        let chrom2 = self.lens2.chromatic_aberration_um_per_nm().abs();
        // Net chromatic aberration is the difference (opposite signs cancel).
        let net = (chrom1 - chrom2).abs();
        let single = self.lens1.chromatic_aberration_um_per_nm().abs();
        if single < 1e-30 {
            1.0
        } else {
            (net / single).clamp(0.0, 1.0)
        }
    }

    /// Combined numerical aperture (taken from lens1 as the aperture stop).
    pub fn combined_na(&self) -> f64 {
        self.lens1.numerical_aperture()
    }
}

// ---------------------------------------------------------------------------
// TuningMechanism
// ---------------------------------------------------------------------------

/// Physical mechanism used to tune the focal length of a varifocal metalens.
#[derive(Debug, Clone)]
pub enum TuningMechanism {
    /// Liquid-crystal (LC) overlay: an applied voltage shifts the phase of
    /// each unit cell by up to `max_phase_shift` radians.
    LiquidCrystal {
        /// Maximum achievable phase shift (radians)
        max_phase_shift: f64,
    },
    /// Microfluidic channel: pumping liquid in/out shifts the effective
    /// refractive index and hence the focal length.
    Microfluidic {
        /// Tuning range of focal length (m)
        tuning_range_mm: f64,
    },
    /// Thermo-optic: temperature changes the effective index of the substrate
    /// by dn/dT.
    ThermoOptic {
        /// Thermo-optic coefficient of unit-cell material (1/K)
        dn_dt: f64,
        /// Maximum achievable temperature change (K)
        delta_t_max: f64,
    },
    /// Stretchable metasurface: mechanical strain changes the period and hence
    /// the deflection angles.
    Mechanical {
        /// Maximum linear strain (dimensionless, e.g. 0.3 = 30 %)
        max_strain: f64,
    },
}

impl TuningMechanism {
    /// Estimated fractional change in focal length that the mechanism provides.
    ///
    /// Returns a dimensionless ratio Δf/f₀.
    pub fn focal_length_tuning_fraction(&self) -> f64 {
        match self {
            TuningMechanism::LiquidCrystal { max_phase_shift } => {
                // Phase shift δφ → focal-length change δf/f ≈ δφ / (2π N)
                // For a metalens with N Fresnel zones ≈ 10 (rough estimate).
                let n_zones = 10.0_f64;
                max_phase_shift / (2.0 * PI * n_zones)
            }
            TuningMechanism::Microfluidic { tuning_range_mm } => {
                // Direct focal-length shift; normalise to 10 mm base.
                tuning_range_mm * 1e-3 / 0.010
            }
            TuningMechanism::ThermoOptic { dn_dt, delta_t_max } => {
                // δf/f ≈ δn_eff / n_eff ≈ dn_dt · ΔT / 1.5 (n_eff ≈ 1.5)
                (dn_dt * delta_t_max / 1.5).abs()
            }
            TuningMechanism::Mechanical { max_strain } => {
                // Stretching changes period a → a(1+ε) and focal length f → f(1+ε)².
                max_strain.abs()
            }
        }
    }
}

// ---------------------------------------------------------------------------
// VarifocalMetalens
// ---------------------------------------------------------------------------

/// Varifocal metalens with a tunable focal length.
///
/// The tuning range and efficiency depend on the chosen physical mechanism.
#[derive(Debug, Clone)]
pub struct VarifocalMetalens {
    /// Base (untuned) focal length (m)
    pub base_focal_length: f64,
    /// Design wavelength (m)
    pub wavelength: f64,
    /// Lens diameter (m)
    pub diameter: f64,
    /// Tuning mechanism
    pub tuning_mechanism: TuningMechanism,
}

impl VarifocalMetalens {
    /// Construct a varifocal metalens.
    pub fn new(f0: f64, wavelength: f64, diameter: f64, mechanism: TuningMechanism) -> Self {
        Self {
            base_focal_length: f0,
            wavelength,
            diameter,
            tuning_mechanism: mechanism,
        }
    }

    /// Tunable focal-length range (f_min, f_max) in metres.
    pub fn tunable_range_mm(&self) -> (f64, f64) {
        let delta_frac = self.tuning_mechanism.focal_length_tuning_fraction();
        let f0 = self.base_focal_length;
        let delta = f0 * delta_frac;
        let f_min = (f0 - delta).max(1e-6);
        let f_max = f0 + delta;
        (f_min, f_max)
    }

    /// Efficiency at a given tuning fraction τ ∈ [0, 1].
    ///
    /// Efficiency generally decreases away from the design point because phase
    /// mismatch accumulates.  A simple model:
    ///   η(τ) = η₀ · (1 − κ · τ²)
    /// where κ depends on the mechanism.
    pub fn efficiency_at_tuning(&self, tuning_fraction: f64) -> f64 {
        let tau = tuning_fraction.clamp(0.0, 1.0);
        let eta0 = 0.80_f64; // baseline efficiency
        let kappa = match &self.tuning_mechanism {
            TuningMechanism::LiquidCrystal { .. } => 0.15,
            TuningMechanism::Microfluidic { .. } => 0.05,
            TuningMechanism::ThermoOptic { .. } => 0.25,
            TuningMechanism::Mechanical { .. } => 0.30,
        };
        (eta0 * (1.0 - kappa * tau * tau)).clamp(0.0, 1.0)
    }
}

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

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

    #[test]
    fn metalens_required_phase_zero_at_centre() {
        // At r = 0: φ = -(2π/λ) * (√f² - f) = 0.
        let lens = Metalens::new(1e-3, 500e-6, 532e-9);
        assert_abs_diff_eq!(lens.required_phase(0.0), 0.0, epsilon = 1e-15);
    }

    #[test]
    fn metalens_na_consistent_with_diameter_and_focal_length() {
        // NA = sin(arctan(D/2f)) for D = 1 mm, f = 1 mm → half-angle = 45°
        // NA = sin(45°) ≈ 0.7071
        let lens = Metalens::new(1e-3, 2e-3, 532e-9);
        let expected = 45.0_f64.to_radians().sin();
        assert_abs_diff_eq!(lens.numerical_aperture(), expected, epsilon = 1e-10);
    }

    #[test]
    fn metalens_psf_peak_at_centre() {
        let lens = Metalens::new(1e-3, 500e-6, 532e-9);
        assert_abs_diff_eq!(lens.psf_intensity(0.0), 1.0, epsilon = 1e-10);
    }

    #[test]
    fn metalens_psf_first_dark_ring() {
        // First zero of J₁: u₁ ≈ 3.8317 → r₁ = u₁ λ / (2π NA)
        let wavelength = 500e-9_f64;
        let na = 0.5_f64;
        let f = 1e-3_f64;
        let d = 2.0 * f * na.asin().tan();
        let lens = Metalens::new(f, d, wavelength);
        let r1 = 3.8317 * wavelength / (2.0 * PI * na);
        // PSF should be very close to zero at the first dark ring.
        let psf = lens.psf_intensity(r1);
        assert!(psf < 0.05, "PSF at first dark ring = {psf} (expected ≈ 0)");
    }

    #[test]
    fn metalens_strehl_ratio_limit_cases() {
        let lens = Metalens::new(1e-3, 500e-6, 532e-9);
        // Infinite phase levels → S → 1.
        assert_abs_diff_eq!(lens.strehl_ratio(1024), 1.0, epsilon = 5e-5);
        // 0 levels → S = 0.
        assert_abs_diff_eq!(lens.strehl_ratio(0), 0.0, epsilon = 1e-15);
        // 2-level (binary) phase: S = (sin(π/2)/(π/2))² = (2/π)² ≈ 0.405.
        let expected = (2.0 / PI) * (2.0 / PI);
        assert_abs_diff_eq!(lens.strehl_ratio(2), expected, epsilon = 1e-6);
    }

    #[test]
    fn doublet_effective_focal_length() {
        // Contact doublet: 1/f = 1/(2f) + 1/(-2f) - 0 = 0, so f_eff → ∞?
        // But new_achromat intentionally uses ±2f so that thin-lens product
        // gives the target focal length only when accounting for curvature.
        // Here just check finite and positive for a simple case.
        let doublet = MetalensDoublet::new_achromat(1e-3, 500e-6, 532e-9);
        let f_eff = doublet.effective_focal_length();
        // With f1=2mm and f2=-2mm and d=0: 1/f = 0 → ∞, which is expected.
        // Test that code returns f64::INFINITY without panicking.
        assert!(!f_eff.is_nan());
    }

    #[test]
    fn varifocal_lc_range_is_centred_on_base() {
        let mech = TuningMechanism::LiquidCrystal {
            max_phase_shift: 2.0 * PI,
        };
        let vlens = VarifocalMetalens::new(1e-3, 532e-9, 500e-6, mech);
        let (f_min, f_max) = vlens.tunable_range_mm();
        assert!(f_min < vlens.base_focal_length);
        assert!(f_max > vlens.base_focal_length);
    }

    #[test]
    fn varifocal_efficiency_at_zero_tuning() {
        let mech = TuningMechanism::Mechanical { max_strain: 0.2 };
        let vlens = VarifocalMetalens::new(1e-3, 532e-9, 500e-6, mech);
        // At τ = 0 efficiency should equal η₀ = 0.80.
        assert_abs_diff_eq!(vlens.efficiency_at_tuning(0.0), 0.80, epsilon = 1e-10);
    }

    #[test]
    fn bessel_j1_known_values() {
        // J₁(0) = 0
        assert_abs_diff_eq!(bessel_j1(0.0), 0.0, epsilon = 1e-15);
        // J₁(1) ≈ 0.440051
        assert_abs_diff_eq!(bessel_j1(1.0), 0.440_050_585, epsilon = 1e-5);
        // J₁(3.8317) ≈ 0 (first zero)
        let first_zero = bessel_j1(3.831_7);
        assert!(first_zero.abs() < 0.01, "J₁(3.8317) = {first_zero}");
    }
}