-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_09.pyx
54 lines (42 loc) · 1.4 KB
/
euler_09.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
import numpy as np
cimport numpy as np
ctypedef np.float64_t DTYPE_t
cdef class ODES:
def func(self, np.ndarray[DTYPE_t, ndim=1] x, double t,
np.ndarray[DTYPE_t, ndim=1] dxdt):
self._func(<double*>x.data, t, <double*>dxdt.data)
return dxdt
cdef void _func(self, double* x, double t, double* dxdt):
dxdt[0] = x[1]
dxdt[1] = - x[0]
cpdef euler(self, 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
cdef np.ndarray[DTYPE_t, ndim=1] dxdt
cdef double dt, tcur, tlast, *px, *pt, *pdxdt, *pX
N = len(x0)
M = len(t)
X = np.zeros((M, N), float)
x = np.zeros(N, float)
dxdt = np.zeros(N, float)
px = <double*>x.data
pt = <double*>t.data
pdxdt = <double*>dxdt.data
pX = <double*>X.data
# Pre-loop setup
for n in range(N):
pX[0 + n] = px[n] = <double>x0[n]
tlast = t[0]
# Main loop
for m in range(1, M):
tcur = pt[m]
dt = tcur - tlast
self._func(px, tlast, pdxdt)
for n in range(N):
px[n] += pdxdt[n] * dt
pX[m*N + n] = px[n]
tlast = tcur
return X
def euler(x0, t):
return ODES().euler(x0, t)