one-indexed-vec 0.2.0

A one-based (1-indexed) wrapper around Vec<T>: element at position N is accessed via vec[N].
Documentation
//! Orthogonal polynomials.

use crate::Vec1;

impl Vec1<f64> {
    /// Chebyshev polynomials of the first kind.
    ///
    /// Recurrence: `T_0 = 1, T_1 = x, T_n = 2x T_{n-1} - T_{n-2}`.
    ///
    /// Returns `T_0..T_n` as a 1-based vector, with `out[k] = T_{k-1}`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    ///
    /// // T_2(x) = 2x² - 1
    /// let t = Vec1::chebyshev(2, 0.5);
    /// assert!((t[3] - (2.0 * 0.25 - 1.0)).abs() < 1e-10);
    /// ```
    pub fn chebyshev(n: usize, x: f64) -> Self {
        let mut t = Self::with_capacity(n + 1);
        t.push(1.0);
        if n >= 1 {
            t.push(x);
        }
        for k in 2..=n {
            let v = 2.0 * x * t[k] - t[k - 1];
            t.push(v);
        }
        t
    }

    /// Legendre polynomials.
    ///
    /// Recurrence: `P_0 = 1, P_1 = x, (n+1) P_{n+1} = (2n+1) x P_n - n P_{n-1}`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    ///
    /// // P_2(x) = (3x² - 1)/2
    /// let p = Vec1::legendre(2, 0.5);
    /// assert!((p[3] - (3.0 * 0.25 - 1.0) / 2.0).abs() < 1e-10);
    /// ```
    pub fn legendre(n: usize, x: f64) -> Self {
        let mut p = Self::with_capacity(n + 1);
        p.push(1.0);
        if n >= 1 {
            p.push(x);
        }
        for k in 2..=n {
            let kf = k as f64;
            let v = ((2.0 * kf - 1.0) * x * p[k] - (kf - 1.0) * p[k - 1]) / kf;
            p.push(v);
        }
        p
    }

    /// Physicists' Hermite polynomials.
    ///
    /// Recurrence: `H_0 = 1, H_1 = 2x, H_n = 2x H_{n-1} - 2(n-1) H_{n-2}`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    ///
    /// // H_2(x) = 4x² - 2
    /// let h = Vec1::hermite(2, 0.5);
    /// assert!((h[3] - (4.0 * 0.25 - 2.0)).abs() < 1e-10);
    /// ```
    pub fn hermite(n: usize, x: f64) -> Self {
        let mut h = Self::with_capacity(n + 1);
        h.push(1.0);
        if n >= 1 {
            h.push(2.0 * x);
        }
        for k in 2..=n {
            let kf = k as f64;
            let v = 2.0 * x * h[k] - 2.0 * (kf - 1.0) * h[k - 1];
            h.push(v);
        }
        h
    }

    /// Laguerre polynomials.
    ///
    /// Recurrence: `L_0 = 1, L_1 = 1-x, (n+1) L_{n+1} = (2n+1-x) L_n - n L_{n-1}`.
    ///
    /// ```rust
    /// use one_indexed_vec::Vec1;
    ///
    /// // L_2(x) = (x² - 4x + 2)/2
    /// let l = Vec1::laguerre(2, 0.5);
    /// let expected = (0.25 - 2.0 + 2.0) / 2.0;
    /// assert!((l[3] - expected).abs() < 1e-10);
    /// ```
    pub fn laguerre(n: usize, x: f64) -> Self {
        let mut l = Self::with_capacity(n + 1);
        l.push(1.0);
        if n >= 1 {
            l.push(1.0 - x);
        }
        for k in 2..=n {
            let kf = k as f64;
            let v = ((2.0 * kf - 1.0 - x) * l[k] - (kf - 1.0) * l[k - 1]) / kf;
            l.push(v);
        }
        l
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn chebyshev_basic() {
        let t = Vec1::chebyshev(2, 0.5);
        assert!((t[1] - 1.0).abs() < 1e-10);
        assert!((t[2] - 0.5).abs() < 1e-10);
        assert!((t[3] - (2.0 * 0.25 - 1.0)).abs() < 1e-10);
    }

    #[test]
    fn legendre_basic() {
        let p = Vec1::legendre(2, 0.5);
        assert!((p[1] - 1.0).abs() < 1e-10);
        assert!((p[2] - 0.5).abs() < 1e-10);
        assert!((p[3] - (3.0 * 0.25 - 1.0) / 2.0).abs() < 1e-10);
    }

    #[test]
    fn hermite_basic() {
        let h = Vec1::hermite(2, 0.5);
        assert!((h[1] - 1.0).abs() < 1e-10);
        assert!((h[2] - 1.0).abs() < 1e-10);
        assert!((h[3] - (4.0 * 0.25 - 2.0)).abs() < 1e-10);
    }

    #[test]
    fn laguerre_basic() {
        let l = Vec1::laguerre(2, 0.5);
        assert!((l[1] - 1.0).abs() < 1e-10);
        assert!((l[2] - 0.5).abs() < 1e-10);
        let expected = (0.25 - 2.0 + 2.0) / 2.0;
        assert!((l[3] - expected).abs() < 1e-10);
    }

    #[test]
    fn chebyshev_zero_order() {
        let t = Vec1::chebyshev(0, 0.5);
        assert_eq!(t.len(), 1);
        assert!((t[1] - 1.0).abs() < 1e-10);
    }
}