From aade0523aa0044e07d47d0fb2abe74d1f095daa3 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 22:50:43 +0100 Subject: [PATCH 01/30] update imports to include utils --- src/flint/arf.pyx | 2 ++ src/flint/functions.pyx | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/flint/arf.pyx b/src/flint/arf.pyx index 9790218c..ba2b0391 100644 --- a/src/flint/arf.pyx +++ b/src/flint/arf.pyx @@ -1,3 +1,5 @@ +from .utils.conversion cimport prec_to_dps + cdef class arf: cdef arf_t val diff --git a/src/flint/functions.pyx b/src/flint/functions.pyx index b13f40e7..7bb8e6a4 100644 --- a/src/flint/functions.pyx +++ b/src/flint/functions.pyx @@ -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: From 5ffb83ee4844d4a6e23501ed98a05c5a696a39e3 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 22:51:00 +0100 Subject: [PATCH 02/30] refactor out some util functions from pyflint --- src/flint/utils/__init__.py | 0 src/flint/utils/conversion.pxd | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 src/flint/utils/__init__.py create mode 100644 src/flint/utils/conversion.pxd diff --git a/src/flint/utils/__init__.py b/src/flint/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/flint/utils/conversion.pxd b/src/flint/utils/conversion.pxd new file mode 100644 index 00000000..bee04416 --- /dev/null +++ b/src/flint/utils/conversion.pxd @@ -0,0 +1,5 @@ +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))) \ No newline at end of file From a4511c98b51c9085f1398c30d787c2cf9750cf73 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 22:51:29 +0100 Subject: [PATCH 03/30] Refactor out context class from pyflint --- src/flint/flint_base/__init__.py | 0 src/flint/flint_base/flint_context.pxd | 2 + src/flint/flint_base/flint_context.pyx | 78 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/flint/flint_base/__init__.py create mode 100644 src/flint/flint_base/flint_context.pxd create mode 100644 src/flint/flint_base/flint_context.pyx diff --git a/src/flint/flint_base/__init__.py b/src/flint/flint_base/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/flint/flint_base/flint_context.pxd b/src/flint/flint_base/flint_context.pxd new file mode 100644 index 00000000..4725ec71 --- /dev/null +++ b/src/flint/flint_base/flint_context.pxd @@ -0,0 +1,2 @@ +cdef class FlintContext: + pass \ No newline at end of file diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx new file mode 100644 index 00000000..7399d4fe --- /dev/null +++ b/src/flint/flint_base/flint_context.pyx @@ -0,0 +1,78 @@ +from ._flint cimport ( + ARF_RND_DOWN, + arf_rnd_t, + flint_cleanup, + flint_get_num_threads, + flint_set_num_threads +) +from .utils.conversion cimport prec_to_dps, dps_to_prec + +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() \ No newline at end of file From 304dd67aeeb13f2c87c870c81f2c3b27959b7ba7 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 22:51:49 +0100 Subject: [PATCH 04/30] remove some globals from pyflint --- src/flint/pyflint.pyx | 79 ++----------------------------------------- 1 file changed, 2 insertions(+), 77 deletions(-) diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 63af457c..5d8d6fb6 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -6,6 +6,8 @@ cimport flint cimport libc.stdlib cimport cython +from .flint_base.flint_context cimport FlintContext + cdef flint_rand_t global_random_state flint_randinit(global_random_state) @@ -56,83 +58,6 @@ 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 From b5b1831954ac4585f6919ac551ff052cc4bb3c6c Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 22:52:28 +0100 Subject: [PATCH 05/30] include DS_Store to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4c66d67b..636adc59 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ MANIFEST .coverage *.swp .python-version +*.DS_Store \ No newline at end of file From 06064b0f220aab29f12b9f9b930b16768ac7e5c7 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 23:59:26 +0100 Subject: [PATCH 06/30] EOF --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 636adc59..77dc537b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,4 @@ MANIFEST .coverage *.swp .python-version -*.DS_Store \ No newline at end of file +*.DS_Store From f36b6817f03b7a7c486b5d78910743fcd3ea91d6 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Thu, 17 Aug 2023 23:59:37 +0100 Subject: [PATCH 07/30] Add submodule --- setup.py | 82 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/setup.py b/setup.py index 5888fc1a..4d5e96ff 100644 --- a/setup.py +++ b/setup.py @@ -11,22 +11,22 @@ from distutils.sysconfig import get_config_vars -if sys.platform == 'win32': +if sys.platform == "win32": # # This is used in CI to build wheels with mingw64 # - if os.getenv('PYTHON_FLINT_MINGW64'): + if os.getenv("PYTHON_FLINT_MINGW64"): libraries = ["arb", "flint", "mpfr", "gmp"] - includedir = os.path.join(os.path.dirname(__file__), '.local', 'include') - librarydir1 = os.path.join(os.path.dirname(__file__), '.local', 'bin') - librarydir2 = os.path.join(os.path.dirname(__file__), '.local', 'lib') + includedir = os.path.join(os.path.dirname(__file__), ".local", "include") + librarydir1 = os.path.join(os.path.dirname(__file__), ".local", "bin") + librarydir2 = os.path.join(os.path.dirname(__file__), ".local", "lib") librarydirs = [librarydir1, librarydir2] default_include_dirs += [includedir] default_lib_dirs += librarydirs # Add gcc to the PATH in GitHub Actions when this setup.py is called by # cibuildwheel. - os.environ['PATH'] += r';C:\msys64\mingw64\bin' - elif os.getenv('PYTHON_FLINT_MINGW64_TMP'): + os.environ["PATH"] += r";C:\msys64\mingw64\bin" + elif os.getenv("PYTHON_FLINT_MINGW64_TMP"): # This would be used to build under Windows against these libraries if # they have been installed somewhere other than .local libraries = ["arb", "flint", "mpfr", "gmp"] @@ -35,60 +35,70 @@ libraries = ["arb", "flint", "mpir", "mpfr", "pthreads"] else: # On Ubuntu libarb.so is called libflint-arb.so - if os.getenv('PYTHON_FLINT_LIBFLINT_ARB'): - arb = 'flint-arb' + if os.getenv("PYTHON_FLINT_LIBFLINT_ARB"): + arb = "flint-arb" else: - arb = 'arb' + arb = "arb" libraries = [arb, "flint"] - (opt,) = get_config_vars('OPT') - os.environ['OPT'] = " ".join(flag for flag in opt.split() if flag != '-Wstrict-prototypes') + (opt,) = get_config_vars("OPT") + os.environ["OPT"] = " ".join( + flag for flag in opt.split() if flag != "-Wstrict-prototypes" + ) -default_include_dirs += [ - os.path.join(d, "flint") for d in default_include_dirs -] +default_include_dirs += [os.path.join(d, "flint") for d in default_include_dirs] define_macros = [] compiler_directives = { - 'language_level': 3, - 'binding': False, + "language_level": 3, + "binding": False, } # Enable coverage tracing -if os.getenv('PYTHON_FLINT_COVERAGE'): - define_macros.append(('CYTHON_TRACE', 1)) - compiler_directives['linetrace'] = True +if os.getenv("PYTHON_FLINT_COVERAGE"): + define_macros.append(("CYTHON_TRACE", 1)) + compiler_directives["linetrace"] = True ext_modules = [ Extension( - "flint._flint", ["src/flint/pyflint.pyx"], + "flint._flint", + ["src/flint/pyflint.pyx"], + libraries=libraries, + 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, - ) + ), ] for e in ext_modules: e.cython_directives = {"embedsignature": True} setup( - name='python-flint', - cmdclass={'build_ext': build_ext}, + name="python-flint", + cmdclass={"build_ext": build_ext}, ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives), - #ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives, annotate=True), - packages=['flint', 'flint.test'], - package_dir={'': 'src'}, - 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', - author='Fredrik Johansson', - author_email='fredrik.johansson@gmail.com', - license='MIT', - classifiers=['Topic :: Scientific/Engineering :: Mathematics']) + # ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives, annotate=True), + packages=["flint", "flint.test"], + package_dir={"": "src"}, + 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", + author="Fredrik Johansson", + author_email="fredrik.johansson@gmail.com", + license="MIT", + classifiers=["Topic :: Scientific/Engineering :: Mathematics"], +) From 7b7d2d2036a6120f1bacb2fe23f879a723c10293 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 00:00:50 +0100 Subject: [PATCH 08/30] EOF --- src/flint/flint_base/flint_context.pxd | 2 +- src/flint/flint_base/flint_context.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flint/flint_base/flint_context.pxd b/src/flint/flint_base/flint_context.pxd index 4725ec71..f6ca2378 100644 --- a/src/flint/flint_base/flint_context.pxd +++ b/src/flint/flint_base/flint_context.pxd @@ -1,2 +1,2 @@ cdef class FlintContext: - pass \ No newline at end of file + pass diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx index 7399d4fe..e7408c06 100644 --- a/src/flint/flint_base/flint_context.pyx +++ b/src/flint/flint_base/flint_context.pyx @@ -75,4 +75,4 @@ cdef class FlintContext: (self.pretty, self.unicode, self.prec, self.dps, self.cap, self.threads) def cleanup(self): - flint_cleanup() \ No newline at end of file + flint_cleanup() From 622c67118610b981eff360c3815f2c297d0d697c Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 00:30:06 +0100 Subject: [PATCH 09/30] Reverse old formatting --- setup.py | 75 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/setup.py b/setup.py index 4d5e96ff..c6912f1b 100644 --- a/setup.py +++ b/setup.py @@ -11,22 +11,22 @@ from distutils.sysconfig import get_config_vars -if sys.platform == "win32": +if sys.platform == 'win32': # # This is used in CI to build wheels with mingw64 # - if os.getenv("PYTHON_FLINT_MINGW64"): + if os.getenv('PYTHON_FLINT_MINGW64'): libraries = ["arb", "flint", "mpfr", "gmp"] - includedir = os.path.join(os.path.dirname(__file__), ".local", "include") - librarydir1 = os.path.join(os.path.dirname(__file__), ".local", "bin") - librarydir2 = os.path.join(os.path.dirname(__file__), ".local", "lib") + includedir = os.path.join(os.path.dirname(__file__), '.local', 'include') + librarydir1 = os.path.join(os.path.dirname(__file__), '.local', 'bin') + librarydir2 = os.path.join(os.path.dirname(__file__), '.local', 'lib') librarydirs = [librarydir1, librarydir2] default_include_dirs += [includedir] default_lib_dirs += librarydirs # Add gcc to the PATH in GitHub Actions when this setup.py is called by # cibuildwheel. - os.environ["PATH"] += r";C:\msys64\mingw64\bin" - elif os.getenv("PYTHON_FLINT_MINGW64_TMP"): + os.environ['PATH'] += r';C:\msys64\mingw64\bin' + elif os.getenv('PYTHON_FLINT_MINGW64_TMP'): # This would be used to build under Windows against these libraries if # they have been installed somewhere other than .local libraries = ["arb", "flint", "mpfr", "gmp"] @@ -35,43 +35,42 @@ libraries = ["arb", "flint", "mpir", "mpfr", "pthreads"] else: # On Ubuntu libarb.so is called libflint-arb.so - if os.getenv("PYTHON_FLINT_LIBFLINT_ARB"): - arb = "flint-arb" + if os.getenv('PYTHON_FLINT_LIBFLINT_ARB'): + arb = 'flint-arb' else: - arb = "arb" + arb = 'arb' libraries = [arb, "flint"] - (opt,) = get_config_vars("OPT") - os.environ["OPT"] = " ".join( - flag for flag in opt.split() if flag != "-Wstrict-prototypes" - ) + (opt,) = get_config_vars('OPT') + os.environ['OPT'] = " ".join(flag for flag in opt.split() if flag != '-Wstrict-prototypes') -default_include_dirs += [os.path.join(d, "flint") for d in default_include_dirs] +default_include_dirs += [ + os.path.join(d, "flint") for d in default_include_dirs +] define_macros = [] compiler_directives = { - "language_level": 3, - "binding": False, + 'language_level': 3, + 'binding': False, } # Enable coverage tracing -if os.getenv("PYTHON_FLINT_COVERAGE"): - define_macros.append(("CYTHON_TRACE", 1)) - compiler_directives["linetrace"] = True +if os.getenv('PYTHON_FLINT_COVERAGE'): + define_macros.append(('CYTHON_TRACE', 1)) + compiler_directives['linetrace'] = True ext_modules = [ Extension( - "flint._flint", - ["src/flint/pyflint.pyx"], + "flint._flint", ["src/flint/pyflint.pyx"], libraries=libraries, 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"], @@ -86,19 +85,19 @@ e.cython_directives = {"embedsignature": True} setup( - name="python-flint", - cmdclass={"build_ext": build_ext}, + name='python-flint', + cmdclass={'build_ext': build_ext}, ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives), - # ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives, annotate=True), - packages=["flint", "flint.test"], - package_dir={"": "src"}, - 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", - author="Fredrik Johansson", - author_email="fredrik.johansson@gmail.com", - license="MIT", - classifiers=["Topic :: Scientific/Engineering :: Mathematics"], -) + #ext_modules=cythonize(ext_modules, compiler_directives=compiler_directives, annotate=True), + packages=['flint', 'flint.test'], + package_dir={'': 'src'}, + description='Bindings for FLINT and Arb', + long_description=open('README.md').read(), + long_description_content_type='text/markdown', + version='0.4.2', + url='https://github.com/flintlib/python-flint', + author='Fredrik Johansson', + author_email='fredrik.johansson@gmail.com', + license='MIT', + classifiers=['Topic :: Scientific/Engineering :: Mathematics']) + From 2dee3db95efcb3c41441e0fc9d14f88a18e3e7a5 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 00:39:24 +0100 Subject: [PATCH 10/30] Fix relative imports and now get a proper error --- src/flint/flint_base/flint_context.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx index e7408c06..0839fc90 100644 --- a/src/flint/flint_base/flint_context.pyx +++ b/src/flint/flint_base/flint_context.pyx @@ -1,11 +1,11 @@ -from ._flint cimport ( +from flint._flint cimport ( ARF_RND_DOWN, arf_rnd_t, flint_cleanup, flint_get_num_threads, flint_set_num_threads ) -from .utils.conversion cimport prec_to_dps, dps_to_prec +from flint.utils.conversion cimport prec_to_dps, dps_to_prec cdef class FlintContext: cdef public bint pretty From 0f5d2361534ee5757167914a131d947933de4a91 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 10:12:32 +0100 Subject: [PATCH 11/30] Factor out into submodule --- src/flint/flint_base/flint_context.pxd | 11 +++++++++++ src/flint/flint_base/flint_context.pyx | 8 -------- src/flint/pyflint.pyx | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/flint/flint_base/flint_context.pxd b/src/flint/flint_base/flint_context.pxd index f6ca2378..9c1bdcfe 100644 --- a/src/flint/flint_base/flint_context.pxd +++ b/src/flint/flint_base/flint_context.pxd @@ -1,2 +1,13 @@ +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 + pass diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx index 0839fc90..0cd78932 100644 --- a/src/flint/flint_base/flint_context.pyx +++ b/src/flint/flint_base/flint_context.pyx @@ -1,6 +1,5 @@ from flint._flint cimport ( ARF_RND_DOWN, - arf_rnd_t, flint_cleanup, flint_get_num_threads, flint_set_num_threads @@ -8,13 +7,6 @@ from flint._flint cimport ( from flint.utils.conversion cimport prec_to_dps, dps_to_prec 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() diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 5d8d6fb6..062eee0d 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -6,7 +6,7 @@ cimport flint cimport libc.stdlib cimport cython -from .flint_base.flint_context cimport FlintContext +from flint.flint_base.flint_context cimport FlintContext cdef flint_rand_t global_random_state flint_randinit(global_random_state) From 726a699262b6fb5e1599da287aad0d9ef0cf6308 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 10:42:12 +0100 Subject: [PATCH 12/30] ensure submodule c files are skipped --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 77dc537b..0f4ae48e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ build/* dist/* -src/flint/*.c +src/flint/**/*.c src/flint/*.html doc/build/* fmake* From b3e5ec95b69c996df678d6a931838c0f071eb7d4 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 10:42:33 +0100 Subject: [PATCH 13/30] Refactor out more conversions --- src/flint/arb.pyx | 3 +++ src/flint/flint_base/flint_context.pxd | 2 -- src/flint/fmpz.pyx | 3 +++ src/flint/fmpz_mpoly.pyx | 3 +++ src/flint/fmpz_poly.pyx | 2 ++ src/flint/nmod_mat.pyx | 2 ++ src/flint/pyflint.pyx | 27 ------------------------ src/flint/utils/conversion.pxd | 29 +++++++++++++++++++++++++- 8 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/flint/arb.pyx b/src/flint/arb.pyx index 6a52c557..04fed4fc 100644 --- a/src/flint/arb.pyx +++ b/src/flint/arb.pyx @@ -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 diff --git a/src/flint/flint_base/flint_context.pxd b/src/flint/flint_base/flint_context.pxd index 9c1bdcfe..c3124a12 100644 --- a/src/flint/flint_base/flint_context.pxd +++ b/src/flint/flint_base/flint_context.pxd @@ -9,5 +9,3 @@ cdef class FlintContext: cdef arf_rnd_t rnd cdef public bint unicode cdef public long _cap - - pass diff --git a/src/flint/fmpz.pyx b/src/flint/fmpz.pyx index 5900aa0e..3927b07b 100644 --- a/src/flint/fmpz.pyx +++ b/src/flint/fmpz.pyx @@ -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 diff --git a/src/flint/fmpz_mpoly.pyx b/src/flint/fmpz_mpoly.pyx index 32ed47d3..5fab5dcf 100644 --- a/src/flint/fmpz_mpoly.pyx +++ b/src/flint/fmpz_mpoly.pyx @@ -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 """ diff --git a/src/flint/fmpz_poly.pyx b/src/flint/fmpz_poly.pyx index 3f570ca4..43435541 100644 --- a/src/flint/fmpz_poly.pyx +++ b/src/flint/fmpz_poly.pyx @@ -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): diff --git a/src/flint/nmod_mat.pyx b/src/flint/nmod_mat.pyx index 9d9e918d..14a86309 100644 --- a/src/flint/nmod_mat.pyx +++ b/src/flint/nmod_mat.pyx @@ -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 diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 062eee0d..c773e605 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -24,33 +24,6 @@ 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, tp) diff --git a/src/flint/utils/conversion.pxd b/src/flint/utils/conversion.pxd index bee04416..98df1a81 100644 --- a/src/flint/utils/conversion.pxd +++ b/src/flint/utils/conversion.pxd @@ -1,5 +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))) \ No newline at end of file + 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) From b8be99bec6a0759e0b550f291478a046661133ba Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 11:06:48 +0100 Subject: [PATCH 14/30] move away from depreciated properties --- src/flint/flint_base/flint_context.pyx | 76 +++++++++++++------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx index 0cd78932..9a9a6ec9 100644 --- a/src/flint/flint_base/flint_context.pyx +++ b/src/flint/flint_base/flint_context.pyx @@ -18,44 +18,44 @@ cdef class FlintContext: 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() + @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" \ From f7c3bfc5f36d724a7c38b6a9a9ec6dfb0ff57bd9 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 12:19:25 +0100 Subject: [PATCH 15/30] Refactor set up for inclusion of submodules --- setup.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index c6912f1b..bee61039 100644 --- a/setup.py +++ b/setup.py @@ -63,24 +63,24 @@ 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, - ), - 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, - ), +ext_files = [ + ("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module + # Submodules + ("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} From 4e5310b6b3b4faf634bea6f36aad898c612ae2e8 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 12:46:47 +0100 Subject: [PATCH 16/30] move context into new submodule --- setup.py | 2 ++ src/flint/_global_context.pxd | 15 +++++++++++++++ src/flint/_global_context.pyx | 4 ++++ 3 files changed, 21 insertions(+) create mode 100644 src/flint/_global_context.pxd create mode 100644 src/flint/_global_context.pyx diff --git a/setup.py b/setup.py index bee61039..74468028 100644 --- a/setup.py +++ b/setup.py @@ -66,7 +66,9 @@ ext_files = [ ("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module # Submodules + ("flint.global_context", ["src/flint/_global_context.pyx"]), ("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]), + ] ext_options = { diff --git a/src/flint/_global_context.pxd b/src/flint/_global_context.pxd new file mode 100644 index 00000000..46b0800f --- /dev/null +++ b/src/flint/_global_context.pxd @@ -0,0 +1,15 @@ +from flint.flint_base.flint_context cimport FlintContext + +cdef FlintContext thectx = FlintContext() + +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 diff --git a/src/flint/_global_context.pyx b/src/flint/_global_context.pyx new file mode 100644 index 00000000..e090f728 --- /dev/null +++ b/src/flint/_global_context.pyx @@ -0,0 +1,4 @@ +from flint.flint_base.flint_context cimport FlintContext + +cdef FlintContext thectx = FlintContext() +ctx = thectx From 4d7ad876f647a3ebe065867efd88030147048b31 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 12:47:14 +0100 Subject: [PATCH 17/30] Import thectx from module and set ctx --- src/flint/pyflint.pyx | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index c773e605..510471d5 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -6,8 +6,6 @@ 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) @@ -31,24 +29,10 @@ DEF FMPZ_UNKNOWN = 0 DEF FMPZ_REF = 1 DEF FMPZ_TMP = 2 -cdef FlintContext thectx = FlintContext() - +from flint._global_context cimport thectx ctx = 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 - cdef class flint_elem: - def __repr__(self): if ctx.pretty: return self.str() From 039e6875de9d583172b523f639039d32aa59178d Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 12:47:42 +0100 Subject: [PATCH 18/30] Import precision / cap inline functions previously defined as global --- src/flint/acb.pyx | 2 ++ src/flint/acb_mat.pyx | 2 ++ src/flint/acb_poly.pyx | 2 ++ src/flint/acb_series.pyx | 2 ++ src/flint/arb.pyx | 1 + src/flint/arb_mat.pyx | 2 ++ src/flint/arb_poly.pyx | 2 ++ src/flint/arb_series.pyx | 2 ++ src/flint/arf.pyx | 3 ++- src/flint/dirichlet.pyx | 2 ++ src/flint/fmpz_poly.pyx | 1 + 11 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/flint/acb.pyx b/src/flint/acb.pyx index a7151a55..733980f9 100644 --- a/src/flint/acb.pyx +++ b/src/flint/acb.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + cdef int acb_set_python(acb_t x, obj, bint allow_conversion): cdef double re, im diff --git a/src/flint/acb_mat.pyx b/src/flint/acb_mat.pyx index 4f80870d..a9501612 100644 --- a/src/flint/acb_mat.pyx +++ b/src/flint/acb_mat.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + cdef acb_mat_coerce_operands(x, y): if isinstance(y, (fmpz_mat, fmpq_mat, arb_mat)): return x, acb_mat(y) diff --git a/src/flint/acb_poly.pyx b/src/flint/acb_poly.pyx index 252e57d9..8bceba1f 100644 --- a/src/flint/acb_poly.pyx +++ b/src/flint/acb_poly.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + 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) diff --git a/src/flint/acb_series.pyx b/src/flint/acb_series.pyx index dd2eba22..880f3015 100644 --- a/src/flint/acb_series.pyx +++ b/src/flint/acb_series.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + 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) diff --git a/src/flint/arb.pyx b/src/flint/arb.pyx index 04fed4fc..d45cd1fc 100644 --- a/src/flint/arb.pyx +++ b/src/flint/arb.pyx @@ -1,4 +1,5 @@ from cpython.version cimport PY_MAJOR_VERSION +from flint._global_context cimport getprec, getcap from flint.utils.conversion cimport chars_from_str, str_from_chars cdef _str_trunc(s, trunc=0): diff --git a/src/flint/arb_mat.pyx b/src/flint/arb_mat.pyx index 0cb89378..4d895fc6 100644 --- a/src/flint/arb_mat.pyx +++ b/src/flint/arb_mat.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + cdef arb_mat_coerce_operands(x, y): if isinstance(y, (fmpz_mat, fmpq_mat)): return x, arb_mat(y) diff --git a/src/flint/arb_poly.pyx b/src/flint/arb_poly.pyx index a13ed454..9627eb7d 100644 --- a/src/flint/arb_poly.pyx +++ b/src/flint/arb_poly.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + 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) diff --git a/src/flint/arb_series.pyx b/src/flint/arb_series.pyx index 7eff177b..ce96b3cd 100644 --- a/src/flint/arb_series.pyx +++ b/src/flint/arb_series.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + 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) diff --git a/src/flint/arf.pyx b/src/flint/arf.pyx index ba2b0391..1eb81d40 100644 --- a/src/flint/arf.pyx +++ b/src/flint/arf.pyx @@ -1,4 +1,5 @@ -from .utils.conversion cimport prec_to_dps +from flint._global_context cimport getprec, getcap +from flint.utils.conversion cimport prec_to_dps cdef class arf: diff --git a/src/flint/dirichlet.pyx b/src/flint/dirichlet.pyx index 76801ff4..024936fa 100644 --- a/src/flint/dirichlet.pyx +++ b/src/flint/dirichlet.pyx @@ -1,3 +1,5 @@ +from flint._global_context cimport getprec, getcap + cdef dict _dirichlet_group_cache = {} cdef class dirichlet_group(object): diff --git a/src/flint/fmpz_poly.pyx b/src/flint/fmpz_poly.pyx index 43435541..8daf45e1 100644 --- a/src/flint/fmpz_poly.pyx +++ b/src/flint/fmpz_poly.pyx @@ -1,4 +1,5 @@ from cpython.version cimport PY_MAJOR_VERSION +from flint._global_context cimport getprec, getcap cdef any_as_fmpz_poly(x): cdef fmpz_poly res From e76daf4a028ea1d5942bc5396ba2e602f3ae2f72 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 12:57:44 +0100 Subject: [PATCH 19/30] Global context need not be an extension, as its not defining a new class --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 74468028..d1cca31b 100644 --- a/setup.py +++ b/setup.py @@ -66,7 +66,7 @@ ext_files = [ ("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module # Submodules - ("flint.global_context", ["src/flint/_global_context.pyx"]), + # ("flint.global_context", ["src/flint/_global_context.pyx"]), ("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]), ] From f09b791f37bf0b182a00bb09972c1325d7a962f6 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 14:14:11 +0100 Subject: [PATCH 20/30] remove template classes --- src/flint/pyflint.pyx | 146 +----------------------------------------- 1 file changed, 2 insertions(+), 144 deletions(-) diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 510471d5..7b48e470 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -6,6 +6,8 @@ cimport flint cimport libc.stdlib cimport cython +from flint._global_context cimport thectx + cdef flint_rand_t global_random_state flint_randinit(global_random_state) @@ -29,152 +31,8 @@ DEF FMPZ_UNKNOWN = 0 DEF FMPZ_REF = 1 DEF FMPZ_TMP = 2 -from flint._global_context cimport thectx ctx = thectx -cdef class flint_elem: - def __repr__(self): - if ctx.pretty: - return self.str() - else: - return self.repr() - - def __str__(self): - return self.str() - -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) - - def roots(self, **kwargs): - """ - Isolates the complex roots of *self*. See :meth:`.acb_poly.roots` - for details. - """ - return acb_poly(self).roots(**kwargs) - -cdef class flint_mpoly(flint_elem): - """ - Base class for multivariate polynomials. - """ - - -cdef class flint_mat(flint_elem): - """ - Base class for matrices. - """ - - def repr(self): - if ctx.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 - -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) - - include "fmpz.pyx" include "fmpz_poly.pyx" include "fmpz_mpoly.pyx" From 522c50a5c1856066a682a7adcd12627b9bfb90b7 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 14:14:24 +0100 Subject: [PATCH 21/30] Add templates as a submodule --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index d1cca31b..702ace39 100644 --- a/setup.py +++ b/setup.py @@ -67,6 +67,7 @@ ("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module # Submodules # ("flint.global_context", ["src/flint/_global_context.pyx"]), + ("flint.flint_base.flint_base", ["src/flint/flint_base/flint_base.pyx"]), ("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]), ] From 8b1190f47508c6079a77fecf84d3a6698f454995 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 14:14:39 +0100 Subject: [PATCH 22/30] delete unneeded file --- src/flint/_global_context.pyx | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/flint/_global_context.pyx diff --git a/src/flint/_global_context.pyx b/src/flint/_global_context.pyx deleted file mode 100644 index e090f728..00000000 --- a/src/flint/_global_context.pyx +++ /dev/null @@ -1,4 +0,0 @@ -from flint.flint_base.flint_context cimport FlintContext - -cdef FlintContext thectx = FlintContext() -ctx = thectx From 718b1fcdaa073e345d8b09cde46cee61ff0e85be Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 14:15:04 +0100 Subject: [PATCH 23/30] Include needed classes for each flint type --- src/flint/acb.pyx | 1 + src/flint/acb_mat.pyx | 1 + src/flint/acb_poly.pyx | 1 + src/flint/acb_series.pyx | 1 + src/flint/arb.pyx | 2 ++ src/flint/arb_mat.pyx | 1 + src/flint/arb_poly.pyx | 1 + src/flint/arb_series.pyx | 1 + src/flint/fmpq.pyx | 2 ++ src/flint/fmpq_mat.pyx | 2 ++ src/flint/fmpq_poly.pyx | 2 ++ src/flint/fmpq_series.pyx | 2 ++ src/flint/fmpz.pyx | 2 ++ src/flint/fmpz_mat.pyx | 2 ++ src/flint/fmpz_mpoly.pyx | 2 ++ src/flint/fmpz_poly.pyx | 2 ++ src/flint/fmpz_series.pyx | 2 ++ src/flint/nmod.pyx | 2 ++ src/flint/nmod_poly.pyx | 2 ++ src/flint/nmod_series.pyx | 2 ++ 20 files changed, 33 insertions(+) diff --git a/src/flint/acb.pyx b/src/flint/acb.pyx index 733980f9..bc0c8909 100644 --- a/src/flint/acb.pyx +++ b/src/flint/acb.pyx @@ -1,3 +1,4 @@ +from flint.flint_base.flint_base cimport flint_scalar from flint._global_context cimport getprec, getcap cdef int acb_set_python(acb_t x, obj, bint allow_conversion): diff --git a/src/flint/acb_mat.pyx b/src/flint/acb_mat.pyx index a9501612..bf54e1bc 100644 --- a/src/flint/acb_mat.pyx +++ b/src/flint/acb_mat.pyx @@ -1,4 +1,5 @@ from flint._global_context cimport getprec, getcap +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)): diff --git a/src/flint/acb_poly.pyx b/src/flint/acb_poly.pyx index 8bceba1f..fd4413ac 100644 --- a/src/flint/acb_poly.pyx +++ b/src/flint/acb_poly.pyx @@ -1,4 +1,5 @@ from flint._global_context cimport getprec, getcap +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)): diff --git a/src/flint/acb_series.pyx b/src/flint/acb_series.pyx index 880f3015..74bbf73c 100644 --- a/src/flint/acb_series.pyx +++ b/src/flint/acb_series.pyx @@ -1,4 +1,5 @@ from flint._global_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)): diff --git a/src/flint/arb.pyx b/src/flint/arb.pyx index d45cd1fc..3ffe26c4 100644 --- a/src/flint/arb.pyx +++ b/src/flint/arb.pyx @@ -1,5 +1,7 @@ from cpython.version cimport PY_MAJOR_VERSION + from flint._global_context cimport getprec, getcap +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): diff --git a/src/flint/arb_mat.pyx b/src/flint/arb_mat.pyx index 4d895fc6..9e4dbf47 100644 --- a/src/flint/arb_mat.pyx +++ b/src/flint/arb_mat.pyx @@ -1,4 +1,5 @@ from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_base cimport flint_mat cdef arb_mat_coerce_operands(x, y): if isinstance(y, (fmpz_mat, fmpq_mat)): diff --git a/src/flint/arb_poly.pyx b/src/flint/arb_poly.pyx index 9627eb7d..19ecb472 100644 --- a/src/flint/arb_poly.pyx +++ b/src/flint/arb_poly.pyx @@ -1,4 +1,5 @@ from flint._global_context cimport getprec, getcap +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)): diff --git a/src/flint/arb_series.pyx b/src/flint/arb_series.pyx index ce96b3cd..563632c4 100644 --- a/src/flint/arb_series.pyx +++ b/src/flint/arb_series.pyx @@ -1,4 +1,5 @@ from flint._global_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)): diff --git a/src/flint/fmpq.pyx b/src/flint/fmpq.pyx index d70a68f2..e8e79b3b 100644 --- a/src/flint/fmpq.pyx +++ b/src/flint/fmpq.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_scalar + cdef any_as_fmpq(obj): if typecheck(obj, fmpq): return obj diff --git a/src/flint/fmpq_mat.pyx b/src/flint/fmpq_mat.pyx index e81875e4..65d8a0a9 100644 --- a/src/flint/fmpq_mat.pyx +++ b/src/flint/fmpq_mat.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_mat + cdef any_as_fmpq_mat(obj): if typecheck(obj, fmpq_mat): return obj diff --git a/src/flint/fmpq_poly.pyx b/src/flint/fmpq_poly.pyx index c3e68cef..8b39d3b7 100644 --- a/src/flint/fmpq_poly.pyx +++ b/src/flint/fmpq_poly.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_poly + cdef any_as_fmpq_poly(obj): if typecheck(obj, fmpq_poly): return obj diff --git a/src/flint/fmpq_series.pyx b/src/flint/fmpq_series.pyx index a5300ddc..9c76f69d 100644 --- a/src/flint/fmpq_series.pyx +++ b/src/flint/fmpq_series.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_series + cdef fmpq_series_coerce_operands(x, y): if isinstance(y, (int, long, fmpz, fmpz_poly, fmpz_series, fmpq, fmpq_poly)): return x, fmpq_series(y) diff --git a/src/flint/fmpz.pyx b/src/flint/fmpz.pyx index 3927b07b..c91359f8 100644 --- a/src/flint/fmpz.pyx +++ b/src/flint/fmpz.pyx @@ -1,4 +1,6 @@ from cpython.version cimport PY_MAJOR_VERSION + +from flint.flint_base.flint_base cimport flint_scalar from flint.utils.conversion cimport chars_from_str cdef inline int fmpz_set_pylong(fmpz_t x, obj): diff --git a/src/flint/fmpz_mat.pyx b/src/flint/fmpz_mat.pyx index 1ff56ab7..887750b2 100644 --- a/src/flint/fmpz_mat.pyx +++ b/src/flint/fmpz_mat.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_mat + cdef any_as_fmpz_mat(obj): if typecheck(obj, fmpz_mat): return obj diff --git a/src/flint/fmpz_mpoly.pyx b/src/flint/fmpz_mpoly.pyx index 5fab5dcf..f4ddf026 100644 --- a/src/flint/fmpz_mpoly.pyx +++ b/src/flint/fmpz_mpoly.pyx @@ -1,5 +1,7 @@ from cpython.version cimport PY_MAJOR_VERSION + from flint.utils.conversion cimport str_from_chars +from flint.flint_base.flint_base cimport flint_mpoly cdef any_as_fmpz_mpoly(x): cdef fmpz_mpoly res diff --git a/src/flint/fmpz_poly.pyx b/src/flint/fmpz_poly.pyx index 8daf45e1..b2224e61 100644 --- a/src/flint/fmpz_poly.pyx +++ b/src/flint/fmpz_poly.pyx @@ -1,5 +1,7 @@ from cpython.version cimport PY_MAJOR_VERSION + from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_base cimport flint_poly cdef any_as_fmpz_poly(x): cdef fmpz_poly res diff --git a/src/flint/fmpz_series.pyx b/src/flint/fmpz_series.pyx index c8f31343..dcb8cb3a 100644 --- a/src/flint/fmpz_series.pyx +++ b/src/flint/fmpz_series.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_series + cdef fmpz_series_coerce_operands(x, y): if isinstance(y, (int, long, fmpz, fmpz_poly)): return x, fmpz_series(y) diff --git a/src/flint/nmod.pyx b/src/flint/nmod.pyx index 75bce0c3..e212b668 100644 --- a/src/flint/nmod.pyx +++ b/src/flint/nmod.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_scalar + cdef int any_as_nmod(mp_limb_t * val, obj, nmod_t mod) except -1: cdef int success cdef fmpz_t t diff --git a/src/flint/nmod_poly.pyx b/src/flint/nmod_poly.pyx index 9e43194c..6824bb6a 100644 --- a/src/flint/nmod_poly.pyx +++ b/src/flint/nmod_poly.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_poly + cdef any_as_nmod_poly(obj, nmod_t mod): cdef nmod_poly r cdef mp_limb_t v diff --git a/src/flint/nmod_series.pyx b/src/flint/nmod_series.pyx index 125eb69c..160d5f2c 100644 --- a/src/flint/nmod_series.pyx +++ b/src/flint/nmod_series.pyx @@ -1,3 +1,5 @@ +from flint.flint_base.flint_base cimport flint_series + cdef class nmod_series(flint_series): pass From 38c9ffdd74a57a5a98d81a29c959089388c6e0dc Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 14:33:21 +0100 Subject: [PATCH 24/30] Actually include the new files --- src/flint/flint_base/flint_base.pxd | 17 ++++ src/flint/flint_base/flint_base.pyx | 145 ++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 src/flint/flint_base/flint_base.pxd create mode 100644 src/flint/flint_base/flint_base.pyx diff --git a/src/flint/flint_base/flint_base.pxd b/src/flint/flint_base/flint_base.pxd new file mode 100644 index 00000000..95f87e9a --- /dev/null +++ b/src/flint/flint_base/flint_base.pxd @@ -0,0 +1,17 @@ +cdef class flint_elem: + pass + +cdef class flint_scalar(flint_elem): + pass + +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 diff --git a/src/flint/flint_base/flint_base.pyx b/src/flint/flint_base/flint_base.pyx new file mode 100644 index 00000000..68c26939 --- /dev/null +++ b/src/flint/flint_base/flint_base.pyx @@ -0,0 +1,145 @@ +from flint._global_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 + +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) + +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 From 3ab97f6d7db220d371cf196a91b9eeda7856d307 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 16:44:45 +0100 Subject: [PATCH 25/30] Make flint_poly a global until issue #62 is resolved --- src/flint/flint_base/flint_base.pxd | 6 +- src/flint/flint_base/flint_base.pyx | 122 +++++++++++++++------------- src/flint/pyflint.pyx | 63 ++++++++++++++ 3 files changed, 131 insertions(+), 60 deletions(-) diff --git a/src/flint/flint_base/flint_base.pxd b/src/flint/flint_base/flint_base.pxd index 95f87e9a..e8782db2 100644 --- a/src/flint/flint_base/flint_base.pxd +++ b/src/flint/flint_base/flint_base.pxd @@ -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 diff --git a/src/flint/flint_base/flint_base.pyx b/src/flint/flint_base/flint_base.pyx index 68c26939..58354403 100644 --- a/src/flint/flint_base/flint_base.pyx +++ b/src/flint/flint_base/flint_base.pyx @@ -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): """ diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 7b48e470..5f3f7b39 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -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" From a4d3a85b957439a040f12c0b76d4fb007fbf41a8 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 16:45:10 +0100 Subject: [PATCH 26/30] Remove imports until they're ready --- src/flint/acb_poly.pyx | 6 ++++-- src/flint/arb_poly.pyx | 6 ++++-- src/flint/fmpq_poly.pyx | 4 +++- src/flint/fmpz_poly.pyx | 6 ++++-- src/flint/nmod_poly.pyx | 4 +++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/flint/acb_poly.pyx b/src/flint/acb_poly.pyx index fd4413ac..aba434fd 100644 --- a/src/flint/acb_poly.pyx +++ b/src/flint/acb_poly.pyx @@ -1,5 +1,7 @@ -from flint._global_context cimport getprec, getcap -from flint.flint_base.flint_base cimport flint_poly +from flint._global_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)): diff --git a/src/flint/arb_poly.pyx b/src/flint/arb_poly.pyx index 19ecb472..22e2acf5 100644 --- a/src/flint/arb_poly.pyx +++ b/src/flint/arb_poly.pyx @@ -1,5 +1,7 @@ -from flint._global_context cimport getprec, getcap -from flint.flint_base.flint_base cimport flint_poly +from flint._global_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)): diff --git a/src/flint/fmpq_poly.pyx b/src/flint/fmpq_poly.pyx index 8b39d3b7..db2c08ab 100644 --- a/src/flint/fmpq_poly.pyx +++ b/src/flint/fmpq_poly.pyx @@ -1,4 +1,6 @@ -from flint.flint_base.flint_base cimport flint_poly +# TODO: waiting for fix on the roots method, currently +# globally defined. +# from flint.flint_base.flint_base cimport flint_poly cdef any_as_fmpq_poly(obj): if typecheck(obj, fmpq_poly): diff --git a/src/flint/fmpz_poly.pyx b/src/flint/fmpz_poly.pyx index b2224e61..79a68d2c 100644 --- a/src/flint/fmpz_poly.pyx +++ b/src/flint/fmpz_poly.pyx @@ -1,7 +1,9 @@ from cpython.version cimport PY_MAJOR_VERSION -from flint._global_context cimport getprec, getcap -from flint.flint_base.flint_base cimport flint_poly +from flint._global_context cimport getprec +# TODO: waiting for fix on the roots method, currently +# globally defined. +# from flint.flint_base.flint_base cimport flint_poly cdef any_as_fmpz_poly(x): cdef fmpz_poly res diff --git a/src/flint/nmod_poly.pyx b/src/flint/nmod_poly.pyx index 6824bb6a..2e928ecc 100644 --- a/src/flint/nmod_poly.pyx +++ b/src/flint/nmod_poly.pyx @@ -1,4 +1,6 @@ -from flint.flint_base.flint_base cimport flint_poly +# TODO: waiting for fix on the roots method, currently +# globally defined. +# from flint.flint_base.flint_base cimport flint_poly cdef any_as_nmod_poly(obj, nmod_t mod): cdef nmod_poly r From 9a3fd0bda7b58bef866e9c1b521d2d10f4142a50 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 17:02:11 +0100 Subject: [PATCH 27/30] remove old pxd --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 702ace39..060a324c 100644 --- a/setup.py +++ b/setup.py @@ -66,7 +66,6 @@ ext_files = [ ("flint._flint", ["src/flint/pyflint.pyx"]), # Main Module # Submodules - # ("flint.global_context", ["src/flint/_global_context.pyx"]), ("flint.flint_base.flint_base", ["src/flint/flint_base/flint_base.pyx"]), ("flint.flint_base.flint_context", ["src/flint/flint_base/flint_context.pyx"]), From 457f429b5f86650f47dcbba782613952202c9264 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 17:02:18 +0100 Subject: [PATCH 28/30] remove old pxd --- src/flint/_global_context.pxd | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 src/flint/_global_context.pxd diff --git a/src/flint/_global_context.pxd b/src/flint/_global_context.pxd deleted file mode 100644 index 46b0800f..00000000 --- a/src/flint/_global_context.pxd +++ /dev/null @@ -1,15 +0,0 @@ -from flint.flint_base.flint_context cimport FlintContext - -cdef FlintContext thectx = FlintContext() - -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 From 4a16f0e45ee9e001bb779975f04dd30bc2b2bed3 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 17:03:03 +0100 Subject: [PATCH 29/30] Update imports --- src/flint/acb.pyx | 2 +- src/flint/acb_mat.pyx | 3 ++- src/flint/acb_poly.pyx | 2 +- src/flint/acb_series.pyx | 2 +- src/flint/arb.pyx | 2 +- src/flint/arb_mat.pyx | 2 +- src/flint/arb_poly.pyx | 2 +- src/flint/arb_series.pyx | 2 +- src/flint/arf.pyx | 2 +- src/flint/dirichlet.pyx | 2 +- src/flint/flint_base/flint_base.pyx | 2 +- src/flint/fmpz_poly.pyx | 2 +- src/flint/pyflint.pyx | 2 +- 13 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/flint/acb.pyx b/src/flint/acb.pyx index bc0c8909..a3cb36c7 100644 --- a/src/flint/acb.pyx +++ b/src/flint/acb.pyx @@ -1,5 +1,5 @@ from flint.flint_base.flint_base cimport flint_scalar -from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_context cimport getprec cdef int acb_set_python(acb_t x, obj, bint allow_conversion): cdef double re, im diff --git a/src/flint/acb_mat.pyx b/src/flint/acb_mat.pyx index bf54e1bc..6755edd2 100644 --- a/src/flint/acb_mat.pyx +++ b/src/flint/acb_mat.pyx @@ -1,4 +1,5 @@ -from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_context cimport getprec + from flint.flint_base.flint_base cimport flint_mat cdef acb_mat_coerce_operands(x, y): diff --git a/src/flint/acb_poly.pyx b/src/flint/acb_poly.pyx index aba434fd..fe41577c 100644 --- a/src/flint/acb_poly.pyx +++ b/src/flint/acb_poly.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec +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 diff --git a/src/flint/acb_series.pyx b/src/flint/acb_series.pyx index 74bbf73c..a642c46f 100644 --- a/src/flint/acb_series.pyx +++ b/src/flint/acb_series.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec, getcap +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): diff --git a/src/flint/arb.pyx b/src/flint/arb.pyx index 3ffe26c4..19f31ecd 100644 --- a/src/flint/arb.pyx +++ b/src/flint/arb.pyx @@ -1,6 +1,6 @@ from cpython.version cimport PY_MAJOR_VERSION -from flint._global_context cimport getprec, getcap +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 diff --git a/src/flint/arb_mat.pyx b/src/flint/arb_mat.pyx index 9e4dbf47..16cea15b 100644 --- a/src/flint/arb_mat.pyx +++ b/src/flint/arb_mat.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_context cimport getprec from flint.flint_base.flint_base cimport flint_mat cdef arb_mat_coerce_operands(x, y): diff --git a/src/flint/arb_poly.pyx b/src/flint/arb_poly.pyx index 22e2acf5..518cdd51 100644 --- a/src/flint/arb_poly.pyx +++ b/src/flint/arb_poly.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec +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 diff --git a/src/flint/arb_series.pyx b/src/flint/arb_series.pyx index 563632c4..8713080d 100644 --- a/src/flint/arb_series.pyx +++ b/src/flint/arb_series.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec, getcap +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): diff --git a/src/flint/arf.pyx b/src/flint/arf.pyx index 1eb81d40..91da4290 100644 --- a/src/flint/arf.pyx +++ b/src/flint/arf.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_context cimport getprec from flint.utils.conversion cimport prec_to_dps cdef class arf: diff --git a/src/flint/dirichlet.pyx b/src/flint/dirichlet.pyx index 024936fa..8ec160ec 100644 --- a/src/flint/dirichlet.pyx +++ b/src/flint/dirichlet.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport getprec, getcap +from flint.flint_base.flint_context cimport getprec cdef dict _dirichlet_group_cache = {} diff --git a/src/flint/flint_base/flint_base.pyx b/src/flint/flint_base/flint_base.pyx index 58354403..5e4ebff0 100644 --- a/src/flint/flint_base/flint_base.pyx +++ b/src/flint/flint_base/flint_base.pyx @@ -1,4 +1,4 @@ -from flint._global_context cimport thectx +from flint.flint_base.flint_context cimport thectx cdef class flint_elem: def __repr__(self): diff --git a/src/flint/fmpz_poly.pyx b/src/flint/fmpz_poly.pyx index 79a68d2c..0b9ad86a 100644 --- a/src/flint/fmpz_poly.pyx +++ b/src/flint/fmpz_poly.pyx @@ -1,6 +1,6 @@ from cpython.version cimport PY_MAJOR_VERSION -from flint._global_context cimport getprec +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 diff --git a/src/flint/pyflint.pyx b/src/flint/pyflint.pyx index 5f3f7b39..44d4a626 100644 --- a/src/flint/pyflint.pyx +++ b/src/flint/pyflint.pyx @@ -6,7 +6,7 @@ cimport flint cimport libc.stdlib cimport cython -from flint._global_context cimport thectx +from flint.flint_base.flint_context cimport thectx cdef flint_rand_t global_random_state flint_randinit(global_random_state) From 644b5bb4d8f570061ced33a1fcb81fa28d0734b7 Mon Sep 17 00:00:00 2001 From: giacomopope Date: Fri, 18 Aug 2023 17:03:18 +0100 Subject: [PATCH 30/30] Add all global context into this file --- src/flint/flint_base/flint_context.pxd | 14 ++++++++++++++ src/flint/flint_base/flint_context.pyx | 2 ++ 2 files changed, 16 insertions(+) diff --git a/src/flint/flint_base/flint_context.pxd b/src/flint/flint_base/flint_context.pxd index c3124a12..2bcfcdb0 100644 --- a/src/flint/flint_base/flint_context.pxd +++ b/src/flint/flint_base/flint_context.pxd @@ -9,3 +9,17 @@ cdef class FlintContext: 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 diff --git a/src/flint/flint_base/flint_context.pyx b/src/flint/flint_base/flint_context.pyx index 9a9a6ec9..c86a5d10 100644 --- a/src/flint/flint_base/flint_context.pyx +++ b/src/flint/flint_base/flint_context.pyx @@ -68,3 +68,5 @@ cdef class FlintContext: def cleanup(self): flint_cleanup() + +cdef FlintContext thectx = FlintContext()