Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 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
15 changes: 12 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@
library_dirs=default_lib_dirs,
include_dirs=default_include_dirs,
define_macros=define_macros,
)
),
Extension(
"flint.flint_base.flint_context",
["src/flint/flint_base/flint_context.pyx"],
libraries=libraries,
library_dirs=default_lib_dirs,
include_dirs=default_include_dirs,
define_macros=define_macros,
),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to factor this out like:

ext_files = [
    ('"flint._flint", ["src/flint/pyflint.pyx"]),
    ...
]

ext_options = {
    'libraries': libraries,
    ...
}

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

Then it is easier to add to the ext_files list to make more extension modules and we don't need to duplicate the arguments in ext_options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, addressed in commit f7c3bfc

]

for e in ext_modules:
Expand All @@ -86,9 +94,10 @@
description='Bindings for FLINT and Arb',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
version='0.4.1',
url='https://github.com/python-flint/python-flint',
version='0.4.2',
url='https://github.com/flintlib/python-flint',
author='Fredrik Johansson',
author_email='[email protected]',
license='MIT',
classifiers=['Topic :: Scientific/Engineering :: Mathematics'])

3 changes: 3 additions & 0 deletions src/flint/arb.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from cpython.version cimport PY_MAJOR_VERSION
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
2 changes: 2 additions & 0 deletions src/flint/arf.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .utils.conversion cimport prec_to_dps

cdef class arf:

cdef arf_t val
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions src/flint/flint_base/flint_context.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
70 changes: 70 additions & 0 deletions src/flint/flint_base/flint_context.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from flint._flint cimport (
ARF_RND_DOWN,
flint_cleanup,
flint_get_num_threads,
flint_set_num_threads
)
from flint.utils.conversion cimport prec_to_dps, dps_to_prec

cdef class FlintContext:
def __init__(self):
self.default()

def default(self):
self.pretty = True
self.rnd = ARF_RND_DOWN
self.prec = 53
self.unicode = False
self.threads = 1
self.cap = 10

@property
def prec(self):
return self._prec

@prec.setter
def prec(self, prec):
cdef long cprec = prec
if cprec < 2:
raise ValueError("prec must be >= 2")
self._prec = cprec
self._dps = prec_to_dps(cprec)

@property
def dps(self):
return self._dps

@dps.setter
def dps(self, prec):
self.prec = dps_to_prec(prec)

@property
def cap(self):
return self._cap

@cap.setter
def cap(self, long cap):
if cap < 0:
raise ValueError("cap must be >= 0")
self._cap = cap

@property
def threads(self):
return flint_get_num_threads()

@threads.setter
def threads(self, long num):
assert num >= 1 and num <= 64
flint_set_num_threads(num)

def __repr__(self):
return "pretty = %-8s # pretty-print repr() output\n" \
"unicode = %-8s # use unicode characters in output\n" \
"prec = %-8s # real/complex precision (in bits)\n" \
"dps = %-8s # real/complex precision (in digits)\n" \
"cap = %-8s # power series precision\n" \
"threads = %-8s # max number of threads used internally\n" % \
(self.pretty, self.unicode, self.prec, self.dps, self.cap, self.threads)

def cleanup(self):
flint_cleanup()
3 changes: 3 additions & 0 deletions src/flint/fmpz.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from cpython.version cimport PY_MAJOR_VERSION
from flint.utils.conversion cimport chars_from_str

cdef inline int fmpz_set_pylong(fmpz_t x, obj):
cdef int overflow
cdef slong longval
Expand Down
3 changes: 3 additions & 0 deletions src/flint/fmpz_mpoly.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from cpython.version cimport PY_MAJOR_VERSION
from flint.utils.conversion cimport str_from_chars

