|
1 | 1 | //! boost/math/special_functions/hermite.hpp |
2 | 2 |
|
3 | | -use crate::ffi; |
4 | | -use core::ffi::c_uint; |
| 3 | +/// Recurrence relation for [`hermite_h`] |
| 4 | +/// |
| 5 | +/// *H<sub>n+1</sub>(x) = 2xH<sub>n</sub>(x) - 2nH<sub>n-1</sub>(x)* |
| 6 | +/// |
| 7 | +/// # Examples |
| 8 | +/// |
| 9 | +/// ``` |
| 10 | +/// # use boost::math::{hermite_h, hermite_h_next}; |
| 11 | +/// let x = 0.42; |
| 12 | +/// let p0 = hermite_h(0, x); // 1 |
| 13 | +/// let p1 = hermite_h(1, x); // 2x |
| 14 | +/// let p2 = hermite_h(2, x); // 4x² - 2 |
| 15 | +/// let p3 = hermite_h(3, x); // 8x³ - 12x |
| 16 | +/// assert_eq!(hermite_h_next(1, x, p1, p0), p2); |
| 17 | +/// assert_eq!(hermite_h_next(2, x, p2, p1), p3); |
| 18 | +/// ``` |
| 19 | +#[inline(always)] |
| 20 | +pub fn hermite_h_next(n: u32, x: f64, pn: f64, pn_prev: f64) -> f64 { |
| 21 | + 2.0 * hermite_he_next(n, x, pn, pn_prev) |
| 22 | +} |
5 | 23 |
|
6 | 24 | /// Hermite Polynomial *H<sub>n</sub>(x)* |
7 | 25 | /// |
8 | | -/// Corresponds to `boost::math::hermite(n, x)`. |
| 26 | +/// Note that this is the "physicist's" Hermite polynomial. |
| 27 | +/// |
| 28 | +/// This is a pure rust implementation equivalent to the `boost::math::hermite(n, x)` C++ function. |
9 | 29 | /// <https://boost.org/doc/libs/latest/libs/math/doc/html/math_toolkit/sf_poly/hermite.html> |
10 | | -pub fn hermite(n: u32, x: f64) -> f64 { |
11 | | - unsafe { ffi::math_hermite(n as c_uint, x) } |
| 30 | +pub fn hermite_h(n: u32, x: f64) -> f64 { |
| 31 | + // Implement Hermite polynomials via recurrence: |
| 32 | + let (mut p0, mut p1) = (1.0, 2.0 * x); |
| 33 | + if n == 0 { |
| 34 | + p0 |
| 35 | + } else { |
| 36 | + for c in 1..n { |
| 37 | + (p0, p1) = (p1, hermite_h_next(c, x, p1, p0)); |
| 38 | + } |
| 39 | + p1 |
| 40 | + } |
12 | 41 | } |
13 | 42 |
|
14 | | -/// Recurrence relation for [`hermite`] |
| 43 | +/// *k*-th derivative of the Hermite polynomial *H<sub>n</sub>(x)* |
15 | 44 | /// |
16 | | -/// *H<sub>n+1</sub>(x) = 2xH<sub>n</sub>(x) - 2nH<sub>n-1</sub>(x)* |
| 45 | +/// This function does not exist in the Boost Math C++ library. |
| 46 | +#[doc(alias = "hermite_h_prime")] |
| 47 | +pub fn hermite_h_derivative(n: u32, x: f64, k: u32) -> f64 { |
| 48 | + if n < k { |
| 49 | + 0.0 |
| 50 | + } else { |
| 51 | + let mut p = hermite_h(n - k, x); |
| 52 | + for m in 0..k { |
| 53 | + p *= 2.0 * (n - m) as f64 |
| 54 | + } |
| 55 | + p |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Recurrence relation for [`hermite_he`] |
| 60 | +/// |
| 61 | +/// *He<sub>n+1</sub>(x) = x He<sub>n</sub>(x) - n He<sub>n-1</sub>(x)* |
17 | 62 | /// |
18 | 63 | /// # Examples |
19 | 64 | /// |
20 | 65 | /// ``` |
21 | | -/// # use boost::math::{hermite, hermite_next}; |
| 66 | +/// # use boost::math::{hermite_he, hermite_he_next}; |
22 | 67 | /// let x = 0.42; |
23 | | -/// let h0 = hermite(0, x); // 1 |
24 | | -/// let h1 = hermite(1, x); // 2x |
25 | | -/// let h2 = hermite(2, x); // 4x² - 2 |
26 | | -/// let h3 = hermite(3, x); // 8x³ - 12x |
27 | | -/// assert_eq!(hermite_next(1, &x, &h1, &h0), h2); |
28 | | -/// assert_eq!(hermite_next(2, &x, &h2, &h1), h3); |
| 68 | +/// let p0 = hermite_he(0, x); // 1 |
| 69 | +/// let p1 = hermite_he(1, x); // x |
| 70 | +/// let p2 = hermite_he(2, x); // x² - 1 |
| 71 | +/// let p3 = hermite_he(3, x); // x³ - 3x |
| 72 | +/// assert_eq!(hermite_he_next(1, x, p1, p0), p2); |
| 73 | +/// assert_eq!(hermite_he_next(2, x, p2, p1), p3); |
29 | 74 | /// ``` |
30 | | -#[allow(non_snake_case)] |
31 | 75 | #[inline(always)] |
32 | | -pub fn hermite_next(n: u32, x: &f64, Hn: &f64, Hn_1: &f64) -> f64 { |
33 | | - 2.0 * (x * Hn - (n as f64) * Hn_1) |
| 76 | +pub fn hermite_he_next(n: u32, x: f64, pn: f64, pn_prev: f64) -> f64 { |
| 77 | + x * pn - (n as f64) * pn_prev |
| 78 | +} |
| 79 | + |
| 80 | +/// Monic Hermite Polynomial *He<sub>n</sub>(x)* |
| 81 | +/// |
| 82 | +/// Note that this is the "probabilist's" Hermite polynomial, which is monic (leading coefficient |
| 83 | +/// is 1). It is related to the "physicist's" Hermite polynomial ([`hermite_h`]) by |
| 84 | +/// |
| 85 | +/// *He<sub>n</sub>(x) = 2<sup>-n/2</sup> H<sub>n</sub>(x / √2)* |
| 86 | +/// |
| 87 | +/// This function does not exist in the Boost Math C++ library. |
| 88 | +pub fn hermite_he(n: u32, x: f64) -> f64 { |
| 89 | + let (mut p0, mut p1) = (1.0, x); |
| 90 | + if n == 0 { |
| 91 | + p0 |
| 92 | + } else { |
| 93 | + for c in 1..n { |
| 94 | + (p0, p1) = (p1, hermite_he_next(c, x, p1, p0)); |
| 95 | + } |
| 96 | + p1 |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// *k*-th derivative of the Hermite polynomial *He<sub>n</sub>(x)* |
| 101 | +/// |
| 102 | +/// This function does not exist in the Boost Math C++ library. |
| 103 | +#[doc(alias = "hermite_he_prime")] |
| 104 | +pub fn hermite_he_derivative(n: u32, x: f64, k: u32) -> f64 { |
| 105 | + if n < k { |
| 106 | + 0.0 |
| 107 | + } else { |
| 108 | + let mut p = hermite_he(n - k, x); |
| 109 | + for m in 0..k { |
| 110 | + p *= (n - m) as f64 |
| 111 | + } |
| 112 | + p |
| 113 | + } |
34 | 114 | } |
35 | 115 |
|
36 | 116 | #[cfg(test)] |
37 | 117 | mod tests { |
38 | 118 | use super::*; |
| 119 | + use core::f64::consts::FRAC_1_SQRT_2; |
| 120 | + |
| 121 | + const ATOL: f64 = 1e-15; |
| 122 | + const RTOL: f64 = 1e-12; |
| 123 | + |
| 124 | + fn hermite_h_ffi(n: u32, x: f64) -> f64 { |
| 125 | + unsafe { crate::ffi::math_hermite(n as core::ffi::c_uint, x) } |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_hermite_h() { |
| 130 | + for n in 0..20 { |
| 131 | + for x in -10..=10 { |
| 132 | + let x = x as f64 * 0.1; |
| 133 | + let h = hermite_h_ffi(n, x); |
| 134 | + assert_relative_eq!(hermite_h(n, x), h, epsilon = ATOL, max_relative = RTOL); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_hermite_he() { |
| 141 | + for n in 0..20 { |
| 142 | + let c = FRAC_1_SQRT_2.powi(n as i32); |
| 143 | + for x in -10..=10 { |
| 144 | + let x = x as f64 * 0.1; |
| 145 | + let h = c * hermite_h(n, FRAC_1_SQRT_2 * x); |
| 146 | + assert_relative_eq!(hermite_he(n, x), h, epsilon = ATOL, max_relative = RTOL); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
39 | 150 |
|
40 | 151 | #[test] |
41 | | - fn test_hermite() { |
42 | | - assert_relative_eq!(hermite(0, 1.0), 1.0); |
43 | | - assert_relative_eq!(hermite(1, 1.0), 2.0); |
44 | | - assert_relative_eq!(hermite(2, 1.0), 2.0); |
45 | | - assert_relative_eq!(hermite(3, 1.0), -4.0); |
46 | | - assert_relative_eq!(hermite(4, 1.0), -20.0); |
| 152 | + fn test_hermite_h_derivative() { |
| 153 | + for n in 0..20 { |
| 154 | + for x in -10..=10 { |
| 155 | + let x = x as f64 * 0.1; |
| 156 | + |
| 157 | + let d0 = hermite_h_derivative(n, x, 0); |
| 158 | + assert_relative_eq!(d0, hermite_h(n, x)); |
| 159 | + |
| 160 | + let d1 = hermite_h_derivative(n, x, 1); |
| 161 | + if n >= 1 { |
| 162 | + assert_relative_eq!(d1, (2 * n) as f64 * hermite_h(n - 1, x)); |
| 163 | + } else { |
| 164 | + assert_eq!(d1, 0.0) |
| 165 | + } |
| 166 | + |
| 167 | + let d2 = hermite_h_derivative(n, x, 2); |
| 168 | + if n >= 2 { |
| 169 | + assert_relative_eq!(d2, (4 * n * (n - 1)) as f64 * hermite_h(n - 2, x)); |
| 170 | + } else { |
| 171 | + assert_eq!(d2, 0.0) |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn test_hermite_he_derivative() { |
| 179 | + for n in 0..20 { |
| 180 | + for x in -10..=10 { |
| 181 | + let x = x as f64 * 0.1; |
| 182 | + |
| 183 | + let d0 = hermite_he_derivative(n, x, 0); |
| 184 | + assert_relative_eq!(d0, hermite_he(n, x)); |
| 185 | + |
| 186 | + let d1 = hermite_he_derivative(n, x, 1); |
| 187 | + if n >= 1 { |
| 188 | + assert_relative_eq!(d1, n as f64 * hermite_he(n - 1, x)); |
| 189 | + } else { |
| 190 | + assert_eq!(d1, 0.0) |
| 191 | + } |
| 192 | + |
| 193 | + let d2 = hermite_he_derivative(n, x, 2); |
| 194 | + if n >= 2 { |
| 195 | + assert_relative_eq!(d2, (n * (n - 1)) as f64 * hermite_he(n - 2, x)); |
| 196 | + } else { |
| 197 | + assert_eq!(d2, 0.0) |
| 198 | + } |
| 199 | + } |
| 200 | + } |
47 | 201 | } |
48 | 202 | } |
0 commit comments