-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_05.pyx
44 lines (36 loc) · 1002 Bytes
/
euler_05.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
cimport cython
import numpy as np
cimport numpy as np
ctypedef np.float64_t DTYPE_t
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef euler(np.ndarray[DTYPE_t, ndim=1] x0, np.ndarray[DTYPE_t, ndim=1] t):
cdef int n, m, N, M
cdef np.ndarray[DTYPE_t, ndim=2] X
cdef np.ndarray[DTYPE_t, ndim=1] x, dxdt
cdef double dt, tcur, tlast
N = len(x0)
M = len(t)
X = np.zeros((M, N), float)
x = np.zeros(N, float)
dxdt = np.zeros(N, float)
# Pre-loop setup
for n in range(N):
X[0, n] = x[n] = x0[n]
tlast = t[0]
# Main loop
for m in range(1, M):
tcur = t[m]
dt = tcur - tlast
func(x, tlast, dxdt)
for n in range(N):
x[n] += dxdt[n] * dt
X[m, n] = x[n]
tlast = tcur
return X
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef func(np.ndarray[DTYPE_t, ndim=1] x, double t,
np.ndarray[DTYPE_t, ndim=1] dxdt):
dxdt[0] = x[1]
dxdt[1] = - x[0]