sac_base/operations/math/
multiply.rs1use crate::network::node::Node;
2use core::ops;
3use alloc::boxed::Box;
4use core::any::Any;
5use crate::helper::iter::iter_n_inputs;
6
7pub struct Multiply {}
9
10impl Multiply {
11
12 pub fn new<T>() -> Node<T>
26 where T: Copy + ops::Div<Output=T> + ops::Mul<Output=T> + Default
27 {
28 let storage = Box::new(0) as Box<dyn Any>;
29 Node::new(storage, |data_input, _input, output| {
30 iter_n_inputs(data_input, output, |left: T, right: T| {
31 return left * right;
32 })
33 })
34 }
35}
36
37#[cfg(test)]
38mod tests {
39
40 use super::*;
41 use alloc::vec::Vec;
42
43 #[test]
44 fn test_mul() {
45
46 let mut inputs = Vec::new();
47 inputs.push(&[1.0][..]);
48 inputs.push(&[2.0][..]);
49
50 let mut mul = Multiply::new();
51 mul.process((inputs, 1));
52
53 assert_eq!(mul.poll(), &[2.0][..]);
54 }
55
56 #[test]
57 fn test_multiple() {
58
59 let mut inputs = Vec::new();
60 inputs.push(&[1.0][..]);
61 inputs.push(&[2.0][..]);
62 inputs.push(&[2.0][..]);
63
64 let mut mul = Multiply::new();
65 mul.process((inputs, 1));
66
67 assert_eq!(mul.poll(), &[4.0][..]);
68 }
69
70 #[test]
71 fn test_long_slice() {
72
73 let mut inputs = Vec::new();
74 inputs.push(&[1.0, 5.0, 3.0][..]);
75 inputs.push(&[2.0, 2.0][..]);
76 inputs.push(&[2.0, 1.0][..]);
77
78 let mut mul = Multiply::new();
79 mul.process((inputs, 3));
80
81 assert_eq!(mul.poll(), &[4.0, 10.0, 0.0][..]);
82 }
83
84 #[test]
85 fn test_multiple_runs() {
86
87 let mut inputs = Vec::new();
88 inputs.push(&[1.0][..]);
89 inputs.push(&[1.0][..]);
90
91 let mut mul = Multiply::new();
92 mul.process((inputs, 1));
93
94
95 let mut inputs = Vec::new();
96 inputs.push(&[2.0][..]);
97 inputs.push(&[4.0][..]);
98 mul.process((inputs, 1));
99
100 assert_eq!(mul.poll(), &[8.0][..]);
101 }
102}
103