Skip to content

Commit bf9d0d1

Browse files
Merge pull request #63 from GiacomoPope/introduce-submodules
Finish the `flint_base` submodule, depreciate old roots and factor out more utils
2 parents da48588 + f43b291 commit bf9d0d1

26 files changed

+103
-150
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ To do
122122
* Improved printing and string input/output
123123
* IPython hooks (TeX pretty-printing etc.)
124124

125+
CHANGELOG
126+
-------------
127+
128+
0.5.0
129+
130+
- gh-63: The `roots` method of `arb_poly`, and `nmod_poly` is no longer supported. Use `acb_roots(p).roots()` to get the old behaviour of returning the roots as `acb`. Note that the `roots` method of `fmpz_poly` and `fmpq_poly` currently returns the complex roots of the polynomial.
131+
125132
License
126133
------------
127134

src/flint/acb.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_base cimport flint_scalar
23
from flint.flint_base.flint_context cimport getprec
34

src/flint/acb_mat.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec
2-
33
from flint.flint_base.flint_base cimport flint_mat
44

55
cdef acb_mat_coerce_operands(x, y):

src/flint/acb_poly.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec
2-
# TODO: waiting for fix on the roots method, currently
3-
# globally defined.
4-
# from flint.flint_base.flint_base cimport flint_poly
3+
from flint.flint_base.flint_base cimport flint_poly
54

65
cdef acb_poly_coerce_operands(x, y):
76
if isinstance(y, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):

src/flint/acb_series.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec, getcap
23
from flint.flint_base.flint_base cimport flint_series
34

src/flint/arb.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from cpython.version cimport PY_MAJOR_VERSION
22

33
from flint.flint_base.flint_context cimport getprec
44
from flint.flint_base.flint_base cimport flint_scalar
5+
from flint.utils.typecheck cimport typecheck
56
from flint.utils.conversion cimport chars_from_str, str_from_chars
67

78
cdef _str_trunc(s, trunc=0):

src/flint/arb_mat.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec
23
from flint.flint_base.flint_base cimport flint_mat
34

src/flint/arb_poly.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec
2-
# TODO: waiting for fix on the roots method, currently
3-
# globally defined.
4-
# from flint.flint_base.flint_base cimport flint_poly
3+
from flint.flint_base.flint_base cimport flint_poly
54

65
cdef arb_poly_coerce_operands(x, y):
76
if isinstance(y, (int, long, float, fmpz, fmpq, arb, fmpz_poly, fmpq_poly)):

src/flint/arb_series.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_context cimport getprec, getcap
23
from flint.flint_base.flint_base cimport flint_series
34

src/flint/arf.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_context cimport getprec
2+
from flint.utils.typecheck cimport typecheck
23
from flint.utils.conversion cimport prec_to_dps
34

45
cdef class arf:

src/flint/flint_base/flint_base.pxd

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ cdef class flint_elem:
44
cdef class flint_scalar(flint_elem):
55
pass
66

7-
# TODO:
8-
# See .pyx file
9-
# cdef class flint_poly(flint_elem):
10-
# pass
7+
cdef class flint_poly(flint_elem):
8+
pass
119

1210
cdef class flint_mpoly(flint_elem):
1311
pass

src/flint/flint_base/flint_base.pyx

+64-64
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,78 @@ cdef class flint_elem:
1010
def __str__(self):
1111
return self.str()
1212

