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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! defines macro
//!
//! # getter!
//! getter that return a reference
//! ## Examples
//! ```ignore
//! struct a {b: usize}
//! impl a {
//!     getter!(b, usize);
//! }
//! ```
//! ```ignore
//! struct a {b: usize}
//! impl a {
//!     getter!(const, b, usize);
//! }
//! ```
//! # getter_copy!
//! create a getter that copy the value.
//! ## Examples
//! ```ignore
//! struct a {b: usize}
//! impl a {
//!     getter_copy!(b, usize);
//! }
//! ```
//! ```ignore
//! struct a {b: usize}
//! impl a {
//!     getter_copy!(const, b, usize);
//! }
//! ```
//! # project!
//! project an methods on one of the corposant to the structure.
//! ## Example
//! ```ignore
//! struct A{}
//! struct B{ a: A}
//! impl A {
//!     pub const foo(&self, var: f64, var2: &[usize; 5]) -> Bar {
//!         ...
//!     }
//! }
//! impl B {
//!     project!(pub const, foo, a, Bar, var: f64, var2: &[usize; 5]);
//! }
//! ```
//! # project_mut!
//! same as `project!` but with `&mut self` in the signature of the method replacing `&self`
//! ## Example
//! ```ignore
//! struct A{}
//! struct B{ a: A}
//! impl A {
//!     pub foo(&mut self, var: f64, var2: &[usize; 5]) -> Bar {
//!         ...
//!     }
//! }
//! impl B {
//!     project_mut!(pub, foo, a, Bar, var: f64, var2: &[usize; 5]);
//! }
//! ```

