-
Notifications
You must be signed in to change notification settings - Fork 0
/
numba_funcs.py
executable file
·268 lines (236 loc) · 10 KB
/
numba_funcs.py
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
from numba import jit
from numba import guvectorize
from numba import vectorize
import numpy as np
########### IN USE ##############
@vectorize('float64(float64)', nopython=True, target='parallel')
def numbap_cos(a):
return np.cos(a)
@vectorize('float64(float64)', nopython=True, target='parallel')
def numbap_sin(a):
return np.sin(a)
@vectorize(['complex128(float64,float64,float64,float64)'], nopython=True, target='parallel')
def complex_rotation(q,u,cos,sin):
return (q+1j*u)*(cos+1j*sin)
@guvectorize('complex128[:,:,:],float64[:,:,:],complex128[:,:,:],complex128[:],complex128[:,:]',
'(n,a,b),(n,b,c),(n,c,d),(n)->(a,d)',nopython=True, target='parallel')
def instrRIME_integral(m1,m2,m3,K,V):
for n in range(K.shape[0]):
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
V[a,d] += m1[n,a,b] * m2[n,b,c] * m3[n,c,d] * K[n]
############ END IN USE ############
############ NOT IN USE/EXPERIMENTS ################
@guvectorize('complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:],complex128[:,:]',
'(n,a,b),(n,b,c),(n,c,d),(n,d,e),(n,e,f),(n)->(a,f)',nopython=True, target='parallel')
def RIME_integral_2jones(m1,m2,m3,m4,m5,K,V):
for n in range(K.shape[0]):
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
for e in range(2):
for f in range(2):
V[a,f] += m1[n,a,b] * m2[n,b,c] * m3[n,c,d] * m4[n,d,e] * m5[n,e,f] * K[n]
@vectorize(['float64(float64,float64,float64,float64)'], nopython=True, target='parallel')
def complex_rotation_vec_real(q,u,cos,sin):
return cos*q + sin*u
@vectorize(['float64(float64,float64,float64,float64)'], nopython=True, target='parallel')
def complex_rotation_vec_imag(q,u,cos,sin):
return -sin*q + cos*u
@guvectorize('complex128[:,:,:],complex128[:,:,:],float64[:],float64[:],float64[:],float64[:],float64[:],complex128[:],complex128[:,:]',
'(n,a,b),(n,e,f),(n),(n),(n),(n),(n),(n)->(a,f)')
def RIME_integral_linearpol(J,Jh,cos,sin,I,Q,U,K,Vis):
Qn = complex_rotation_vec_real(Q,U,cos,sin)
Un = complex_rotation_vec_imag(Q,U,cos,sin)
C = np.array([
[I + Qn, Un],
[Un, I - Qn]]).transpose(2,0,1)
for n in range(K.shape[0]):
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
Vis[a,d] += J[n,a,b] * C[n,b,c] * Jh[n,c,d] * K[n]
@guvectorize('complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],float64[:],float64[:],complex128[:],complex128[:,:]',
'(n,a,b),(n,b,c),(n,c,d),(n),(n),(n)->(a,d)')
def RIME_integral_W(J,C,Jh,cos,sin,K,Vis):
I = C[:,0,0] + C[:,1,1]
Q = (C[:,0,0] - C[:,1,1])/2
U = C[:,1,1]
QUn = complex_rotation(Q,U,cos,sin)
Qn,Un = QUn.real,QUn.imag
C[:,0,0] = I + Qn
C[:,0,1] = Un
C[:,1,0] = Un
C[:,1,1] = I - Qn
for n in range(K.shape[0]):
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
Vis[a,d] += J[n,a,b] * C[n,b,c] * Jh[n,c,d] * K[n]
# @guvectorize('complex128[:,:,:],complex128[:,:,:],float64[:],float64[:],float64[:],float64[:],float64[:],float64[:],complex128[:],complex128[:,:]',
# '(n,a,b),(n,b,c),(n),(n),(n),(n),(n),(n),(n),(n)->(a,c)')
# def RIME_integral_c(J,Jh,I,Q,U,V,cos,sin,K,Vis):
# QUn = complex_rotation(Q,U,cos,sin)
# Qn,Un = QUn.real,QUn.imag
# C = np.array([
# [I + Qn, Un - 1j*V],
# [Un + 1j*V, I - Qn]]).transpose(2,0,1)
#
# for n in range(K.shape[0]):
# for a in range(2):
# for b in range(2):
# for c in range(2):
# for d in range(2):
# Vis[a,d] += J[n,a,b] * C[n,b,c] * Jh[n,c,d] * K[n]
@jit(nopython=True)
def M(m1,m2):
"""
Computes the matrix multiplication of two arrays of matricies m1 and m2.
m1.shape = m2.shape = (N,2,2)
For each n < N, m_out is the product of the 2x2 matricies m1[n,:,:].m2[n,:,:],
where the first index of the matrix corresponds to a row, and the second
corresponds to a column.
Made double-plus-gooder by the @jit decorator from the numba package.
"""
m_out = np.zeros_like(m1)
for n in range(m1.shape[0]):
for i in range(2):
for j in range(2):
for k in range(2):
m_out[n,i,k] += m1[n,i,j] * m2[n,j,k]
return m_out
@guvectorize('complex128[:,:],complex128[:,:],complex128[:,:]', '(a,b),(b,c)->(a,c)')
def _M(m1,m2,m_out):
for i in range(2):
for j in range(2):
for k in range(2):
m_out[i,k] += m1[i,j] * m2[j,k]
# @guvectorize('complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:]',
# '(n,a,b),(n,b,c),(n,c,d),(n,d,e),(n,e,f)->(n,a,f)', nopython=True, target='parallel')
# def jones_chain(m1,m2,m3,m4,m5,C):
# for a in range(2):
# for b in range(2):
# for c in range(2):
# for d in range(2):
# for e in range(2):
# for f in range(2):
# C[a,f] += m1[a,b] * m2[b,c] * m3[c,d] * m4[d,e] * m5[e,f]
# Need this version to loop over pixels? might have a bunch of zeros in C
@guvectorize('complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:],complex128[:,:,:]',
'(n,a,b),(n,b,c),(n,c,d),(n,d,e),(n,e,f)->(n,a,f)', nopython=True, target='parallel')
def jones_chain(m1,m2,m3,m4,m5,C):
for n in range(C.shape[0]):
for a in range(2):
for b in range(2):
for c in range(2):
for d in range(2):
for e in range(2):
for f in range(2):
C[n,a,f] += m1[n,a,b] * m2[n,b,c] * m3[n,c,d] * m4[n,d,e] * m5[n,e,f]
@jit(nopython=True)
def compose_4M(m1,m2,m3,m4,m5,C):
for nu_i in range(C.shape[0]):
C[nu_i] = M(M(M(M(m1[nu_i],m2[nu_i]),m3[nu_i]),m4[nu_i,]),m5[nu_i])
@guvectorize('complex128[:,:,:],complex128[:], complex128[:,:]', '(n, i, j),(n)->(i,j)',
nopython=True, target='parallel')
def _RIME_integral(C, K, V):
"""
C.shape = (npix, 2, 2)
K.shape = (npix,)
For each component of the 2x2 coherency tensor field C, sum the product
C(p)_ij * exp(-2 * pi * i * b.s(p) ) to produce a model visibility V(b)_ij.
"""
for pi in range(C.shape[0]):
for i in range(2):
for j in range(2):
V[i, j] += C[pi,i,j]*K[pi]
# V /= np.float(np.size(K))
##### fastest?
# Vis[0,0] +=\
# ((J[n,0,0] * Jh[n,0,0] + J[n,0,1] * Jh[n,1,0]) * I\
# + (J[n,0,0] * Jh[n,0,0] - J[n,0,1] * Jh[n,1,0]) * Q\
# + (J[n,0,1] * Jh[n,0,0] + J[n,1,1] * Jh[n,1,0]) * U) * K[n]
#
# Vis[0,1] +=\
# ((J[n,0,0] * Jh[n,0,1] + J[n,0,1] * Jh[n,1,1]) * I\
# + (J[n,0,0] * Jh[n,0,1] - J[n,0,1] * Jh[n,1,1]) * Q\
# + (J[n,0,1] * Jh[n,0,1] + J[n,0,0] * Jh[n,1,1]) * U) * K[n]
#
# Vis[1,0] +=\
# ((J[n,1,0] * Jh[n,0,0] + J[n,1,1] * Jh[n,1,0]) * I\
# + (J[n,1,0] * Jh[n,0,0] - J[n,1,1] * Jh[n,1,0]) * Q\
# + (J[n,1,1] * Jh[n,0,0] + J[n,1,0] * Jh[n,1,0]) * U) * K[n]
#
# Vis[1,1] +=\
# ((J[n,1,0] * Jh[n,0,1] + J[n,1,1] * Jh[n,1,1]) * I\
# + (J[n,1,0] * Jh[n,0,1] - J[n,1,1] * Jh[n,1,1]) * Q\
# + (J[n,1,1] * Jh[n,0,1] + J[n,1,0] * Jh[n,1,1]) * U) * K[n]
# @guvectorize('complex128[:,:,:],complex128[:,:,:],float64[:],float64[:],float64[:],complex128[:],complex128[:,:]',
# '(n,a,b),(n,a,b),(n),(n),(n),(n)->(a,b)', nopython=True, target='parallel')
# def RIME_integral_42(J,Jh,I,Q,U,K,Vis):
# for n in range(K.shape[0]):
# Vis[0,0] +=\
# ((J[n,0,0] * Jh[n,0,0] + J[n,0,1] * Jh[n,1,0]) * I\
# + (J[n,0,0] * Jh[n,0,0] - J[n,0,1] * Jh[n,1,0]) * Q\
# + (J[n,0,1] * Jh[n,0,0] + J[n,1,1] * Jh[n,1,0]) * U) * K[n]
#
# Vis[0,1] +=\
# ((J[n,0,0] * Jh[n,0,1] + J[n,0,1] * Jh[n,1,1]) * I\
# + (J[n,0,0] * Jh[n,0,1] - J[n,0,1] * Jh[n,1,1]) * Q\
# + (J[n,0,1] * Jh[n,0,1] + J[n,0,0] * Jh[n,1,1]) * U) * K[n]
#
# Vis[1,0] +=\
# ((J[n,1,0] * Jh[n,0,0] + J[n,1,1] * Jh[n,1,0]) * I\
# + (J[n,1,0] * Jh[n,0,0] - J[n,1,1] * Jh[n,1,0]) * Q\
# + (J[n,1,1] * Jh[n,0,0] + J[n,1,0] * Jh[n,1,0]) * U) * K[n]
#
# Vis[1,1] +=\
# ((J[n,1,0] * Jh[n,0,1] + J[n,1,1] * Jh[n,1,1]) * I\
# + (J[n,1,0] * Jh[n,0,1] - J[n,1,1] * Jh[n,1,1]) * Q\
# + (J[n,1,1] * Jh[n,0,1] + J[n,1,0] * Jh[n,1,1]) * U) * K[n]
####### end fastest
# This doesn't even work!
# @guvectorize('float64[:],float64[:],float64[:],float64[:],complex128[:]',
# '(n),(n),(n),(n)->(n)', nopython=True, target='parallel')
# def complex_rotation(q,u,cos,sin,qun):
# qun = (q + 1j*u) * (cos + 1j*sin)
## Old versions
# @jit(nopython=True)
# def M(m1,m2):
# """
# Computes the matrix multiplication of two arrays of matricies m1 and m2.
# m1.shape = m2.shape = (N,2,2)
# For each n < N, m_out is the product of the 2x2 matricies m1[n,:,:].m2[n,:,:],
# where the first index of the matrix corresponds to a row, and the second
# corresponds to a column.
#
# Made double-plus-gooder by the @jit decorator from the numba package.
# """
# m_out = np.zeros_like(m1)
# for n in range(len(m1[:,0,0])):
# for i in range(2):
# for j in range(2):
# for k in range(2):
# m_out[n,i,k] += m1[n,i,j] * m2[n,j,k]
# return m_out
#
# @jit(nopython=True)
# def RIME_integral(C, K, V):
# """
# C.shape = (npix, 2, 2)
# K.shape = (npix,)
#
# For each component of the 2x2 coherency tensor field C, sum the product
# C(p)_ij * exp(-2 * pi * i * b.s(p) ) to produce a model visibility V(b)_ij.
# """
# npix = np.size(K)
# for i in range(2):
# for j in range(2):
# for pi in range(npix):
# V[i, j] += C[pi,i,j]*K[pi]
# return V / np.float(npix)