use crate::algebra::{
abstr::{Field, Scalar},
linear::{matrix::General, vector::Vector},
};
use std::ops::Mul;
impl<T> Mul<General<T>> for Vector<T>
where
T: Field + Scalar,
{
type Output = Vector<T>;
fn mul(self, rhs: General<T>) -> Self::Output {
&self * &rhs
}
}
impl<'a, T> Mul<&'a General<T>> for &Vector<T>
where
T: Field + Scalar,
{
type Output = Vector<T>;
fn mul(self, rhs: &'a General<T>) -> Self::Output {
let (rhs_m, rhs_n): (usize, usize) = rhs.dim();
let (_m, n): (usize, usize) = self.dim();
if n != rhs_m {
panic!("Vector and matrix dimensions do not match");
}
let mut res: Vec<T> = Vec::with_capacity(rhs_n);
for i in 0..rhs_n {
let mut sum: T = T::zero();
for k in 0..rhs_m {
sum += self[k] * rhs[[k, i]];
}
res.push(sum);
}
Vector::new_row(res)
}
}
impl<T> Mul<T> for Vector<T>
where
T: Field + Scalar,
{
type Output = Vector<T>;
fn mul(self, rhs: T) -> Self::Output {
Vector {
data: &self.data * (&rhs),
}
}
}
impl<T> Mul<&T> for &Vector<T>
where
T: Field + Scalar,
{
type Output = Vector<T>;
fn mul(self, rhs: &T) -> Self::Output {
Vector {
data: (&self.data).mul(rhs),
}
}
}
impl<'a, T> Mul<&'a T> for &mut Vector<T>
where
T: Field + Scalar,
{
type Output = Self;
fn mul(self, rhs: &'a T) -> Self::Output {
let _ = &mut self.data * rhs;
self
}
}