fix C style array declaration deprecated in cython
[liba.git] / python / src / a.pyx
bloba376731a2dfa280408dea4b49dfd6d55e6bd8931
1 #cython: autotestdict=False
2 #cython: auto_pickle=False
3 #cython: boundscheck=False
4 #cython: wraparound=False
5 from a cimport *
6 from cpython cimport *
7 from cpython.array cimport array
8 from cython.parallel import prange
10 cdef inline array_i8(object o):
11 return array('b', o)
13 cdef inline array_u8(object o):
14 return array('B', o)
16 cdef inline array_i16(object o):
17 return array('h', o)
19 cdef inline array_u16(object o):
20 return array('H', o)
22 cdef inline array_i32(object o):
23 if INT32_MAX == INT_MAX:
24 return array('i', o)
25 return array('l', o)
27 cdef inline array_u32(object o):
28 if UINT32_MAX == UINT_MAX:
29 return array('I', o)
30 return array('L', o)
32 cdef inline array_i64(object o):
33 if INT64_MAX == LONG_MAX:
34 return array('l', o)
35 return array('q', o)
37 cdef inline array_u64(object o):
38 if UINT64_MAX == ULONG_MAX:
39 return array('L', o)
40 return array('Q', o)
42 cdef inline array_f32(object o):
43 return array('f', o)
45 cdef inline array_f64(object o):
46 return array('d', o)
48 cdef inline array_num(object o):
49 if A_FLOAT_TYPE == A_FLOAT_SINGLE:
50 return array('f', o)
51 return array('d', o)
53 def hash_bkdr(bytes str, a_u32 val=0) -> a_u32:
54 return a_hash_bkdr(str, val)
56 def hash_sdbm(bytes str, a_u32 val=0) -> a_u32:
57 return a_hash_sdbm(str, val)
59 from a.crc cimport *
61 cdef class crc8:
62 cdef readonly array table
63 def __cinit__(self):
64 self.table = array_u8((0,) * 0x100)
65 def gen(self, a_u8 poly, bint reversed=0):
66 if reversed:
67 a_crc8l_init(<a_u8 *>self.table.data.as_voidptr, poly)
68 else:
69 a_crc8m_init(<a_u8 *>self.table.data.as_voidptr, poly)
70 return self
71 def __init__(self, a_u8 poly, bint reversed=0):
72 self.gen(poly, reversed)
73 def __call__(self, bytes block, a_u8 value=0):
74 cdef const unsigned char *p = <const unsigned char *>block
75 return a_crc8(<a_u8 *>self.table.data.as_voidptr, p, len(block), value)
76 def pack(self, bytes block, a_u8 value=0):
77 cdef size_t n = len(block)
78 block = block + bytes(1)
79 cdef unsigned char *p = <unsigned char *>block
80 value = a_crc8(<a_u8 *>self.table.data.as_voidptr, p, n, value)
81 p[n] = value
82 return block
84 cdef class crc16:
85 cdef readonly array table
86 def __cinit__(self):
87 self.table = array_u16((0,) * 0x100)
88 cdef a_u16 (*eval)(const a_u16 *, const void *, a_size, a_u16)
89 def gen(self, a_u16 poly, bint reversed=0):
90 if reversed:
91 a_crc16l_init(<a_u16 *>self.table.data.as_voidptr, poly)
92 self.eval = a_crc16l
93 else:
94 a_crc16m_init(<a_u16 *>self.table.data.as_voidptr, poly)
95 self.eval = a_crc16m
96 return self
97 def __init__(self, a_u16 poly, bint reversed=0):
98 self.gen(poly, reversed)
99 def __call__(self, bytes block, a_u16 value=0):
100 cdef const unsigned char *p = <const unsigned char *>block
101 return self.eval(<a_u16 *>self.table.data.as_voidptr, p, len(block), value)
102 def pack(self, bytes block, a_u16 value=0):
103 cdef size_t n = len(block)
104 block = block + bytes(2)
105 cdef unsigned char *p = <unsigned char *>block
106 value = self.eval(<a_u16 *>self.table.data.as_voidptr, p, n, value)
107 if self.eval == a_crc16m:
108 a_u16_setb(p + n, value)
109 else:
110 a_u16_setl(p + n, value)
111 return block
113 cdef class crc32:
114 cdef readonly array table
115 def __cinit__(self):
116 self.table = array_u32((0,) * 0x100)
117 cdef a_u32 (*eval)(const a_u32 *, const void *, a_size, a_u32)
118 def gen(self, a_u32 poly, bint reversed=0):
119 if reversed:
120 a_crc32l_init(<a_u32 *>self.table.data.as_voidptr, poly)
121 self.eval = a_crc32l
122 else:
123 a_crc32m_init(<a_u32 *>self.table.data.as_voidptr, poly)
124 self.eval = a_crc32m
125 return self
126 def __init__(self, a_u32 poly, bint reversed=0):
127 self.gen(poly, reversed)
128 def __call__(self, bytes block, a_u32 value=0):
129 cdef const unsigned char *p = <const unsigned char *>block
130 return self.eval(<a_u32 *>self.table.data.as_voidptr, p, len(block), value)
131 def pack(self, bytes block, a_u32 value=0):
132 cdef size_t n = len(block)
133 block = block + bytes(4)
134 cdef unsigned char *p = <unsigned char *>block
135 value = self.eval(<a_u32 *>self.table.data.as_voidptr, p, n, value)
136 if self.eval == a_crc32m:
137 a_u32_setb(p + n, value)
138 else:
139 a_u32_setl(p + n, value)
140 return block
142 cdef class crc64:
143 cdef readonly array table
144 def __cinit__(self):
145 self.table = array_u64((0,) * 0x100)
146 cdef a_u64 (*eval)(const a_u64 *, const void *, a_size, a_u64)
147 def gen(self, a_u64 poly, bint reversed=0):
148 if reversed:
149 a_crc64l_init(<a_u64 *>self.table.data.as_voidptr, poly)
150 self.eval = a_crc64l
151 else:
152 a_crc64m_init(<a_u64 *>self.table.data.as_voidptr, poly)
153 self.eval = a_crc64m
154 return self
155 def __init__(self, a_u64 poly, bint reversed=0):
156 self.gen(poly, reversed)
157 def __call__(self, bytes block, a_u64 value=0):
158 cdef const unsigned char *p = <const unsigned char *>block
159 return self.eval(<a_u64 *>self.table.data.as_voidptr, p, len(block), value)
160 def pack(self, bytes block, a_u64 value=0):
161 cdef size_t n = len(block)
162 block = block + bytes(8)
163 cdef unsigned char *p = <unsigned char *>block
164 value = self.eval(<a_u64 *>self.table.data.as_voidptr, p, n, value)
165 if self.eval == a_crc64m:
166 a_u64_setb(p + n, value)
167 else:
168 a_u64_setl(p + n, value)
169 return block
171 from a.hpf cimport *
173 cdef class hpf:
174 cdef a_hpf ctx
175 def __init__(self, a_float fc, a_float ts):
176 a_hpf_init(&self.ctx, a_hpf_gen(fc, ts))
177 def gen(self, a_float fc, a_float ts):
178 a_hpf_init(&self.ctx, a_hpf_gen(fc, ts))
179 return self
180 def __call__(self, a_float x):
181 return a_hpf_iter(&self.ctx, x)
182 def zero(self):
183 a_hpf_zero(&self.ctx)
184 return self
185 property alpha:
186 def __get__(self):
187 return self.ctx.alpha
188 def __set__(self, a_float alpha):
189 self.ctx.alpha = alpha
190 property output:
191 def __get__(self):
192 return self.ctx.output
193 property input:
194 def __get__(self):
195 return self.ctx.input
197 from a.lpf cimport *
199 cdef class lpf:
200 cdef a_lpf ctx
201 def __init__(self, a_float fc, a_float ts):
202 a_lpf_init(&self.ctx, a_lpf_gen(fc, ts))
203 def gen(self, a_float fc, a_float ts):
204 a_lpf_init(&self.ctx, a_lpf_gen(fc, ts))
205 return self
206 def __call__(self, a_float x):
207 return a_lpf_iter(&self.ctx, x)
208 def zero(self):
209 a_lpf_zero(&self.ctx)
210 return self
211 property alpha:
212 def __get__(self):
213 return self.ctx.alpha
214 def __set__(self, a_float alpha):
215 self.ctx.alpha = alpha
216 property output:
217 def __get__(self):
218 return self.ctx.output
220 from a.math cimport *
222 def isqrt(x: int):
223 if x <= 1:
224 return x
225 x0 = 1 << ((x.bit_length() + 1) >> 1)
226 x1 = (x0 + x // x0) >> 1
227 while x0 > x1:
228 x0 = x1
229 x1 = (x0 + x // x0) >> 1
230 return x0
232 def sqrt_u32(x):
233 cdef array y
234 cdef a_u32 *p
235 cdef Py_ssize_t i, n
236 if PyObject_HasAttrString(x, "__contains__"):
237 n = len(x)
238 y = array_u32(x)
239 p = <a_u32 *>y.data.as_voidptr
240 for i in prange(n, nogil=True):
241 p[i] = a_u32_sqrt(p[i])
242 return array_u16(y)
243 return a_u32_sqrt(x)
245 def sqrt_u64(x):
246 cdef array y
247 cdef a_u64 *p
248 cdef Py_ssize_t i, n
249 if PyObject_HasAttrString(x, "__contains__"):
250 n = len(x)
251 y = array_u64(x)
252 p = <a_u64 *>y.data.as_voidptr
253 for i in prange(n, nogil=True):
254 p[i] = a_u64_sqrt(p[i])
255 return array_u32(y)
256 return a_u64_sqrt(x)
258 def rsqrt_f32(x):
259 cdef array y
260 cdef a_f32 *p
261 cdef Py_ssize_t i, n
262 if PyObject_HasAttrString(x, "__contains__"):
263 n = len(x)
264 y = array_f32(x)
265 p = <a_f32 *>y.data.as_voidptr
266 for i in prange(n, nogil=True):
267 p[i] = a_f32_rsqrt(p[i])
268 return y
269 return a_f32_rsqrt(x)
271 def rsqrt_f64(x):
272 cdef array y
273 cdef a_f64 *p
274 cdef Py_ssize_t i, n
275 if PyObject_HasAttrString(x, "__contains__"):
276 n = len(x)
277 y = array_f64(x)
278 p = <a_f64 *>y.data.as_voidptr
279 for i in prange(n, nogil=True):
280 p[i] = a_f64_rsqrt(p[i])
281 return y
282 return a_f64_rsqrt(x)
284 from a.mf cimport *
286 cdef class mf:
287 NUL = A_MF_NUL
288 GAUSS = A_MF_GAUSS
289 GAUSS2 = A_MF_GAUSS2
290 GBELL = A_MF_GBELL
291 SIG = A_MF_SIG
292 DSIG = A_MF_DSIG
293 PSIG = A_MF_PSIG
294 TRAP = A_MF_TRAP
295 TRI = A_MF_TRI
296 LINS = A_MF_LINS
297 LINZ = A_MF_LINZ
298 S = A_MF_S
299 Z = A_MF_Z
300 PI = A_MF_PI
301 @staticmethod
302 def __call__(unsigned int e, x, a):
303 cdef array y
304 cdef a_float *p
305 cdef Py_ssize_t i, n
306 cdef unsigned int m = e
307 cdef array a_ = array_num(a)
308 cdef a_float *a_p = <a_float *>a_.data.as_voidptr
309 if PyObject_HasAttrString(x, "__contains__"):
310 n = len(x)
311 y = array_num(x)
312 p = <a_float *>y.data.as_voidptr
313 for i in prange(n, nogil=True):
314 p[i] = a_mf(m, p[i], a_p)
315 return y
316 return a_mf(m, x, a_p)
317 @staticmethod
318 def gauss(x, a_float sigma, a_float c):
319 cdef array y
320 cdef a_float *p
321 cdef Py_ssize_t i, n
322 if PyObject_HasAttrString(x, "__contains__"):
323 n = len(x)
324 y = array_num(x)
325 p = <a_float *>y.data.as_voidptr
326 for i in prange(n, nogil=True):
327 p[i] = a_mf_gauss(p[i], sigma, c)
328 return y
329 return a_mf_gauss(x, sigma, c)
330 @staticmethod
331 def gauss2(x, a_float sigma1, a_float c1, a_float sigma2, a_float c2):
332 cdef array y
333 cdef a_float *p
334 cdef Py_ssize_t i, n
335 if PyObject_HasAttrString(x, "__contains__"):
336 n = len(x)
337 y = array_num(x)
338 p = <a_float *>y.data.as_voidptr
339 for i in prange(n, nogil=True):
340 p[i] = a_mf_gauss2(p[i], sigma1, c1, sigma2, c2)
341 return y
342 return a_mf_gauss2(x, sigma1, c1, sigma2, c2)
343 @staticmethod
344 def gbell(x, a_float a, a_float b, a_float c):
345 cdef array y
346 cdef a_float *p
347 cdef Py_ssize_t i, n
348 if PyObject_HasAttrString(x, "__contains__"):
349 n = len(x)
350 y = array_num(x)
351 p = <a_float *>y.data.as_voidptr
352 for i in prange(n, nogil=True):
353 p[i] = a_mf_gbell(p[i], a, b, c)
354 return y
355 return a_mf_gbell(x, a, b, c)
356 @staticmethod
357 def sig(x, a_float a, a_float c):
358 cdef array y
359 cdef a_float *p
360 cdef Py_ssize_t i, n
361 if PyObject_HasAttrString(x, "__contains__"):
362 n = len(x)
363 y = array_num(x)
364 p = <a_float *>y.data.as_voidptr
365 for i in prange(n, nogil=True):
366 p[i] = a_mf_sig(p[i], a, c)
367 return y
368 return a_mf_sig(x, a, c)
369 @staticmethod
370 def dsig(x, a_float a1, a_float c1, a_float a2, a_float c2):
371 cdef array y
372 cdef a_float *p
373 cdef Py_ssize_t i, n
374 if PyObject_HasAttrString(x, "__contains__"):
375 n = len(x)
376 y = array_num(x)
377 p = <a_float *>y.data.as_voidptr
378 for i in prange(n, nogil=True):
379 p[i] = a_mf_dsig(p[i], a1, c1, a2, c2)
380 return y
381 return a_mf_dsig(x, a1, c1, a2, c2)
382 @staticmethod
383 def psig(x, a_float a1, a_float c1, a_float a2, a_float c2):
384 cdef array y
385 cdef a_float *p
386 cdef Py_ssize_t i, n
387 if PyObject_HasAttrString(x, "__contains__"):
388 n = len(x)
389 y = array_num(x)
390 p = <a_float *>y.data.as_voidptr
391 for i in prange(n, nogil=True):
392 p[i] = a_mf_psig(p[i], a1, c1, a2, c2)
393 return y
394 return a_mf_psig(x, a1, c1, a2, c2)
395 @staticmethod
396 def trap(x, a_float a, a_float b, a_float c, a_float d):
397 cdef array y
398 cdef a_float *p
399 cdef Py_ssize_t i, n
400 if PyObject_HasAttrString(x, "__contains__"):
401 n = len(x)
402 y = array_num(x)
403 p = <a_float *>y.data.as_voidptr
404 for i in prange(n, nogil=True):
405 p[i] = a_mf_trap(p[i], a, b, c, d)
406 return y
407 return a_mf_trap(x, a, b, c, d)
408 @staticmethod
409 def tri(x, a_float a, a_float b, a_float c):
410 cdef array y
411 cdef a_float *p
412 cdef Py_ssize_t i, n
413 if PyObject_HasAttrString(x, "__contains__"):
414 n = len(x)
415 y = array_num(x)
416 p = <a_float *>y.data.as_voidptr
417 for i in prange(n, nogil=True):
418 p[i] = a_mf_tri(p[i], a, b, c)
419 return y
420 return a_mf_tri(x, a, b, c)
421 @staticmethod
422 def lins(x, a_float a, a_float b):
423 cdef array y
424 cdef a_float *p
425 cdef Py_ssize_t i, n
426 if PyObject_HasAttrString(x, "__contains__"):
427 n = len(x)
428 y = array_num(x)
429 p = <a_float *>y.data.as_voidptr
430 for i in prange(n, nogil=True):
431 p[i] = a_mf_lins(p[i], a, b)
432 return y
433 return a_mf_lins(x, a, b)
434 @staticmethod
435 def linz(x, a_float a, a_float b):
436 cdef array y
437 cdef a_float *p
438 cdef Py_ssize_t i, n
439 if PyObject_HasAttrString(x, "__contains__"):
440 n = len(x)
441 y = array_num(x)
442 p = <a_float *>y.data.as_voidptr
443 for i in prange(n, nogil=True):
444 p[i] = a_mf_linz(p[i], a, b)
445 return y
446 return a_mf_linz(x, a, b)
447 @staticmethod
448 def s(x, a_float a, a_float b):
449 cdef array y
450 cdef a_float *p
451 cdef Py_ssize_t i, n
452 if PyObject_HasAttrString(x, "__contains__"):
453 n = len(x)
454 y = array_num(x)
455 p = <a_float *>y.data.as_voidptr
456 for i in prange(n, nogil=True):
457 p[i] = a_mf_s(p[i], a, b)
458 return y
459 return a_mf_s(x, a, b)
460 @staticmethod
461 def z(x, a_float a, a_float b):
462 cdef array y
463 cdef a_float *p
464 cdef Py_ssize_t i, n
465 if PyObject_HasAttrString(x, "__contains__"):
466 n = len(x)
467 y = array_num(x)
468 p = <a_float *>y.data.as_voidptr
469 for i in prange(n, nogil=True):
470 p[i] = a_mf_z(p[i], a, b)
471 return y
472 return a_mf_z(x, a, b)
473 @staticmethod
474 def pi(x, a_float a, a_float b, a_float c, a_float d):
475 cdef array y
476 cdef a_float *p
477 cdef Py_ssize_t i, n
478 if PyObject_HasAttrString(x, "__contains__"):
479 n = len(x)
480 y = array_num(x)
481 p = <a_float *>y.data.as_voidptr
482 for i in prange(n, nogil=True):
483 p[i] = a_mf_pi(p[i], a, b, c, d)
484 return y
485 return a_mf_pi(x, a, b, c, d)
487 from a.pid cimport *
489 cdef class pid:
490 cdef a_pid ctx
491 def __init__(self):
492 self.ctx.kp = 1
493 self.ctx.summax = +A_FLOAT_INF
494 self.ctx.summin = -A_FLOAT_INF
495 self.ctx.outmax = +A_FLOAT_INF
496 self.ctx.outmin = -A_FLOAT_INF
497 a_pid_init(&self.ctx)
498 def kpid(self, a_float kp, a_float ki, a_float kd):
499 a_pid_kpid(&self.ctx, kp, ki, kd)
500 return self
501 def run(self, a_float set, a_float fdb):
502 return a_pid_run(&self.ctx, set, fdb)
503 def pos(self, a_float set, a_float fdb):
504 return a_pid_pos(&self.ctx, set, fdb)
505 def inc(self, a_float set, a_float fdb):
506 return a_pid_inc(&self.ctx, set, fdb)
507 def zero(self):
508 a_pid_zero(&self.ctx)
509 return self
510 property kp:
511 def __get__(self):
512 return self.ctx.kp
513 def __set__(self, a_float kp):
514 self.ctx.kp = kp
515 property ki:
516 def __get__(self):
517 return self.ctx.ki
518 def __set__(self, a_float ki):
519 self.ctx.ki = ki
520 property kd:
521 def __get__(self):
522 return self.ctx.kd
523 def __set__(self, a_float kd):
524 self.ctx.kd = kd
525 property summax:
526 def __get__(self):
527 return self.ctx.summax
528 def __set__(self, a_float summax):
529 self.ctx.summax = summax
530 property summin:
531 def __get__(self):
532 return self.ctx.summin
533 def __set__(self, a_float summin):
534 self.ctx.summin = summin
535 property sum:
536 def __get__(self):
537 return self.ctx.sum
538 property outmax:
539 def __get__(self):
540 return self.ctx.outmax
541 def __set__(self, a_float outmax):
542 self.ctx.outmax = outmax
543 property outmin:
544 def __get__(self):
545 return self.ctx.outmin
546 def __set__(self, a_float outmin):
547 self.ctx.outmin = outmin
548 property out:
549 def __get__(self):
550 return self.ctx.out
551 property fdb:
552 def __get__(self):
553 return self.ctx.fdb
554 property err:
555 def __get__(self):
556 return self.ctx.err
558 from a.pid_fuzzy cimport *
560 cdef class pid_fuzzy:
561 CAP = A_PID_FUZZY_CAP
562 CAP_ALGEBRA = A_PID_FUZZY_CAP_ALGEBRA
563 CAP_BOUNDED = A_PID_FUZZY_CAP_BOUNDED
564 CUP = A_PID_FUZZY_CUP
565 CUP_ALGEBRA = A_PID_FUZZY_CUP_ALGEBRA
566 CUP_BOUNDED = A_PID_FUZZY_CUP_BOUNDED
567 EQU = A_PID_FUZZY_EQU
568 cdef a_pid_fuzzy ctx
569 cdef readonly array me
570 cdef readonly array mec
571 cdef readonly array mkp
572 cdef readonly array mki
573 cdef readonly array mkd
574 def __init__(self):
575 self.ctx.pid.summax = +A_FLOAT_INF
576 self.ctx.pid.summin = -A_FLOAT_INF
577 self.ctx.pid.outmax = +A_FLOAT_INF
578 self.ctx.pid.outmin = -A_FLOAT_INF
579 self.ctx.kp = self.ctx.pid.kp = 1
580 self.ctx.op = a_fuzzy_equ
581 a_pid_fuzzy_init(&self.ctx)
582 def op(self, unsigned int op):
583 a_pid_fuzzy_set_op(&self.ctx, op)
584 return self
585 def rule(self, me, mec, mkp, mki, mkd):
586 self.me = array_num([_2 for _1 in me for _2 in _1])
587 self.mec = array_num([_2 for _1 in mec for _2 in _1])
588 self.mkp = array_num([_2 for _1 in mkp for _2 in _1])
589 self.mki = array_num([_2 for _1 in mki for _2 in _1])
590 self.mkd = array_num([_2 for _1 in mkd for _2 in _1])
591 a_pid_fuzzy_rule(&self.ctx, <unsigned int>len(me),
592 <a_float *>self.me.data.as_voidptr,
593 <a_float *>self.mec.data.as_voidptr,
594 <a_float *>self.mkp.data.as_voidptr,
595 <a_float *>self.mki.data.as_voidptr,
596 <a_float *>self.mkd.data.as_voidptr)
597 return self
598 def set_block(self, unsigned int num):
599 cdef void *ptr = a_pid_fuzzy_block(&self.ctx)
600 ptr = PyMem_Realloc(ptr, A_PID_FUZZY_BLOCK(num))
601 a_pid_fuzzy_set_block(&self.ctx, ptr, num)
602 return self
603 def kpid(self, a_float kp, a_float ki, a_float kd):
604 a_pid_fuzzy_kpid(&self.ctx, kp, ki, kd)
605 return self
606 def run(self, a_float set, a_float fdb):
607 return a_pid_fuzzy_run(&self.ctx, set, fdb)
608 def pos(self, a_float set, a_float fdb):
609 return a_pid_fuzzy_pos(&self.ctx, set, fdb)
610 def inc(self, a_float set, a_float fdb):
611 return a_pid_fuzzy_inc(&self.ctx, set, fdb)
612 def __dealloc__(self):
613 PyMem_Free(a_pid_fuzzy_block(&self.ctx))
614 def zero(self):
615 a_pid_fuzzy_zero(&self.ctx)
616 return self
617 property kp:
618 def __get__(self):
619 return self.ctx.kp
620 def __set__(self, a_float kp):
621 self.ctx.pid.kp = kp
622 self.ctx.kp = kp
623 property ki:
624 def __get__(self):
625 return self.ctx.ki
626 def __set__(self, a_float ki):
627 self.ctx.pid.ki = ki
628 self.ctx.ki = ki
629 property kd:
630 def __get__(self):
631 return self.ctx.kd
632 def __set__(self, a_float kd):
633 self.ctx.pid.kd = kd
634 self.ctx.kd = kd
635 property summax:
636 def __get__(self):
637 return self.ctx.pid.summax
638 def __set__(self, a_float summax):
639 self.ctx.pid.summax = summax
640 property summin:
641 def __get__(self):
642 return self.ctx.pid.summin
643 def __set__(self, a_float summin):
644 self.ctx.pid.summin = summin
645 property sum:
646 def __get__(self):
647 return self.ctx.pid.sum
648 property outmax:
649 def __get__(self):
650 return self.ctx.pid.outmax
651 def __set__(self, a_float outmax):
652 self.ctx.pid.outmax = outmax
653 property outmin:
654 def __get__(self):
655 return self.ctx.pid.outmin
656 def __set__(self, a_float outmin):
657 self.ctx.pid.outmin = outmin
658 property out:
659 def __get__(self):
660 return self.ctx.pid.out
661 property fdb:
662 def __get__(self):
663 return self.ctx.pid.fdb
664 property err:
665 def __get__(self):
666 return self.ctx.pid.err
667 property order:
668 def __get__(self):
669 return self.ctx.order
670 property block:
671 def __get__(self):
672 return self.ctx.block
673 def __set__(self, unsigned int block):
674 self.set_block(block)
676 from a.pid_neuro cimport *
678 cdef class pid_neuro:
679 cdef a_pid_neuro ctx
680 def __init__(self):
681 self.ctx.pid.summax = +A_FLOAT_INF
682 self.ctx.pid.summin = -A_FLOAT_INF
683 self.ctx.pid.outmax = +A_FLOAT_INF
684 self.ctx.pid.outmin = -A_FLOAT_INF
685 self.ctx.k = self.ctx.pid.kp = 1
686 self.ctx.wp = 0.1
687 self.ctx.wi = 0.1
688 self.ctx.wd = 0.1
689 a_pid_neuro_init(&self.ctx)
690 def kpid(self, a_float k, a_float kp, a_float ki, a_float kd):
691 a_pid_neuro_kpid(&self.ctx, k, kp, ki, kd)
692 return self
693 def wpid(self, a_float wp, a_float wi, a_float wd):
694 a_pid_neuro_wpid(&self.ctx, wp, wi, wd)
695 return self
696 def run(self, a_float set, a_float fdb):
697 return a_pid_neuro_run(&self.ctx, set, fdb)
698 def inc(self, a_float set, a_float fdb):
699 return a_pid_neuro_inc(&self.ctx, set, fdb)
700 def zero(self):
701 a_pid_neuro_zero(&self.ctx)
702 return self
703 property k:
704 def __get__(self):
705 return self.ctx.k
706 def __set__(self, a_float k):
707 self.ctx.k = k
708 property kp:
709 def __get__(self):
710 return self.ctx.pid.kp
711 def __set__(self, a_float kp):
712 self.ctx.pid.kp = kp
713 property ki:
714 def __get__(self):
715 return self.ctx.pid.ki
716 def __set__(self, a_float ki):
717 self.ctx.pid.ki = ki
718 property kd:
719 def __get__(self):
720 return self.ctx.pid.kd
721 def __set__(self, a_float kd):
722 self.ctx.pid.kd = kd
723 property wp:
724 def __get__(self):
725 return self.ctx.wp
726 def __set__(self, a_float wp):
727 self.ctx.wp = wp
728 property wi:
729 def __get__(self):
730 return self.ctx.wi
731 def __set__(self, a_float wi):
732 self.ctx.wi = wi
733 property wd:
734 def __get__(self):
735 return self.ctx.wd
736 def __set__(self, a_float wd):
737 self.ctx.wd = wd
738 property outmax:
739 def __get__(self):
740 return self.ctx.pid.outmax
741 def __set__(self, a_float outmax):
742 self.ctx.pid.outmax = outmax
743 property outmin:
744 def __get__(self):
745 return self.ctx.pid.outmin
746 def __set__(self, a_float outmin):
747 self.ctx.pid.outmin = outmin
748 property out:
749 def __get__(self):
750 return self.ctx.pid.out
751 property fdb:
752 def __get__(self):
753 return self.ctx.pid.fdb
754 property err:
755 def __get__(self):
756 return self.ctx.pid.err
757 property ec:
758 def __get__(self):
759 return self.ctx.ec
761 from a.poly cimport *
763 def poly_eval(x, *a):
764 cdef array y
765 cdef array a_
766 cdef a_float *p
767 cdef a_float *a_p
768 cdef Py_ssize_t i, n
769 cdef a_size a_n = len(a)
770 a_ = array_num(a)
771 a_p = <a_float *>a_.data.as_voidptr
772 if PyObject_HasAttrString(x, "__contains__"):
773 n = len(x)
774 y = array_num(x)
775 p = <a_float *>y.data.as_voidptr
776 for i in prange(n, nogil=True):
777 p[i] = a_poly_eval(a_p, a_n, p[i])
778 return y
779 return a_poly_eval(a_p, a_n, x)
781 def poly_evar(x, *a):
782 cdef array y
783 cdef array a_
784 cdef a_float *p
785 cdef a_float *a_p
786 cdef Py_ssize_t i, n
787 cdef a_size a_n = len(a)
788 a_ = array_num(a)
789 a_p = <a_float *>a_.data.as_voidptr
790 if PyObject_HasAttrString(x, "__contains__"):
791 n = len(x)
792 y = array_num(x)
793 p = <a_float *>y.data.as_voidptr
794 for i in prange(n, nogil=True):
795 p[i] = a_poly_evar(a_p, a_n, p[i])
796 return y
797 return a_poly_evar(a_p, a_n, x)
799 from a.tf cimport *
801 cdef class tf:
802 cdef a_tf ctx
803 def __init__(self, num, den):
804 tf.num.__set__(self, num)
805 tf.den.__set__(self, den)
806 def __call__(self, a_float x):
807 return a_tf_iter(&self.ctx, x)
808 def zero(self):
809 a_tf_zero(&self.ctx)
810 return self
811 cdef array _num
812 cdef readonly array input
813 property num:
814 def __get__(self):
815 return self._num
816 def __set__(self, num):
817 self._num = array_num(num)
818 self.input = array_num(num)
819 a_tf_set_num(&self.ctx, <unsigned int>len(num), <a_float *>self._num.data.as_voidptr, <a_float *>self.input.data.as_voidptr)
820 cdef array _den
821 cdef readonly array output
822 property den:
823 def __get__(self):
824 return self._den
825 def __set__(self, den):
826 self._den = array_num(den)
827 self.output = array_num(den)
828 a_tf_set_den(&self.ctx, <unsigned int>len(den), <a_float *>self._den.data.as_voidptr, <a_float *>self.output.data.as_voidptr)
830 from a.trajbell cimport *
832 cdef class trajbell:
833 cdef a_trajbell ctx
834 def gen(self, a_float jm, a_float am, a_float vm, a_float p0, a_float p1, a_float v0=0, a_float v1=0):
835 return a_trajbell_gen(&self.ctx, jm, am, vm, p0, p1, v0, v1)
836 def pos(self, dt):
837 cdef array x
838 cdef a_float *p
839 cdef Py_ssize_t i, n
840 if PyObject_HasAttrString(dt, "__contains__"):
841 n = len(dt)
842 x = array_num(dt)
843 p = <a_float *>x.data.as_voidptr
844 for i in prange(n, nogil=True):
845 p[i] = a_trajbell_pos(&self.ctx, p[i])
846 return x
847 return a_trajbell_pos(&self.ctx, dt)
848 def vel(self, dt):
849 cdef array x
850 cdef a_float *p
851 cdef Py_ssize_t i, n
852 if PyObject_HasAttrString(dt, "__contains__"):
853 n = len(dt)
854 x = array_num(dt)
855 p = <a_float *>x.data.as_voidptr
856 for i in prange(n, nogil=True):
857 p[i] = a_trajbell_vel(&self.ctx, p[i])
858 return x
859 return a_trajbell_vel(&self.ctx, dt)
860 def acc(self, dt):
861 cdef array x
862 cdef a_float *p
863 cdef Py_ssize_t i, n
864 if PyObject_HasAttrString(dt, "__contains__"):
865 n = len(dt)
866 x = array_num(dt)
867 p = <a_float *>x.data.as_voidptr
868 for i in prange(n, nogil=True):
869 p[i] = a_trajbell_acc(&self.ctx, p[i])
870 return x
871 return a_trajbell_acc(&self.ctx, dt)
872 def jer(self, dt):
873 cdef array x
874 cdef a_float *p
875 cdef Py_ssize_t i, n
876 if PyObject_HasAttrString(dt, "__contains__"):
877 n = len(dt)
878 x = array_num(dt)
879 p = <a_float *>x.data.as_voidptr
880 for i in prange(n, nogil=True):
881 p[i] = a_trajbell_jer(&self.ctx, p[i])
882 return x
883 return a_trajbell_jer(&self.ctx, dt)
884 property t:
885 def __get__(self):
886 return self.ctx.t
887 property tv:
888 def __get__(self):
889 return self.ctx.tv
890 property ta:
891 def __get__(self):
892 return self.ctx.ta
893 property td:
894 def __get__(self):
895 return self.ctx.td
896 property taj:
897 def __get__(self):
898 return self.ctx.taj
899 property tdj:
900 def __get__(self):
901 return self.ctx.tdj
902 property p0:
903 def __get__(self):
904 return self.ctx.p0
905 property p1:
906 def __get__(self):
907 return self.ctx.p1
908 property v0:
909 def __get__(self):
910 return self.ctx.v0
911 property v1:
912 def __get__(self):
913 return self.ctx.v1
914 property vm:
915 def __get__(self):
916 return self.ctx.vm
917 property jm:
918 def __get__(self):
919 return self.ctx.jm
920 property am:
921 def __get__(self):
922 return self.ctx.am
923 property dm:
924 def __get__(self):
925 return self.ctx.dm
927 from a.trajpoly3 cimport *
929 cdef class trajpoly3:
930 cdef a_trajpoly3 ctx
931 def __init__(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0):
932 a_trajpoly3_gen(&self.ctx, ts, p0, p1, v0, v1)
933 def gen(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0):
934 a_trajpoly3_gen(&self.ctx, ts, p0, p1, v0, v1)
935 return self
936 def pos(self, dt):
937 cdef array x
938 cdef a_float *p
939 cdef Py_ssize_t i, n
940 if PyObject_HasAttrString(dt, "__contains__"):
941 n = len(dt)
942 x = array_num(dt)
943 p = <a_float *>x.data.as_voidptr
944 for i in prange(n, nogil=True):
945 p[i] = a_trajpoly3_pos(&self.ctx, p[i])
946 return x
947 return a_trajpoly3_pos(&self.ctx, dt)
948 def vel(self, dt):
949 cdef array x
950 cdef a_float *p
951 cdef Py_ssize_t i, n
952 if PyObject_HasAttrString(dt, "__contains__"):
953 n = len(dt)
954 x = array_num(dt)
955 p = <a_float *>x.data.as_voidptr
956 for i in prange(n, nogil=True):
957 p[i] = a_trajpoly3_vel(&self.ctx, p[i])
958 return x
959 return a_trajpoly3_vel(&self.ctx, dt)
960 def acc(self, dt):
961 cdef array x
962 cdef a_float *p
963 cdef Py_ssize_t i, n
964 if PyObject_HasAttrString(dt, "__contains__"):
965 n = len(dt)
966 x = array_num(dt)
967 p = <a_float *>x.data.as_voidptr
968 for i in prange(n, nogil=True):
969 p[i] = a_trajpoly3_acc(&self.ctx, p[i])
970 return x
971 return a_trajpoly3_acc(&self.ctx, dt)
972 property p:
973 def __get__(self):
974 return self.ctx.p
975 property v:
976 def __get__(self):
977 return self.ctx.v
978 property a:
979 def __get__(self):
980 return self.ctx.a
982 from a.trajpoly5 cimport *
984 cdef class trajpoly5:
985 cdef a_trajpoly5 ctx
986 def __init__(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0, a_float a0=0, a_float a1=0):
987 a_trajpoly5_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1)
988 def gen(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0, a_float a0=0, a_float a1=0):
989 a_trajpoly5_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1)
990 return self
991 def pos(self, dt):
992 cdef array x
993 cdef a_float *p
994 cdef Py_ssize_t i, n
995 if PyObject_HasAttrString(dt, "__contains__"):
996 n = len(dt)
997 x = array_num(dt)
998 p = <a_float *>x.data.as_voidptr
999 for i in prange(n, nogil=True):
1000 p[i] = a_trajpoly5_pos(&self.ctx, p[i])
1001 return x
1002 return a_trajpoly5_pos(&self.ctx, dt)
1003 def vel(self, dt):
1004 cdef array x
1005 cdef a_float *p
1006 cdef Py_ssize_t i, n
1007 if PyObject_HasAttrString(dt, "__contains__"):
1008 n = len(dt)
1009 x = array_num(dt)
1010 p = <a_float *>x.data.as_voidptr
1011 for i in prange(n, nogil=True):
1012 p[i] = a_trajpoly5_vel(&self.ctx, p[i])
1013 return x
1014 return a_trajpoly5_vel(&self.ctx, dt)
1015 def acc(self, dt):
1016 cdef array x
1017 cdef a_float *p
1018 cdef Py_ssize_t i, n
1019 if PyObject_HasAttrString(dt, "__contains__"):
1020 n = len(dt)
1021 x = array_num(dt)
1022 p = <a_float *>x.data.as_voidptr
1023 for i in prange(n, nogil=True):
1024 p[i] = a_trajpoly5_acc(&self.ctx, p[i])
1025 return x
1026 return a_trajpoly5_acc(&self.ctx, dt)
1027 property p:
1028 def __get__(self):
1029 return self.ctx.p
1030 property v:
1031 def __get__(self):
1032 return self.ctx.v
1033 property a:
1034 def __get__(self):
1035 return self.ctx.a
1037 from a.trajpoly7 cimport *
1039 cdef class trajpoly7:
1040 cdef a_trajpoly7 ctx
1041 def __init__(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0, a_float a0=0, a_float a1=0, a_float j0=0, a_float j1=0):
1042 a_trajpoly7_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1, j0, j1)
1043 def gen(self, a_float ts, a_float p0, a_float p1, a_float v0=0, a_float v1=0, a_float a0=0, a_float a1=0, a_float j0=0, a_float j1=0):
1044 a_trajpoly7_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1, j0, j1)
1045 return self
1046 def pos(self, dt):
1047 cdef array x
1048 cdef a_float *p
1049 cdef Py_ssize_t i, n
1050 if PyObject_HasAttrString(dt, "__contains__"):
1051 n = len(dt)
1052 x = array_num(dt)
1053 p = <a_float *>x.data.as_voidptr
1054 for i in prange(n, nogil=True):
1055 p[i] = a_trajpoly7_pos(&self.ctx, p[i])
1056 return x
1057 return a_trajpoly7_pos(&self.ctx, dt)
1058 def vel(self, dt):
1059 cdef array x
1060 cdef a_float *p
1061 cdef Py_ssize_t i, n
1062 if PyObject_HasAttrString(dt, "__contains__"):
1063 n = len(dt)
1064 x = array_num(dt)
1065 p = <a_float *>x.data.as_voidptr
1066 for i in prange(n, nogil=True):
1067 p[i] = a_trajpoly7_vel(&self.ctx, p[i])
1068 return x
1069 return a_trajpoly7_vel(&self.ctx, dt)
1070 def acc(self, dt):
1071 cdef array x
1072 cdef a_float *p
1073 cdef Py_ssize_t i, n
1074 if PyObject_HasAttrString(dt, "__contains__"):
1075 n = len(dt)
1076 x = array_num(dt)
1077 p = <a_float *>x.data.as_voidptr
1078 for i in prange(n, nogil=True):
1079 p[i] = a_trajpoly7_acc(&self.ctx, p[i])
1080 return x
1081 return a_trajpoly7_acc(&self.ctx, dt)
1082 def jer(self, dt):
1083 cdef array x
1084 cdef a_float *p
1085 cdef Py_ssize_t i, n
1086 if PyObject_HasAttrString(dt, "__contains__"):
1087 n = len(dt)
1088 x = array_num(dt)
1089 p = <a_float *>x.data.as_voidptr
1090 for i in prange(n, nogil=True):
1091 p[i] = a_trajpoly7_jer(&self.ctx, p[i])
1092 return x
1093 return a_trajpoly7_jer(&self.ctx, dt)
1094 property p:
1095 def __get__(self):
1096 return self.ctx.p
1097 property v:
1098 def __get__(self):
1099 return self.ctx.v
1100 property a:
1101 def __get__(self):
1102 return self.ctx.a
1103 property j:
1104 def __get__(self):
1105 return self.ctx.j
1107 from a.trajtrap cimport *
1109 cdef class trajtrap:
1110 cdef a_trajtrap ctx
1111 def gen(self, a_float vm, a_float ac, a_float de, a_float p0, a_float p1, a_float v0=0, a_float v1=0):
1112 return a_trajtrap_gen(&self.ctx, vm, ac, de, p0, p1, v0, v1)
1113 def pos(self, dt):
1114 cdef array x
1115 cdef a_float *p
1116 cdef Py_ssize_t i, n
1117 if PyObject_HasAttrString(dt, "__contains__"):
1118 n = len(dt)
1119 x = array_num(dt)
1120 p = <a_float *>x.data.as_voidptr
1121 for i in prange(n, nogil=True):
1122 p[i] = a_trajtrap_pos(&self.ctx, p[i])
1123 return x
1124 return a_trajtrap_pos(&self.ctx, dt)
1125 def vel(self, dt):
1126 cdef array x
1127 cdef a_float *p
1128 cdef Py_ssize_t i, n
1129 if PyObject_HasAttrString(dt, "__contains__"):
1130 n = len(dt)
1131 x = array_num(dt)
1132 p = <a_float *>x.data.as_voidptr
1133 for i in prange(n, nogil=True):
1134 p[i] = a_trajtrap_vel(&self.ctx, p[i])
1135 return x
1136 return a_trajtrap_vel(&self.ctx, dt)
1137 def acc(self, dt):
1138 cdef array x
1139 cdef a_float *p
1140 cdef Py_ssize_t i, n
1141 if PyObject_HasAttrString(dt, "__contains__"):
1142 n = len(dt)
1143 x = array_num(dt)
1144 p = <a_float *>x.data.as_voidptr
1145 for i in prange(n, nogil=True):
1146 p[i] = a_trajtrap_acc(&self.ctx, p[i])
1147 return x
1148 return a_trajtrap_acc(&self.ctx, dt)
1149 property t:
1150 def __get__(self):
1151 return self.ctx.t
1152 property p0:
1153 def __get__(self):
1154 return self.ctx.p0
1155 property p1:
1156 def __get__(self):
1157 return self.ctx.p1
1158 property v0:
1159 def __get__(self):
1160 return self.ctx.v0
1161 property v1:
1162 def __get__(self):
1163 return self.ctx.v1
1164 property vc:
1165 def __get__(self):
1166 return self.ctx.vc
1167 property ta:
1168 def __get__(self):
1169 return self.ctx.ta
1170 property td:
1171 def __get__(self):
1172 return self.ctx.td
1173 property pa:
1174 def __get__(self):
1175 return self.ctx.pa
1176 property pd:
1177 def __get__(self):
1178 return self.ctx.pd
1179 property ac:
1180 def __get__(self):
1181 return self.ctx.ac
1182 property de:
1183 def __get__(self):
1184 return self.ctx.de
1186 from a.version cimport *
1188 cdef class version:
1189 cdef a_version ctx
1190 def __init__(self, unsigned int major = 0, unsigned int minor = 0, unsigned int third = 0, unsigned int extra = 0):
1191 self.ctx.major = major
1192 self.ctx.minor = minor
1193 self.ctx.third = third
1194 self.ctx.extra = extra
1195 self.ctx.alpha[0] = 46
1196 def __repr__(self):
1197 cdef char[48] str
1198 a_version_tostr(&self.ctx, str, sizeof(str))
1199 return str.decode()
1200 @staticmethod
1201 def check(unsigned int major = 0, unsigned int minor = 0, unsigned int patch = 0):
1202 return a_version_check(major, minor, patch)
1203 def cmp(self, version that):
1204 return a_version_cmp(&self.ctx, &that.ctx)
1205 def __lt__(self, version that):
1206 return a_version_lt(&self.ctx, &that.ctx)
1207 def __gt__(self, version that):
1208 return a_version_gt(&self.ctx, &that.ctx)
1209 def __le__(self, version that):
1210 return a_version_le(&self.ctx, &that.ctx)
1211 def __ge__(self, version that):
1212 return a_version_ge(&self.ctx, &that.ctx)
1213 def __eq__(self, version that):
1214 return a_version_eq(&self.ctx, &that.ctx)
1215 def __ne__(self, version that):
1216 return a_version_ne(&self.ctx, &that.ctx)
1217 def parse(self, bytes ver):
1218 a_version_parse(&self.ctx, ver)
1219 return self
1220 def __hash__(self):
1221 return object.__hash__(self)
1222 property major:
1223 def __get__(self):
1224 return self.ctx.major
1225 def __set__(self, unsigned int major):
1226 self.ctx.major = major
1227 property minor:
1228 def __get__(self):
1229 return self.ctx.minor
1230 def __set__(self, unsigned int minor):
1231 self.ctx.minor = minor
1232 property third:
1233 def __get__(self):
1234 return self.ctx.third
1235 def __set__(self, unsigned int third):
1236 self.ctx.third = third
1237 property extra:
1238 def __get__(self):
1239 return self.ctx.extra
1240 def __set__(self, unsigned int extra):
1241 self.ctx.extra = extra
1242 property alpha:
1243 def __get__(self):
1244 cdef char[5] alpha
1245 a_version_alpha(&self.ctx, alpha)
1246 return alpha
1247 def __set__(self, bytes alpha):
1248 a_version_set_alpha(&self.ctx, alpha)
1249 MAJOR = A_VERSION_MAJOR
1250 MINOR = A_VERSION_MINOR
1251 PATCH = A_VERSION_PATCH
1252 TWEAK = A_VERSION_TWEAK
1254 if PY_MAJOR_VERSION >= 3:
1255 VERSION = A_VERSION.decode()
1256 else:
1257 VERSION = A_VERSION