1use crate::structure::matrix::Matrix;
2
3pub trait Vector {
9 type Scalar;
10 fn add_vec(&self, rhs: &Self) -> Self;
11 fn sub_vec(&self, rhs: &Self) -> Self;
12 fn mul_scalar(&self, rhs: Self::Scalar) -> Self;
13}
14
15#[derive(Debug, Copy, Clone)]
27pub enum Norm {
28 L1,
29 L2,
30 Lp(f64),
31 LInf,
32 F,
33 Lpq(f64, f64),
34}
35
36pub trait Normed: Vector {
38 type UnsignedScalar;
39 fn norm(&self, kind: Norm) -> Self::UnsignedScalar;
40 fn normalize(&self, kind: Norm) -> Self
41 where
42 Self: Sized;
43}
44
45pub trait InnerProduct: Normed {
47 fn dot(&self, rhs: &Self) -> Self::Scalar;
48}
49
50pub trait LinearOp<T: Vector, S: Vector> {
52 fn apply(&self, rhs: &T) -> S;
53}
54
55pub trait VectorProduct: Vector {
57 fn cross(&self, other: &Self) -> Self;
58 fn outer(&self, other: &Self) -> Matrix;
59}
60
61pub trait MatrixProduct {
63 fn kronecker(&self, other: &Self) -> Self;
64 fn hadamard(&self, other: &Self) -> Self;
65}
66
67impl Vector for f64 {
72 type Scalar = Self;
73
74 fn add_vec(&self, rhs: &Self) -> Self {
75 self + rhs
76 }
77
78 fn sub_vec(&self, rhs: &Self) -> Self {
79 self - rhs
80 }
81
82 fn mul_scalar(&self, rhs: Self::Scalar) -> Self {
83 self * rhs
84 }
85}
86
87impl Normed for f64 {
88 type UnsignedScalar = f64;
89 fn norm(&self, _kind: Norm) -> Self::Scalar {
90 self.abs()
91 }
92
93 fn normalize(&self, _kind: Norm) -> Self
94 where
95 Self: Sized,
96 {
97 self / self.abs()
98 }
99}
100
101#[cfg(feature = "parallel")]
107pub trait ParallelVector {
108 type Scalar;
109 fn par_add_vec(&self, rhs: &Self) -> Self;
110 fn par_sub_vec(&self, rhs: &Self) -> Self;
111 fn par_mul_scalar(&self, rhs: Self::Scalar) -> Self;
112}
113
114#[cfg(feature = "parallel")]
116pub trait ParallelNormed: Vector {
117 type UnsignedScalar;
118 fn par_norm(&self, kind: Norm) -> Self::UnsignedScalar;
119}
120
121#[cfg(feature = "parallel")]
123pub trait ParallelInnerProduct: ParallelNormed {
124 fn par_dot(&self, rhs: &Self) -> Self::Scalar;
125}
126
127#[cfg(feature = "parallel")]
129pub trait ParallelMatrixProduct {
130 fn par_hadamard(&self, other: &Self) -> Self;
131}
132
133#[cfg(feature = "parallel")]
135pub trait ParallelVectorProduct: Vector {
136 fn par_cross(&self, other: &Self) -> Self;
137}