Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate num_traits;
- extern crate num_complex;
- use num_complex::Complex64;
- fn generic_scalar_product<T>(v: &[T], w: &[T]) -> T
- where
- T: num_traits::Zero + std::ops::Mul<Output = T> + Copy
- {
- let length = v.len();
- assert_eq!(length, w.len());
- // We initialise `sum` to a "zero" of type T
- // using the `zero` method provided by the `Zero` trait
- let mut sum = T::zero();
- for index in 0..length {
- sum = sum + v[index] * w[index];
- }
- sum
- }
- fn main() {
- // Complex
- 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 }];
- 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 }];
- assert_eq!(generic_scalar_product(&v, &w), Complex64 { re: 3.0, im: 0.0 });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement