use crate::structure::matrix::Matrix;
pub trait Vector {
type Scalar;
fn add_vec(&self, rhs: &Self) -> Self;
fn sub_vec(&self, rhs: &Self) -> Self;
fn mul_scalar(&self, rhs: Self::Scalar) -> Self;
}
#[derive(Debug, Copy, Clone)]
pub enum Norm {
L1,
L2,
Lp(f64),
LInf,
F,
Lpq(f64, f64),
}
pub trait Normed: Vector {
type UnsignedScalar;
fn norm(&self, kind: Norm) -> Self::UnsignedScalar;
fn normalize(&self, kind: Norm) -> Self
where
Self: Sized;
}
pub trait InnerProduct: Normed {
fn dot(&self, rhs: &Self) -> Self::Scalar;
}
pub trait LinearOp<T: Vector, S: Vector> {
fn apply(&self, rhs: &T) -> S;
}
pub trait VectorProduct: Vector {
fn cross(&self, other: &Self) -> Self;
fn outer(&self, other: &Self) -> Matrix;
}
pub trait MatrixProduct {
fn kronecker(&self, other: &Self) -> Self;
fn hadamard(&self, other: &Self) -> Self;
}
impl Vector for f64 {
type Scalar = Self;
fn add_vec(&self, rhs: &Self) -> Self {
self + rhs
}
fn sub_vec(&self, rhs: &Self) -> Self {
self - rhs
}
fn mul_scalar(&self, rhs: Self::Scalar) -> Self {
self * rhs
}
}
impl Normed for f64 {
type UnsignedScalar = f64;
fn norm(&self, _kind: Norm) -> Self::Scalar {
self.abs()
}
fn normalize(&self, _kind: Norm) -> Self
where
Self: Sized,
{
self / self.abs()
}
}
#[cfg(feature = "parallel")]
pub trait ParallelVector {
type Scalar;
fn par_add_vec(&self, rhs: &Self) -> Self;
fn par_sub_vec(&self, rhs: &Self) -> Self;
fn par_mul_scalar(&self, rhs: Self::Scalar) -> Self;
}
#[cfg(feature = "parallel")]
pub trait ParallelNormed: Vector {
type UnsignedScalar;
fn par_norm(&self, kind: Norm) -> Self::UnsignedScalar;
}
#[cfg(feature = "parallel")]
pub trait ParallelInnerProduct: ParallelNormed {
fn par_dot(&self, rhs: &Self) -> Self::Scalar;
}
#[cfg(feature = "parallel")]
pub trait ParallelMatrixProduct {
fn par_hadamard(&self, other: &Self) -> Self;
}
#[cfg(feature = "parallel")]
pub trait ParallelVectorProduct: Vector {
fn par_cross(&self, other: &Self) -> Self;
}