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
//! Bessel Function Visualization Example
//!
//! This example generates a CSV file with values of various Bessel functions
//! that can be used for visualization with plotting tools.
//!
//! The CSV format is:
//! x, J0(x), J1(x), J2(x), J3(x), Y0(x), Y1(x), I0(x), I1(x), K0(x), K1(x)
use scirs2_special::bessel::{i0, i1, j0, j1, jn, k0, k1, y0, y1};
use std::fs::File;
use std::io::{self, Write};
#[allow(dead_code)]
fn main() -> io::Result<()> {
// Create output file
let mut file = File::create("bessel_functions.csv")?;
// Write CSV header
writeln!(
file,
"x,J0(x),J1(x),J2(x),J3(x),Y0(x),Y1(x),I0(x),I1(x),K0(x),K1(x)"
)?;
// Generate values from 0.1 to 20.0
let mut x = 0.1;
while x <= 20.0 {
// Evaluate Bessel functions
let j0_val = j0(x);
let j1_val = j1(x);
let j2_val = jn(2, x);
let j3_val = jn(3, x);
let y0_val = y0(x);
let y1_val = y1(x);
let i0_val = i0(x);
let i1_val = i1(x);
let k0_val = k0(x);
let k1_val = k1(x);
// Write row to CSV
writeln!(
file,
"{:.4},{:.6},{:.6},{:.6},{:.6},{:.6},{:.6},{:.6},{:.6},{:.6},{:.6}",
x, j0_val, j1_val, j2_val, j3_val, y0_val, y1_val, i0_val, i1_val, k0_val, k1_val
)?;
// Increase x with smaller steps for lower values to better capture oscillations
if x < 1.0 {
x += 0.05;
} else if x < 5.0 {
x += 0.1;
} else {
x += 0.2;
}
}
println!("Generated bessel_functions.csv for visualization");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_positive_values() {
// Test that all Bessel functions produce valid results for a range of inputs
for x in [0.5, 1.0, 5.0, 10.0].iter() {
// Test that regular Bessel functions produce finite values
let j0_val: f64 = j0(*x);
let j1_val: f64 = j1(*x);
let j2_val: f64 = jn(2, *x);
// Check that values are finite (not NaN or infinite)
assert!(
j0_val.is_finite(),
"J0({}) = {} should be finite",
x,
j0_val
);
assert!(
j1_val.is_finite(),
"J1({}) = {} should be finite",
x,
j1_val
);
assert!(
j2_val.is_finite(),
"J2({}) = {} should be finite",
x,
j2_val
);
// Modified functions should be positive for positive inputs
assert!(i0(*x) > 0.0);
assert!(i1(*x) > 0.0);
assert!(k0(*x) > 0.0);
assert!(k1(*x) > 0.0);
}
}
}