From 048915293fc58ab30f895d9a50469a9cb8d3b071 Mon Sep 17 00:00:00 2001 From: David Einstein Date: Thu, 17 Aug 2023 09:27:28 -0400 Subject: [PATCH] Made the tests pass. Made fmpz_mpoly_ctx return a string instead of bytes. --- src/flint/fmpz_mpoly.pyx | 94 +++------------------------------------- src/flint/test/test.py | 5 ++- 2 files changed, 11 insertions(+), 88 deletions(-) diff --git a/src/flint/fmpz_mpoly.pyx b/src/flint/fmpz_mpoly.pyx index 2a07705f..e793a670 100644 --- a/src/flint/fmpz_mpoly.pyx +++ b/src/flint/fmpz_mpoly.pyx @@ -71,9 +71,15 @@ cdef class fmpz_mpoly_ctx: raise ValueError("Unimplemented term order %s" % ordering) self.py_names = tuple(bytes(name, 'utf-8') for name in names) self.c_names = libc.stdlib.malloc(nvars * sizeof(char *)) + self._init = True for i in range(nvars): self.c_names[i] = self.py_names[i] + def __dealloc__(self): + if self._init: + libc.stdlib.free(self.c_names) + self._init = False + cpdef slong nvars(self): return self.val.minfo.nvars @@ -87,7 +93,7 @@ cdef class fmpz_mpoly_ctx: cpdef name(self, slong i): assert i >= 0 and i < self.val.minfo.nvars - return self.py_names[i] + return self.py_names[i].decode('utf-8') cpdef gen(self, slong i): cdef fmpz_mpoly res @@ -237,8 +243,6 @@ cdef class fmpz_mpoly(flint_mpoly): def __hash__(self): s = str(self) -# i = s.index("(nvars") -# s = s[:i] return hash(s) def coefficient(self, slong i): @@ -269,25 +273,6 @@ cdef class fmpz_mpoly(flint_mpoly): libc.stdlib.free(tmp) return res - # @staticmethod - # def gen(slong i, slong nvars=-1, ordering=None): - # cdef fmpz_mpoly res - # assert i >= 0 - # if nvars <= 0: - # nvars = i + 1 - # assert i < nvars - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = get_fmpz_mpoly_context(nvars, ordering) - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_gen(res.val, i, res.ctx.val) - # return res - - # @staticmethod - # def gens(slong n, ordering=None): - # # todo: (i, n)? or just (i)? - # return tuple(fmpz_mpoly.gen(i, n) for i in range(n)) - def repr(self): return self.str() + " (nvars=%s, ordering=%s names=%s)" % (self.ctx.nvars(), self.ctx.ordering(), self.ctx.py_names) @@ -382,39 +367,6 @@ cdef class fmpz_mpoly(flint_mpoly): return -res return NotImplemented - # def __sub__(self, other): - # cdef fmpz_mpoly res - # if typecheck(self, fmpz_mpoly): - # if typecheck(other, fmpz_mpoly): - # if (self).ctx is not (other).ctx: - # ctx, (self, other) = coerce_fmpz_mpolys(self, other) - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (self).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_sub(res.val, (self).val, (other).val, res.ctx.val) - # return res - # else: - # other = any_as_fmpz(other) - # if other is not NotImplemented: - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (self).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_sub_fmpz(res.val, (self).val, (other).val, res.ctx.val) - # return res - # else: - # self = any_as_fmpz(self) - # if self is not NotImplemented: - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (other).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_sub_fmpz(res.val, (other).val, (self).val, res.ctx.val) - # fmpz_mpoly_neg(res.val, res.val, res.ctx.val) - # return res - # return NotImplemented - def __mul__(self, other): cdef fmpz_mpoly res if typecheck(other, fmpz_mpoly): @@ -449,38 +401,6 @@ cdef class fmpz_mpoly(flint_mpoly): return res return NotImplemented - # def __mul__(self, other): - # cdef fmpz_mpoly res - # if typecheck(self, fmpz_mpoly): - # if typecheck(other, fmpz_mpoly): - # if (self).ctx is not (other).ctx: - # ctx, (self, other) = coerce_fmpz_mpolys(self, other) - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (self).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_mul(res.val, (self).val, (other).val, res.ctx.val) - # return res - # else: - # other = any_as_fmpz(other) - # if other is not NotImplemented: - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (self).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_scalar_mul_fmpz(res.val, (self).val, (other).val, res.ctx.val) - # return res - # else: - # self = any_as_fmpz(self) - # if self is not NotImplemented: - # res = fmpz_mpoly.__new__(fmpz_mpoly) - # res.ctx = (other).ctx - # fmpz_mpoly_init(res.val, res.ctx.val) - # res._init = True - # fmpz_mpoly_scalar_mul_fmpz(res.val, (other).val, (self).val, res.ctx.val) - # return res - # return NotImplemented - def __pow__(self, other, modulus): cdef fmpz_mpoly res if modulus is not None: diff --git a/src/flint/test/test.py b/src/flint/test/test.py index 439914a2..7d30a34e 100644 --- a/src/flint/test/test.py +++ b/src/flint/test/test.py @@ -628,7 +628,7 @@ def set_bad(i,j): def test_fmpz_mpoly(): Zp = flint.fmpz_mpoly - getctx = flint.get_fmpz_mpoly_ctx + getctx = flint.get_fmpz_mpoly_context ctx = getctx(4) assert ctx.nvars() == 4 assert ctx.ordering() == "lex" @@ -637,6 +637,8 @@ def test_fmpz_mpoly(): ctx1 = getctx(4, order) assert ctx1.ordering() == order ctx = getctx(4, "lex", 'w,x,y,z') + p1 = ctx.gen(0) + ctx.gen(1) - ctx.gen(2) * ctx.gen(3) + assert p1 == Zp("w + x - y * z", ctx) def test_fmpz_series(): @@ -1581,6 +1583,7 @@ def test_pickling(): test_fmpz_poly_factor, test_fmpz_poly_functions, test_fmpz_mat, + test_fmpz_mpoly, test_fmpz_series, test_fmpq, test_fmpq_poly,