Debian Codegraph @

extern/bsplines.pxi

raw
1 cdef extern from "bs.h":
2 ctypedef enum bs_errorcode:
3 BS_OK = 0
4 BS_OUTOFMEMORY = 1
5 BS_DOMAINERROR = 2
6 BS_NOTMONOTONIC = 3
7 BS_SIZEMISMATCH = 4
8 BS_BCSIZEMISMATCH = 5
9 BS_TOOFEWPOINTS = 6
10
11 ctypedef struct bs_array:
12 double *data
13 int size
14 int stride
15
16 ctypedef struct bs_array2d:
17 double *data
18 int sizes[2]
19 int strides[2]
20
21 ctypedef struct bs_range:
22 double min
23 double max
24
25 ctypedef enum bs_bctype:
26 BS_DERIV1
27 BS_DERIV2
28 BS_NOTAKNOT
29
30 ctypedef struct bs_bc:
31 bs_bctype type
32 double value
33
34 ctypedef struct bs_bcs:
35 bs_bc left
36 bs_bc right
37
38 ctypedef enum bs_exttype:
39 BS_EXTRAPOLATE
40 BS_CONSTANT
41 BS_VALUE
42 BS_RAISE
43
44 ctypedef struct bs_ext:
45 bs_exttype type
46 double value
47
48 ctypedef struct bs_exts:
49 bs_ext left
50 bs_ext right
51
52 ctypedef struct bs_spline1d:
53 double *knots
54 double *coeffs
55 int n
56
57 ctypedef struct bs_bcarray:
58 bs_bctype type
59 double *data
60 int size
61 int stride
62
63 ctypedef struct bs_bcarray_pair:
64 bs_bcarray left
65 bs_bcarray right
66
67 bs_errorcode bs_spline1d_create(bs_array x, bs_array y, bs_bcs bcs, bs_exts exts, bs_spline1d **out)
68 bs_errorcode bs_spline1d_eval(bs_spline1d *spline, bs_array x, bs_array out)
69 void bs_spline1d_free(bs_spline1d *spline)
70
71 ctypedef struct bs_spline2d:
72 double *xknots
73 double *xconsts
74 double *yknots
75 double *yconsts
76 double *coeffs
77 int nx
78 int ny
79 bs_exts xexts
80 bs_exts yexts
81
82 bs_errorcode bs_spline2d_create(bs_array x, bs_array y, bs_array2d z,
83 bs_bcarray_pair xbcs, bs_bcarray_pair ybcs,
84 bs_exts xexts, bs_exts yexts,
85 bs_spline2d **out)
86 bs_errorcode bs_spline2d_eval(bs_spline2d *spline, bs_array x, bs_array y, bs_array2d out)
87 void bs_spline2d_free(bs_spline2d *spline)
88
89
90 ctypedef struct bs_uspline1d:
91 bs_range x
92 double didx
93 double *coeffs
94 int n
95 bs_exts exts
96
97 bs_errorcode bs_uspline1d_create(bs_range x, bs_array y,
98 bs_bcs bcs, bs_exts exts,
99 bs_uspline1d **out)
100 bs_errorcode bs_uspline1d_eval(bs_uspline1d *spline, bs_array x,
101 bs_array out)
102 void bs_uspline1d_free(bs_uspline1d *spline)
103
104
105 #------------------------------------------------------------------------------
106 # helpers
107
108 class DomainError(Exception):
109 """Raised when spline input(s) are outside spline boundaries."""
110 pass
111
112 cdef int assert_ok(bs_errorcode code) except -1:
113 """raise an exception if the error code is not OK"""
114 if code == BS_OK:
115 return 0
116 elif code == BS_OUTOFMEMORY:
117 raise MemoryError()
118 elif code == BS_DOMAINERROR:
119 raise DomainError()
120 elif code == BS_NOTMONOTONIC:
121 raise ValueError("input array(s) not monotonically increasing")
122 elif code == BS_SIZEMISMATCH:
123 raise ValueError("input array size mismatch")
124 elif code == BS_BCSIZEMISMATCH:
125 raise ValueError("boundary condition size mismatch")
126 elif code == BS_TOOFEWPOINTS:
127 raise ValueError("Too few points in input array. required: 2 + (1 for each not-a-knot condition)")
128 else:
129 raise Exception("unrecognized error")
130
131
132 cdef inline bs_array to_bs_array(double[:] x):
133 return bs_array(&x[0], x.shape[0], x.strides[0]//sizeof(double))
134
135
136 cdef inline bs_array2d to_bs_array2d(double[:, :] x):
137 cdef bs_array2d out
138 out.data = &x[0, 0]
139 out.sizes[0] = x.shape[0]
140 out.sizes[1] = x.shape[1]
141 out.strides[0] = x.strides[0] // sizeof(double)
142 out.strides[1] = x.strides[1] // sizeof(double)
143 return out