numr 0.5.1

High-performance numerical computing with multi-backend GPU acceleration (CPU/CUDA/WebGPU)
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
//! Orthogonal polynomials and spherical harmonics
//!
//! - Legendre polynomials P_n(x)
//! - Associated Legendre functions P_n^m(x)
//! - Spherical harmonics Y_n^m(θ, φ)
//!
//! # References
//! - DLMF 14: Legendre and Related Functions
//! - DLMF 18.9: Recurrence Relations
//! - Abramowitz & Stegun 8.6, 8.8, 22.7

use std::f64::consts::PI;

/// Maximum allowed order for Legendre polynomials
const MAX_ORDER: i32 = 100;

/// Legendre polynomial P_n(x).
///
/// ```text
/// P_n(x) is the solution of (1-x²)y'' - 2xy' + n(n+1)y = 0
/// ```
///
/// # Properties
/// - Domain: x ∈ [-1, 1]
/// - P_n(1) = 1
/// - P_n(-1) = (-1)^n
/// - P_0(x) = 1, P_1(x) = x
///
/// # Algorithm
/// Three-term recurrence: (n+1)P_{n+1}(x) = (2n+1)xP_n(x) - nP_{n-1}(x)
pub fn legendre_p_scalar(n: i32, x: f64) -> f64 {
    if n < 0 {
        // P_{-n-1}(x) = P_n(x)
        return legendre_p_scalar(-n - 1, x);
    }
    if n > MAX_ORDER {
        return f64::NAN;
    }
    if x.is_nan() {
        return f64::NAN;
    }

    // Special cases
    if n == 0 {
        return 1.0;
    }
    if n == 1 {
        return x;
    }

    // Three-term recurrence
    let mut p_prev = 1.0; // P_0
    let mut p_curr = x; // P_1

    for k in 1..n {
        let k_f = k as f64;
        let p_next = ((2.0 * k_f + 1.0) * x * p_curr - k_f * p_prev) / (k_f + 1.0);
        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Associated Legendre function P_n^m(x).
///
/// ```text
/// P_n^m(x) = (-1)^m (1-x²)^(m/2) d^m/dx^m P_n(x)
/// ```
///
/// Uses Condon-Shortley phase convention (factor of (-1)^m).
///
/// # Properties
/// - Domain: x ∈ [-1, 1], 0 ≤ m ≤ n
/// - P_n^0(x) = P_n(x)
/// - P_n^n(x) = (-1)^n (2n-1)!! (1-x²)^(n/2)
///
/// # Algorithm
/// 1. Start from P_m^m(x) using explicit formula
/// 2. Use recurrence to increase n: (n-m+1)P_{n+1}^m = (2n+1)xP_n^m - (n+m)P_{n-1}^m
pub fn legendre_p_assoc_scalar(n: i32, m: i32, x: f64) -> f64 {
    if n < 0 || m < 0 {
        return f64::NAN;
    }
    if m > n {
        return 0.0;
    }
    if n > MAX_ORDER {
        return f64::NAN;
    }
    if x.is_nan() {
        return f64::NAN;
    }
    if x.abs() > 1.0 {
        return f64::NAN;
    }

    // Special case m = 0
    if m == 0 {
        return legendre_p_scalar(n, x);
    }

    // Compute P_m^m(x) = (-1)^m (2m-1)!! (1-x²)^(m/2)
    let sin_theta = (1.0 - x * x).sqrt();
    let mut pmm = 1.0;
    let mut fact = 1.0;

    for _ in 1..=m {
        pmm *= -fact * sin_theta;
        fact += 2.0;
    }

    // If n == m, we're done
    if n == m {
        return pmm;
    }

    // Compute P_{m+1}^m(x) = x(2m+1)P_m^m(x)
    let pmm1 = x * (2 * m + 1) as f64 * pmm;

    if n == m + 1 {
        return pmm1;
    }

    // Use recurrence to get P_n^m(x)
    let mut p_prev = pmm;
    let mut p_curr = pmm1;

    for k in (m + 1)..n {
        let k_f = k as f64;
        let m_f = m as f64;
        let p_next = ((2.0 * k_f + 1.0) * x * p_curr - (k_f + m_f) * p_prev) / (k_f - m_f + 1.0);
        p_prev = p_curr;
        p_curr = p_next;
    }

    p_curr
}

/// Real spherical harmonic Y_n^m(θ, φ).
///
/// ```text
/// Y_n^m(θ, φ) = N_n^m P_n^|m|(cos θ) e^{imφ}
/// ```
///
/// where N_n^m is a normalization factor.
///
/// Returns the real part for m ≥ 0, imaginary part times i for m < 0.
/// More specifically:
/// - m > 0: Y_n^m ∝ P_n^m(cos θ) cos(mφ)
/// - m = 0: Y_n^0 ∝ P_n(cos θ)
/// - m < 0: Y_n^m ∝ P_n^|m|(cos θ) sin(|m|φ)
///
/// # Normalization
/// Uses Schmidt semi-normalization (common in geophysics):
/// ∫∫ |Y_n^m|² sin θ dθ dφ = 4π/(2n+1)
///
/// # Arguments
/// - n: degree (n ≥ 0)
/// - m: order (-n ≤ m ≤ n)
/// - theta: polar angle θ ∈ [0, π] (colatitude)
/// - phi: azimuthal angle φ ∈ [0, 2π)
pub fn sph_harm_scalar(n: i32, m: i32, theta: f64, phi: f64) -> f64 {
    if n < 0 {
        return f64::NAN;
    }
    if m.abs() > n {
        return 0.0;
    }
    if theta.is_nan() || phi.is_nan() {
        return f64::NAN;
    }

    let abs_m = m.abs();
    let cos_theta = theta.cos();

    // Compute associated Legendre function
    let p_nm = legendre_p_assoc_scalar(n, abs_m, cos_theta);

    // Normalization factor (Schmidt semi-normalization)
    let norm = sph_harm_norm(n, abs_m);

    // Angular part
    let angular = if m > 0 {
        (m as f64 * phi).cos()
    } else if m < 0 {
        (abs_m as f64 * phi).sin()
    } else {
        1.0
    };

    norm * p_nm * angular
}

/// Compute normalization factor for spherical harmonics
fn sph_harm_norm(n: i32, m: i32) -> f64 {
    // Schmidt semi-normalization:
    // N_n^m = sqrt((2 - δ_{m0}) * (n-m)! / (n+m)!)
    if m == 0 {
        1.0
    } else {
        // Compute (n-m)! / (n+m)! = 1/((n-m+1)(n-m+2)...(n+m))
        let mut ratio = 1.0;
        for k in (n - m + 1)..=(n + m) {
            ratio /= k as f64;
        }
        (2.0 * ratio).sqrt()
    }
}

/// Compute spherical harmonic with full complex normalization
///
/// Uses the physics/quantum mechanics convention with orthonormalization:
/// ∫∫ Y_l^m* Y_l'^m' sin θ dθ dφ = δ_{ll'} δ_{mm'}
///
/// Returns (real_part, imag_part).
pub fn sph_harm_complex_scalar(n: i32, m: i32, theta: f64, phi: f64) -> (f64, f64) {
    if n < 0 || m.abs() > n {
        return (f64::NAN, f64::NAN);
    }
    if theta.is_nan() || phi.is_nan() {
        return (f64::NAN, f64::NAN);
    }

    let abs_m = m.abs();
    let cos_theta = theta.cos();

    // Associated Legendre with |m|
    let p_nm = legendre_p_assoc_scalar(n, abs_m, cos_theta);

    // Full normalization factor
    // K_l^m = sqrt((2l+1)/(4π) * (l-|m|)!/(l+|m|)!)
    let norm = sph_harm_full_norm(n, abs_m);

    // Condon-Shortley phase for negative m
    let phase = if m < 0 && (abs_m % 2 != 0) { -1.0 } else { 1.0 };

    // e^{imφ} = cos(mφ) + i sin(mφ)
    let m_phi = m as f64 * phi;
    let real = phase * norm * p_nm * m_phi.cos();
    let imag = phase * norm * p_nm * m_phi.sin();

    (real, imag)
}

/// Full orthonormal normalization factor
fn sph_harm_full_norm(n: i32, m: i32) -> f64 {
    // K_n^m = sqrt((2n+1)/(4π) * (n-m)!/(n+m)!)
    let n_f = n as f64;

    // Compute (n-m)! / (n+m)!
    let mut ratio = 1.0;
    for k in (n - m + 1)..=(n + m) {
        ratio /= k as f64;
    }

    ((2.0 * n_f + 1.0) / (4.0 * PI) * ratio).sqrt()
}

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

    const TOL: f64 = 1e-10;

    fn assert_close(a: f64, b: f64, tol: f64, msg: &str) {
        let diff = (a - b).abs();
        assert!(
            diff < tol || (a.is_nan() && b.is_nan()),
            "{}: expected {}, got {}, diff {}",
            msg,
            b,
            a,
            diff
        );
    }

    #[test]
    fn test_legendre_p_special_values() {
        // P_n(1) = 1 for all n
        for n in 0..=10 {
            assert_close(legendre_p_scalar(n, 1.0), 1.0, TOL, &format!("P_{}(1)", n));
        }

        // P_n(-1) = (-1)^n
        for n in 0..=10 {
            let expected = if n % 2 == 0 { 1.0 } else { -1.0 };
            assert_close(
                legendre_p_scalar(n, -1.0),
                expected,
                TOL,
                &format!("P_{}(-1)", n),
            );
        }

        // P_n(0) = 0 for odd n
        for n in [1, 3, 5, 7, 9] {
            assert_close(legendre_p_scalar(n, 0.0), 0.0, TOL, &format!("P_{}(0)", n));
        }
    }

    #[test]
    fn test_legendre_p_explicit() {
        // P_0(x) = 1
        assert_close(legendre_p_scalar(0, 0.5), 1.0, TOL, "P_0(0.5)");

        // P_1(x) = x
        assert_close(legendre_p_scalar(1, 0.5), 0.5, TOL, "P_1(0.5)");

        // P_2(x) = (3x² - 1)/2
        let x = 0.5;
        assert_close(
            legendre_p_scalar(2, x),
            (3.0 * x * x - 1.0) / 2.0,
            TOL,
            "P_2(0.5)",
        );

        // P_3(x) = (5x³ - 3x)/2
        assert_close(
            legendre_p_scalar(3, x),
            (5.0 * x * x * x - 3.0 * x) / 2.0,
            TOL,
            "P_3(0.5)",
        );
    }

    #[test]
    fn test_legendre_p_assoc_m0() {
        // P_n^0(x) = P_n(x)
        for n in 0..=5 {
            for &x in &[-0.5, 0.0, 0.5, 0.9] {
                assert_close(
                    legendre_p_assoc_scalar(n, 0, x),
                    legendre_p_scalar(n, x),
                    TOL,
                    &format!("P_{}^0({}) = P_{}({})", n, x, n, x),
                );
            }
        }
    }

    #[test]
    fn test_legendre_p_assoc_explicit() {
        let x: f64 = 0.6;
        let sin_theta = (1.0 - x * x).sqrt();

        // P_1^1(x) = -sqrt(1-x²)
        assert_close(legendre_p_assoc_scalar(1, 1, x), -sin_theta, TOL, "P_1^1");

        // P_2^1(x) = -3x sqrt(1-x²)
        assert_close(
            legendre_p_assoc_scalar(2, 1, x),
            -3.0 * x * sin_theta,
            TOL,
            "P_2^1",
        );

        // P_2^2(x) = 3(1-x²)
        assert_close(
            legendre_p_assoc_scalar(2, 2, x),
            3.0 * (1.0 - x * x),
            TOL,
            "P_2^2",
        );
    }

    #[test]
    fn test_legendre_p_assoc_m_greater_n() {
        // P_n^m = 0 for m > n
        assert_close(legendre_p_assoc_scalar(2, 3, 0.5), 0.0, TOL, "P_2^3");
        assert_close(legendre_p_assoc_scalar(1, 5, 0.5), 0.0, TOL, "P_1^5");
    }

    #[test]
    fn test_sph_harm_orthogonality() {
        // Test that Y_0^0 is constant (spherically symmetric)
        let y00_1 = sph_harm_scalar(0, 0, 0.5, 0.0);
        let y00_2 = sph_harm_scalar(0, 0, 1.0, 1.0);
        let y00_3 = sph_harm_scalar(0, 0, 2.0, 3.0);

        assert_close(y00_1, y00_2, TOL, "Y_0^0 constant");
        assert_close(y00_2, y00_3, TOL, "Y_0^0 constant");
    }

    #[test]
    fn test_sph_harm_symmetry() {
        // Y_n^m(θ, φ + 2π) = Y_n^m(θ, φ)
        let theta = 0.8;
        let phi = 1.2;

        for n in 0..=3 {
            for m in -n..=n {
                let y1 = sph_harm_scalar(n, m, theta, phi);
                let y2 = sph_harm_scalar(n, m, theta, phi + 2.0 * PI);
                assert_close(y1, y2, 1e-9, &format!("Y_{}^{} 2π periodicity", n, m));
            }
        }
    }

    #[test]
    fn test_sph_harm_complex_normalization() {
        // |Y_1^0(θ, φ)|² integrated over sphere should be 1/(4π)
        // Y_1^0 = sqrt(3/(4π)) cos θ
        let (re, im) = sph_harm_complex_scalar(1, 0, 0.0, 0.0); // θ=0 -> cos θ = 1
        let expected = (3.0 / (4.0 * PI)).sqrt();
        assert_close(re, expected, 1e-9, "Y_1^0(0,0) normalization");
        assert_close(im, 0.0, 1e-9, "Y_1^0(0,0) imaginary part");
    }

    #[test]
    fn test_legendre_nan() {
        assert!(legendre_p_scalar(5, f64::NAN).is_nan());
        assert!(legendre_p_assoc_scalar(5, 2, f64::NAN).is_nan());
        assert!(sph_harm_scalar(5, 2, f64::NAN, 0.0).is_nan());
    }
}