13+
1314
cdef class flint_scalar(flint_elem):
1415
pass
15-
16-
# TODO:
17-
# We cannot include this class until we can import
18-
# acb_poly, so for now we leave this class as a global
19-
# inside pyflint.pyx
20-
#
21-
# cdef class flint_poly(flint_elem):
22-
# """
23-
# Base class for polynomials.
24-
# """
25-
26-
# def __iter__(self):
27-
# cdef long i, n
28-
# n = self.length()
29-
# for i in range(n):
30-
# yield self[i]
31-
32-
# def coeffs(self):
33-
# return list(self)
34-
35-
# def str(self, bint ascending=False):
36-
# """
37-
# Convert to a human-readable string (generic implementation for
38-
# all polynomial types).
39-
40-
# If *ascending* is *True*, the monomials are output from low degree to
41-
# high, otherwise from high to low.
42-
# """
43-
# coeffs = [str(c) for c in self]
44-
# if not coeffs:
45-
# return "0"
46-
# s = []
47-
# coeffs = enumerate(coeffs)
48-
# if not ascending:
49-
# coeffs = reversed(list(coeffs))
50-
# for i, c in coeffs:
51-
# if c == "0":
52-
# continue
53-
# else:
54-
# if c.startswith("-") or (" " in c):
55-
# c = "(" + c + ")"
56-
# if i == 0:
57-
# s.append("%s" % c)
58-
# elif i == 1:
59-
# if c == "1":
60-
# s.append("x")
61-
# else:
62-
# s.append("%s*x" % c)
63-
# else:
64-
# if c == "1":
65-
# s.append("x^%s" % i)
66-
# else:
67-
# s.append("%s*x^%s" % (c, i))
68-
# return " + ".join(s)
69-
70-
# def roots(self, **kwargs):
71-
# """
72-
# Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
73-
# for details.
74-
# """
75-
# # TODO:
76-
# # To avoid circular imports, we import within the method
77-
# from XXX.XXX.acb_poly import acb_poly
78-
# return acb_poly(self).roots(**kwargs)
16+
17+
18+
cdef class flint_poly(flint_elem):
19+
"""
20+
Base class for polynomials.
21+
"""
22+
23+
def __iter__(self):
24+
cdef long i, n
25+
n = self.length()
26+
for i in range(n):
27+
yield self[i]
28+
29+
def coeffs(self):
30+
return list(self)
31+
32+
def str(self, bint ascending=False):
33+
"""
34+
Convert to a human-readable string (generic implementation for
35+
all polynomial types).
36+
37+
If *ascending* is *True*, the monomials are output from low degree to
38+
high, otherwise from high to low.
39+
"""
40+
coeffs = [str(c) for c in self]
41+
if not coeffs:
42+
return "0"
43+
s = []
44+
coeffs = enumerate(coeffs)
45+
if not ascending:
46+
coeffs = reversed(list(coeffs))
47+
for i, c in coeffs:
48+
if c == "0":
49+
continue
50+
else:
51+
if c.startswith("-") or (" " in c):
52+
c = "(" + c + ")"
53+
if i == 0:
54+
s.append("%s" % c)
55+
elif i == 1:
56+
if c == "1":
57+
s.append("x")
58+
else:
59+
s.append("%s*x" % c)
60+
else:
61+
if c == "1":
62+
s.append("x^%s" % i)
63+
else:
64+
s.append("%s*x^%s" % (c, i))
65+
return " + ".join(s)
66+
67+
def roots(self):
68+
"""
69+
Deprecated function.
70+
71+
To recover roots of a polynomial, first convert to acb:
72+
73+
acb_poly(input_poly).roots()
74+
"""
75+
raise NotImplementedError('This method is no longer supported. To recover the complex roots first convert to acb_poly')
76+
77+
7978

8079
cdef class flint_mpoly(flint_elem):
8180
"""
8281
Base class for multivariate polynomials.
8382
"""
8483

