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
268
269
270
271
272
273
274
275
276
277
278
279
//! Collection of macros which implement the base traits
//! on the base enums `BaseR2r`, `BaseR2c`, `BaseC2c`
#![macro_use]
/// Implement transform trait across Base enums
macro_rules! impl_funspace_elemental_for_base {
($base: ident, $a: ty, $b: ty, $($var:ident),*) => {
impl<A: FloatNum> BaseSize for $base<A> {
/// Size in physical space
fn len_phys(&self) -> usize {
match self {
$(Self::$var(ref b) => b.len_phys(),)*
}
}
/// Size in spectral space
fn len_spec(&self) -> usize {
match self {
$(Self::$var(ref b) => b.len_spec(),)*
}
}
/// Size of orthogonal space
fn len_orth(&self) -> usize {
match self {
$(Self::$var(ref b) => b.len_orth(),)*
}
}
}
impl<A: FloatNum> BaseElements for $base<A> {
/// Real valued scalar type
type RealNum = A;
/// Return kind of base
fn base_kind(&self) -> BaseKind{
match self {
$(Self::$var(ref b) => b.base_kind(),)*
}
}
/// Return kind of transform
fn transform_kind(&self) -> TransformKind {
match self {
$(Self::$var(ref b) => b.transform_kind(),)*
}
}
/// Grid coordinates
fn coords(&self) -> Vec<Self::RealNum> {
match self {
$(Self::$var(ref b) => b.coords(),)*
}
}
}
impl<A: FloatNum> BaseMatOpDiffmat for $base<A> {
//// Scalar type of matrix
type NumType = $b;
/// Explicit differential operator $ D $
fn diffmat(&self, deriv: usize) -> Array2<Self::NumType> {
match self {
$(Self::$var(ref b) => b.diffmat(deriv),)*
}
}
/// Explicit inverse of differential operator $ D^* $
fn diffmat_pinv(&self, deriv: usize) -> (Array2<Self::NumType>, Array2<Self::NumType>) {
match self {
$(Self::$var(ref b) => b.diffmat_pinv(deriv),)*
}
}
}
impl<A: FloatNum> BaseMatOpStencil for $base<A> {
//// Scalar type of matrix
type NumType = A;
/// Transformation stencil composite -> orthogonal space
fn stencil(&self) -> Array2<Self::NumType> {
match self {
$(Self::$var(ref b) => b.stencil(),)*
}
}
/// Inverse of transformation stencil
fn stencil_inv(&self) -> Array2<Self::NumType> {
match self {
$(Self::$var(ref b) => b.stencil_inv(),)*
}
}
}
impl<A: FloatNum> BaseMatOpLaplacian for $base<A> {
/// Scalar type of laplacian matrix
type NumType = A;
/// Laplacian $ L $
fn laplacian(&self) -> Array2<Self::NumType> {
match self {
$(Self::$var(ref b) => b.laplacian(),)*
}
}
/// Pseudoinverse matrix of Laplacian $ L^{-1} $
///
/// Returns pseudoinverse and pseudoidentity,i.e
/// ``(D_pinv, I_pinv)``
///
/// ```text
/// D_pinv @ D = I_pinv
/// ``
fn laplacian_pinv(&self) -> (Array2<Self::NumType>, Array2<Self::NumType>) {
match self {
$(Self::$var(ref b) => b.laplacian_pinv(),)*
}
}
}
impl<A, T> BaseFromOrtho<T> for $base<A>
where
A: FloatNum,
T: ScalarNum
+ Add<$b, Output = T>
+ Mul<$b, Output = T>
+ Div<$b, Output = T>
+ Sub<$b, Output = T>,
{
fn to_ortho_slice(&self, indata: &[T], outdata: &mut [T])
{
match self {
$(Self::$var(ref b) => b.to_ortho_slice(indata, outdata),)*
}
}
fn from_ortho_slice(&self, indata: &[T], outdata: &mut [T])
{
match self {
$(Self::$var(ref b) => b.from_ortho_slice(indata, outdata),)*
}
}
}
impl<A, T> BaseGradient<T> for $base<A>
where
A: FloatNum,
T: ScalarNum
+ Add<$b, Output = T>
+ Mul<$b, Output = T>
+ Div<$b, Output = T>
+ Sub<$b, Output = T>,
{
fn gradient_slice(&self, indata: &[T], outdata: &mut [T], n_times: usize)
{
match self {
$(Self::$var(ref b) => b.gradient_slice(indata, outdata, n_times),)*
}
}
}
impl<A: FloatNum + ScalarNum> BaseTransform for $base<A> {
type Physical = $a;
type Spectral = $b;
fn forward_slice(&self, indata: &[Self::Physical], outdata: &mut [Self::Spectral])
{
match self {
$(Self::$var(ref b) => b.forward_slice(indata, outdata),)*
}
}
fn backward_slice(&self, indata: &[Self::Spectral], outdata: &mut [Self::Physical])
{
match self {
$(Self::$var(ref b) => b.backward_slice(indata, outdata),)*
}
}
}
};
}
/// Apply a method that takes a slice on multidimensional arrays
macro_rules! apply_along_axis {
(
$(#[$meta:meta])* $i: ident, $t1: ty, $t2: ty, $f: ident, $l1: ident, $l2: ident, $e:expr
) => {
$(#[$meta])*
fn $i<S1, S2, D>(
&self,
indata: &ArrayBase<S1, D>,
outdata: &mut ArrayBase<S2, D>,
axis: usize,
) where
S1: Data<Elem = $t1>,
S2: Data<Elem = $t2> + DataMut,
D: Dimension,
$t1: Clone,
$t2: Clone + Zero + Copy,
{
assert!(indata.is_standard_layout());
assert!(outdata.is_standard_layout());
check_array_axis(indata, self.$l1(), axis, $e);
check_array_axis(outdata, self.$l2(), axis, $e);
let outer_axis = indata.ndim() - 1;
if axis == outer_axis {
// Data is contiguous in memory
Zip::from(indata.lanes(Axis(axis)))
.and(outdata.lanes_mut(Axis(axis)))
.for_each(|x, mut y| {
self.$f(x.as_slice().unwrap(), y.as_slice_mut().unwrap());
});
} else {
// Data is *not* contiguous in memory.
let mut scratch: Vec<$t2> = vec![<$t2>::zero(); outdata.shape()[axis]];
Zip::from(indata.lanes(Axis(axis)))
.and(outdata.lanes_mut(Axis(axis)))
.for_each(|x, mut y| {
self.$f(&x.to_vec(), &mut scratch);
for (yi, si) in y.iter_mut().zip(scratch.iter()) {
*yi = *si;
}
});
}
}
};
}
/// Apply a method that takes a slice on multidimensional arrays.
/// Uses parallel iterators
macro_rules! par_apply_along_axis {
(
$(#[$meta:meta])* $i: ident, $t1: ty, $t2: ty, $f: ident, $l1: ident, $l2: ident, $e:expr
) => {
$(#[$meta])*
fn $i<S1, S2, D>(
&self,
indata: &ArrayBase<S1, D>,
outdata: &mut ArrayBase<S2, D>,
axis: usize,
) where
S1: Data<Elem = $t1>,
S2: Data<Elem = $t2> + DataMut,
D: Dimension,
$t1: Clone + Send + Sync,
$t2: Clone + Zero + Copy + Send + Sync,
Self: Sync,
{
assert!(indata.is_standard_layout());
assert!(outdata.is_standard_layout());
check_array_axis(indata, self.$l1(), axis, $e);
check_array_axis(outdata, self.$l2(), axis, $e);
let outer_axis = indata.ndim() - 1;
if axis == outer_axis {
// Data is contiguous in memory
Zip::from(indata.lanes(Axis(axis)))
.and(outdata.lanes_mut(Axis(axis)))
.par_for_each(|x, mut y| {
self.$f(x.as_slice().unwrap(), y.as_slice_mut().unwrap());
});
} else {
// Data is *not* contiguous in memory.
let scratch_len = outdata.shape()[axis];
Zip::from(indata.lanes(Axis(axis)))
.and(outdata.lanes_mut(Axis(axis)))
.par_for_each(|x, mut y| {
let mut scratch: Vec<$t2> = vec![<$t2>::zero(); scratch_len];
self.$f(&x.to_vec(), &mut scratch);
for (yi, si) in y.iter_mut().zip(scratch.iter()) {
*yi = *si;
}
});
}
}
};
}