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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::linspace::{LinSpace, LinearInterpolation};
use core::ops::Range;
use num_traits::real::Real;
pub type Arange<T> = LinSpace<T>;
pub fn arange<R, F>(range: R, step: F) -> Arange<F>
where
(R, F): Into<ArangeImpl<F>>,
{
let ArangeImpl { interpolate, steps } = (range, step).into();
Arange::new(steps, interpolate)
}
pub struct ArangeImpl<T> {
pub interpolate: LinearInterpolation<T>,
pub steps: usize,
}
impl<F: Real> From<(Range<F>, F)> for ArangeImpl<F> {
fn from((range, step): (Range<F>, F)) -> Self {
let Range { start, end } = range;
ArangeImpl {
interpolate: LinearInterpolation { start, step },
steps: ((end - start) / step).ceil().to_usize().unwrap(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_arange() {
let it = arange(0.0..2.0, 0.5);
assert!(it.eq(vec![0.0, 0.5, 1.0, 1.5]));
}
}