Skip to content

Commit

Permalink
Made the tests pass.
Browse files Browse the repository at this point in the history
Made fmpz_mpoly_ctx return a string instead of bytes.
  • Loading branch information
deinst committed Aug 17, 2023
1 parent c24b2ff commit 0489152
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 88 deletions.
94 changes: 7 additions & 87 deletions src/flint/fmpz_mpoly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <char**>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

Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 (<fmpz_mpoly>self).ctx is not (<fmpz_mpoly>other).ctx:
# ctx, (self, other) = coerce_fmpz_mpolys(self, other)
# res = fmpz_mpoly.__new__(fmpz_mpoly)
# res.ctx = (<fmpz_mpoly>self).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_sub(res.val, (<fmpz_mpoly>self).val, (<fmpz_mpoly>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 = (<fmpz_mpoly>self).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_sub_fmpz(res.val, (<fmpz_mpoly>self).val, (<fmpz>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 = (<fmpz_mpoly>other).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_sub_fmpz(res.val, (<fmpz_mpoly>other).val, (<fmpz>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):
Expand Down Expand Up @@ -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 (<fmpz_mpoly>self).ctx is not (<fmpz_mpoly>other).ctx:
# ctx, (self, other) = coerce_fmpz_mpolys(self, other)
# res = fmpz_mpoly.__new__(fmpz_mpoly)
# res.ctx = (<fmpz_mpoly>self).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_mul(res.val, (<fmpz_mpoly>self).val, (<fmpz_mpoly>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 = (<fmpz_mpoly>self).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_scalar_mul_fmpz(res.val, (<fmpz_mpoly>self).val, (<fmpz>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 = (<fmpz_mpoly>other).ctx
# fmpz_mpoly_init(res.val, res.ctx.val)
# res._init = True
# fmpz_mpoly_scalar_mul_fmpz(res.val, (<fmpz_mpoly>other).val, (<fmpz>self).val, res.ctx.val)
# return res
# return NotImplemented

def __pow__(self, other, modulus):
cdef fmpz_mpoly res
if modulus is not None:
Expand Down
5 changes: 4 additions & 1 deletion src/flint/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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():
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0489152

Please sign in to comment.