macro_rules! getter {
    ($(#[$meta:meta])* $v:vis $(,)? $i:ident, $t:ty) => {
        getter!($(#[$meta])* $v $i() -> $t);
    };
    ($(#[$meta:meta])* $v:vis $(,)? const, $i:ident, $t:ty) => {
        getter!($(#[$meta])* $v const $i() -> $t);
    };
    ($(#[$meta:meta])* $v:vis $i:ident() -> $t:ty) => {
        $(#[$meta])*
        $v fn $i(&self) -> &$t {
            &self.$i
        }
    };
    ($(#[$meta:meta])* $v:vis const $i:ident() -> $t:ty) => {
        $(#[$meta])*
        $v const fn $i(&self) -> &$t {
            &self.$i
        }
    };
}

macro_rules! getter_copy {
    ($(#[$meta:meta])* $v:vis $(,)? $i:ident, $t:ty) => {
        getter_copy!($(#[$meta])* $v $i() -> $t);
    };
    ($(#[$meta:meta])* $v:vis $(,)? const, $i:ident, $t:ty) => {
        getter_copy!($(#[$meta])* $v const $i() -> $t);
    };
    ($(#[$meta:meta])* $v:vis $i:ident() -> $t:ty) => {
        $(#[$meta])*
        $v fn $i(&self) -> $t {
            self.$i
        }
    };
    ($(#[$meta:meta])* $v:vis const $i:ident() -> $t:ty) => {
        $(#[$meta])*
        $v const fn $i(&self) -> $t {
            self.$i
        }
    };
}

macro_rules! project {
    ($(#[$meta:meta])* $v:vis $(,)? $i:ident, $data:ident, $t:ty $(, $arg:ident : $type_arg:ty )* ) => {
        project!($(#[$meta])* $v $data.$i($( $arg : $type_arg ),*) -> $t);
    };
    ($(#[$meta:meta])* $v:vis $(,)? const, $i:ident, $data:ident, $t:ty $(, $arg:ident : $type_arg:ty )* ) => {
        project!($(#[$meta])* $v const $data.$i($( $arg : $type_arg ),*) -> $t);
    };
    ($(#[$meta:meta])* $v:vis $data:ident.$i:ident($($arg:ident : $type_arg:ty ),*) -> $t:ty) => {
        $(#[$meta])*
        $v fn $i(&self $(, $arg : $type_arg)*) -> $t {
            self.$data.$i($($arg, )*)
        }
    };
    ($(#[$meta:meta])* $v:vis const $data:ident.$i:ident($($arg:ident : $type_arg:ty ),*) -> $t:ty) => {
        $(#[$meta])*
        $v const fn $i(&self $(, $arg : $type_arg)*) -> $t {
            self.$data.$i($($arg, )*)
        }
    };
}

macro_rules! project_mut {
    ($(#[$meta:meta])* $v:vis $(,)? $i:ident, $data:ident, $t:ty $(, $arg:ident : $type_arg:ty )* ) => {
        project_mut!($(#[$meta])* $v $data.$i($( $arg: $type_arg ),*) -> $t);
    };
    ($(#[$meta:meta])* $v:vis $(,)? const, $i:ident, $data:ident, $t:ty $(, $arg:ident : $type_arg:ty )* ) => {
        project_mut!($(#[$meta])* $v const $data.$i($( $arg : $type_arg ),*) -> $t);
    };
    ($(#[$meta:meta])* $v:vis $data:ident.$i:ident($( $arg:ident : $type_arg:ty ),*) -> $t:ty) => {
        $(#[$meta])*
        $v fn $i(&mut self $(, $arg : $type_arg)*) -> $t {
            self.$data.$i($($arg, )*)
        }
    };
    ($(#[$meta:meta])* $v:vis const $data:ident.$i:ident($( $arg:ident : $type_arg:ty ),*) -> $t:ty) => {
        $(#[$meta])*
        $v const fn $i(&mut self $(, $arg : $type_arg)*) -> $t {
            self.$data.$i($($arg, )*)
        }
    };
}

#[macro_export]
/// assert if two matrices are approximately the same
// TODO example
macro_rules! assert_eq_matrix {
    ($e:expr, $e2:expr, $epsilon:expr) => {
        let e = $e;
        let e2 = $e2;
        assert!((e - e2).norm() < $epsilon, "assertion failed: norm `{} > {}`", (e - e2).norm(), $epsilon)
    };
    ($e:expr, $e2:expr, $epsilon:expr, $($arg:tt)+) => {
        let e = $e;
        let e2 = $e2;
        assert!((e - e2).norm() < $epsilon, "assertion failed: norm `{} > {}` : {}", (e - e2).norm(), $epsilon, format_args!($($arg)*))
    };
}

#[macro_export]
/// assert if two complex are approximately the same
// TODO example
macro_rules! assert_eq_complex {
    ($e:expr, $e2:expr, $epsilon:expr) => {
        {
            use nalgebra::ComplexField;
            let e = $e;
            let e2 = $e2;
            assert!((e - e2).modulus() < $epsilon, "assertion failed: `({} - {}).modulus() > {}", e, e2, $epsilon);
        }
    };
    ($e:expr, $e2:expr, $epsilon:expr, $($arg:tt)+) => {
        {
            use nalgebra::ComplexField;
            let e = $e;
            let e2 = $e2;
            assert!((e - e2).modulus() < $epsilon, "assertion failed: `({} - {}).modulus() > {} : {}", e, e2, $epsilon, format_args!($($arg)*));
        }
    };
}

#[macro_export]
/// assert if the matrix is U(2) ( unitary 2 x 2)
// TODO example
macro_rules! assert_matrix_is_unitary_2 {
    ($m:expr, $epsilon:expr) => {{
        use nalgebra::ComplexField;
        let m = $m;
        assert!(
            (m.determinant().modulus() - 1_f64).abs() < $epsilon,
            "determinant {} of {} is not of norm 1",
            m.determinant().modulus(),
            m
        );
        assert!(
            (m * m.adjoint() - nalgebra::Matrix2::identity()).norm() < $epsilon,
            "The matrix is not unitary"
        );
    }};
}

#[macro_export]
/// assert if the matrix is U(3) (unitary 3 x 3)
// TODO example
macro_rules! assert_matrix_is_unitary_3 {
    ($m:expr, $epsilon:expr) => {{
        use nalgebra::ComplexField;
        let m = $m;
        assert!(
            (m.determinant().modulus() - 1_f64).abs() < $epsilon,
            "determinant {} of {} is not of norm 1",
            m.determinant().modulus(),
            m
        );
        assert!(
            (m * m.adjoint() - nalgebra::Matrix3::identity()).norm() < $epsilon,
            "The matrix {} is not unitary",
            m
        );
    }};
}

#[macro_export]
/// assert if the matrix is SU(2) (special unitary)
// TODO examples
macro_rules! assert_matrix_is_su_2 {
    ($m:expr, $epsilon:expr) => {{
        use nalgebra::ComplexField;
        let m = $m;
        assert!(
            (m.determinant() - nalgebra::Complex::from(1_f64)).modulus() < $epsilon,
            "determinant {} of {} is not of norm 1",
            m.determinant().modulus(),
            m
        );
        assert!(
            (m * m.adjoint() - nalgebra::Matrix2::identity()).norm() < $epsilon,
            "The matrix {} is not unitary",
            m
        );
    }};
}

#[macro_export]
/// assert if the matrix is SU(3) (special unitary)
// TODO examples
macro_rules! assert_matrix_is_su_3 {
    ($m:expr, $epsilon:expr) => {{
        use nalgebra::ComplexField;
        let m = $m;
        assert!(
            (m.determinant() - nalgebra::Complex::from(1_f64)).modulus() < $epsilon,
            "determinant {} of {} is not of norm 1",
            m.determinant().modulus(),
            m
        );
        assert!(
            (m * m.adjoint() - nalgebra::Matrix3::identity()).norm() < $epsilon,
            "The matrix is not unitary {}",
            m
        );
    }};
}