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
//! Optics: diffraction and standing waves.

use crate::Vec1;

impl Vec1<f64> {
    /// Bragg diffraction angles (in radians) for a given set of orders.
    ///
    /// `n * lambda = 2 * d * sin(theta)`  =>  `theta_n = arcsin(n * lambda / (2 * d))`
    ///
    /// `self` holds the diffraction orders `n`. Returns `f64::NAN` for
    /// orders where the argument to `arcsin` exceeds 1.
    ///
    /// ```rust
    /// use one_indexed_vec::{vec1, Vec1};
    ///
    /// let orders: Vec1<f64> = vec1![1.0, 2.0, 3.0];
    /// let angles = orders.bragg_angles(1.0, 3.0);
    /// assert!(angles[1] > 0.0);
    /// ```
    pub fn bragg_angles(&self, wavelength: f64, d_spacing: f64) -> Vec1<f64> {
        assert!(wavelength > 0.0, "bragg_angles: wavelength must be > 0");
        assert!(d_spacing > 0.0, "bragg_angles: d_spacing must be > 0");
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            let sin_theta = self[i] * wavelength / (2.0 * d_spacing);
            if sin_theta.abs() <= 1.0 {
                out.push(libm::asin(sin_theta));
            } else {
                out.push(f64::NAN);
            }
        }
        out
    }

    /// Standing-wave frequencies of a string fixed at both ends.
    ///
    /// `f_n = n * v / (2 * L)`
    ///
    /// `self` holds the mode numbers `n`.
    ///
    /// ```rust
    /// use one_indexed_vec::{vec1, Vec1};
    ///
    /// let n: Vec1<f64> = vec1![1.0, 2.0, 3.0];
    /// let f = n.standing_wave_frequencies(343.0, 1.0);
    /// assert!((f[1] - 171.5).abs() < 1e-10);
    /// assert!((f[2] - 343.0).abs() < 1e-10);
    /// ```
    pub fn standing_wave_frequencies(&self, speed: f64, length: f64) -> Vec1<f64> {
        assert!(length > 0.0, "standing_wave_frequencies: length must be > 0");
        let factor = speed / (2.0 * length);
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            out.push(self[i] * factor);
        }
        out
    }

    /// Diffraction grating maxima angles.
    ///
    /// `d * sin(theta) = n * lambda`  =>  `theta_n = arcsin(n * lambda / d)`
    ///
    /// `self` holds the orders `n`. `f64::NAN` for orders beyond the
    /// diffraction limit.
    pub fn grating_angles(&self, wavelength: f64, grating_spacing: f64) -> Vec1<f64> {
        assert!(wavelength > 0.0, "grating_angles: wavelength must be > 0");
        assert!(grating_spacing > 0.0, "grating_angles: grating_spacing must be > 0");
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            let sin_theta = self[i] * wavelength / grating_spacing;
            if sin_theta.abs() <= 1.0 {
                out.push(libm::asin(sin_theta));
            } else {
                out.push(f64::NAN);
            }
        }
        out
    }

    /// Thin-lens image distance from the thin-lens equation.
    ///
    /// `1/f = 1/d_o + 1/d_i`  =>  `d_i = f * d_o / (d_o - f)`
    ///
    /// `self` holds object distances `d_o`. The focal length `f` is a scalar.
    pub fn thin_lens_image_distance(&self, focal_length: f64) -> Vec1<f64> {
        let mut out = Vec1::with_capacity(self.len());
        for i in 1..=self.len() {
            let d_o = self[i];
            let denom = d_o - focal_length;
            if denom.abs() < 1e-15 {
                out.push(f64::INFINITY);
            } else {
                out.push(focal_length * d_o / denom);
            }
        }
        out
    }
}

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

    #[test]
    fn bragg_angles_basic() {
        let orders = vec1![1.0f64, 2.0, 3.0];
        let angles = orders.bragg_angles(1.0, 3.0);
        assert!(angles[1] > 0.0);
        assert!(angles[2] > angles[1]);
        assert!(angles[3] > angles[2]);
    }

    #[test]
    fn bragg_angles_beyond_limit() {
        let orders = vec1![10.0f64];
        let angles = orders.bragg_angles(1.0, 1.0);
        assert!(angles[1].is_nan());
    }

    #[test]
    fn standing_wave_frequencies_basic() {
        let n = vec1![1.0f64, 2.0, 3.0];
        let f = n.standing_wave_frequencies(343.0, 1.0);
        assert!((f[1] - 171.5).abs() < 1e-10);
        assert!((f[2] - 343.0).abs() < 1e-10);
        assert!((f[3] - 514.5).abs() < 1e-10);
    }

    #[test]
    fn grating_angles_basic() {
        let orders = vec1![1.0f64, 2.0];
        let angles = orders.grating_angles(500e-9, 2e-6);
        assert!(angles[1] > 0.0);
        assert!(angles[2] > angles[1]);
    }

    #[test]
    fn thin_lens_basic() {
        let d_o = vec1![20.0f64];
        let d_i = d_o.thin_lens_image_distance(10.0);
        assert!((d_i[1] - 20.0).abs() < 1e-10);
    }
}