concision_core/func/activate/
binary.rs

1/*
2   Appellation: binary <activate>
3   Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use nd::{Array, ArrayBase, Data, Dimension};
6use num::{One, Zero};
7
8///
9pub fn heavyside<T>(x: T) -> T
10where
11    T: One + PartialOrd + Zero,
12{
13    if x > T::zero() {
14        T::one()
15    } else {
16        T::zero()
17    }
18}
19
20unary!(Heavyside::heavyside(self),);
21
22macro_rules! impl_heavyside {
23    ($($ty:ty),* $(,)*) => {
24        $(impl_heavyside!(@impl $ty);)*
25    };
26    (@impl $ty:ty) => {
27        impl Heavyside for $ty {
28            type Output = $ty;
29
30            fn heavyside(self) -> Self::Output {
31                heavyside(self)
32            }
33        }
34    };
35}
36
37impl_heavyside!(f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,);
38
39impl<A, B, S, D> Heavyside for ArrayBase<S, D>
40where
41    A: Clone + Heavyside<Output = B>,
42    D: Dimension,
43    S: Data<Elem = A>,
44{
45    type Output = Array<B, D>;
46
47    fn heavyside(self) -> Self::Output {
48        self.mapv(Heavyside::heavyside)
49    }
50}
51
52impl<'a, A, B, S, D> Heavyside for &'a ArrayBase<S, D>
53where
54    A: Clone + Heavyside<Output = B>,
55    D: Dimension,
56    S: Data<Elem = A>,
57{
58    type Output = Array<B, D>;
59
60    fn heavyside(self) -> Self::Output {
61        self.mapv(Heavyside::heavyside)
62    }
63}