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
51
52
53
54
55
use crate::core::math::{ArgminAdd, ArgminMul, ArgminScaledAdd};
impl<T, U, W> ArgminScaledAdd<T, U, W> for W
where
U: ArgminMul<T, T>,
W: ArgminAdd<T, W>,
{
#[inline]
fn scaled_add(&self, factor: &U, vec: &T) -> W {
self.add(&factor.mul(vec))
}
}
#[cfg(test)]
mod tests {
use super::*;
use paste::item;
macro_rules! make_test {
($t:ty) => {
item! {
#[test]
fn [<test_scaledadd_ $t>]() {
let a = 2 as $t;
let b = 4 as $t;
let c = 10 as $t;
let res = <$t as ArgminScaledAdd<$t, $t, $t>>::scaled_add(&a, &b, &c);
assert!(((42 as $t - res) as f64).abs() < std::f64::EPSILON);
}
}
};
}
make_test!(isize);
make_test!(usize);
make_test!(i8);
make_test!(u8);
make_test!(i16);
make_test!(u16);
make_test!(i32);
make_test!(u32);
make_test!(i64);
make_test!(u64);
make_test!(f32);
make_test!(f64);
}