84+
8585
cdef class flint_series(flint_elem):
8686
"""
8787
Base class for power series.

src/flint/fmpq.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_base cimport flint_scalar
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef any_as_fmpq(obj):
45
if typecheck(obj, fmpq):

src/flint/fmpq_mat.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_base cimport flint_mat
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef any_as_fmpq_mat(obj):
45
if typecheck(obj, fmpq_mat):

src/flint/fmpq_poly.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# TODO: waiting for fix on the roots method, currently
2-
# globally defined.
3-
# from flint.flint_base.flint_base cimport flint_poly
1+
from flint.utils.typecheck cimport typecheck
2+
from flint.flint_base.flint_base cimport flint_poly
43

54
cdef any_as_fmpq_poly(obj):
65
if typecheck(obj, fmpq_poly):

src/flint/fmpq_series.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_base cimport flint_series
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef fmpq_series_coerce_operands(x, y):
45
if isinstance(y, (int, long, fmpz, fmpz_poly, fmpz_series, fmpq, fmpq_poly)):

src/flint/fmpz.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from cpython.version cimport PY_MAJOR_VERSION
22

33
from flint.flint_base.flint_base cimport flint_scalar
4+
from flint.utils.typecheck cimport typecheck
45
from flint.utils.conversion cimport chars_from_str
56

67
cdef inline int fmpz_set_pylong(fmpz_t x, obj):

src/flint/fmpz_mat.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_base cimport flint_mat
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef any_as_fmpz_mat(obj):
45
if typecheck(obj, fmpz_mat):

src/flint/fmpz_mpoly.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from cpython.version cimport PY_MAJOR_VERSION
22

33
from flint.utils.conversion cimport str_from_chars
4+
from flint.utils.typecheck cimport typecheck
45
from flint.flint_base.flint_base cimport flint_mpoly
56

67
cdef any_as_fmpz_mpoly(x):

src/flint/fmpz_poly.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from cpython.version cimport PY_MAJOR_VERSION
22

33
from flint.flint_base.flint_context cimport getprec
4-
# TODO: waiting for fix on the roots method, currently
5-
# globally defined.
6-
# from flint.flint_base.flint_base cimport flint_poly
4+
from flint.flint_base.flint_base cimport flint_poly
5+
from flint.utils.typecheck cimport typecheck
76

87
cdef any_as_fmpz_poly(x):
98
cdef fmpz_poly res

src/flint/fmpz_series.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from flint.utils.typecheck cimport typecheck
12
from flint.flint_base.flint_base cimport flint_series
23

34
cdef fmpz_series_coerce_operands(x, y):

src/flint/nmod.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.flint_base.flint_base cimport flint_scalar
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef int any_as_nmod(mp_limb_t * val, obj, nmod_t mod) except -1:
45
cdef int success

src/flint/nmod_mat.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from flint.utils.conversion cimport matrix_to_str
2+
from flint.utils.typecheck cimport typecheck
23

34
cdef any_as_nmod_mat(obj, nmod_t mod):
45
cdef nmod_mat r

src/flint/nmod_poly.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# TODO: waiting for fix on the roots method, currently
2-
# globally defined.
3-
# from flint.flint_base.flint_base cimport flint_poly
1+
from flint.flint_base.flint_base cimport flint_poly
2+
from flint.utils.typecheck cimport typecheck
43

54
cdef any_as_nmod_poly(obj, nmod_t mod):
65
cdef nmod_poly r

src/flint/pyflint.pyx

-66
Original file line numberDiff line numberDiff line change
@@ -24,78 +24,12 @@ cdef extern from "Python.h":
2424
double PyComplex_RealAsDouble(PyObject *op)
2525
double PyComplex_ImagAsDouble(PyObject *op)
2626

27-
cdef inline bint typecheck(object ob, object tp):
28-
return PyObject_TypeCheck(ob, <PyTypeObject*>tp)
29-
3027
DEF FMPZ_UNKNOWN = 0
3128
DEF FMPZ_REF = 1
3229
DEF FMPZ_TMP = 2
3330

3431
ctx = thectx
3532

36-
# TODO:
37-
# This should be factored out into flint_base
38-
# but we cannot do this until we can import
39-
# acb_poly to allow the roots() method to remain
40-
41-
from flint.flint_base.flint_base cimport flint_elem
42-
43-
cdef class flint_poly(flint_elem):
44-
"""
45-
Base class for polynomials.
46-
"""
47-
48-
def __iter__(self):
49-
cdef long i, n
50-
n = self.length()
51-
for i in range(n):
52-
yield self[i]
53-
54-
def coeffs(self):
55-
return list(self)
56-
57-
def str(self, bint ascending=False):
58-
"""
59-
Convert to a human-readable string (generic implementation for
60-
all polynomial types).
61-
62-
If *ascending* is *True*, the monomials are output from low degree to
63-
high, otherwise from high to low.
64-
"""
65-
coeffs = [str(c) for c in self]
66-
if not coeffs:
67-
return "0"
68-
s = []
69-
coeffs = enumerate(coeffs)
70-
if not ascending:
71-
coeffs = reversed(list(coeffs))
72-
for i, c in coeffs:
73-
if c == "0":
74-
continue
75-
else:
76-
if c.startswith("-") or (" " in c):
77-
c = "(" + c + ")"
78-
if i == 0:
79-
s.append("%s" % c)
80-
elif i == 1:
81-
if c == "1":
82-
s.append("x")
83-
else:
84-
s.append("%s*x" % c)
85-
else:
86-
if c == "1":
87-
s.append("x^%s" % i)
88-
else:
89-
s.append("%s*x^%s" % (c, i))
90-
return " + ".join(s)
91-
92-
def roots(self, **kwargs):
93-
"""
94-
Isolates the complex roots of *self*. See :meth:`.acb_poly.roots`
95-
for details.
96-
"""
97-
return acb_poly(self).roots(**kwargs)
98-
9933
include "fmpz.pyx"
10034
include "fmpz_poly.pyx"
10135
include "fmpz_mpoly.pyx"

0 commit comments

Comments
 (0)