lib_jsl_special_functions/
spherical_harmonics.rs1use num_complex::Complex;
10use std::f64::consts::PI;
11
12pub fn associated_legendre(l: u32, m: i32, x: f64) -> f64 {
17 let am = m.unsigned_abs();
18 if am > l {
19 return 0.0;
20 }
21 let s = (1.0 - x * x).max(0.0).sqrt();
22 let mut pmm = 1.0;
24 for k in 1..=am {
25 pmm *= -((2 * k - 1) as f64) * s;
26 }
27 let mut value = pmm;
28 if l > am {
29 let mut prev = pmm;
30 let mut cur = x * (2 * am + 1) as f64 * pmm;
31 for ll in am + 2..=l {
32 let next = ((2 * ll - 1) as f64 * x * cur - (ll + am - 1) as f64 * prev) / (ll - am) as f64;
33 prev = cur;
34 cur = next;
35 }
36 value = cur;
37 }
38 if m < 0 {
39 let ratio: f64 = (l - am + 1..=l + am).map(|k| 1.0 / k as f64).product();
41 let sign = if am.is_multiple_of(2) { 1.0 } else { -1.0 };
42 value *= sign * ratio;
43 }
44 value
45}
46
47pub fn normalized_associated_legendre(l: u32, m: u32, x: f64) -> f64 {
51 if m > l {
52 return 0.0;
53 }
54 let s = (1.0 - x * x).max(0.0).sqrt();
55 let mut pmm = (1.0 / (4.0 * PI)).sqrt();
56 for k in 1..=m {
57 let k = k as f64;
58 pmm *= -((2.0 * k + 1.0) / (2.0 * k)).sqrt() * s;
59 }
60 if l == m {
61 return pmm;
62 }
63 let mf = m as f64;
64 let a = |ll: f64| ((4.0 * ll * ll - 1.0) / (ll * ll - mf * mf)).sqrt();
65 let mut prev = pmm;
66 let mut cur = x * (2.0 * mf + 3.0).sqrt() * pmm;
67 for ll in m + 2..=l {
68 let llf = ll as f64;
69 let next = a(llf) * (x * cur - prev / a(llf - 1.0));
70 prev = cur;
71 cur = next;
72 }
73 cur
74}
75
76pub fn spherical_harmonic(l: u32, m: i32, theta: f64, phi: f64) -> Complex<f64> {
79 let am = m.unsigned_abs();
80 let p = normalized_associated_legendre(l, am, theta.cos());
81 let y = Complex::from_polar(p, am as f64 * phi);
82 if m >= 0 {
83 y
84 } else if am.is_multiple_of(2) {
85 y.conj()
86 } else {
87 -y.conj()
88 }
89}
90
91pub fn real_spherical_harmonic(l: u32, m: i32, theta: f64, phi: f64) -> f64 {
95 let am = m.unsigned_abs();
96 let p = normalized_associated_legendre(l, am, theta.cos());
97 let sign = if am.is_multiple_of(2) { 1.0 } else { -1.0 };
98 match m.signum() {
99 0 => p,
100 1 => 2f64.sqrt() * sign * p * (am as f64 * phi).cos(),
101 _ => 2f64.sqrt() * sign * p * (am as f64 * phi).sin(),
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108 use crate::orthogonal_polynomials::legendre;
109
110 fn close(a: f64, b: f64, tol: f64) -> bool {
111 (a - b).abs() <= tol * (1.0 + b.abs())
112 }
113
114 fn factorial(n: u32) -> f64 {
115 (1..=n).map(|k| k as f64).product()
116 }
117
118 #[test]
119 fn low_orders_match_closed_forms() {
120 for &t in &[0.2f64, 1.0, 2.3] {
121 let (c, s) = (t.cos(), t.sin());
122 assert!(close(associated_legendre(1, 1, c), -s, 1e-14));
123 assert!(close(associated_legendre(2, 1, c), -3.0 * c * s, 1e-14));
124 assert!(close(associated_legendre(2, 2, c), 3.0 * s * s, 1e-14));
125 assert!(close(associated_legendre(3, 2, c), 15.0 * c * s * s, 1e-14));
126 assert!(close(associated_legendre(2, -1, c), 0.5 * c * s, 1e-14));
127 let phi = 0.7;
128 let y10 = spherical_harmonic(1, 0, t, phi);
129 assert!(close(y10.re, (3.0 / (4.0 * PI)).sqrt() * c, 1e-14) && y10.im == 0.0);
130 let y11 = spherical_harmonic(1, 1, t, phi);
131 let want = Complex::from_polar(-(3.0 / (8.0 * PI)).sqrt() * s, phi);
132 assert!((y11 - want).norm() < 1e-14);
133 let y22 = spherical_harmonic(2, 2, t, phi);
134 assert!((y22 - Complex::from_polar(0.25 * (15.0 / (2.0 * PI)).sqrt() * s * s, 2.0 * phi)).norm() < 1e-14);
135 assert!((spherical_harmonic(3, -1, t, phi) + spherical_harmonic(3, 1, t, phi).conj()).norm() < 1e-14);
137 }
138 for l in 0..10 {
139 assert!(close(associated_legendre(l, 0, 0.37), legendre(l, 0.37), 1e-13));
140 }
141 assert_eq!(associated_legendre(2, 3, 0.5), 0.0);
142 assert_eq!(spherical_harmonic(2, -3, 0.5, 0.1), Complex::new(0.0, 0.0));
143 }
144
145 #[test]
146 fn normalized_values_agree_and_stay_stable_at_high_degree() {
147 for l in 0..20u32 {
148 for m in 0..=l {
149 let norm = ((2 * l + 1) as f64 / (4.0 * PI) * factorial(l - m) / factorial(l + m)).sqrt();
150 let x = -0.63;
151 assert!(close(normalized_associated_legendre(l, m, x), norm * associated_legendre(l, m as i32, x), 1e-11), "{l} {m}");
152 }
153 }
154 let l = 400;
156 let theta = 1.1;
157 let sum: f64 = (-(l as i32)..=l as i32).map(|m| spherical_harmonic(l, m, theta, 0.3).norm_sqr()).sum();
158 assert!(close(sum, (2 * l + 1) as f64 / (4.0 * PI), 1e-10), "{sum}");
159 }
160
161 #[test]
162 fn harmonics_are_orthonormal_on_the_sphere() {
163 let (nt, np) = (400, 16);
166 let integrate = |f: &dyn Fn(f64, f64) -> Complex<f64>| -> Complex<f64> {
167 let h = PI / nt as f64;
168 let mut total = Complex::new(0.0, 0.0);
169 for i in 0..=nt {
170 let t = i as f64 * h;
171 let w = if i == 0 || i == nt { 1.0 } else if i % 2 == 1 { 4.0 } else { 2.0 } * h / 3.0 * t.sin();
172 for j in 0..np {
173 let p = 2.0 * PI * j as f64 / np as f64;
174 total += f(t, p) * w * (2.0 * PI / np as f64);
175 }
176 }
177 total
178 };
179 let modes = [(0u32, 0i32), (1, -1), (1, 1), (2, 0), (3, 2), (3, -2), (4, 1)];
180 for &(l1, m1) in &modes {
181 for &(l2, m2) in &modes {
182 let ip = integrate(&|t, p| spherical_harmonic(l1, m1, t, p) * spherical_harmonic(l2, m2, t, p).conj());
183 let want = if (l1, m1) == (l2, m2) { 1.0 } else { 0.0 };
184 assert!((ip - want).norm() < 1e-8, "complex ({l1},{m1}) ({l2},{m2}): {ip}");
185 let rp = integrate(&|t, p| Complex::new(real_spherical_harmonic(l1, m1, t, p) * real_spherical_harmonic(l2, m2, t, p), 0.0));
186 assert!((rp - want).norm() < 1e-8, "real ({l1},{m1}) ({l2},{m2}): {rp}");
187 }
188 }
189 }
190}