Advertisement
Guest User

Untitled

a guest
May 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. use std::ops::{Add, Mul};
  2.  
  3. fn dot<N>(v1: &[N], v2: &[N]) -> N
  4. where
  5. N: Add<Output = N> + Mul<Output = N> + Default + Copy,
  6. {
  7. let mut total = N::default();
  8. for i in 0..v1.len() {
  9. total = total + v1[i] * v2[i];
  10. }
  11. total
  12. }
  13.  
  14. #[test]
  15. fn test_dot() {
  16. assert_eq!(dot(&[1, 2, 3, 4], &[1, 1, 1, 1]), 10);
  17. assert_eq!(dot(&[53.0, 7.0], &[1.0, 5.0]), 88.0);
  18. }
  19.  
  20. fn main() {
  21. let a = 5;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement