use super::Array;
use std::ops::{Add, Div, Mul, Neg, Rem, Sub};
impl<T> Add for Array<T>
where
T: Clone + Add<Output = T>,
{
type Output = Array<T>;
fn add(self, other: Array<T>) -> Self::Output {
self.add_broadcast(&other).unwrap_or_else(|_| {
let result = &self.data + &other.data;
Array { data: result }
})
}
}
impl<'b, T> Add<&'b Array<T>> for &Array<T>
where
T: Clone + Add<Output = T>,
{
type Output = Array<T>;
fn add(self, other: &'b Array<T>) -> Self::Output {
self.add_broadcast(other).unwrap_or_else(|_| {
let result = &self.data + &other.data;
Array { data: result }
})
}
}
impl<T> Sub for Array<T>
where
T: Clone + Sub<Output = T>,
{
type Output = Array<T>;
fn sub(self, other: Array<T>) -> Self::Output {
self.subtract_broadcast(&other).unwrap_or_else(|_| {
let result = &self.data - &other.data;
Array { data: result }
})
}
}
impl<'b, T> Sub<&'b Array<T>> for &Array<T>
where
T: Clone + Sub<Output = T>,
{
type Output = Array<T>;
fn sub(self, other: &'b Array<T>) -> Self::Output {
self.subtract_broadcast(other).unwrap_or_else(|_| {
let result = &self.data - &other.data;
Array { data: result }
})
}
}
impl<T> Mul for Array<T>
where
T: Clone + Mul<Output = T>,
{
type Output = Array<T>;
fn mul(self, other: Array<T>) -> Self::Output {
self.multiply_broadcast(&other).unwrap_or_else(|_| {
let result = &self.data * &other.data;
Array { data: result }
})
}
}
impl<'b, T> Mul<&'b Array<T>> for &Array<T>
where
T: Clone + Mul<Output = T>,
{
type Output = Array<T>;
fn mul(self, other: &'b Array<T>) -> Self::Output {
self.multiply_broadcast(other).unwrap_or_else(|_| {
let result = &self.data * &other.data;
Array { data: result }
})
}
}
impl<T> Div for Array<T>
where
T: Clone + Div<Output = T>,
{
type Output = Array<T>;
fn div(self, other: Array<T>) -> Self::Output {
self.divide_broadcast(&other).unwrap_or_else(|_| {
let result = &self.data / &other.data;
Array { data: result }
})
}
}
impl<'b, T> Div<&'b Array<T>> for &Array<T>
where
T: Clone + Div<Output = T>,
{
type Output = Array<T>;
fn div(self, other: &'b Array<T>) -> Self::Output {
self.divide_broadcast(other).unwrap_or_else(|_| {
let result = &self.data / &other.data;
Array { data: result }
})
}
}
impl<T> Rem for Array<T>
where
T: Clone + Rem<Output = T>,
{
type Output = Array<T>;
fn rem(self, other: Array<T>) -> Self::Output {
self.broadcast_op(&other, |a, b| {
let result = &a.data % &b.data;
Array { data: result }
})
.unwrap_or_else(|_| {
let result = &self.data % &other.data;
Array { data: result }
})
}
}
impl<'b, T> Rem<&'b Array<T>> for &Array<T>
where
T: Clone + Rem<Output = T>,
{
type Output = Array<T>;
fn rem(self, other: &'b Array<T>) -> Self::Output {
self.broadcast_op(other, |a, b| {
let result = &a.data % &b.data;
Array { data: result }
})
.unwrap_or_else(|_| {
let result = &self.data % &other.data;
Array { data: result }
})
}
}
impl<T> Add<T> for Array<T>
where
T: Clone + Add<Output = T>,
{
type Output = Array<T>;
fn add(self, scalar: T) -> Self::Output {
self.add_scalar(scalar)
}
}
impl<T> Add<T> for &Array<T>
where
T: Clone + Add<Output = T>,
{
type Output = Array<T>;
fn add(self, scalar: T) -> Self::Output {
self.add_scalar(scalar)
}
}
impl<T> Sub<T> for Array<T>
where
T: Clone + Sub<Output = T>,
{
type Output = Array<T>;
fn sub(self, scalar: T) -> Self::Output {
self.subtract_scalar(scalar)
}
}
impl<T> Sub<T> for &Array<T>
where
T: Clone + Sub<Output = T>,
{
type Output = Array<T>;
fn sub(self, scalar: T) -> Self::Output {
self.subtract_scalar(scalar)
}
}
impl<T> Mul<T> for Array<T>
where
T: Clone + Mul<Output = T>,
{
type Output = Array<T>;
fn mul(self, scalar: T) -> Self::Output {
self.multiply_scalar(scalar)
}
}
impl<T> Mul<T> for &Array<T>
where
T: Clone + Mul<Output = T>,
{
type Output = Array<T>;
fn mul(self, scalar: T) -> Self::Output {
self.multiply_scalar(scalar)
}
}
impl<T> Div<T> for Array<T>
where
T: Clone + Div<Output = T>,
{
type Output = Array<T>;
fn div(self, scalar: T) -> Self::Output {
self.divide_scalar(scalar)
}
}
impl<T> Div<T> for &Array<T>
where
T: Clone + Div<Output = T>,
{
type Output = Array<T>;
fn div(self, scalar: T) -> Self::Output {
self.divide_scalar(scalar)
}
}
impl<T> Neg for Array<T>
where
T: Clone + Neg<Output = T>,
{
type Output = Array<T>;
fn neg(self) -> Self::Output {
self.map(|x| -x)
}
}
impl<T> Neg for &Array<T>
where
T: Clone + Neg<Output = T>,
{
type Output = Array<T>;
fn neg(self) -> Self::Output {
self.map(|x| -x)
}
}