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
/*
   Appellation: binary <activate>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use nd::{Array, ArrayBase, Data, Dimension};
use num::{One, Zero};

///
pub fn heavyside<T>(x: &T) -> T
where
    T: One + PartialOrd + Zero,
{
    if x > &T::zero() {
        T::one()
    } else {
        T::zero()
    }
}

build_unary_trait!(Heavyside.heavyside,);

macro_rules! impl_heavyside {
    ($($ty:ty),* $(,)*) => {
        $(impl_heavyside!(@impl $ty);)*
    };
    (@impl $ty:ty) => {
        impl Heavyside for $ty {
            type Output = $ty;

            fn heavyside(&self) -> Self::Output {
                heavyside(self)
            }
        }
    };
}

impl_heavyside!(f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,);

impl<A, S, D> Heavyside for ArrayBase<S, D>
where
    A: Heavyside,
    D: Dimension,
    S: Data<Elem = A>,
{
    type Output = Array<<A as Heavyside>::Output, D>;

    fn heavyside(&self) -> Self::Output {
        self.map(Heavyside::heavyside)
    }
}