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
//! Extended Struve function interface
//!
//! Provides top-level public wrappers matching the requested API:
//!
//! * `struve_h(nu, x)` – Struve H_ν(x)
//! * `struve_l(nu, x)` – Modified Struve L_ν(x)
//! * `struve_h0(x)` – H_0(x) specialisation
//! * `struve_h1(x)` – H_1(x) specialisation
//!
//! All functions delegate to the high-quality implementations in `crate::struve`.
use crate::error::SpecialResult;
/// Struve function H_ν(x).
///
/// The Struve function is defined by:
/// ```text
/// H_ν(x) = (x/2)^{ν+1} ∑_{m=0}^∞ (-1)^m (x/2)^{2m} / [Γ(m+3/2) Γ(m+ν+3/2)]
/// ```
///
/// For small |x| a power series is used; for large |x| an asymptotic form
/// relative to Bessel functions of the second kind is used.
///
/// # Arguments
///
/// * `nu` – Order (real)
/// * `x` – Argument (real, x ≥ 0 recommended)
///
/// # Returns
///
/// * `Ok(f64)` – value of H_ν(x)
/// * `Err` – if inputs are NaN
///
/// # Examples
///
/// ```
/// use scirs2_special::struve_h;
///
/// // H_0(0) = 0
/// let h0_0 = struve_h(0.0, 0.0).expect("H_0(0)");
/// assert!((h0_0 - 0.0).abs() < 1e-14);
///
/// // H_1(0) = 0
/// let h1_0 = struve_h(1.0, 0.0).expect("H_1(0)");
/// assert!((h1_0 - 0.0).abs() < 1e-14);
/// ```
#[inline]
pub fn struve_h(nu: f64, x: f64) -> SpecialResult<f64> {
crate::struve::struve(nu, x)
}
/// Modified Struve function L_ν(x).
///
/// The modified Struve function is defined by:
/// ```text
/// L_ν(x) = -i^{-ν} H_ν(ix)
/// = (x/2)^{ν+1} ∑_{m=0}^∞ (x/2)^{2m} / [Γ(m+3/2) Γ(m+ν+3/2)]
/// ```
///
/// All terms are positive, so L_ν diverges for large x (unlike H_ν which
/// oscillates like Bessel Y).
///
/// # Arguments
///
/// * `nu` – Order (real)
/// * `x` – Argument (real, x ≥ 0 recommended)
///
/// # Returns
///
/// * `Ok(f64)` – value of L_ν(x)
/// * `Err` – if inputs are NaN
///
/// # Examples
///
/// ```
/// use scirs2_special::struve_l;
///
/// // L_0(0) = 0
/// let l0_0 = struve_l(0.0, 0.0).expect("L_0(0)");
/// assert!((l0_0 - 0.0).abs() < 1e-14);
///
/// // L should grow for large x
/// let l0_5 = struve_l(0.0, 5.0).expect("L_0(5)");
/// assert!(l0_5 > 0.0 && l0_5.is_finite());
/// ```
#[inline]
pub fn struve_l(nu: f64, x: f64) -> SpecialResult<f64> {
crate::struve::mod_struve(nu, x)
}
/// Struve function H_0(x) — order-0 specialisation.
///
/// H_0(x) = (2/π) [1 - cos x / x - ∫_0^x sin t / t dt] (for large x)
///
/// # Arguments
///
/// * `x` – Real argument
///
/// # Returns
///
/// * `Ok(f64)` – value of H_0(x)
/// * `Err` – if `x` is NaN
///
/// # Examples
///
/// ```
/// use scirs2_special::struve_h0;
///
/// // H_0(0) = 0
/// let h = struve_h0(0.0).expect("H_0(0)");
/// assert!(h.abs() < 1e-14);
/// ```
#[inline]
pub fn struve_h0(x: f64) -> SpecialResult<f64> {
crate::struve::struve(0.0, x)
}
/// Struve function H_1(x) — order-1 specialisation.
///
/// H_1(x) = (2/π) [1 - J_0(x)] + (2x/π) [- 1/3 + x²/15 - …]
///
/// # Arguments
///
/// * `x` – Real argument
///
/// # Returns
///
/// * `Ok(f64)` – value of H_1(x)
/// * `Err` – if `x` is NaN
///
/// # Examples
///
/// ```
/// use scirs2_special::struve_h1;
///
/// // H_1(0) = 0
/// let h = struve_h1(0.0).expect("H_1(0)");
/// assert!(h.abs() < 1e-14);
/// ```
#[inline]
pub fn struve_h1(x: f64) -> SpecialResult<f64> {
crate::struve::struve(1.0, x)
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
#[test]
fn test_struve_h0_at_zero() {
let val = struve_h0(0.0).expect("H_0(0)");
assert_relative_eq!(val, 0.0, epsilon = 1e-14);
}
#[test]
fn test_struve_h1_at_zero() {
let val = struve_h1(0.0).expect("H_1(0)");
assert_relative_eq!(val, 0.0, epsilon = 1e-14);
}
#[test]
fn test_struve_h_consistency_with_h0() {
// struve_h(0, x) should equal struve_h0(x)
for x in [0.5_f64, 1.0, 2.0, 5.0] {
let h = struve_h(0.0, x).expect("H");
let h0 = struve_h0(x).expect("H0");
assert!((h - h0).abs() < 1e-14, "at x={x}: struve_h={h}, struve_h0={h0}");
}
}
#[test]
fn test_struve_h_consistency_with_h1() {
for x in [0.5_f64, 1.0, 2.0, 5.0] {
let h = struve_h(1.0, x).expect("H");
let h1 = struve_h1(x).expect("H1");
assert!((h - h1).abs() < 1e-14, "at x={x}: struve_h={h}, struve_h1={h1}");
}
}
#[test]
fn test_struve_l0_positive() {
// L_0(x) > 0 for x > 0
for x in [0.5_f64, 1.0, 2.0, 5.0] {
let l = struve_l(0.0, x).expect("L_0");
assert!(l > 0.0, "L_0({x}) should be positive, got {l}");
}
}
#[test]
fn test_struve_l_consistency_with_mod_struve() {
// struve_l should be consistent with the existing mod_struve export
for x in [1.0_f64, 2.0, 3.0] {
let l = struve_l(0.0, x).expect("struve_l");
let ms = crate::struve::mod_struve(0.0, x).expect("mod_struve");
assert!((l - ms).abs() < 1e-14, "at x={x}: struve_l={l}, mod_struve={ms}");
}
}
#[test]
fn test_struve_h_small_argument() {
// For small x, H_0(x) ≈ 2x/π
let x = 0.1_f64;
let h0 = struve_h0(x).expect("H_0(0.1)");
let approx = 2.0 * x / std::f64::consts::PI;
assert_relative_eq!(h0, approx, epsilon = 1e-4);
}
#[test]
fn test_struve_h_h1_small_argument() {
// For small x, H_1(x) ≈ 2x²/(3π)
let x = 0.1_f64;
let h1 = struve_h1(x).expect("H_1(0.1)");
let approx = 2.0 * x * x / (3.0 * std::f64::consts::PI);
assert_relative_eq!(h1, approx, epsilon = 1e-4);
}
#[test]
fn test_struve_h_finite() {
// All calls with finite input should return finite values
for nu in [0.0_f64, 1.0, 2.0, 0.5, 1.5] {
for x in [0.0_f64, 1.0, 5.0, 10.0] {
let val = struve_h(nu, x).expect("struve_h");
assert!(val.is_finite(), "H_{nu}({x}) should be finite, got {val}");
}
}
}
#[test]
fn test_struve_l_monotone() {
// L_0 is monotonically increasing for x > 0
let l1 = struve_l(0.0, 1.0).expect("L_0(1)");
let l2 = struve_l(0.0, 2.0).expect("L_0(2)");
let l3 = struve_l(0.0, 3.0).expect("L_0(3)");
assert!(l2 > l1, "L_0 should increase: L(1)={l1}, L(2)={l2}");
assert!(l3 > l2, "L_0 should increase: L(2)={l2}, L(3)={l3}");
}
}