Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. extern crate num_traits;
  2. extern crate num_complex;
  3.  
  4. use num_complex::Complex64;
  5.  
  6. fn generic_scalar_product<T>(v: &[T], w: &[T]) -> T
  7. where
  8. T: num_traits::Zero + std::ops::Mul<Output = T> + Copy
  9. {
  10. let length = v.len();
  11. assert_eq!(length, w.len());
  12.  
  13. // We initialise `sum` to a "zero" of type T
  14. // using the `zero` method provided by the `Zero` trait
  15. let mut sum = T::zero();
  16. for index in 0..length {
  17. sum = sum + v[index] * w[index];
  18. }
  19. sum
  20. }
  21.  
  22. fn main() {
  23. // Complex
  24. let v: Vec<Complex64> = vec![Complex64 { re: 1.0, im: 0.0 }, Complex64 { re: 1.0, im: 0.0 }, Complex64 { re: 1.0, im: 0.0 }];
  25. let w: Vec<Complex64> = vec![Complex64 { re: 1.0, im: 0.0 }, Complex64 { re: 1.0, im: 0.0 }, Complex64 { re: 1.0, im: 0.0 }];
  26. assert_eq!(generic_scalar_product(&v, &w), Complex64 { re: 3.0, im: 0.0 });
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement