ruvector_math/spectral/
chebyshev.rs

1//! Chebyshev Polynomials
2//!
3//! Efficient polynomial approximation using Chebyshev basis.
4//! Key for matrix function approximation without eigendecomposition.
5
6use std::f64::consts::PI;
7
8/// Chebyshev polynomial of the first kind
9#[derive(Debug, Clone)]
10pub struct ChebyshevPolynomial {
11    /// Polynomial degree
12    pub degree: usize,
13}
14
15impl ChebyshevPolynomial {
16    /// Create Chebyshev polynomial T_n
17    pub fn new(degree: usize) -> Self {
18        Self { degree }
19    }
20
21    /// Evaluate T_n(x) using recurrence
22    /// T_0(x) = 1, T_1(x) = x, T_{n+1}(x) = 2x·T_n(x) - T_{n-1}(x)
23    pub fn eval(&self, x: f64) -> f64 {
24        if self.degree == 0 {
25            return 1.0;
26        }
27        if self.degree == 1 {
28            return x;
29        }
30
31        let mut t_prev = 1.0;
32        let mut t_curr = x;
33
34        for _ in 2..=self.degree {
35            let t_next = 2.0 * x * t_curr - t_prev;
36            t_prev = t_curr;
37            t_curr = t_next;
38        }
39
40        t_curr
41    }
42
43    /// Evaluate all Chebyshev polynomials T_0(x) through T_n(x)
44    pub fn eval_all(x: f64, max_degree: usize) -> Vec<f64> {
45        if max_degree == 0 {
46            return vec![1.0];
47        }
48
49        let mut result = Vec::with_capacity(max_degree + 1);
50        result.push(1.0);
51        result.push(x);
52
53        for k in 2..=max_degree {
54            let t_k = 2.0 * x * result[k - 1] - result[k - 2];
55            result.push(t_k);
56        }
57
58        result
59    }
60
61    /// Chebyshev nodes for interpolation: x_k = cos((2k+1)π/(2n))
62    pub fn nodes(n: usize) -> Vec<f64> {
63        (0..n)
64            .map(|k| ((2 * k + 1) as f64 * PI / (2 * n) as f64).cos())
65            .collect()
66    }
67
68    /// Derivative: T'_n(x) = n * U_{n-1}(x) where U is Chebyshev of second kind
69    pub fn derivative(&self, x: f64) -> f64 {
70        if self.degree == 0 {
71            return 0.0;
72        }
73        if self.degree == 1 {
74            return 1.0;
75        }
76
77        // Use: T'_n(x) = n * U_{n-1}(x)
78        // where U_0 = 1, U_1 = 2x, U_{n+1} = 2x*U_n - U_{n-1}
79        let n = self.degree;
80        let mut u_prev = 1.0;
81        let mut u_curr = 2.0 * x;
82
83        for _ in 2..n {
84            let u_next = 2.0 * x * u_curr - u_prev;
85            u_prev = u_curr;
86            u_curr = u_next;
87        }
88
89        n as f64 * if n == 1 { u_prev } else { u_curr }
90    }
91}
92
93/// Chebyshev expansion of a function
94/// f(x) ≈ Σ c_k T_k(x)
95#[derive(Debug, Clone)]
96pub struct ChebyshevExpansion {
97    /// Chebyshev coefficients c_k
98    pub coefficients: Vec<f64>,
99}
100
101impl ChebyshevExpansion {
102    /// Create from coefficients
103    pub fn new(coefficients: Vec<f64>) -> Self {
104        Self { coefficients }
105    }
106
107    /// Approximate function on [-1, 1] using n+1 Chebyshev nodes
108    pub fn from_function<F: Fn(f64) -> f64>(f: F, degree: usize) -> Self {
109        let n = degree + 1;
110        let nodes = ChebyshevPolynomial::nodes(n);
111
112        // Evaluate function at nodes
113        let f_values: Vec<f64> = nodes.iter().map(|&x| f(x)).collect();
114
115        // Compute coefficients via DCT-like formula
116        let mut coefficients = Vec::with_capacity(n);
117
118        for k in 0..n {
119            let mut c_k = 0.0;
120            for (j, &f_j) in f_values.iter().enumerate() {
121                let t_k_at_node = ChebyshevPolynomial::new(k).eval(nodes[j]);
122                c_k += f_j * t_k_at_node;
123            }
124            c_k *= 2.0 / n as f64;
125            if k == 0 {
126                c_k *= 0.5;
127            }
128            coefficients.push(c_k);
129        }
130
131        Self { coefficients }
132    }
133
134    /// Approximate exp(-t*x) for heat kernel (x in [0, 2])
135    /// Maps [0, 2] to [-1, 1] via x' = x - 1
136    pub fn heat_kernel(t: f64, degree: usize) -> Self {
137        Self::from_function(|x| {
138            let exponent = -t * (x + 1.0);
139            // Clamp to prevent overflow (exp(709) ≈ max f64, exp(-745) ≈ 0)
140            let clamped = exponent.clamp(-700.0, 700.0);
141            clamped.exp()
142        }, degree)
143    }
144
145    /// Approximate low-pass filter: 1 if λ < cutoff, 0 otherwise
146    /// Smooth transition via sigmoid-like function
147    pub fn low_pass(cutoff: f64, degree: usize) -> Self {
148        let steepness = 10.0 / cutoff.max(0.1);
149        Self::from_function(
150            |x| {
151                let lambda = (x + 1.0) / 2.0 * 2.0; // Map [-1,1] to [0,2]
152                let exponent = steepness * (lambda - cutoff);
153                // Clamp to prevent overflow
154                let clamped = exponent.clamp(-700.0, 700.0);
155                1.0 / (1.0 + clamped.exp())
156            },
157            degree,
158        )
159    }
160
161    /// Evaluate expansion at point x using Clenshaw recurrence
162    /// More numerically stable than direct summation
163    pub fn eval(&self, x: f64) -> f64 {
164        if self.coefficients.is_empty() {
165            return 0.0;
166        }
167        if self.coefficients.len() == 1 {
168            return self.coefficients[0];
169        }
170
171        // Clenshaw recurrence
172        let n = self.coefficients.len();
173        let mut b_next = 0.0;
174        let mut b_curr = 0.0;
175
176        for k in (1..n).rev() {
177            let b_prev = 2.0 * x * b_curr - b_next + self.coefficients[k];
178            b_next = b_curr;
179            b_curr = b_prev;
180        }
181
182        self.coefficients[0] + x * b_curr - b_next
183    }
184
185    /// Evaluate expansion on vector: apply filter to each component
186    pub fn eval_vector(&self, x: &[f64]) -> Vec<f64> {
187        x.iter().map(|&xi| self.eval(xi)).collect()
188    }
189
190    /// Degree of expansion
191    pub fn degree(&self) -> usize {
192        self.coefficients.len().saturating_sub(1)
193    }
194
195    /// Truncate to lower degree
196    pub fn truncate(&self, new_degree: usize) -> Self {
197        let n = (new_degree + 1).min(self.coefficients.len());
198        Self {
199            coefficients: self.coefficients[..n].to_vec(),
200        }
201    }
202
203    /// Add two expansions
204    pub fn add(&self, other: &Self) -> Self {
205        let max_len = self.coefficients.len().max(other.coefficients.len());
206        let mut coefficients = vec![0.0; max_len];
207
208        for (i, &c) in self.coefficients.iter().enumerate() {
209            coefficients[i] += c;
210        }
211        for (i, &c) in other.coefficients.iter().enumerate() {
212            coefficients[i] += c;
213        }
214
215        Self { coefficients }
216    }
217
218    /// Scale by constant
219    pub fn scale(&self, s: f64) -> Self {
220        Self {
221            coefficients: self.coefficients.iter().map(|&c| c * s).collect(),
222        }
223    }
224
225    /// Derivative expansion
226    /// d/dx Σ c_k T_k(x) = Σ c'_k T_k(x)
227    pub fn derivative(&self) -> Self {
228        let n = self.coefficients.len();
229        if n <= 1 {
230            return Self::new(vec![0.0]);
231        }
232
233        let mut d_coeffs = vec![0.0; n - 1];
234
235        // Backward recurrence for derivative coefficients
236        for k in (0..n - 1).rev() {
237            d_coeffs[k] = 2.0 * (k + 1) as f64 * self.coefficients[k + 1];
238            if k + 2 < n {
239                d_coeffs[k] += if k == 0 { 0.0 } else { d_coeffs[k + 2] };
240            }
241        }
242
243        // First coefficient needs halving
244        if !d_coeffs.is_empty() {
245            d_coeffs[0] *= 0.5;
246        }
247
248        Self { coefficients: d_coeffs }
249    }
250}
251
252#[cfg(test)]
253mod tests {
254    use super::*;
255
256    #[test]
257    fn test_chebyshev_polynomial() {
258        // T_0(x) = 1
259        assert!((ChebyshevPolynomial::new(0).eval(0.5) - 1.0).abs() < 1e-10);
260
261        // T_1(x) = x
262        assert!((ChebyshevPolynomial::new(1).eval(0.5) - 0.5).abs() < 1e-10);
263
264        // T_2(x) = 2x² - 1
265        let t2_at_half = 2.0 * 0.5 * 0.5 - 1.0;
266        assert!((ChebyshevPolynomial::new(2).eval(0.5) - t2_at_half).abs() < 1e-10);
267
268        // T_3(x) = 4x³ - 3x
269        let t3_at_half = 4.0 * 0.5_f64.powi(3) - 3.0 * 0.5;
270        assert!((ChebyshevPolynomial::new(3).eval(0.5) - t3_at_half).abs() < 1e-10);
271    }
272
273    #[test]
274    fn test_eval_all() {
275        let x = 0.5;
276        let all = ChebyshevPolynomial::eval_all(x, 5);
277
278        assert_eq!(all.len(), 6);
279        for (k, &t_k) in all.iter().enumerate() {
280            let expected = ChebyshevPolynomial::new(k).eval(x);
281            assert!((t_k - expected).abs() < 1e-10);
282        }
283    }
284
285    #[test]
286    fn test_chebyshev_nodes() {
287        let nodes = ChebyshevPolynomial::nodes(4);
288        assert_eq!(nodes.len(), 4);
289
290        // All nodes should be in [-1, 1]
291        for &x in &nodes {
292            assert!(x >= -1.0 && x <= 1.0);
293        }
294    }
295
296    #[test]
297    fn test_expansion_constant() {
298        let expansion = ChebyshevExpansion::from_function(|_| 5.0, 3);
299
300        // Should approximate 5.0 everywhere
301        for x in [-0.9, -0.5, 0.0, 0.5, 0.9] {
302            assert!((expansion.eval(x) - 5.0).abs() < 0.1);
303        }
304    }
305
306    #[test]
307    fn test_expansion_linear() {
308        let expansion = ChebyshevExpansion::from_function(|x| 2.0 * x + 1.0, 5);
309
310        for x in [-0.8, -0.3, 0.0, 0.4, 0.7] {
311            let expected = 2.0 * x + 1.0;
312            assert!(
313                (expansion.eval(x) - expected).abs() < 0.1,
314                "x={}, expected={}, got={}",
315                x,
316                expected,
317                expansion.eval(x)
318            );
319        }
320    }
321
322    #[test]
323    fn test_heat_kernel() {
324        let heat = ChebyshevExpansion::heat_kernel(1.0, 10);
325
326        // At x = -1 (λ = 0): exp(0) = 1
327        let at_zero = heat.eval(-1.0);
328        assert!((at_zero - 1.0).abs() < 0.1);
329
330        // At x = 1 (λ = 2): exp(-2) ≈ 0.135
331        let at_two = heat.eval(1.0);
332        assert!((at_two - (-2.0_f64).exp()).abs() < 0.1);
333    }
334
335    #[test]
336    fn test_clenshaw_stability() {
337        // High degree expansion should still be numerically stable
338        let expansion = ChebyshevExpansion::from_function(|x| x.sin(), 20);
339
340        for x in [-0.9, 0.0, 0.9] {
341            let approx = expansion.eval(x);
342            let exact = x.sin();
343            assert!(
344                (approx - exact).abs() < 0.01,
345                "x={}, approx={}, exact={}",
346                x,
347                approx,
348                exact
349            );
350        }
351    }
352}