-
Notifications
You must be signed in to change notification settings - Fork 35
/
eval_cubic_splines_cython.pyx
164 lines (130 loc) · 8.25 KB
/
eval_cubic_splines_cython.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
from __future__ import division
import numpy as np
from cython import double, float
ctypedef fused floating:
float
double
A44d = np.array([
[-1.0/6.0, 3.0/6.0, -3.0/6.0, 1.0/6.0],
[ 3.0/6.0, -6.0/6.0, 0.0/6.0, 4.0/6.0],
[-3.0/6.0, 3.0/6.0, 3.0/6.0, 1.0/6.0],
[ 1.0/6.0, 0.0/6.0, 0.0/6.0, 0.0/6.0]
])
dA44d = np.zeros((4,4))
for i in range(1,4):
dA44d[:,i] = A44d[:,i-1]*(4-i)
d2A44d = np.zeros((4,4))
for i in range(1,4):
d2A44d[:,i] = dA44d[:,i-1]*(4-i)
import cython
from libc.math cimport floor
from cython.parallel import parallel, prange
from cython import nogil
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def vec_eval_cubic_spline_3( floating[:] smin, floating[:] smax, long[:] orders, floating[:,:,::1] coefs, floating[:,::1] svec, floating[::1] vals):
cdef int M0 = orders[0+1]
cdef floating start0 = smin[0]
cdef floating dinv0 = (orders[0]-1.0)/(smax[0]-smin[0])
cdef int M1 = orders[1+1]
cdef floating start1 = smin[1]
cdef floating dinv1 = (orders[1]-1.0)/(smax[1]-smin[1])
cdef int M2 = orders[2+1]
cdef floating start2 = smin[2]
cdef floating dinv2 = (orders[2]-1.0)/(smax[2]-smin[2])
if floating is double:
dtype = np.float64
else:
dtype = np.float32
cdef int N = svec.shape[0]
cdef int n
cdef int n_x = coefs.shape[0]
cdef int k
cdef floating[:,::1] Ad = np.array(A44d, dtype=dtype)
cdef floating[:,::1] dAd = np.array(dA44d, dtype=dtype)
cdef int i0, i1, i2
cdef floating x0, x1, x2
cdef floating u0, u1, u2
cdef floating t0, t1, t2
cdef floating extrap0, extrap1, extrap2
cdef floating Phi0_0, Phi0_1, Phi0_2, Phi0_3, Phi1_0, Phi1_1, Phi1_2, Phi1_3, Phi2_0, Phi2_1, Phi2_2, Phi2_3
cdef floating tp0_0, tp0_1, tp0_2, tp0_3, tp1_0, tp1_1, tp1_2, tp1_3, tp2_0, tp2_1, tp2_2, tp2_3
#cdef floating [:,:,::1] C = coefs
#cdef floating [:] vals = np.zeros(N, dtype=dtype)
cdef floating tpx_0, tpx_1, tpx_2, tpx_3
cdef floating tpy_0, tpy_1, tpy_2, tpy_3
# with nogil, parallel():
# for n in prange(N):
for n in range(N):
x0 = svec[n,0]
x1 = svec[n,1]
x2 = svec[n,2]
u0 = (x0 - start0)*dinv0
i0 = <int> u0
i0 = max( min(i0,M0-2), 0 )
t0 = u0-i0
u1 = (x1 - start1)*dinv1
i1 = <int> u1
i1 = max( min(i1,M1-2), 0 )
t1 = u1-i1
u2 = (x2 - start2)*dinv2
i2 = <int> u2
i2 = max( min(i2,M2-2), 0 )
t2 = u2-i2
#
# extrap0 = 0 if (t0 < 0 or t0 >= 1) else 1
#
# extrap1 = 0 if (t1 < 0 or t1 >= 1) else 1
#
# extrap2 = 0 if (t2 < 0 or t2 >= 1) else 1
#
tp0_0 = t0*t0*t0; tp0_1 = t0*t0; tp0_2 = t0; tp0_3 = 1.0;
tp1_0 = t1*t1*t1; tp1_1 = t1*t1; tp1_2 = t1; tp1_3 = 1.0;
tp2_0 = t2*t2*t2; tp2_1 = t2*t2; tp2_2 = t2; tp2_3 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3*(coefs[i0+1,i1+1,i2+3])) + Phi1_2*(Phi2_0*(coefs[i0+1,i1+2,i2+0]) + Phi2_1*(coefs[i0+1,i1+2,i2+1]) + Phi2_2*(coefs[i0+1,i1+2,i2+2]) + Phi2_3*(coefs[i0+1,i1+2,i2+3])) + Phi1_3*(Phi2_0*(coefs[i0+1,i1+3,i2+0]) + Phi2_1*(coefs[i0+1,i1+3,i2+1]) + Phi2_2*(coefs[i0+1,i1+3,i2+2]) + Phi2_3*(coefs[i0+1,i1+3,i2+3]))) + Phi0_2*(Phi1_0*(Phi2_0*(coefs[i0+2,i1+0,i2+0]) + Phi2_1*(coefs[i0+2,i1+0,i2+1]) + Phi2_2*(coefs[i0+2,i1+0,i2+2]) + Phi2_3*(coefs[i0+2,i1+0,i2+3])) + Phi1_1*(Phi2_0*(coefs[i0+2,i1+1,i2+0]) + Phi2_1*(coefs[i0+2,i1+1,i2+1]) + Phi2_2*(coefs[i0+2,i1+1,i2+2]) + Phi2_3*(coefs[i0+2,i1+1,i2+3])) + Phi1_2*(Phi2_0*(coefs[i0+2,i1+2,i2+0]) + Phi2_1*(coefs[i0+2,i1+2,i2+1]) + Phi2_2*(coefs[i0+2,i1+2,i2+2]) + Phi2_3*(coefs[i0+2,i1+2,i2+3])) + Phi1_3*(Phi2_0*(coefs[i0+2,i1+3,i2+0]) + Phi2_1*(coefs[i0+2,i1+3,i2+1]) + Phi2_2*(coefs[i0+2,i1+3,i2+2]) + Phi2_3*(coefs[i0+2,i1+3,i2+3]))) + Phi0_3*(Phi1_0*(Phi2_0*(coefs[i0+3,i1+0,i2+0]) + Phi2_1*(coefs[i0+3,i1+0,i2+1]) + Phi2_2*(coefs[i0+3,i1+0,i2+2]) + Phi2_3*(coefs[i0+3,i1+0,i2+3])) + Phi1_1*(Phi2_0*(coefs[i0+3,i1+1,i2+0]) + Phi2_1*(coefs[i0+3,i1+1,i2+1]) + Phi2_2*(coefs[i0+3,i1+1,i2+2]) + Phi2_3*(coefs[i0+3,i1+1,i2+3])) + Phi1_2*(Phi2_0*(coefs[i0+3,i1+2,i2+0]) + Phi2_1*(coefs[i0+3,i1+2,i2+1]) + Phi2_2*(coefs[i0+3,i1+2,i2+2]) + Phi2_3*(coefs[i0+3,i1+2,i2+3])) + Phi1_3*(Phi2_0*(coefs[i0+3,i1+3,i2+0]) + Phi2_1*(coefs[i0+3,i1+3,i2+1]) + Phi2_2*(coefs[i0+3,i1+3,i2+2]) + Phi2_3*(coefs[i0+3,i1+3,i2+3])))