cdef any_as_fmpz_mpoly(x):
cdef fmpz_mpoly res
"""
Expand Down
2 changes: 2 additions & 0 deletions src/flint/fmpz_poly.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from cpython.version cimport PY_MAJOR_VERSION

cdef any_as_fmpz_poly(x):
cdef fmpz_poly res
if typecheck(x, fmpz_poly):
Expand Down
2 changes: 2 additions & 0 deletions src/flint/functions.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .utils.conversion cimport dps_to_prec

# xxx: this doesn't work when changed to a cdef function. why?
def __goodness(x, bint parts=True, metric=None):
if metric is not None:
Expand Down
2 changes: 2 additions & 0 deletions src/flint/nmod_mat.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from flint.utils.conversion cimport matrix_to_str

cdef any_as_nmod_mat(obj, nmod_t mod):
cdef nmod_mat r
cdef mp_limb_t v
Expand Down
106 changes: 2 additions & 104 deletions src/flint/pyflint.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ cimport flint
cimport libc.stdlib
cimport cython

from flint.flint_base.flint_context cimport FlintContext

cdef flint_rand_t global_random_state
flint_randinit(global_random_state)

Expand All @@ -22,117 +24,13 @@ cdef extern from "Python.h":
double PyComplex_RealAsDouble(PyObject *op)
double PyComplex_ImagAsDouble(PyObject *op)

from cpython.version cimport PY_MAJOR_VERSION

cdef chars_from_str(s):
if PY_MAJOR_VERSION < 3:
return s
else:
return s.encode('ascii')

cdef str_from_chars(s):
if PY_MAJOR_VERSION < 3:
return str(s)
else:
return bytes(s).decode('ascii')

cdef matrix_to_str(tab):
if len(tab) == 0 or len(tab[0]) == 0:
return "[]"
tab = [[str(c) for c 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)

cdef inline bint typecheck(object ob, object tp):
return PyObject_TypeCheck(ob, <PyTypeObject*>tp)

DEF FMPZ_UNKNOWN = 0
DEF FMPZ_REF = 1
DEF FMPZ_TMP = 2


cdef long prec_to_dps(n):
return max(1, int(round(int(n)/3.3219280948873626)-1))

cdef long dps_to_prec(n):
return max(1, int(round((int(n)+1)*3.3219280948873626)))

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

def __init__(self):
self.default()

def default(self):
self.pretty = True
self.rnd = ARF_RND_DOWN
self.prec = 53
self.unicode = False
self.threads = 1
self.cap = 10

property prec:

def __set__(self, prec):
cdef long cprec = prec
if cprec < 2:
raise ValueError("prec must be >= 2")
self._prec = cprec
self._dps = prec_to_dps(cprec)

def __get__(self):
return self._prec

property dps:

def __set__(self, prec):
self.prec = dps_to_prec(prec)

def __get__(self):
return self._dps

property cap:

def __set__(self, long cap):
if cap < 0:
raise ValueError("cap must be >= 0")
self._cap = cap

def __get__(self):
return self._cap

property threads:

def __set__(self, long num):
assert num >= 1 and num <= 64
flint_set_num_threads(num)

def __get__(self):
return flint_get_num_threads()

def __repr__(self):
return "pretty = %-8s # pretty-print repr() output\n" \
"unicode = %-8s # use unicode characters in output\n" \
"prec = %-8s # real/complex precision (in bits)\n" \
"dps = %-8s # real/complex precision (in digits)\n" \
"cap = %-8s # power series precision\n" \
"threads = %-8s # max number of threads used internally\n" % \
(self.pretty, self.unicode, self.prec, self.dps, self.cap, self.threads)

def cleanup(self):
flint_cleanup()

cdef FlintContext thectx = FlintContext()

ctx = thectx
Expand Down
Empty file added src/flint/utils/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions src/flint/utils/conversion.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from cpython.version cimport PY_MAJOR_VERSION

cdef inline long prec_to_dps(n):
return max(1, int(round(int(n)/3.3219280948873626)-1))

cdef inline long dps_to_prec(n):
return max(1, int(round((int(n)+1)*3.3219280948873626)))

cdef inline chars_from_str(s):
if PY_MAJOR_VERSION < 3:
return s
else:
return s.encode('ascii')

cdef inline str_from_chars(s):
if PY_MAJOR_VERSION < 3:
return str(s)
else:
return bytes(s).decode('ascii')

cdef inline matrix_to_str(tab):
if len(tab) == 0 or len(tab[0]) == 0:
return "[]"
tab = [[str(c) for c 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)