use core::ops::{
Neg, Add, Sub, Mul, Div,
};
use num_traits::{Zero, One};
pub trait Conj {
fn conj(self) -> Self;
}
pub trait Dot {
type Output;
fn dot(self, other: Self) -> Self::Output;
}
pub trait NormSqr: Sized {
type Output;
fn norm_sqr(self) -> Self::Output;
fn abs_sqr(self) -> Self::Output {
self.norm_sqr()
}
}
pub trait Norm: Sized {
type Output;
fn norm(self) -> Self::Output;
fn abs(self) -> Self::Output {
self.norm()
}
}
pub trait NormL1 {
type Output;
fn norm_l1(self) -> Self::Output;
}
pub trait Algebra<T: Algebra = Self>:
Neg<Output=Self> +
Add<Output=Self> +
Sub<Output=Self> +
Mul<Output=Self> +
Div<Output=Self> +
Add<T, Output=Self> +
Sub<T, Output=Self> +
Mul<T, Output=Self> +
Div<T, Output=Self> +
Zero +
One +
Conj +
NormSqr<Output=T>
{}
macro_rules! derive_primitive { ($T:ident) => (
impl Conj for $T {
fn conj(self) -> Self {
self
}
}
impl Dot for $T {
type Output = Self;
fn dot(self, other: Self) -> Self {
self*other
}
}
impl NormSqr for $T {
type Output = Self;
fn norm_sqr(self) -> Self {
self*self
}
}
impl Norm for $T {
type Output = Self;
fn norm(self) -> Self {
self.abs()
}
}
impl NormL1 for $T {
type Output = Self;
fn norm_l1(self) -> Self {
self.abs()
}
}
impl Algebra for $T {}
) }
derive_primitive!(i8);
derive_primitive!(i16);
derive_primitive!(i32);
derive_primitive!(i64);
derive_primitive!(f32);
derive_primitive!(f64);