Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce submodules into python-flint #61

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
aade052
update imports to include utils
GiacomoPope Aug 17, 2023
5ffb83e
refactor out some util functions from pyflint
GiacomoPope Aug 17, 2023
a4511c9
Refactor out context class from pyflint
GiacomoPope Aug 17, 2023
304dd67
remove some globals from pyflint
GiacomoPope Aug 17, 2023
b5b1831
include DS_Store to gitignore
GiacomoPope Aug 17, 2023
06064b0
EOF
GiacomoPope Aug 17, 2023
f36b681
Add submodule
GiacomoPope Aug 17, 2023
7b7d2d2
EOF
GiacomoPope Aug 17, 2023
622c671
Reverse old formatting
GiacomoPope Aug 17, 2023
2dee3db
Fix relative imports and now get a proper error
GiacomoPope Aug 17, 2023
0f5d236
Factor out into submodule
GiacomoPope Aug 18, 2023
726a699
ensure submodule c files are skipped
GiacomoPope Aug 18, 2023
b3e5ec9
Refactor out more conversions
GiacomoPope Aug 18, 2023
b8be99b
move away from depreciated properties
GiacomoPope Aug 18, 2023
f7c3bfc
Refactor set up for inclusion of submodules
GiacomoPope Aug 18, 2023
4e5310b
move context into new submodule
GiacomoPope Aug 18, 2023
4d7ad87
Import thectx from module and set ctx
GiacomoPope Aug 18, 2023
039e687
Import precision / cap inline functions previously defined as global
GiacomoPope Aug 18, 2023
e76daf4
Global context need not be an extension, as its not defining a new class
GiacomoPope Aug 18, 2023
f09b791
remove template classes
GiacomoPope Aug 18, 2023
522c50a
Add templates as a submodule
GiacomoPope Aug 18, 2023
8b1190f
delete unneeded file
GiacomoPope Aug 18, 2023
718b1fc
Include needed classes for each flint type
GiacomoPope Aug 18, 2023
38c9ffd
Actually include the new files
GiacomoPope Aug 18, 2023
3ab97f6
Make flint_poly a global until issue #62 is resolved
GiacomoPope Aug 18, 2023
a4d3a85
Remove imports until they're ready
GiacomoPope Aug 18, 2023
9a3fd0b
remove old pxd
GiacomoPope Aug 18, 2023
457f429
remove old pxd
GiacomoPope Aug 18, 2023
4a16f0e
Update imports
GiacomoPope Aug 18, 2023
644b5bb
Add all global context into this file
GiacomoPope Aug 18, 2023
da48588
Merge pull request #61 from GiacomoPope/introduce-submodules
oscarbenjamin Aug 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build/*
dist/*
src/flint/*.c
src/flint/**/*.c
src/flint/*.html
doc/build/*
fmake*
Expand All @@ -16,3 +16,4 @@ MANIFEST
.coverage
*.swp
.python-version
*.DS_Store
27 changes: 19 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,26 @@
compiler_directives['linetrace'] = True


ext_modules = [
Extension(
"flint._flint", ["src/flint/pyflint.pyx"],
libraries=libraries,
library_dirs=default_lib_dirs,
include_dirs=default_include_dirs,
define_macros=define_macros,
)
ext_files = [
("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module
# Submodules
("flint.flint_base.flint_base", ["src/flint/flint_base/flint_base.pyx"]),
("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]),

]

ext_options = {
"libraries" : libraries,
"library_dirs" : default_lib_dirs,
"include_dirs" : default_include_dirs,
"define_macros" : define_macros,
}

ext_modules = []
for mod_name, src_files in ext_files:
ext = Extension(mod_name, src_files, **ext_options)
ext_modules.append(ext)

for e in ext_modules:
e.cython_directives = {"embedsignature": True}

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

3 changes: 3 additions & 0 deletions src/flint/acb.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from flint.flint_base.flint_base cimport flint_scalar
from flint.flint_base.flint_context cimport getprec

cdef int acb_set_python(acb_t x, obj, bint allow_conversion):
cdef double re, im

Expand Down
4 changes: 4 additions & 0 deletions src/flint/acb_mat.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from flint.flint_base.flint_context cimport getprec

from flint.flint_base.flint_base cimport flint_mat

cdef acb_mat_coerce_operands(x, y):
if isinstance(y, (fmpz_mat, fmpq_mat, arb_mat)):
return x, acb_mat(y)
Expand Down
5 changes: 5 additions & 0 deletions src/flint/acb_poly.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from flint.flint_base.flint_context cimport getprec
# TODO: waiting for fix on the roots method, currently
# globally defined.
# from flint.flint_base.flint_base cimport flint_poly

cdef acb_poly_coerce_operands(x, y):
if isinstance(y, (int, long, float, complex, fmpz, fmpq, arb, acb, fmpz_poly, fmpq_poly, arb_poly)):
return x, acb_poly(y)
Expand Down
3 changes: 3 additions & 0 deletions src/flint/acb_series.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from flint.flint_base.flint_context cimport getprec, getcap
from flint.flint_base.flint_base cimport flint_series

cdef acb_series_coerce_operands(x, y):
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)):
return x, acb_series(y)
Expand Down
6 changes: 6 additions & 0 deletions src/flint/arb.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from cpython.version cimport PY_MAJOR_VERSION

from flint.flint_base.flint_context cimport getprec
from flint.flint_base.flint_base cimport flint_scalar
from flint.utils.conversion cimport chars_from_str, str_from_chars

cdef _str_trunc(s, trunc=0):
if trunc > 0 and len(s) > 3 * trunc:
left = right = trunc
Expand Down
3 changes: 3 additions & 0 deletions src/flint/arb_mat.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from flint.flint_base.flint_context cimport getprec
from flint.flint_base.flint_base cimport flint_mat

cdef arb_mat_coerce_operands(x, y):
if isinstance(y, (fmpz_mat, fmpq_mat)):
return x, arb_mat(y)
Expand Down
5 changes: 5 additions & 0 deletions src/flint/arb_poly.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from flint.flint_base.flint_context cimport getprec
# TODO: waiting for fix on the roots method, currently
# globally defined.
# from flint.flint_base.flint_base cimport flint_poly

cdef arb_poly_coerce_operands(x, y):
if isinstance(y, (int, long, float, fmpz, fmpq, arb, fmpz_poly, fmpq_poly)):
return x, arb_poly(y)
Expand Down
3 changes: 3 additions & 0 deletions src/flint/arb_series.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from flint.flint_base.flint_context cimport getprec, getcap
from flint.flint_base.flint_base cimport flint_series

cdef arb_series_coerce_operands(x, y):
if isinstance(y, (int, long, float, fmpz, fmpz_poly, fmpz_series, fmpq, fmpq_poly, fmpq_series, arb, arb_poly)):
return x, arb_series(y)
Expand Down
3 changes: 3 additions & 0 deletions src/flint/arf.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from flint.flint_base.flint_context cimport getprec
from flint.utils.conversion cimport prec_to_dps

cdef class arf:

cdef arf_t val
Expand Down
2 changes: 2 additions & 0 deletions src/flint/dirichlet.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from flint.flint_base.flint_context cimport getprec

cdef dict _dirichlet_group_cache = {}

cdef class dirichlet_group(object):
Expand Down
Empty file.
19 changes: 19 additions & 0 deletions src/flint/flint_base/flint_base.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cdef class flint_elem:
pass

cdef class flint_scalar(flint_elem):
pass

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

cdef class flint_mpoly(flint_elem):
pass

cdef class flint_mat(flint_elem):
pass

cdef class flint_series(flint_elem):
pass
151 changes: 151 additions & 0 deletions src/flint/flint_base/flint_base.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from flint.flint_base.flint_context cimport thectx

cdef class flint_elem:
def __repr__(self):
if thectx.pretty:
return self.str()
else:
return self.repr()

def __str__(self):
return self.str()

cdef class flint_scalar(flint_elem):
pass

# 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):
"""
Base class for multivariate polynomials.
"""

cdef class flint_series(flint_elem):
"""
Base class for power series.
"""
def __iter__(self):
cdef long i, n
n = self.length()
for i in range(n):
yield self[i]

def coeffs(self):
return list(self)


cdef class flint_mat(flint_elem):
"""
Base class for matrices.
"""

def repr(self):
if thectx.pretty:
return str(self)
# XXX
return "%s(%i, %i, [%s])" % (type(self).__name__,
self.nrows(), self.ncols(), (", ".join(map(str, self.entries()))))

def str(self, *args, **kwargs):
tab = self.table()
if len(tab) == 0 or len(tab[0]) == 0:
return "[]"
tab = [[r.str(*args, **kwargs) for r in row] for row in tab]
widths = []
for i in xrange(len(tab[0])):
w = max([len(row[i]) for row in tab])
widths.append(w)
for i in xrange(len(tab)):
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
tab[i] = "[" + (", ".join(tab[i])) + "]"
return "\n".join(tab)

def entries(self):
cdef long i, j, m, n
m = self.nrows()
n = self.ncols()
L = [None] * (m * n)
for i from 0 <= i < m:
for j from 0 <= j < n:
L[i*n + j] = self[i, j]
return L

def __iter__(self):
cdef long i, j, m, n
m = self.nrows()
n = self.ncols()
for i from 0 <= i < m:
for j from 0 <= j < n:
yield self[i, j]

def table(self):
cdef long i, m, n
m = self.nrows()
n = self.ncols()
L = self.entries()
return [L[i*n : (i+1)*n] for i in range(m)]

# supports mpmath conversions
tolist = table
25 changes: 25 additions & 0 deletions src/flint/flint_base/flint_context.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from flint._flint cimport (
arf_rnd_t,
)

cdef class FlintContext:
cdef public bint pretty
cdef public long _prec
cdef public long _dps
cdef arf_rnd_t rnd
cdef public bint unicode
cdef public long _cap

cdef FlintContext thectx

cdef inline long getprec(long prec=0):
if prec > 1:
return prec
else:
return thectx._prec

cdef inline long getcap(long cap=-1):
if cap >= 0:
return cap
else:
return thectx._cap
Loading