lib_jsl_interpolation/one_d/
chebyshev_approximations.rs1use lib_jsl_core::*;
6use std::f64::consts::PI;
7
8#[derive(Debug, Clone)]
10pub struct ChebyshevApproximation {
11 a: f64,
12 b: f64,
13 coefficients: Vec<f64>,
14}
15
16impl ChebyshevApproximation {
17 pub fn new(f: impl Fn(f64) -> f64, a: f64, b: f64, n: usize) -> Result<Self, ErrorsJSL> {
19 if n == 0 || a.partial_cmp(&b) != Some(std::cmp::Ordering::Less) {
20 return Err(ErrorsJSL::InvalidInputRange("need n > 0 and a < b"));
21 }
22 let fv: Vec<f64> = (0..n)
23 .map(|k| f(0.5 * (b - a) * (PI * (k as f64 + 0.5) / n as f64).cos() + 0.5 * (b + a)))
24 .collect();
25 let coefficients = (0..n)
26 .map(|j| {
27 let s: f64 = (0..n).map(|k| fv[k] * (PI * j as f64 * (k as f64 + 0.5) / n as f64).cos()).sum();
28 2.0 * s / n as f64
29 })
30 .collect();
31 Ok(ChebyshevApproximation { a, b, coefficients })
32 }
33
34 pub fn coefficients(&self) -> &[f64] {
36 &self.coefficients
37 }
38
39 pub fn truncate(&mut self, tol: f64) {
41 while self.coefficients.len() > 1 && self.coefficients.last().is_some_and(|c| c.abs() < tol) {
42 self.coefficients.pop();
43 }
44 }
45
46 pub fn evaluate(&self, x: f64) -> f64 {
48 let t = (2.0 * x - self.a - self.b) / (self.b - self.a);
49 let (mut d, mut dd) = (0.0, 0.0);
50 for &c in self.coefficients[1..].iter().rev() {
51 let sv = d;
52 d = 2.0 * t * d - dd + c;
53 dd = sv;
54 }
55 t * d - dd + 0.5 * self.coefficients[0]
56 }
57
58 pub fn derivative(&self) -> ChebyshevApproximation {
60 let n = self.coefficients.len();
61 let mut der = vec![0.0; n + 1];
64 for j in (1..n).rev() {
65 der[j - 1] = der[j + 1] + 2.0 * j as f64 * self.coefficients[j];
66 }
67 der.truncate(n.saturating_sub(1).max(1));
68 let scale = 2.0 / (self.b - self.a);
69 for c in der.iter_mut() {
70 *c *= scale;
71 }
72 ChebyshevApproximation { a: self.a, b: self.b, coefficients: der }
73 }
74
75 pub fn integral(&self) -> ChebyshevApproximation {
77 let n = self.coefficients.len();
78 let con = 0.25 * (self.b - self.a);
79 let c = |k: usize| self.coefficients.get(k).copied().unwrap_or(0.0);
80 let mut out = vec![0.0; n + 1];
81 let mut sum = 0.0;
82 let mut fac = 1.0;
83 for (j, o) in out.iter_mut().enumerate().skip(1) {
84 *o = con * (c(j - 1) - c(j + 1)) / j as f64;
85 sum += fac * *o;
86 fac = -fac;
87 }
88 out[0] = 2.0 * sum;
90 ChebyshevApproximation { a: self.a, b: self.b, coefficients: out }
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn approximates_smooth_functions_to_machine_precision() {
100 let c = ChebyshevApproximation::new(f64::exp, -1.0, 2.0, 24).unwrap();
101 for k in 0..=30 {
102 let x = -1.0 + 3.0 * k as f64 / 30.0;
103 assert!((c.evaluate(x) - x.exp()).abs() < 1e-13, "x={x}");
104 }
105 let mut t = c.clone();
106 t.truncate(1e-14);
107 assert!(t.coefficients().len() < 22);
108 }
109
110 #[test]
111 fn derivative_and_integral() {
112 let c = ChebyshevApproximation::new(f64::sin, 0.0, 3.0, 30).unwrap();
113 let d = c.derivative();
114 let i = c.integral();
115 for k in 1..10 {
116 let x = k as f64 * 0.3;
117 assert!((d.evaluate(x) - x.cos()).abs() < 1e-11, "d x={x}");
118 assert!((i.evaluate(x) - (1.0 - x.cos())).abs() < 1e-11, "i x={x}");
119 }
120 }
121
122 #[test]
123 fn rejects_bad_interval() {
124 assert!(ChebyshevApproximation::new(|x| x, 1.0, 1.0, 4).is_err());
125 }
126}