tract_linalg/frame/
tanh.rs

1macro_rules! tanh_impl {
2    ($ti: ident, $func: ident, $nr: expr, $alignment_items: expr, $cond: expr) => {
3        ew_impl!($ti, $func, $nr, $alignment_items);
4        #[cfg(test)]
5        paste! {
6            mod [<test_ $func>] {
7                use super::*;
8                tanh_frame_tests!($cond, $ti, $func);
9            }
10        }
11    };
12}
13
14#[cfg(test)]
15#[macro_use]
16pub mod test {
17    use crate::frame::element_wise::*;
18    use crate::LADatum;
19    use num_traits::float::Float;
20    use num_traits::AsPrimitive;
21    use proptest::test_runner::TestCaseResult;
22
23    #[macro_export]
24    macro_rules! tanh_frame_tests {
25        ($cond:expr, $t:ty, $ker:ty) => {
26            proptest::proptest! {
27                #[test]
28                fn tanh(xs in proptest::collection::vec(-25f32..25.0, 0..100)) {
29                    if $cond {
30                        $crate::frame::tanh::test::test_tanh::<$ker, $t>(&*xs).unwrap()
31                    }
32                }
33            }
34
35            #[test]
36            fn tanh_4_magic() {
37                if $cond {
38                    $crate::frame::tanh::test::test_tanh::<$ker, $t>(&[0f32, -20.0, 20.0, 0.0])
39                        .unwrap()
40                }
41            }
42
43            #[test]
44            fn tanh_4zeros() {
45                if $cond {
46                    $crate::frame::tanh::test::test_tanh::<$ker, $t>(&[0.0; 4]).unwrap();
47                }
48            }
49
50            #[test]
51            fn tanh_20_ones() {
52                if $cond {
53                    $crate::frame::tanh::test::test_tanh::<$ker, $t>(&[1.0; 20]).unwrap();
54                }
55            }
56
57            #[test]
58            fn tanh_18_zeros() {
59                if $cond {
60                    $crate::frame::tanh::test::test_tanh::<$ker, $t>(&[0.0; 18]).unwrap();
61                }
62            }
63
64            #[test]
65            fn tanh_foo() {
66                if $cond {
67                    $crate::frame::tanh::test::test_tanh::<$ker, $t>(&[0.67503357]).unwrap();
68                }
69            }
70
71            #[test]
72            fn tanh_asymptots() {
73                use tract_data::internal::*;
74                use $crate::frame::element_wise::*;
75                if $cond {
76                    let mut input: Vec<$t> = [-100f32, 100f32]
77                        .iter()
78                        .map(|x| <f32 as num_traits::AsPrimitive<$t>>::as_(*x))
79                        .collect();
80                    let expected: Vec<$t> = [-1f32, 1f32]
81                        .iter()
82                        .map(|x| <f32 as num_traits::AsPrimitive<$t>>::as_(*x))
83                        .collect();
84                    <$ker>::ew().run(&mut input).unwrap();
85                    tensor1(&input)
86                        .close_enough(&tensor1(&expected), Approximation::Close)
87                        .unwrap();
88                }
89            }
90        };
91    }
92
93    pub fn test_tanh<K: ElementWiseKer<T>, T: LADatum + Float>(values: &[f32]) -> TestCaseResult
94    where
95        f32: AsPrimitive<T>,
96    {
97        crate::setup_test_logger();
98        let values: Vec<T> = values.iter().copied().map(|x| x.as_()).collect();
99        crate::frame::element_wise::test::test_element_wise::<K, _, _>(&values, |x| x.tanh())
100    }
101}