From 66bf98639f747b95473e65b6676698b3df4a1620 Mon Sep 17 00:00:00 2001 From: tqfx Date: Thu, 4 Apr 2024 15:53:41 +0800 Subject: [PATCH] support for PyPy --- python/liba.pyi | 8 +- python/src/a.pyx | 569 ++++++++++++++++++++++++++----------------- python/test/3rd/mf.py | 28 +-- python/test/3rd/trajbell.py | 10 +- python/test/3rd/trajpoly3.py | 6 +- python/test/3rd/trajpoly5.py | 6 +- python/test/3rd/trajpoly7.py | 8 +- python/test/3rd/trajtrap.py | 8 +- python/test/a.py | 4 +- python/test/poly.py | 4 +- setup.py | 20 -- 11 files changed, 383 insertions(+), 288 deletions(-) diff --git a/python/liba.pyi b/python/liba.pyi index 0370e5d..4e0bf00 100644 --- a/python/liba.pyi +++ b/python/liba.pyi @@ -24,7 +24,7 @@ def rsqrt_f64(x: float) -> float: ... class crc8: def __init__(self, poly: int, reversed=False) -> None: ... def gen(self, poly: int, reversed=False) -> crc8: ... - def __call__(self, block: bytes | str, value=0) -> int: ... + def __call__(self, block: bytes, value=0) -> int: ... def pack(self, block: bytes, value=0) -> bytes: ... @property def table(self) -> array[int]: ... @@ -32,7 +32,7 @@ class crc8: class crc16: def __init__(self, poly: int, reversed=False) -> None: ... def gen(self, poly: int, reversed=False) -> crc16: ... - def __call__(self, block: bytes | str, value=0) -> int: ... + def __call__(self, block: bytes, value=0) -> int: ... def pack(self, block: bytes, value=0) -> bytes: ... @property def table(self) -> array[int]: ... @@ -40,7 +40,7 @@ class crc16: class crc32: def __init__(self, poly: int, reversed=False) -> None: ... def gen(self, poly: int, reversed=False) -> crc32: ... - def __call__(self, block: bytes | str, value=0) -> int: ... + def __call__(self, block: bytes, value=0) -> int: ... def pack(self, block: bytes, value=0) -> bytes: ... @property def table(self) -> array[int]: ... @@ -48,7 +48,7 @@ class crc32: class crc64: def __init__(self, poly: int, reversed=False) -> None: ... def gen(self, poly: int, reversed=False) -> crc64: ... - def __call__(self, block: bytes | str, value=0) -> int: ... + def __call__(self, block: bytes, value=0) -> int: ... def pack(self, block: bytes, value=0) -> bytes: ... @property def table(self) -> array[int]: ... diff --git a/python/src/a.pyx b/python/src/a.pyx index 4a3aab3..31beb7a 100644 --- a/python/src/a.pyx +++ b/python/src/a.pyx @@ -7,52 +7,93 @@ #cython: c_string_type=bytes #cython: c_string_encoding=utf-8 from cython.parallel import prange -from cpython.array cimport array +from cython.view cimport array from cpython cimport * from a cimport * -cdef inline object array_i8(object o): - return array('b', o) +cdef object u8_new(Py_ssize_t n): + return array(shape=(n,), itemsize=1, format='B') -cdef inline object array_u8(object o): - return array('B', o) +cdef object u16_new(Py_ssize_t n): + return array(shape=(n,), itemsize=2, format='H') -cdef inline object array_i16(object o): - return array('h', o) +cdef object u32_new(Py_ssize_t n): + cdef str u32 = 'I' + if UINT32_MAX > UINT_MAX: + u32 = 'L' + return array(shape=(n,), itemsize=4, format=u32) -cdef inline object array_u16(object o): - return array('H', o) +cdef a_u32[::1] u32_set(object o, object x, Py_ssize_t n): + cdef a_u32[::1] r = o + cdef Py_ssize_t i + for i in range(n): + r[i] = x[i] + return r -cdef inline object array_i32(object o): - if INT32_MAX == INT_MAX: - return array('i', o) - return array('l', o) +cdef object u64_new(Py_ssize_t n): + cdef str u64 = 'L' + if UINT64_MAX > ULONG_MAX: + u64 = 'Q' + return array(shape=(n,), itemsize=8, format=u64) -cdef inline object array_u32(object o): - if UINT32_MAX == UINT_MAX: - return array('I', o) - return array('L', o) +cdef a_u64[::1] u64_set(object o, object x, Py_ssize_t n): + cdef a_u64[::1] r = o + cdef Py_ssize_t i + for i in range(n): + r[i] = x[i] + return r -cdef inline object array_i64(object o): - if INT64_MAX == LONG_MAX: - return array('l', o) - return array('q', o) +cdef object f32_new(Py_ssize_t n): + return array(shape=(n,), itemsize=4, format='f') -cdef inline object array_u64(object o): - if UINT64_MAX == ULONG_MAX: - return array('L', o) - return array('Q', o) +cdef a_f32[::1] f32_set(object o, object x, Py_ssize_t n): + cdef a_f32[::1] r = o + cdef Py_ssize_t i + for i in range(n): + r[i] = x[i] + return r -cdef inline object array_f32(object o): - return array('f', o) +cdef object f64_new(Py_ssize_t n): + return array(shape=(n,), itemsize=8, format='d') -cdef inline object array_f64(object o): - return array('d', o) +cdef a_f64[::1] f64_set(object o, object x, Py_ssize_t n): + cdef a_f64[::1] r = o + cdef Py_ssize_t i + for i in range(n): + r[i] = x[i] + return r -cdef inline object array_num(object o): +cdef object num_new(Py_ssize_t n): + cdef int z = 8 + cdef str num = 'd' if A_FLOAT_TYPE == A_FLOAT_SINGLE: - return array('f', o) - return array('d', o) + z, num = 4, 'f' + return array(shape=(n,), itemsize=z, format=num) + +cdef a_float[::1] num_set(object o, object x, Py_ssize_t n): + cdef a_float[::1] r = o + cdef Py_ssize_t i + for i in range(n): + r[i] = x[i] + return r + +cdef object num2_new(object x2): + cdef Py_ssize_t n = 0 + cdef object x1 + for x1 in x2: + n += len(x1) + return num_new(n) + +cdef a_float[::1] num2_set(object o, object x2): + cdef a_float[::1] r = o + cdef Py_ssize_t n = 0 + cdef object x1 + cdef a_float x + for x1 in x2: + for x in x1: + r[n] = x + n += 1 + return r def hash_bkdr(const char *str, a_u32 val=0) -> a_u32: return a_hash_bkdr(str, val) @@ -65,7 +106,7 @@ from a.crc cimport * cdef class crc8: cdef readonly object table def __cinit__(self): - self.table = array_u8((0,) * 0x100) + self.table = u8_new(0x100) def gen(self, a_u8 poly, bint reversed=0): cdef a_u8[::1] table = self.table if reversed: @@ -75,13 +116,13 @@ cdef class crc8: return self def __init__(self, a_u8 poly, bint reversed=0): self.gen(poly, reversed) - def __call__(self, const char *block, a_u8 value=0): + def __call__(self, bytes block, a_u8 value=0): cdef const a_u8[::1] table = self.table - return a_crc8(&table[0], block, len(block), value) + return a_crc8(&table[0], block, len(block), value) def pack(self, bytes block, a_u8 value=0): cdef size_t n = len(block) block = block + bytes(1) - cdef unsigned char *p = block + cdef char *p = block cdef const a_u8[::1] table = self.table value = a_crc8(&table[0], p, n, value) p[n] = value @@ -90,7 +131,7 @@ cdef class crc8: cdef class crc16: cdef readonly object table def __cinit__(self): - self.table = array_u16((0,) * 0x100) + self.table = u16_new(0x100) cdef a_u16 (*eval)(const a_u16 *, const void *, a_size, a_u16) def gen(self, a_u16 poly, bint reversed=0): cdef a_u16[::1] table = self.table @@ -103,13 +144,13 @@ cdef class crc16: return self def __init__(self, a_u16 poly, bint reversed=0): self.gen(poly, reversed) - def __call__(self, const char *block, a_u16 value=0): + def __call__(self, bytes block, a_u16 value=0): cdef const a_u16[::1] table = self.table - return self.eval(&table[0], block, len(block), value) + return self.eval(&table[0], block, len(block), value) def pack(self, bytes block, a_u16 value=0): cdef size_t n = len(block) block = block + bytes(2) - cdef unsigned char *p = block + cdef char *p = block cdef const a_u16[::1] table = self.table value = self.eval(&table[0], p, n, value) if self.eval == a_crc16m: @@ -121,7 +162,7 @@ cdef class crc16: cdef class crc32: cdef readonly object table def __cinit__(self): - self.table = array_u32((0,) * 0x100) + self.table = u32_new(0x100) cdef a_u32 (*eval)(const a_u32 *, const void *, a_size, a_u32) def gen(self, a_u32 poly, bint reversed=0): cdef a_u32[::1] table = self.table @@ -134,13 +175,13 @@ cdef class crc32: return self def __init__(self, a_u32 poly, bint reversed=0): self.gen(poly, reversed) - def __call__(self, const char *block, a_u32 value=0): + def __call__(self, bytes block, a_u32 value=0): cdef const a_u32[::1] table = self.table - return self.eval(&table[0], block, len(block), value) + return self.eval(&table[0], block, len(block), value) def pack(self, bytes block, a_u32 value=0): cdef size_t n = len(block) block = block + bytes(4) - cdef unsigned char *p = block + cdef char *p = block cdef const a_u32[::1] table = self.table value = self.eval(&table[0], p, n, value) if self.eval == a_crc32m: @@ -152,7 +193,7 @@ cdef class crc32: cdef class crc64: cdef readonly object table def __cinit__(self): - self.table = array_u64((0,) * 0x100) + self.table = u64_new(0x100) cdef a_u64 (*eval)(const a_u64 *, const void *, a_size, a_u64) def gen(self, a_u64 poly, bint reversed=0): cdef a_u64[::1] table = self.table @@ -165,13 +206,13 @@ cdef class crc64: return self def __init__(self, a_u64 poly, bint reversed=0): self.gen(poly, reversed) - def __call__(self, const char *block, a_u64 value=0): + def __call__(self, bytes block, a_u64 value=0): cdef const a_u64[::1] table = self.table - return self.eval(&table[0], block, len(block), value) + return self.eval(&table[0], block, len(block), value) def pack(self, bytes block, a_u64 value=0): cdef size_t n = len(block) block = block + bytes(8) - cdef unsigned char *p = block + cdef char *p = block cdef const a_u64[::1] table = self.table value = self.eval(&table[0], p, n, value) if self.eval == a_crc64m: @@ -232,6 +273,7 @@ cdef class lpf: from a.math cimport * def isqrt(x: int): + cdef object x0, x1 if x <= 1: return x x0 = 1 << ((x.bit_length() + 1) >> 1) @@ -242,47 +284,55 @@ def isqrt(x: int): return x0 def sqrt_u32(x): - cdef a_u32[::1] o + cdef object r + cdef a_u32[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_u32(x) + r = u32_new(n) + p = u32_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_u32_sqrt(o[i]) - return array_u16(o) + p[i] = a_u32_sqrt(p[i]) + return r return a_u32_sqrt(x) def sqrt_u64(x): - cdef a_u64[::1] o + cdef object r + cdef a_u64[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_u64(x) + r = u64_new(n) + p = u64_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_u64_sqrt(o[i]) - return array_u32(o) + p[i] = a_u64_sqrt(p[i]) + return r return a_u64_sqrt(x) def rsqrt_f32(x): - cdef a_f32[::1] o + cdef object r + cdef a_f32[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_f32(x) + r = f32_new(n) + p = f32_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_f32_rsqrt(o[i]) - return o.base + p[i] = a_f32_rsqrt(p[i]) + return r return a_f32_rsqrt(x) def rsqrt_f64(x): - cdef a_f64[::1] o + cdef object r + cdef a_f64[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_f64(x) + r = f64_new(n) + p = f64_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_f64_rsqrt(o[i]) - return o.base + p[i] = a_f64_rsqrt(p[i]) + return r return a_f64_rsqrt(x) from a.mf cimport * @@ -304,159 +354,186 @@ cdef class mf: PI = A_MF_PI @staticmethod def __call__(unsigned int e, x, a): - cdef a_float[::1] o - cdef Py_ssize_t i, n - cdef unsigned int m = e - cdef a_float[::1] a_ = array_num(a) + cdef object r + cdef a_float[::1] p + cdef Py_ssize_t i, n = len(a) + cdef a_float[::1] q = num_set(num_new(n), a, n) if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf(m, o[i], &a_[0]) - return o.base - return a_mf(m, x, &a_[0]) + p[i] = a_mf(e, p[i], &q[0]) + return r + return a_mf(e, x, &q[0]) @staticmethod def gauss(x, a_float sigma, a_float c): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_gauss(o[i], sigma, c) - return o.base + p[i] = a_mf_gauss(p[i], sigma, c) + return r return a_mf_gauss(x, sigma, c) @staticmethod def gauss2(x, a_float sigma1, a_float c1, a_float sigma2, a_float c2): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_gauss2(o[i], sigma1, c1, sigma2, c2) - return o.base + p[i] = a_mf_gauss2(p[i], sigma1, c1, sigma2, c2) + return r return a_mf_gauss2(x, sigma1, c1, sigma2, c2) @staticmethod def gbell(x, a_float a, a_float b, a_float c): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_gbell(o[i], a, b, c) - return o.base + p[i] = a_mf_gbell(p[i], a, b, c) + return r return a_mf_gbell(x, a, b, c) @staticmethod def sig(x, a_float a, a_float c): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_sig(o[i], a, c) - return o.base + p[i] = a_mf_sig(p[i], a, c) + return r return a_mf_sig(x, a, c) @staticmethod def dsig(x, a_float a1, a_float c1, a_float a2, a_float c2): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_dsig(o[i], a1, c1, a2, c2) - return o.base + p[i] = a_mf_dsig(p[i], a1, c1, a2, c2) + return r return a_mf_dsig(x, a1, c1, a2, c2) @staticmethod def psig(x, a_float a1, a_float c1, a_float a2, a_float c2): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_psig(o[i], a1, c1, a2, c2) - return o.base + p[i] = a_mf_psig(p[i], a1, c1, a2, c2) + return r return a_mf_psig(x, a1, c1, a2, c2) @staticmethod def trap(x, a_float a, a_float b, a_float c, a_float d): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_trap(o[i], a, b, c, d) - return o.base + p[i] = a_mf_trap(p[i], a, b, c, d) + return r return a_mf_trap(x, a, b, c, d) @staticmethod def tri(x, a_float a, a_float b, a_float c): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_tri(o[i], a, b, c) - return o.base + p[i] = a_mf_tri(p[i], a, b, c) + return r return a_mf_tri(x, a, b, c) @staticmethod def lins(x, a_float a, a_float b): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_lins(o[i], a, b) - return o.base + p[i] = a_mf_lins(p[i], a, b) + return r return a_mf_lins(x, a, b) @staticmethod def linz(x, a_float a, a_float b): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_linz(o[i], a, b) - return o.base + p[i] = a_mf_linz(p[i], a, b) + return r return a_mf_linz(x, a, b) @staticmethod def s(x, a_float a, a_float b): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_s(o[i], a, b) - return o.base + p[i] = a_mf_s(p[i], a, b) + return r return a_mf_s(x, a, b) @staticmethod def z(x, a_float a, a_float b): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_z(o[i], a, b) - return o.base + p[i] = a_mf_z(p[i], a, b) + return r return a_mf_z(x, a, b) @staticmethod def pi(x, a_float a, a_float b, a_float c, a_float d): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_mf_pi(o[i], a, b, c, d) - return o.base + p[i] = a_mf_pi(p[i], a, b, c, d) + return r return a_mf_pi(x, a, b, c, d) from a.pid cimport * @@ -558,17 +635,17 @@ cdef class pid_fuzzy: a_pid_fuzzy_set_op(&self.ctx, op) return self def rule(self, me, mec, mkp, mki, mkd): - self.me = array_num([_2 for _1 in me for _2 in _1]) - self.mec = array_num([_2 for _1 in mec for _2 in _1]) - self.mkp = array_num([_2 for _1 in mkp for _2 in _1]) - self.mki = array_num([_2 for _1 in mki for _2 in _1]) - self.mkd = array_num([_2 for _1 in mkd for _2 in _1]) - cdef const a_float[::1] e = self.me - cdef const a_float[::1] ec = self.mec - cdef const a_float[::1] kp = self.mkp - cdef const a_float[::1] ki = self.mki - cdef const a_float[::1] kd = self.mkd - a_pid_fuzzy_rule(&self.ctx, len(me), &e[0], &ec[0], &kp[0], &ki[0], &kd[0]) + self.me = num2_new(me) + self.mec = num2_new(mec) + self.mkp = num2_new(mkp) + self.mki = num2_new(mki) + self.mkd = num2_new(mkd) + cdef a_float[::1] e = num2_set(self.me, me) + cdef a_float[::1] ec = num2_set(self.mec, mec) + cdef a_float[::1] kp = num2_set(self.mkp, mkp) + cdef a_float[::1] ki = num2_set(self.mki, mki) + cdef a_float[::1] kd = num2_set(self.mkd, mkd) + a_pid_fuzzy_rule(&self.ctx, len(me), &e[0], &ec[0], &kp[0], &ki[0], &kd[0]) return self def set_block(self, unsigned int num): cdef void *ptr = a_pid_fuzzy_block(&self.ctx) @@ -736,30 +813,32 @@ cdef class pid_neuro: from a.poly cimport * def poly_eval(x, *a): - cdef a_float[::1] o - cdef Py_ssize_t i, n - cdef a_size n_ = len(a) - cdef a_float[::1] a_ = array_num(a) + cdef object r + cdef a_float[::1] p + cdef Py_ssize_t i, n, m = len(a) + cdef a_float[::1] q = num_set(num_new(m), a, m) if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_poly_eval(&a_[0], n_, o[i]) - return o.base - return a_poly_eval(&a_[0], n_, x) + p[i] = a_poly_eval(&q[0], m, p[i]) + return r + return a_poly_eval(&q[0], m, x) def poly_evar(x, *a): - cdef a_float[::1] o - cdef Py_ssize_t i, n - cdef a_size n_ = len(a) - cdef a_float[::1] a_ = array_num(a) + cdef object r + cdef a_float[::1] p + cdef Py_ssize_t i, n, m = len(a) + cdef a_float[::1] q = num_set(num_new(m), a, m) if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_poly_evar(&a_[0], n_, o[i]) - return o.base - return a_poly_evar(&a_[0], n_, x) + p[i] = a_poly_evar(&q[0], m, p[i]) + return r + return a_poly_evar(&q[0], m, x) from a.tf cimport * @@ -779,22 +858,24 @@ cdef class tf: def __get__(self): return self._num def __set__(self, num): - self._num = array_num(num) - self.input = array_num(num) - cdef a_float[::1] _num = self._num + cdef unsigned int n = len(num) + self._num = num_new(n) + self.input = num_new(n) + cdef a_float[::1] _num = num_set(self._num, num, n) cdef a_float[::1] input = self.input - a_tf_set_num(&self.ctx, len(num), &_num[0], &input[0]) + a_tf_set_num(&self.ctx, n, &_num[0], &input[0]) cdef readonly object _den cdef readonly object output property den: def __get__(self): return self._den def __set__(self, den): - self._den = array_num(den) - self.output = array_num(den) - cdef a_float[::1] _den = self._den + cdef unsigned int n = len(den) + self._den = num_new(n) + self.output = num_new(n) + cdef a_float[::1] _den = num_set(self._den, den, n) cdef a_float[::1] output = self.output - a_tf_set_den(&self.ctx, len(den), &_den[0], &output[0]) + a_tf_set_den(&self.ctx, n, &_den[0], &output[0]) from a.trajbell cimport * @@ -803,44 +884,52 @@ cdef class trajbell: 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): return a_trajbell_gen(&self.ctx, jm, am, vm, p0, p1, v0, v1) def pos(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajbell_pos(&self.ctx, o[i]) - return o.base + p[i] = a_trajbell_pos(&self.ctx, p[i]) + return r return a_trajbell_pos(&self.ctx, x) def vel(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajbell_vel(&self.ctx, o[i]) - return o.base + p[i] = a_trajbell_vel(&self.ctx, p[i]) + return r return a_trajbell_vel(&self.ctx, x) def acc(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajbell_acc(&self.ctx, o[i]) - return o.base + p[i] = a_trajbell_acc(&self.ctx, p[i]) + return r return a_trajbell_acc(&self.ctx, x) def jer(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajbell_jer(&self.ctx, o[i]) - return o.base + p[i] = a_trajbell_jer(&self.ctx, p[i]) + return r return a_trajbell_jer(&self.ctx, x) property t: def __get__(self): @@ -895,34 +984,40 @@ cdef class trajpoly3: a_trajpoly3_gen(&self.ctx, ts, p0, p1, v0, v1) return self def pos(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly3_pos(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly3_pos(&self.ctx, p[i]) + return r return a_trajpoly3_pos(&self.ctx, x) def vel(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly3_vel(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly3_vel(&self.ctx, p[i]) + return r return a_trajpoly3_vel(&self.ctx, x) def acc(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly3_acc(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly3_acc(&self.ctx, p[i]) + return r return a_trajpoly3_acc(&self.ctx, x) property p: def __get__(self): @@ -944,34 +1039,40 @@ cdef class trajpoly5: a_trajpoly5_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1) return self def pos(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly5_pos(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly5_pos(&self.ctx, p[i]) + return r return a_trajpoly5_pos(&self.ctx, x) def vel(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly5_vel(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly5_vel(&self.ctx, p[i]) + return r return a_trajpoly5_vel(&self.ctx, x) def acc(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly5_acc(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly5_acc(&self.ctx, p[i]) + return r return a_trajpoly5_acc(&self.ctx, x) property p: def __get__(self): @@ -993,44 +1094,52 @@ cdef class trajpoly7: a_trajpoly7_gen(&self.ctx, ts, p0, p1, v0, v1, a0, a1, j0, j1) return self def pos(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly7_pos(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly7_pos(&self.ctx, p[i]) + return r return a_trajpoly7_pos(&self.ctx, x) def vel(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly7_vel(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly7_vel(&self.ctx, p[i]) + return r return a_trajpoly7_vel(&self.ctx, x) def acc(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly7_acc(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly7_acc(&self.ctx, p[i]) + return r return a_trajpoly7_acc(&self.ctx, x) def jer(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajpoly7_jer(&self.ctx, o[i]) - return o.base + p[i] = a_trajpoly7_jer(&self.ctx, p[i]) + return r return a_trajpoly7_jer(&self.ctx, x) property p: def __get__(self): @@ -1052,34 +1161,40 @@ cdef class trajtrap: 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): return a_trajtrap_gen(&self.ctx, vm, ac, de, p0, p1, v0, v1) def pos(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajtrap_pos(&self.ctx, o[i]) - return o.base + p[i] = a_trajtrap_pos(&self.ctx, p[i]) + return r return a_trajtrap_pos(&self.ctx, x) def vel(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajtrap_vel(&self.ctx, o[i]) - return o.base + p[i] = a_trajtrap_vel(&self.ctx, p[i]) + return r return a_trajtrap_vel(&self.ctx, x) def acc(self, x): - cdef a_float[::1] o + cdef object r + cdef a_float[::1] p cdef Py_ssize_t i, n if PyObject_HasAttrString(x, "__contains__"): n = len(x) - o = array_num(x) + r = num_new(n) + p = num_set(r, x, n) for i in prange(n, nogil=True): - o[i] = a_trajtrap_acc(&self.ctx, o[i]) - return o.base + p[i] = a_trajtrap_acc(&self.ctx, p[i]) + return r return a_trajtrap_acc(&self.ctx, x) property t: def __get__(self): diff --git a/python/test/3rd/mf.py b/python/test/3rd/mf.py index b9e92b2..0f7b514 100755 --- a/python/test/3rd/mf.py +++ b/python/test/3rd/mf.py @@ -19,7 +19,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.gauss(x, 1, 0) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_gauss.png")) @@ -28,7 +28,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.gauss2(x, 1, -1, 1, +1) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_gauss2.png")) @@ -37,7 +37,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.gbell(x, 2, 4, 0) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_gbell.png")) @@ -46,7 +46,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.sig(x, 2, 0) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_sig.png")) @@ -55,7 +55,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.dsig(x, 5, -2, +5, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_dsig.png")) @@ -64,7 +64,7 @@ x = np.arange(-3, 3, 0.001) y = liba.mf.psig(x, 5, -2, -5, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_psig.png")) @@ -73,7 +73,7 @@ x = np.arange(0, 2, 0.001) y = liba.mf.tri(x, 0, 1, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_tri.png")) @@ -83,7 +83,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.trap(x, 0, 1, 2, 3) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_trap.png")) @@ -92,7 +92,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.lins(x, 1, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_lins.png")) @@ -101,7 +101,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.linz(x, 1, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_linz.png")) @@ -110,7 +110,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.s(x, 1, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_s.png")) @@ -119,7 +119,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.z(x, 1, 2) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_z.png")) @@ -129,7 +129,7 @@ x = np.arange(0, 3, 0.001) y = liba.mf.pi(x, 0, 1, 2, 3) plt.figure(title) plt.title(title) -plt.plot(x, y) +plt.plot(x, np.array(y, copy=False)) plt.grid(True) plt.savefig(os.path.join(base, "mf_pi.png")) @@ -149,6 +149,6 @@ x = np.arange(-3 * S, +3 * S, 0.001) plt.figure(title) plt.title(title) for param in params: - plt.plot(x, liba.mf()(param[0], x, param[1:])) + plt.plot(x, np.array(liba.mf()(param[0], x, param[1:]), copy=False)) plt.savefig(os.path.join(base, "mf.png")) plt.show() diff --git a/python/test/3rd/trajbell.py b/python/test/3rd/trajbell.py index 9948deb..add1bd4 100755 --- a/python/test/3rd/trajbell.py +++ b/python/test/3rd/trajbell.py @@ -23,24 +23,24 @@ plt.figure("bell-shaped velocity trajectory") plt.subplot(411) plt.title("bell-shaped velocity trajectory") plt.ylabel("Position") -plt.plot(data, traj.pos(data), "r-", label="q") +plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label="q") plt.legend() plt.subplot(412) plt.ylabel("Velocity") -plt.plot(data, traj.vel(data), "b-", label="v") +plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label="v") plt.legend() plt.subplot(413) plt.ylabel("Acceleration") -plt.plot(data, traj.acc(data), "g-", label="a") +plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label="a") plt.legend() plt.subplot(414) plt.ylabel("Jerk") -plt.plot(data, traj.jer(data), "k-", label="j") +plt.plot(data, np.array(traj.jer(data), copy=False), "k-", label="j") plt.legend() plt.xlabel("t") -plt.savefig(os.path.join(base, "bell-shaped_trajectory.png")) +plt.savefig(os.path.join(base, "trajectory_bell-shaped.png")) plt.show() diff --git a/python/test/3rd/trajpoly3.py b/python/test/3rd/trajpoly3.py index 8794f16..4f6f72a 100755 --- a/python/test/3rd/trajpoly3.py +++ b/python/test/3rd/trajpoly3.py @@ -52,17 +52,17 @@ plt.figure("3 polynomial trajectory") plt.subplot(311) plt.title("cubic polynomial trajectory") plt.ylabel("Position") -plt.plot(data, traj.pos(data), "r-", label=text_p) +plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label=text_p) plt.legend() plt.subplot(312) plt.ylabel("Velocity") -plt.plot(data, traj.vel(data), "b-", label=text_v) +plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label=text_v) plt.legend() plt.subplot(313) plt.ylabel("Acceleration") -plt.plot(data, traj.acc(data), "g-", label=text_a) +plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label=text_a) plt.legend() plt.xlabel("t") diff --git a/python/test/3rd/trajpoly5.py b/python/test/3rd/trajpoly5.py index e9ea29d..aff3b14 100755 --- a/python/test/3rd/trajpoly5.py +++ b/python/test/3rd/trajpoly5.py @@ -64,17 +64,17 @@ plt.figure("5 polynomial trajectory") plt.subplot(311) plt.title("puintic polynomial trajectory") plt.ylabel("Position") -plt.plot(data, traj.pos(data), "r-", label=text_p) +plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label=text_p) plt.legend() plt.subplot(312) plt.ylabel("Velocity") -plt.plot(data, traj.vel(data), "b-", label=text_v) +plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label=text_v) plt.legend() plt.subplot(313) plt.ylabel("Acceleration") -plt.plot(data, traj.acc(data), "g-", label=text_a) +plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label=text_a) plt.legend() plt.xlabel("t") diff --git a/python/test/3rd/trajpoly7.py b/python/test/3rd/trajpoly7.py index ecd9e96..ede3121 100755 --- a/python/test/3rd/trajpoly7.py +++ b/python/test/3rd/trajpoly7.py @@ -89,22 +89,22 @@ plt.figure("7 polynomial trajectory") plt.subplot(411) plt.title("hepta polynomial trajectory") plt.ylabel("Position") -plt.plot(data, traj.pos(data), "r-", label=text_p) +plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label=text_p) plt.legend() plt.subplot(412) plt.ylabel("Velocity") -plt.plot(data, traj.vel(data), "b-", label=text_v) +plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label=text_v) plt.legend() plt.subplot(413) plt.ylabel("Acceleration") -plt.plot(data, traj.acc(data), "g-", label=text_a) +plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label=text_a) plt.legend() plt.subplot(414) plt.ylabel("Jerk") -plt.plot(data, traj.jer(data), "k-", label=text_j) +plt.plot(data, np.array(traj.jer(data), copy=False), "k-", label=text_j) plt.legend() plt.xlabel("t") diff --git a/python/test/3rd/trajtrap.py b/python/test/3rd/trajtrap.py index 96e92b5..9dfd8cf 100755 --- a/python/test/3rd/trajtrap.py +++ b/python/test/3rd/trajtrap.py @@ -23,19 +23,19 @@ plt.figure("trapezoidal velocity trajectory") plt.subplot(311) plt.title("trapezoidal velocity trajectory") plt.ylabel("Position") -plt.plot(data, traj.pos(data), "r-", label="q") +plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label="q") plt.legend() plt.subplot(312) plt.ylabel("Velocity") -plt.plot(data, traj.vel(data), "b-", label="v") +plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label="v") plt.legend() plt.subplot(313) plt.ylabel("Acceleration") -plt.plot(data, traj.acc(data), "g-", label="a") +plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label="a") plt.legend() plt.xlabel("t") -plt.savefig(os.path.join(base, "trapezoidal_trajectory.png")) +plt.savefig(os.path.join(base, "trajectory_trapezoidal.png")) plt.show() diff --git a/python/test/a.py b/python/test/a.py index 99b4664..5ffc5c6 100755 --- a/python/test/a.py +++ b/python/test/a.py @@ -12,6 +12,6 @@ import liba # type: ignore print(liba.hash_bkdr("0123456789")) print(liba.hash_sdbm("0123456789")) print(liba.sqrt_u64(7), liba.sqrt_u32(7)) -print(liba.sqrt_u64([4, 8]), liba.sqrt_u32([4, 8])) +print(list(liba.sqrt_u64([4, 8])), list(liba.sqrt_u32([4, 8]))) print(liba.rsqrt_f64(4), liba.rsqrt_f32(4)) -print(liba.rsqrt_f64([4, 8]), liba.rsqrt_f32([4, 8])) +print(list(liba.rsqrt_f64([4, 8])), list(liba.rsqrt_f32([4, 8]))) diff --git a/python/test/poly.py b/python/test/poly.py index 1e0ebd6..6e0791f 100755 --- a/python/test/poly.py +++ b/python/test/poly.py @@ -8,7 +8,7 @@ sys.path.insert(0, path) import liba # type: ignore k = [1, 2, 3] -print(k, liba.poly_eval(k, *k)) print(k, liba.poly_eval(2, *k)) -print(k, liba.poly_evar(k, *k)) +print(k, list(liba.poly_eval(k, *k))) print(k, liba.poly_evar(2, *k)) +print(k, list(liba.poly_evar(k, *k))) diff --git a/setup.py b/setup.py index 30169b2..1a958ae 100755 --- a/setup.py +++ b/setup.py @@ -78,25 +78,6 @@ def check_math(text=""): return text -def update_pyx(source): - _P = "array import" - _C = "cpython.array cimport" - with open(source, "r") as f: - context = f.read() - try: - name = sys.implementation.name - except AttributeError: - name = "cpython" - if "PyPy" in sys.version: - name = "pypy" - if name == "cpython" and context.find(_P) > 0: - with open(source, "wb") as f: - f.write(context.replace(_P, _C).encode("UTF-8")) - if name != "cpython" and context.find(_C) > 0: - with open(source, "wb") as f: - f.write(context.replace(_C, _P).encode("UTF-8")) - - def configure(config): with open("setup.cfg", "r") as f: version = findall(r"version = (\S+)", f.read())[0] @@ -135,7 +116,6 @@ if LIBA_FLOAT != 8: define_macros += [("A_SIZE_FLOAT", LIBA_FLOAT)] if USE_CYTHON and os.path.exists("python/src/a.pyx"): sources += ["python/src/a.pyx"] - update_pyx("python/src/a.pyx") elif os.path.exists("python/src/a.c"): sources += ["python/src/a.c"] if not os.path.exists(base): -- 2.11.4.GIT