concision_core/activate/traits/
activate.rs

1/*
2    Appellation: rho <module>
3    Created At: 2026.01.12:09:50:26
4    Contrib: @FL03
5*/
6use concision_traits::Tanh;
7use num_traits::{Float, One, Zero};
8
9/// [`Activate`] is a higher-kinded trait that provides a mechanism to apply a function over the
10/// elements within a container or structure.
11pub trait Activate<T> {
12    type Cont<U>;
13
14    fn rho<F, U>(&self, f: F) -> Self::Cont<U>
15    where
16        F: FnMut(T) -> U;
17
18    fn heavyside(&self) -> Self::Cont<T>
19    where
20        T: PartialOrd + One + Zero,
21    {
22        self.rho(|i| if i > T::zero() { T::one() } else { T::zero() })
23    }
24    fn relu(&self) -> Self::Cont<T>
25    where
26        T: PartialOrd + Zero,
27    {
28        self.rho(|i| if i > T::zero() { i } else { T::zero() })
29    }
30
31    fn sinh(&self) -> Self::Cont<T>
32    where
33        T: Float,
34    {
35        self.rho(|i| i.sinh())
36    }
37
38    fn tanh(&self) -> Self::Cont<<T as Tanh>::Output>
39    where
40        T: Tanh,
41    {
42        self.rho(|i| i.tanh())
43    }
44}
45/*
46 ************* Implementations *************
47*/
48use ndarray::{Array, ArrayBase, Data, Dimension};
49
50impl<S, D, A> Activate<A> for ArrayBase<S, D, A>
51where
52    S: Data<Elem = A>,
53    D: Dimension,
54    A: Clone,
55{
56    type Cont<U> = Array<U, D>;
57
58    fn rho<F, U>(&self, f: F) -> Self::Cont<U>
59    where
60        F: FnMut(A) -> U,
61    {
62        self.mapv(f)
63    }
64}