-
Notifications
You must be signed in to change notification settings - Fork 74
/
summator.pyx
262 lines (213 loc) · 7.15 KB
/
summator.pyx
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
#cython: language_level=3, boundscheck=False, wraparound=False, cdivision=True
# -*- coding: utf-8 -*-
"""
This is the variogram estimater, implemented in cython.
"""
import numpy as np
cimport cython
from cython.parallel import prange
from libc.math cimport sin, cos
cimport numpy as np
DTYPE = np.double
ctypedef np.double_t DTYPE_t
def summate_unstruct(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:,:] pos
):
cdef int i, j, d, X_len, N
cdef double phase
cdef int dim
dim = pos.shape[0]
X_len = pos.shape[1]
N = cov_samples.shape[1]
cdef double[:] summed_modes = np.zeros(X_len, dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(N):
phase = 0.
for d in range(dim):
phase += cov_samples[d,j] * pos[d,i]
summed_modes[i] += z_1[j] * cos(phase) + z_2[j] * sin(phase)
return np.asarray(summed_modes)
def summate_struct(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y=None,
const double[:] z=None,
):
if y == None and z == None:
return summate_struct_1d(cov_samples, z_1, z_2, x)
elif z == None:
return summate_struct_2d(cov_samples, z_1, z_2, x, y)
else:
return summate_struct_3d(cov_samples, z_1, z_2, x, y, z)
def summate_struct_1d(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
):
cdef int i, j, X_len, N
cdef double phase
X_len = x.shape[0]
N = cov_samples.shape[1]
cdef double[:] summed_modes = np.zeros(X_len, dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(N):
phase = cov_samples[0,j] * x[i]
summed_modes[i] += z_1[j] * cos(phase) + z_2[j] * sin(phase)
return np.asarray(summed_modes)
def summate_struct_2d(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y,
):
cdef int i, j, k, X_len, Y_len, N
cdef double phase
X_len = x.shape[0]
Y_len = y.shape[0]
N = cov_samples.shape[1]
cdef double[:,:] summed_modes = np.zeros((X_len, Y_len), dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(Y_len):
for k in range(N):
phase = cov_samples[0,k] * x[i] + cov_samples[1,k] * y[j]
summed_modes[i,j] += z_1[k] * cos(phase) + z_2[k] * sin(phase)
return np.asarray(summed_modes)
def summate_struct_3d(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y,
const double[:] z,
):
cdef int i, j, k, l, X_len, Y_len, Z_len, N
cdef double phase
X_len = x.shape[0]
Y_len = y.shape[0]
Z_len = z.shape[0]
N = cov_samples.shape[1]
cdef double[:,:,:] summed_modes = np.zeros((X_len, Y_len, Z_len), dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(Y_len):
for k in range(Z_len):
for l in range(N):
phase = (
cov_samples[0,l] * x[i] +
cov_samples[1,l] * y[j] +
cov_samples[2,l] * z[k]
)
summed_modes[i,j,k] += z_1[l] * cos(phase) + z_2[l] * sin(phase)
return np.asarray(summed_modes)
cdef (double) abs_square(const double[:] vec) nogil:
cdef int i
cdef double r = 0.
for i in range(vec.shape[0]):
r += vec[i]**2
return r
def summate_incompr_unstruct(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:,:] pos
):
cdef int i, j, d, X_len, N
cdef double phase
cdef int dim
cdef double k_2
dim = pos.shape[0]
cdef double[:] e1 = np.zeros(dim, dtype=DTYPE)
e1[0] = 1.
cdef double[:] proj = np.empty(dim, dtype=DTYPE)
X_len = pos.shape[1]
N = cov_samples.shape[1]
cdef double[:,:] summed_modes = np.zeros((dim, X_len), dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(N):
k_2 = abs_square(cov_samples[:,j])
phase = 0.
for d in range(dim):
phase += cov_samples[d,j] * pos[d,i]
for d in range(dim):
proj[d] = e1[d] - cov_samples[d,j] * cov_samples[0,j] / k_2
summed_modes[d,i] += proj[d] * (z_1[j] * cos(phase) + z_2[j] * sin(phase))
return np.asarray(summed_modes)
def summate_incompr_struct(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y=None,
const double[:] z=None,
):
if z == None:
return summate_incompr_struct_2d(cov_samples, z_1, z_2, x, y)
else:
return summate_incompr_struct_3d(cov_samples, z_1, z_2, x, y, z)
def summate_incompr_struct_2d(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y,
):
cdef int i, j, k, d, X_len, Y_len, N
cdef double phase
cdef int dim = 2
cdef double k_2
cdef double[:] e1 = np.zeros(dim, dtype=DTYPE)
e1[0] = 1.
cdef double[:] proj = np.empty(dim, dtype=DTYPE)
X_len = x.shape[0]
Y_len = y.shape[0]
N = cov_samples.shape[1]
cdef double[:,:,:] summed_modes = np.zeros((dim, X_len, Y_len), dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(Y_len):
for k in range(N):
k_2 = abs_square(cov_samples[:,k])
phase = cov_samples[0,k] * x[i] + cov_samples[1,k] * y[j]
for d in range(dim):
proj[d] = e1[d] - cov_samples[d,k] * cov_samples[0,k] / k_2
summed_modes[d,i,j] += proj[d] * (z_1[k] * cos(phase) + z_2[k] * sin(phase))
return np.asarray(summed_modes)
def summate_incompr_struct_3d(
const double[:,:] cov_samples,
const double[:] z_1,
const double[:] z_2,
const double[:] x,
const double[:] y,
const double[:] z,
):
cdef int i, j, k, l, d, X_len, Y_len, Z_len, N
cdef double phase
cdef int dim = 3
cdef double k_2
cdef double[:] e1 = np.zeros(dim, dtype=DTYPE)
e1[0] = 1.
cdef double[:] proj = np.empty(dim, dtype=DTYPE)
X_len = x.shape[0]
Y_len = y.shape[0]
Z_len = z.shape[0]
N = cov_samples.shape[1]
cdef double[:,:,:,:] summed_modes = np.zeros((dim, X_len, Y_len, Z_len), dtype=DTYPE)
for i in prange(X_len, nogil=True):
for j in range(Y_len):
for k in range(Z_len):
for l in range(N):
k_2 = abs_square(cov_samples[:,l])
phase = (
cov_samples[0,l] * x[i] +
cov_samples[1,l] * y[j] +
cov_samples[2,l] * z[k]
)
for d in range(dim):
proj[d] = e1[d] - cov_samples[d,l] * cov_samples[0,l] / k_2
summed_modes[d,i,j,k] += proj[d] * (z_1[l] * cos(phase) + z_2[l] * sin(phase))
return np.asarray(summed_modes)