1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! Sequence utilities

/// Generate a linearlly spaced set of points between start and stop
pub fn linspace(start: f64, stop: f64, num: i32) -> Vec<f64> {
    let step = (stop - start) / f64::from(num - 1);
    (0..num).map(|i| start + f64::from(i) * step).collect()
}

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

    const TOL: f64 = 1E-8;

    #[test]
    fn test_linspace() {
        let expected: Vec<f64> = vec![
            1.0, 1.18367347, 1.36734694, 1.55102041, 1.73469388, 1.91836735,
            2.10204082, 2.28571429, 2.46938776, 2.65306122, 2.83673469,
            3.02040816, 3.20408163, 3.3877551, 3.57142857, 3.75510204,
            3.93877551, 4.12244898, 4.30612245, 4.48979592, 4.67346939,
            4.85714286, 5.04081633, 5.2244898, 5.40816327, 5.59183673,
            5.7755102, 5.95918367, 6.14285714, 6.32653061, 6.51020408,
            6.69387755, 6.87755102, 7.06122449, 7.24489796, 7.42857143,
            7.6122449, 7.79591837, 7.97959184, 8.16326531, 8.34693878,
            8.53061224, 8.71428571, 8.89795918, 9.08163265, 9.26530612,
            9.44897959, 9.63265306, 9.81632653, 10.0,
        ];

        assert::close(expected, linspace(1.0, 10.0, 50), TOL);
    }
}