Skip to content

Commit

Permalink
Make flint_poly a global until issue flintlib#62 is resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 18, 2023
1 parent 38c9ffd commit 3ab97f6
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/flint/flint_base/flint_base.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ cdef class flint_elem:
cdef class flint_scalar(flint_elem):
pass

cdef class flint_poly(flint_elem):
pass
# TODO:
# See .pyx file
# cdef class flint_poly(flint_elem):
# pass

cdef class flint_mpoly(flint_elem):
pass
Expand Down
122 changes: 64 additions & 58 deletions src/flint/flint_base/flint_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,70 @@ cdef class flint_elem:

cdef class flint_scalar(flint_elem):
pass

cdef class flint_poly(flint_elem):
"""
Base class for polynomials.
"""

def __iter__(self):
cdef long i, n
n = self.length()
for i in range(n):
yield self[i]

def coeffs(self):
return list(self)

def str(self, bint ascending=False):
"""
Convert to a human-readable string (generic implementation for
all polynomial types).
If *ascending* is *True*, the monomials are output from low degree to
high, otherwise from high to low.
"""
coeffs = [str(c) for c in self]
if not coeffs:
return "0"
s = []
coeffs = enumerate(coeffs)
if not ascending:
coeffs = reversed(list(coeffs))
for i, c in coeffs:
if c == "0":
continue
else:
if c.startswith("-") or (" " in c):
c = "(" + c + ")"
if i == 0:
s.append("%s" % c)
elif i == 1:
if c == "1":
s.append("x")
else:
s.append("%s*x" % c)
else:
if c == "1":
s.append("x^%s" % i)
else:
s.append("%s*x^%s" % (c, i))
return " + ".join(s)

# TODO: why is this template class defining something for
# acb_poly??
# def roots(self, **kwargs):
# """
# Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
# for details.
# """
# return acb_poly(self).roots(**kwargs)

# TODO:
# We cannot include this class until we can import
# acb_poly, so for now we leave this class as a global
# inside pyflint.pyx
#
# cdef class flint_poly(flint_elem):
# """
# Base class for polynomials.
# """

# def __iter__(self):
# cdef long i, n
# n = self.length()
# for i in range(n):
# yield self[i]

# def coeffs(self):
# return list(self)

# def str(self, bint ascending=False):
# """
# Convert to a human-readable string (generic implementation for
# all polynomial types).

# If *ascending* is *True*, the monomials are output from low degree to
# high, otherwise from high to low.
# """
# coeffs = [str(c) for c in self]
# if not coeffs:
# return "0"
# s = []
# coeffs = enumerate(coeffs)
# if not ascending:
# coeffs = reversed(list(coeffs))
# for i, c in coeffs:
# if c == "0":
# continue
# else:
# if c.startswith("-") or (" " in c):
# c = "(" + c + ")"
# if i == 0:
# s.append("%s" % c)
# elif i == 1:
# if c == "1":
# s.append("x")
# else:
# s.append("%s*x" % c)
# else:
# if c == "1":
# s.append("x^%s" % i)
# else:
# s.append("%s*x^%s" % (c, i))
# return " + ".join(s)

# def roots(self, **kwargs):
# """
# Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
# for details.
# """
# # TODO:
# # To avoid circular imports, we import within the method
# from XXX.XXX.acb_poly import acb_poly
# return acb_poly(self).roots(**kwargs)

cdef class flint_mpoly(flint_elem):
"""
Expand Down
63 changes: 63 additions & 0 deletions src/flint/pyflint.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,69 @@ DEF FMPZ_TMP = 2

ctx = thectx

# TODO:
# This should be factored out into flint_base
# but we cannot do this until we can import
# acb_poly to allow the roots() method to remain

from flint.flint_base.flint_base cimport flint_elem

cdef class flint_poly(flint_elem):
"""
Base class for polynomials.
"""

def __iter__(self):
cdef long i, n
n = self.length()
for i in range(n):
yield self[i]

def coeffs(self):
return list(self)

def str(self, bint ascending=False):
"""
Convert to a human-readable string (generic implementation for
all polynomial types).
If *ascending* is *True*, the monomials are output from low degree to
high, otherwise from high to low.
"""
coeffs = [str(c) for c in self]
if not coeffs:
return "0"
s = []
coeffs = enumerate(coeffs)
if not ascending:
coeffs = reversed(list(coeffs))
for i, c in coeffs:
if c == "0":
continue
else:
if c.startswith("-") or (" " in c):
c = "(" + c + ")"
if i == 0:
s.append("%s" % c)
elif i == 1:
if c == "1":
s.append("x")
else:
s.append("%s*x" % c)
else:
if c == "1":
s.append("x^%s" % i)
else:
s.append("%s*x^%s" % (c, i))
return " + ".join(s)

def roots(self, **kwargs):
"""
Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
for details.
"""
return acb_poly(self).roots(**kwargs)

include "fmpz.pyx"
include "fmpz_poly.pyx"
include "fmpz_mpoly.pyx"
Expand Down

0 comments on commit 3ab97f6

Please sign in to comment.