packages

interpolate::poly::chebyshev

On this page

Chebyshev series sum_k c_k T_k(u) on a domain [a, b], u the affine map of x onto [-1, 1]. The basis of choice for approximating smooth functions: interpolation at Chebyshev points converges geometrically for analytic functions and never suffers Runge's phenomenon.

Structs

ChebyshevSource

pub struct Chebyshev

A Chebyshev series on [a, b]. It is a Curve (Clenshaw's recurrence, O(n), backward stable) and takes + - * with another series on the same domain or an f64.

nabi
use interpolate::{Chebyshev, Curve};

let c = Chebyshev::interpolate(f64::exp, 20, 0.0..1.0);
assert((c.at(0.3) - 0.3f64.exp()).abs() < 1e-14);
assert((c.integrate() - (1f64.exp() - 1.0)).abs() < 1e-14);
  • pub fn new(coef: Vector<f64>, domain: Range<f64>) throws (InterpError) -> Chebyshev§

    Source

    The series with coefficients coef (of T_0, T_1, ...) on domain.

    nabi
    use interpolate::{Chebyshev, Curve};
    
    let t2 = try Chebyshev::new([0, 0, 1], -1.0..1.0);      // 2x^2 - 1
    assert(t2.at(0.5) == -0.5);
    
  • pub fn interpolate(f: fn(f64) -> f64, n: usize, domain: Range<f64>) -> Chebyshev§

    Source

    The interpolant of f at the n Chebyshev points of the first kind on domain, x_j = cos(pi (j + 1/2) / n) mapped, by the discrete cosine sum (O(n^2)). For analytic f the error falls geometrically with n.

    nabi
    use interpolate::{Chebyshev, Curve};
    
    let runge = |x: f64| -> f64 { 1.0 / (1.0 + 25.0 * x * x) };
    let c = Chebyshev::interpolate(runge, 200, -1.0..1.0);
    assert((c.at(0.7) - runge(0.7)).abs() < 1e-14);
    
  • pub fn fit(x: Vector<f64>, y: Vector<f64>, deg: usize) throws (InterpError) -> Chebyshev§

    Source

    The least-squares series of degree deg through (x, y) on the domain [min x, max x], by pivoted QR (linalg::lstsq) on the Chebyshev-Vandermonde matrix, which stays well conditioned at high degree.

    nabi
    use interpolate::{Chebyshev, Curve};
    
    let c = try Chebyshev::fit([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], 2);
    assert((c.at(2.5) - 6.25).abs() < 1e-13);
    
  • pub fn coefficients(self) -> Vector<f64>§

    Source

    The coefficients of T_0, T_1, ...

    nabi
    use interpolate::Chebyshev;
    
    let c = try Chebyshev::new([1, 2], 0.0..1.0);
    assert(c.coefficients()[1] == 2.0);
    
  • pub fn domain(self) -> (f64, f64)§

    Source

    The domain (a, b).

    nabi
    use interpolate::Chebyshev;
    
    assert((try Chebyshev::new([1], 2.0..5.0)).domain() == (2.0, 5.0));
    
  • pub fn degree(self) -> usize§

    Source

    The degree.

    nabi
    use interpolate::Chebyshev;
    
    assert((try Chebyshev::new([1, 0, 3], -1.0..1.0)).degree() == 2);
    
  • pub fn deriv(self, m: usize = 1) -> Chebyshev§

    Source

    The m-th derivative (in x, so scaled by 2 / (b - a) per order), by the backward recurrence c'_{k-1} = c'_{k+1} + 2k c_k.

    nabi
    use interpolate::{Chebyshev, Curve};
    
    let c = Chebyshev::interpolate(f64::sin, 25, 0.0..2.0);
    assert((c.deriv(1).at(1.0) - 1f64.cos()).abs() < 1e-13);
    
  • pub fn integ(self, m: usize = 1) -> Chebyshev§

    Source

    The m-th antiderivative, each one zero at a.

    nabi
    use interpolate::{Chebyshev, Curve};
    
    let c = Chebyshev::interpolate(f64::cos, 25, 0.0..2.0);
    assert((c.integ(1).at(1.5) - 1.5f64.sin()).abs() < 1e-14);
    
  • pub fn integrate(self) -> f64§

    Source

    The integral over the domain.

    nabi
    use interpolate::Chebyshev;
    
    let c = try Chebyshev::new([1], 0.0..3.0);
    assert(c.integrate() == 3.0);
    
  • pub fn roots(self) throws (InterpError) -> Vector<f64>§

    Source

    The real roots in the domain, rising, after dropping trailing coefficients at rounding level: eigenvalues of the colleague matrix (linalg::eig) with imaginary part below 1e-6 and real part in [-1, 1] (within 1e-6, so roots on the domain's ends stay), mapped to x and polished by Newton steps.

    nabi
    use interpolate::Chebyshev;
    
    let c = Chebyshev::interpolate(f64::sin, 30, 1.0..10.0);
    let r = try c.roots();
    assert(r.len() == 3 && (r[0] - f64::PI).abs() < 1e-13);
    
  • pub fn to_polynomial(self) -> Polynomial§

    Source

    The same function as a power series in x. Converting loses digits for high degrees (the power basis is ill conditioned); keep the series for evaluation.

    nabi
    use interpolate::{Chebyshev, Polynomial};
    
    let t2 = try Chebyshev::new([0, 0, 1], -1.0..1.0);
    assert(t2.to_polynomial() == Polynomial::new([-1, 0, 2]));
    
Trait implementations 8
  • impl Curve for Chebyshev
  • impl ops::Add for Chebyshev
  • impl ops::Add<f64> for Chebyshev
  • impl ops::Sub for Chebyshev
  • impl ops::Sub<f64> for Chebyshev
  • impl ops::Mul for Chebyshev
  • impl ops::Mul<f64> for Chebyshev
  • impl ops::Neg for Chebyshev