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
//! Extra basic numeric operations on `Vec1` beyond what's in `vec1_core`.

use core::ops::{Add, Mul};

use crate::Vec1;

impl<T> Vec1<T>
where
    T: Copy + Add<Output = T> + Default,
{
    /// Cumulative sum (identical to [`Vec1::prefix_sum`], kept for symmetry).
    #[inline]
    pub fn cumsum(&self) -> Vec1<T> {
        self.prefix_sum()
    }
}

impl<T> Vec1<T>
where
    T: Copy + Mul<Output = T> + Default,
{
    /// Cumulative product.
    ///
    /// ```rust
    /// use one_indexed_vec::vec1;
    /// let v = vec1![2, 3, 4];
    /// assert_eq!(v.cumulative_product().as_slice(), &[2, 6, 24]);
    /// ```
    #[inline]
    pub fn cumulative_product(&self) -> Vec1<T> {
        let mut out = Vec1::with_capacity(self.len());
        let mut acc = T::default();
        let mut first = true;
        for &x in self.iter() {
            if first {
                acc = x;
                first = false;
            } else {
                acc = acc * x;
            }
            out.push(acc);
        }
        out
    }
}

impl<T> Vec1<T>
where
    T: Copy + core::ops::Add<Output = T>,
{
    /// Element-wise addition of two equal-length vectors.
    ///
    /// ```rust
    /// use one_indexed_vec::vec1;
    /// let a = vec1![1, 2, 3];
    /// let b = vec1![4, 5, 6];
    /// assert_eq!(a.add_elementwise(&b).as_slice(), &[5, 7, 9]);
    /// ```
    #[inline]
    pub fn add_elementwise(&self, other: &Vec1<T>) -> Vec1<T> {
        assert_eq!(self.len(), other.len(), "add_elementwise: length mismatch");
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] + other[i]);
        }
        out
    }
}

impl<T> Vec1<T>
where
    T: Copy + core::ops::Mul<Output = T>,
{
    /// Element-wise product (Hadamard product).
    ///
    /// ```rust
    /// use one_indexed_vec::vec1;
    /// let a = vec1![1, 2, 3];
    /// let b = vec1![4, 5, 6];
    /// assert_eq!(a.mul_elementwise(&b).as_slice(), &[4, 10, 18]);
    /// ```
    #[inline]
    pub fn mul_elementwise(&self, other: &Vec1<T>) -> Vec1<T> {
        assert_eq!(self.len(), other.len(), "mul_elementwise: length mismatch");
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] * other[i]);
        }
        out
    }
}

impl<T> Vec1<T>
where
    T: Copy + core::ops::Sub<Output = T>,
{
    /// Element-wise subtraction.
    #[inline]
    pub fn sub_elementwise(&self, other: &Vec1<T>) -> Vec1<T> {
        assert_eq!(self.len(), other.len(), "sub_elementwise: length mismatch");
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] - other[i]);
        }
        out
    }
}

impl<T> Vec1<T>
where
    T: Copy + core::ops::Mul<Output = T> + core::ops::Add<Output = T> + Default,
{
    /// Outer product: `out[i * m + j] = self[i] * other[j]`.
    ///
    /// Returns a flat `Vec1` of length `self.len() * other.len()`,
    /// row-major.
    #[inline]
    pub fn outer_product(&self, other: &Vec1<T>) -> Vec1<T> {
        let n = self.len();
        let m = other.len();
        let mut out = Vec1::with_capacity(n * m);
        for i in 1..=n {
            for j in 1..=m {
                out.push(self[i] * other[j]);
            }
        }
        out
    }
}

impl Vec1<f64> {
    /// Scales each element by `factor`.
    ///
    /// ```rust
    /// use one_indexed_vec::vec1;
    /// let v = vec1![1.0, 2.0, 3.0];
    /// assert_eq!(v.scale(2.0).as_slice(), &[2.0, 4.0, 6.0]);
    /// ```
    #[inline]
    pub fn scale(&self, factor: f64) -> Vec1<f64> {
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] * factor);
        }
        out
    }

    /// Shifts each element by `offset`.
    #[inline]
    pub fn shift(&self, offset: f64) -> Vec1<f64> {
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] + offset);
        }
        out
    }
}

#[cfg(test)]
mod tests {
    use crate::vec1;

    #[test]
    fn cumsum_basic() {
        assert_eq!(vec1![1, 2, 3, 4].cumsum().as_slice(), &[1, 3, 6, 10]);
    }

    #[test]
    fn cumulative_product_basic() {
        assert_eq!(vec1![2, 3, 4].cumulative_product().as_slice(), &[2, 6, 24]);
    }

    #[test]
    fn add_mul_sub_elementwise() {
        let a = vec1![1, 2, 3];
        let b = vec1![4, 5, 6];
        assert_eq!(a.add_elementwise(&b).as_slice(), &[5, 7, 9]);
        assert_eq!(a.mul_elementwise(&b).as_slice(), &[4, 10, 18]);
        assert_eq!(b.sub_elementwise(&a).as_slice(), &[3, 3, 3]);
    }

    #[test]
    fn outer_product_basic() {
        let a = vec1![1, 2];
        let b = vec1![3, 4, 5];
        let out = a.outer_product(&b);
        assert_eq!(out.as_slice(), &[3, 4, 5, 6, 8, 10]);
    }

    #[test]
    fn scale_and_shift() {
        let v = vec1![1.0, 2.0, 3.0];
        assert_eq!(v.scale(2.0).as_slice(), &[2.0, 4.0, 6.0]);
        assert_eq!(v.shift(10.0).as_slice(), &[11.0, 12.0, 13.0]);
    }
}