Skip to content

Commit da48588

Browse files
Merge pull request #61 from GiacomoPope/introduce-submodules
Introduce submodules into python-flint
2 parents 71bf110 + 644b5bb commit da48588

34 files changed

+407
-213
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
build/*
22
dist/*
3-
src/flint/*.c
3+
src/flint/**/*.c
44
src/flint/*.html
55
doc/build/*
66
fmake*
@@ -16,3 +16,4 @@ MANIFEST
1616
.coverage
1717
*.swp
1818
.python-version
19+
*.DS_Store

setup.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,26 @@
6363
compiler_directives['linetrace'] = True
6464

6565

66-
ext_modules = [
67-
Extension(
68-
"flint._flint", ["src/flint/pyflint.pyx"],
69-
libraries=libraries,
70-
library_dirs=default_lib_dirs,
71-
include_dirs=default_include_dirs,
72-
define_macros=define_macros,
73-
)
66+
ext_files = [
67+
("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module
68+
# Submodules
69+
("flint.flint_base.flint_base", ["src/flint/flint_base/flint_base.pyx"]),
70+
("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]),
71+
7472
]
7573

74+
ext_options = {
75+
"libraries" : libraries,
76+
"library_dirs" : default_lib_dirs,
77+
"include_dirs" : default_include_dirs,
78+
"define_macros" : define_macros,
79+
}
80+
81+
ext_modules = []
82+
for mod_name, src_files in ext_files:
83+
ext = Extension(mod_name, src_files, **ext_options)
84+
ext_modules.append(ext)
85+
7686
for e in ext_modules:
7787
e.cython_directives = {"embedsignature": True}
7888

@@ -92,3 +102,4 @@
92102
author_email='[email protected]',
93103
license='MIT',
94104
classifiers=['Topic :: Scientific/Engineering :: Mathematics'])
105+

src/flint/acb.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from flint.flint_base.flint_base cimport flint_scalar
2+
from flint.flint_base.flint_context cimport getprec
3+
14
cdef int acb_set_python(acb_t x, obj, bint allow_conversion):
25
cdef double re, im
36

src/flint/acb_mat.pyx

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from flint.flint_base.flint_context cimport getprec
2+
3+
from flint.flint_base.flint_base cimport flint_mat
4+
15
cdef acb_mat_coerce_operands(x, y):
26
if isinstance(y, (fmpz_mat, fmpq_mat, arb_mat)):
37
return x, acb_mat(y)

src/flint/acb_poly.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
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
5+
16
cdef acb_poly_coerce_operands(x, y):
27
if isinstance(y, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):
38
return x, acb_poly(y)

src/flint/acb_series.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from flint.flint_base.flint_context cimport getprec, getcap
2+
from flint.flint_base.flint_base cimport flint_series
3+
14
cdef acb_series_coerce_operands(x, y):
25
if isinstance(y, (int, long, float, complex, fmpz, fmpz_poly, fmpz_series, fmpq, fmpq_poly, fmpq_series, arb, arb_poly, arb_series, acb, acb_poly)):
36
return x, acb_series(y)

src/flint/arb.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from cpython.version cimport PY_MAJOR_VERSION
2+
3+
from flint.flint_base.flint_context cimport getprec
4+
from flint.flint_base.flint_base cimport flint_scalar
5+
from flint.utils.conversion cimport chars_from_str, str_from_chars
6+
17
cdef _str_trunc(s, trunc=0):
28
if trunc > 0 and len(s) > 3 * trunc:
39
left = right = trunc

src/flint/arb_mat.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from flint.flint_base.flint_context cimport getprec
2+
from flint.flint_base.flint_base cimport flint_mat
3+
14
cdef arb_mat_coerce_operands(x, y):
25
if isinstance(y, (fmpz_mat, fmpq_mat)):
36
return x, arb_mat(y)

src/flint/arb_poly.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
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
5+
16
cdef arb_poly_coerce_operands(x, y):
27
if isinstance(y, (int, long, float, fmpz, fmpq, arb, fmpz_poly, fmpq_poly)):
38
return x, arb_poly(y)

src/flint/arb_series.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from flint.flint_base.flint_context cimport getprec, getcap
2+
from flint.flint_base.flint_base cimport flint_series
3+
14
cdef arb_series_coerce_operands(x, y):
25
if isinstance(y, (int, long, float, fmpz, fmpz_poly, fmpz_series, fmpq, fmpq_poly, fmpq_series, arb, arb_poly)):
36
return x, arb_series(y)

src/flint/arf.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from flint.flint_base.flint_context cimport getprec
2+
from flint.utils.conversion cimport prec_to_dps
3+
14
cdef class arf:
25

36
cdef arf_t val

src/flint/dirichlet.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from flint.flint_base.flint_context cimport getprec
2+
13
cdef dict _dirichlet_group_cache = {}
24

35
cdef class dirichlet_group(object):

src/flint/flint_base/__init__.py

Whitespace-only changes.

src/flint/flint_base/flint_base.pxd

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cdef class flint_elem:
2+
pass
3+
4+
cdef class flint_scalar(flint_elem):
5+
pass
6+
7+
# TODO:
8+
# See .pyx file
9+
# cdef class flint_poly(flint_elem):
10+
# pass
11+
12+
cdef class flint_mpoly(flint_elem):
13+
pass
14+
15+
cdef class flint_mat(flint_elem):
16+
pass
17+
18+
cdef class flint_series(flint_elem):
19+
pass

src/flint/flint_base/flint_base.pyx

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
from flint.flint_base.flint_context cimport thectx
2+
3+
cdef class flint_elem:
4+
def __repr__(self):
5+
if thectx.pretty:
6+
return self.str()
7+
else:
8+
return self.repr()
9+
10+
def __str__(self):
11+
return self.str()
12+
13+
cdef class flint_scalar(flint_elem):
14+
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)
79+
80+
cdef class flint_mpoly(flint_elem):
81+
"""
82+
Base class for multivariate polynomials.
83+
"""
84+
85+
cdef class flint_series(flint_elem):
86+
"""
87+
Base class for power series.
88+
"""
89+
def __iter__(self):
90+
cdef long i, n
91+
n = self.length()
92+
for i in range(n):
93+
yield self[i]
94+
95+
def coeffs(self):
96+
return list(self)
97+
98+
99+
cdef class flint_mat(flint_elem):
100+
"""
101+
Base class for matrices.
102+
"""
103+
104+
def repr(self):
105+
if thectx.pretty:
106+
return str(self)
107+
# XXX
108+
return "%s(%i, %i, [%s])" % (type(self).__name__,
109+
self.nrows(), self.ncols(), (", ".join(map(str, self.entries()))))
110+
111+
def str(self, *args, **kwargs):
112+
tab = self.table()
113+
if len(tab) == 0 or len(tab[0]) == 0:
114+
return "[]"
115+
tab = [[r.str(*args, **kwargs) for r in row] for row in tab]
116+
widths = []
117+
for i in xrange(len(tab[0])):
118+
w = max([len(row[i]) for row in tab])
119+
widths.append(w)
120+
for i in xrange(len(tab)):
121+
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
122+
tab[i] = "[" + (", ".join(tab[i])) + "]"
123+
return "\n".join(tab)
124+
125+
def entries(self):
126+
cdef long i, j, m, n
127+
m = self.nrows()
128+
n = self.ncols()
129+
L = [None] * (m * n)
130+
for i from 0 <= i < m:
131+
for j from 0 <= j < n:
132+
L[i*n + j] = self[i, j]
133+
return L
134+
135+
def __iter__(self):
136+
cdef long i, j, m, n
137+
m = self.nrows()
138+
n = self.ncols()
139+
for i from 0 <= i < m:
140+
for j from 0 <= j < n:
141+
yield self[i, j]
142+
143+
def table(self):
144+
cdef long i, m, n
145+
m = self.nrows()
146+
n = self.ncols()
147+
L = self.entries()
148+
return [L[i*n : (i+1)*n] for i in range(m)]
149+
150+
# supports mpmath conversions
151+
tolist = table
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flint._flint cimport (
2+
arf_rnd_t,
3+
)
4+
5+
cdef class FlintContext:
6+
cdef public bint pretty
7+
cdef public long _prec
8+
cdef public long _dps
9+
cdef arf_rnd_t rnd
10+
cdef public bint unicode
11+
cdef public long _cap
12+
13+
cdef FlintContext thectx
14+
15+
cdef inline long getprec(long prec=0):
16+
if prec > 1:
17+
return prec
18+
else:
19+
return thectx._prec
20+
21+
cdef inline long getcap(long cap=-1):
22+
if cap >= 0:
23+
return cap
24+
else:
25+
return thectx._cap

0 commit comments

Comments
 (0)