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