acme_tensor/actions/create/
linspace.rs

1/*
2    Appellation: linspace <mod>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use super::step_size;
6use num::traits::{FromPrimitive, Num};
7
8pub trait Linspace<T> {
9    fn linspace(start: T, stop: T, steps: usize) -> Self;
10}
11
12pub trait LinspaceExt<T>: Linspace<T> {
13    fn linspace_until(stop: T, steps: usize) -> Self;
14}
15
16impl<S, T> LinspaceExt<T> for S
17where
18    S: Linspace<T>,
19    T: Default,
20{
21    fn linspace_until(stop: T, steps: usize) -> Self {
22        S::linspace(T::default(), stop, steps)
23    }
24}
25
26impl<T> Linspace<T> for Vec<T>
27where
28    T: Copy + Default + FromPrimitive + Num + PartialOrd,
29{
30    fn linspace(start: T, stop: T, steps: usize) -> Self {
31        let step = step_size(start, stop, steps);
32        let mut vec = Vec::with_capacity(steps);
33        let mut value = start;
34        for _ in 0..steps {
35            vec.push(value);
36            value = value + step;
37        }
38        vec
39    }
40}