1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use num_traits::float::FloatCore;
pub trait NormL1 {
type Output;
fn norm_l1(self) -> Self::Output;
}
pub trait NormL2 {
type Output;
fn norm_l2(self) -> Self::Output;
}
pub trait NormLInf {
type Output;
fn norm_l_inf(self) -> Self::Output;
}
pub trait Dot<V = Self> {
type Output;
fn dot(self, other: V) -> Self::Output;
}
pub trait Outer<V> {
type Output;
fn outer(self, other: V) -> Self::Output;
}
macro_rules! derive_primitive_base {
($T:ident) => {
impl Dot for $T {
type Output = Self;
fn dot(self, other: Self) -> Self {
self * other
}
}
};
}
macro_rules! derive_primitive_unsigned {
($T:ident) => {
derive_primitive_base!($T);
impl NormL1 for $T {
type Output = Self;
fn norm_l1(self) -> Self {
self
}
}
impl NormL2 for $T {
type Output = Self;
fn norm_l2(self) -> Self {
self
}
}
impl NormLInf for $T {
type Output = Self;
fn norm_l_inf(self) -> Self {
self
}
}
};
}
macro_rules! derive_primitive_signed {
($T:ident) => {
derive_primitive_base!($T);
impl NormL1 for $T {
type Output = Self;
fn norm_l1(self) -> Self {
self.abs()
}
}
impl NormL2 for $T {
type Output = Self;
fn norm_l2(self) -> Self {
self.abs()
}
}
impl NormLInf for $T {
type Output = Self;
fn norm_l_inf(self) -> Self {
self.abs()
}
}
};
}
macro_rules! derive_primitive_float {
($T:ident) => {
derive_primitive_base!($T);
impl NormL1 for $T {
type Output = Self;
fn norm_l1(self) -> Self {
<$T as FloatCore>::abs(self)
}
}
impl NormL2 for $T {
type Output = Self;
fn norm_l2(self) -> Self {
<$T as FloatCore>::abs(self)
}
}
impl NormLInf for $T {
type Output = Self;
fn norm_l_inf(self) -> Self {
<$T as FloatCore>::abs(self)
}
}
};
}
derive_primitive_unsigned!(u8);
derive_primitive_unsigned!(u16);
derive_primitive_unsigned!(u32);
derive_primitive_unsigned!(u64);
derive_primitive_unsigned!(usize);
derive_primitive_signed!(i8);
derive_primitive_signed!(i16);
derive_primitive_signed!(i32);
derive_primitive_signed!(i64);
derive_primitive_signed!(isize);
derive_primitive_float!(f32);
derive_primitive_float!(f64);