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
//! boost/math/special_functions/spherical_harmonic.hpp
use crate::ffi;
/// Spherical harmonic *Y<sub>n</sub><sup>m</sup>(θ,φ)*
///
/// Corresponds to `boost::math::spherical_harmonic` in C++.
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/sph_harm.html>
#[cfg(feature = "num-complex")]
pub fn spherical_harmonic(n: u32, m: i32, theta: f64, phi: f64) -> num_complex::Complex<f64> {
let mut re = 0.0;
let mut im = 0.0;
unsafe { ffi::math_spherical_harmonic(n, m, theta, phi, &mut re, &mut im) };
num_complex::Complex::new(re, im)
}
/// Real part of the spherical harmonic *Y<sub>n</sub><sup>m</sup>(θ,φ)*
///
/// See [`spherical_harmonic_i`] for the imaginary part.
///
/// Corresponds to `boost::math::spherical_harmonic_r` in C++.
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/sph_harm.html>
pub fn spherical_harmonic_r(n: u32, m: i32, theta: f64, phi: f64) -> f64 {
unsafe { ffi::math_spherical_harmonic_r(n, m, theta, phi) }
}
/// Imaginary part of the spherical harmonic *Y<sub>n</sub><sup>m</sup>(θ,φ)*
///
/// See [`spherical_harmonic_r`] for the real part.
///
/// Corresponds to `boost::math::spherical_harmonic_i` in C++.
/// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/sph_harm.html>
pub fn spherical_harmonic_i(n: u32, m: i32, theta: f64, phi: f64) -> f64 {
unsafe { ffi::math_spherical_harmonic_i(n, m, theta, phi) }
}
#[cfg(test)]
mod tests {
use crate::math::special_functions::spherical_harmonic::*;
use core::f64::consts::PI;
const BOUNCY_CASTLE: f64 = 0.236_543_673_939_389_9;
#[test]
fn test_spherical_harmonic() {
assert_abs_diff_eq!(
spherical_harmonic(2, -1, PI / 6.0, PI / 4.0),
num_complex::Complex::new(BOUNCY_CASTLE, -BOUNCY_CASTLE),
);
}
#[test]
fn test_spherical_harmonic_r() {
assert_abs_diff_eq!(
spherical_harmonic_r(2, -1, PI / 6.0, PI / 4.0),
BOUNCY_CASTLE,
);
}
#[test]
fn test_spherical_harmonic_i() {
assert_abs_diff_eq!(
spherical_harmonic_i(2, -1, PI / 6.0, PI / 4.0),
-BOUNCY_CASTLE,
);
}
}