use std::ops::{Mul, AddAssign};
use crate::vector::Vector;
impl<T> Mul for &Vector<T>
where
T: Copy + Mul<Output = T> + AddAssign + Default
{
type Output = T;
fn mul(self, rhs: Self) -> Self::Output {
if self.len() != rhs.len() {
panic!("Cannot find dot product of two differently sized vectors.")
}
let mut product = T::default();
for idx in 0..self.len() {
product += self.list[idx] * rhs.list[idx]
}
product
}
}
impl<T> Mul<Vector<T>> for &Vector<T>
where
T: Copy + Mul<Output = T> + AddAssign + Default
{
type Output = T;
fn mul(self, rhs: Vector<T>) -> Self::Output {
if self.len() != rhs.len() {
panic!("Cannot find dot product of two differently sized vectors.")
}
let mut product = T::default();
for idx in 0..self.len() {
product += self.list[idx] * rhs.list[idx]
}
product
}
}
impl<T> Mul<&Vector<T>> for Vector<T>
where
T: Copy + Mul<Output = T> + AddAssign + Default
{
type Output = T;
fn mul(self, rhs: &Vector<T>) -> Self::Output {
if self.len() != rhs.len() {
panic!("Cannot find dot product of two differently sized vectors.")
}
let mut product = T::default();
for idx in 0..self.len() {
product += self.list[idx] * rhs.list[idx]
}
product
}
}
impl<T> Mul for Vector<T>
where
T: Copy + Mul<Output = T> + AddAssign + Default
{
type Output = T;
fn mul(self, rhs: Self) -> Self::Output {
if self.len() != rhs.len() {
panic!("Cannot find dot product of two differently sized vectors.")
}
let mut product = T::default();
for idx in 0..self.len() {
product += self.list[idx] * rhs.list[idx]
}
product
}
}
impl<T> Mul<T> for &Vector<T>
where
T: Copy + Mul<Output = T>
{
type Output = Vector<T>;
fn mul(self, rhs: T) -> Self::Output {
let mut params = Vec::with_capacity(self.len());
for item in self.list() {
params.push(rhs * *item)
}
Vector::from(params)
}
}
impl<T> Mul<T> for Vector<T>
where
T: Copy + Mul<Output = T>
{
type Output = Vector<T>;
fn mul(self, rhs: T) -> Self::Output {
let mut params = Vec::with_capacity(self.len());
for item in self.list() {
params.push(rhs * *item)
}
Vector::from(params)
}
}