use crate::algebra::{
abstr::{Field, Scalar},
linear::matrix::General,
};
use std::ops::Add;
impl<T> Add<General<T>> for General<T>
where
T: Field + Scalar,
{
type Output = General<T>;
fn add(self, rhs: Self) -> Self::Output {
debug_assert_eq!(self.dim(), rhs.dim());
let (m, n): (usize, usize) = rhs.dim();
let mut c: General<T> = rhs;
T::xaxpy(
(m * n) as i32,
T::one(),
&self.data[..],
1,
&mut c.data[..],
1,
);
return c;
}
}
impl<'a, 'b, T> Add<&'b General<T>> for &'a General<T>
where
T: Field + Scalar,
{
type Output = General<T>;
fn add(self, rhs: &'b General<T>) -> Self::Output {
debug_assert_eq!(self.dim(), rhs.dim());
let (m, n): (usize, usize) = rhs.dim();
let mut c: General<T> = rhs.clone();
T::xaxpy(
(m * n) as i32,
T::one(),
&self.data[..],
1,
&mut c.data[..],
1,
);
return c;
}
}
impl<'a, 'b, T> Add<&'b General<T>> for &'a mut General<T>
where
T: Field + Scalar,
{
type Output = &'a mut General<T>;
fn add(self, rhs: &'b General<T>) -> Self::Output {
debug_assert_eq!(self.dim(), rhs.dim());
let (m, n): (usize, usize) = rhs.dim();
T::xaxpy(
(m * n) as i32,
T::one(),
&rhs.data[..],
1,
&mut self.data[..],
1,
);
return self;
}
}
impl<T> Add<T> for General<T>
where
T: Field + Scalar,
{
type Output = General<T>;
fn add(mut self, rhs: T) -> Self::Output {
let _ = &mut self + &rhs;
self
}
}
impl<'a, 'b, T> Add<&'b T> for &'a General<T>
where
T: Field + Scalar,
{
type Output = General<T>;
fn add(self, rhs: &T) -> Self::Output {
let mut a: General<T> = self.clone();
let _ = &mut a + rhs;
a
}
}
impl<'a, 'b, T> Add<&'b T> for &'a mut General<T>
where
T: Field + Scalar,
{
type Output = &'a mut General<T>;
fn add(self, rhs: &T) -> Self::Output {
self.data.iter_mut().for_each(|x: &mut T| *x += *rhs);
self
}
}