forked from pressel/pycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseSolvers.pyx
61 lines (43 loc) · 1.44 KB
/
SparseSolvers.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
#!python
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdivision=True
import numpy as np
cimport numpy as np
from cpython.mem cimport PyMem_Malloc, PyMem_Realloc, PyMem_Free
import cython
cdef class TDMA:
def __init__(self):
pass
cdef void initialize(self, Py_ssize_t n):
self.n = n
self.scratch = <double*> PyMem_Malloc(self.n * sizeof(double))
return
cdef inline void solve(self, double* x, double* a, double* b, double* c) nogil:
cdef:
Py_ssize_t i
double m
self.scratch[0] = c[0]/b[0]
x[0] = x[0]/b[0]
with nogil:
for i in xrange(1,self.n):
m = 1.0/(b[i] - a[i] * self.scratch[i-1])
self.scratch[i] = c[i] * m
x[i] = (x[i] - a[i] * x[i-1])*m
for i in xrange(self.n-1,-1,-1):
x[i] = x[i] - self.scratch[i] * x[i+1]
return
cdef void destroy(self):
PyMem_Free(self.scratch)
return
def test(self):
n = 4
cdef double [:] a = np.array([0,-1,-1,-1],dtype=np.double)
cdef double [:] b = np.array([4,4,4,4],dtype=np.double)
cdef double [:] c = np.array([-1,-1,-1,0],dtype=np.double)
cdef double [:] d = np.array([5,5,10,23],dtype=np.double)
self.initialize(4)
self.solve(&d[0],&a[0],&b[0],&c[0])
print(np.array(d))
return