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

Automatically generate fmpz_* pxds from FLINT headers #215

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions bin/all_rst_to_pxd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

FLINT_DOC_DIR=$1

set -e

modules="\
fmpz\
fmpz_factor\
fmpz_poly\
fmpz_poly_factor\
fmpz_mat\
fmpz_lll\
arf\
arb\
arb_poly\
arb_mat\
acb\
acb_poly\
acb_mat\
"

for module in $modules; do
echo "Processing $module"
bin/rst_to_pxd.py flint/$module --flint-doc-dir=$FLINT_DOC_DIR > src/flint/flintlib/$module.pxd
done
49 changes: 37 additions & 12 deletions bin/rst_to_pxd.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@
# recognize a function definition in rst
is_func = re.compile(r"\.\.( )+(c:)?function( )*::")
# rename types to avoid python -- c name collisions
rename_types = [(re.compile(r"\bfmpz\b"),"fmpz_struct"),(re.compile(r"\bfmpq\b"), "fmpq_struct")]
rename_types = [
(re.compile(r"\bfmpz\b"),"fmpz_struct"),
(re.compile(r"\bfmpq\b"), "fmpq_struct"),
(re.compile(r"\bin\b"), "in_"),
(re.compile(r"\blambda\b"), "lambda_"),
]
# comment out functions which use these types
comment_types = re.compile(r"(\bFILE\b)|(\bmpz_t\b)|(\bmpq_t\b)")
comment_set = set(["FILE", "mpz_t", "mpq_t"])
c_types = set(["char", "short", "long", "int", "float", "double"])
c_types = set(["void", "char", "short", "long", "int", "float", "double"])
type_modifers = re.compile(r"\*|(\bconst\b)|(\bunsigned\b)|(\bsigned\b)")
import_dict = {}

Expand Down Expand Up @@ -79,13 +84,15 @@ def undecorate(str):
remove variable name, const, ``*``, etc. to just get types
"""
ret = str.strip()
ret = ret[:ret.rfind(' ')]
if ' ' in ret:
ret = ret[:ret.rfind(' ')]
ret = re.sub(type_modifers, '', ret)
return ret.strip()

def get_parameter_types(str):
params = str[str.find("(") + 1 : str.rfind(")")].split(",")
return [undecorate(s) for s in params]
params.append(str.split()[0])
return [undecorate(s) for s in params if s]

def clean_types(function):
ret = function.strip()
Expand All @@ -98,8 +105,15 @@ def get_functions(file):
Get a list of functions from an rst file
"""
ret = []
macros = []
in_list = False
for line in file:
# Keep track of the macros
# We want to give them types in cython...
if line.startswith('.. macro'):
macros.append(line.strip())
continue

m = is_func.match(line)
if m:
ret.append( clean_types(line[m.end():]))
Expand All @@ -110,7 +124,7 @@ def get_functions(file):
in_list = False
else:
ret.append(clean_types(line))
return ret
return ret, macros

def get_all_types(function_list):
ret = set()
Expand All @@ -119,6 +133,11 @@ def get_all_types(function_list):
ret.add(t)
return ret


def has_types(line, types):
return any(t in types for t in get_parameter_types(line))


def gen_imports(function_list):
"""
Generate import statements for known functions.
Expand All @@ -132,10 +151,12 @@ def gen_imports(function_list):
imports[import_dict[t]].append(t)
else:
ret.add(t)
for k,v in imports.items():
types = ", ".join(v)
for k,v in sorted(imports.items()):
types = ", ".join(sorted(v))
print("from flint.flintlib." + k + " cimport " + types)
return ret
return sorted(ret)



def generate_pxd_file(h_name, opts):
fill_import_dict(opts.flint_lib_dir)
Expand All @@ -146,14 +167,18 @@ def generate_pxd_file(h_name, opts):
docdir = opts.flint_doc_dir
name = name[6:]
with open(os.path.join(docdir, name + ".rst")) as f:
l = get_functions(f)
s = gen_imports(l)
l, macros = get_functions(f)
unknown_types = gen_imports(l)
print()
for t in unknown_types:
print("# unknown type " + t)
print()
print ("\n# unimported types ", s - comment_set)
for m in macros:
print("# " + m)
print()
print(r'cdef extern from "' + h_name +r'.h":')
for f in l:
if comment_types.search(f):
if has_types(f, unknown_types):
print(" # " + f)
else:
print(" " + f)
Expand Down
2 changes: 1 addition & 1 deletion src/flint/flint_base/flint_context.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flint.flintlib.arf cimport (
from flint.flintlib.arf_types cimport (
arf_rnd_t,
)

Expand Down
4 changes: 2 additions & 2 deletions src/flint/flint_base/flint_context.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flint.flintlib.arf cimport ARF_RND_DOWN
from flint.flintlib.arf_types cimport arf_rnd_t
from flint.flintlib.flint cimport (
flint_cleanup,
flint_get_num_threads,
Expand All @@ -12,7 +12,7 @@ cdef class FlintContext:

def default(self):
self.pretty = True
self.rnd = ARF_RND_DOWN
self.rnd = arf_rnd_t.ARF_RND_DOWN
self.prec = 53
self.unicode = False
self.threads = 1
Expand Down
40 changes: 25 additions & 15 deletions src/flint/flintlib/acb.pxd
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
from flint.flintlib.flint cimport ulong, slong, flint_rand_t
from flint.flintlib.arb cimport arb_struct, arb_t, arb_ptr
from flint.flintlib.acb_types cimport acb_ptr, acb_srcptr, acb_t
from flint.flintlib.arb_types cimport arb_ptr, arb_srcptr, arb_t, mag_srcptr, mag_t
from flint.flintlib.arf_types cimport arf_srcptr, arf_t
from flint.flintlib.flint cimport flint_rand_t, fmpz_struct, slong, ulong
from flint.flintlib.fmpq cimport fmpq_t
from flint.flintlib.fmpz cimport fmpz_t, fmpz_struct
from flint.flintlib.arf cimport arf_t, arf_srcptr
from flint.flintlib.mag cimport mag_t, mag_srcptr
from flint.flintlib.fmpz_types cimport fmpz_t

cdef extern from "flint/acb.h":
ctypedef struct acb_struct:
arb_struct real
arb_struct imag

ctypedef acb_struct * acb_ptr
ctypedef const acb_struct * acb_srcptr
ctypedef acb_struct acb_t[1]
# unknown type FILE

arb_ptr acb_realref(const acb_t x)
arb_ptr acb_imagref(const acb_t x)
# .. macro:: acb_realref(x)
# .. macro:: acb_imagref(x)

cdef extern from "flint/acb.h":
void acb_init(acb_t x)
void acb_clear(acb_t x)
acb_ptr _acb_vec_init(slong n)
Expand Down Expand Up @@ -47,12 +41,16 @@ cdef extern from "flint/acb.h":
void acb_add_error_arb(acb_t x, const arb_t err)
void acb_get_mid(acb_t m, const acb_t x)
void acb_print(const acb_t x)
# void acb_fprint(FILE * file, const acb_t x)
void acb_printd(const acb_t x, slong digits)
# void acb_fprintd(FILE * file, const acb_t x, slong digits)
void acb_printn(const acb_t x, slong digits, ulong flags)
# void acb_fprintn(FILE * file, const acb_t x, slong digits, ulong flags)
void acb_randtest(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
void acb_randtest_special(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
void acb_randtest_precise(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
void acb_randtest_param(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
void acb_urandom(acb_t z, flint_rand_t state, slong prec)
int acb_is_zero(const acb_t z)
int acb_is_one(const acb_t z)
int acb_is_finite(const acb_t z)
Expand Down Expand Up @@ -105,6 +103,7 @@ cdef extern from "flint/acb.h":
void acb_sub(acb_t z, const acb_t x, const acb_t y, slong prec)
void acb_mul_onei(acb_t z, const acb_t x)
void acb_div_onei(acb_t z, const acb_t x)
void acb_mul_i_pow_si(acb_t z, const acb_t x, slong k)
void acb_mul_ui(acb_t z, const acb_t x, ulong y, slong prec)
void acb_mul_si(acb_t z, const acb_t x, slong y, slong prec)
void acb_mul_fmpz(acb_t z, const acb_t x, const fmpz_t y, slong prec)
Expand Down Expand Up @@ -144,6 +143,7 @@ cdef extern from "flint/acb.h":
void acb_sqrt_analytic(acb_t r, const acb_t z, int analytic, slong prec)
void acb_rsqrt(acb_t r, const acb_t z, slong prec)
void acb_rsqrt_analytic(acb_t r, const acb_t z, int analytic, slong prec)
void acb_sqrts(acb_t y1, acb_t y2, const acb_t x, slong prec)
void acb_quadratic_roots_fmpz(acb_t r1, acb_t r2, const fmpz_t a, const fmpz_t b, const fmpz_t c, slong prec)
void acb_root_ui(acb_t r, const acb_t z, ulong k, slong prec)
void acb_pow_fmpz(acb_t y, const acb_t b, const fmpz_t e, slong prec)
Expand Down Expand Up @@ -227,9 +227,16 @@ cdef extern from "flint/acb.h":
void _acb_vec_zero(acb_ptr A, slong n)
int _acb_vec_is_zero(acb_srcptr vec, slong len)
int _acb_vec_is_real(acb_srcptr v, slong len)
int _acb_vec_is_finite(acb_srcptr vec, slong len)
int _acb_vec_equal(acb_srcptr vec1, acb_srcptr vec2, slong len)
int _acb_vec_overlaps(acb_srcptr vec1, acb_srcptr vec2, slong len)
int _acb_vec_contains(acb_srcptr vec1, acb_srcptr vec2, slong len)
void _acb_vec_set(acb_ptr res, acb_srcptr vec, slong len)
void _acb_vec_set_round(acb_ptr res, acb_srcptr vec, slong len, slong prec)
void _acb_vec_swap(acb_ptr vec1, acb_ptr vec2, slong len)
void _acb_vec_get_real(arb_ptr re, acb_srcptr vec, slong len)
void _acb_vec_get_imag(arb_ptr im, acb_srcptr vec, slong len)
void _acb_vec_set_real_imag(acb_ptr vec, arb_srcptr re, arb_srcptr im, slong len)
void _acb_vec_neg(acb_ptr res, acb_srcptr vec, slong len)
void _acb_vec_add(acb_ptr res, acb_srcptr vec1, acb_srcptr vec2, slong len, slong prec)
void _acb_vec_sub(acb_ptr res, acb_srcptr vec1, acb_srcptr vec2, slong len, slong prec)
Expand All @@ -245,6 +252,7 @@ cdef extern from "flint/acb.h":
void _acb_vec_scalar_div_arb(acb_ptr res, acb_srcptr vec, slong len, const arb_t c, slong prec)
void _acb_vec_scalar_mul_fmpz(acb_ptr res, acb_srcptr vec, slong len, const fmpz_t c, slong prec)
void _acb_vec_scalar_div_fmpz(acb_ptr res, acb_srcptr vec, slong len, const fmpz_t c, slong prec)
void _acb_vec_sqr(acb_ptr res, acb_srcptr vec, slong len, slong prec)
slong _acb_vec_bits(acb_srcptr vec, slong len)
void _acb_vec_set_powers(acb_ptr xs, const acb_t x, slong len, slong prec)
void _acb_vec_unit_roots(acb_ptr z, slong order, slong len, slong prec)
Expand All @@ -254,3 +262,5 @@ cdef extern from "flint/acb.h":
void _acb_vec_trim(acb_ptr res, acb_srcptr vec, slong len)
int _acb_vec_get_unique_fmpz_vec(fmpz_struct * res, acb_srcptr vec, slong len)
void _acb_vec_sort_pretty(acb_ptr vec, slong len)
void _acb_vec_printd(acb_srcptr vec, slong len, slong digits)
void _acb_vec_printn(acb_srcptr vec, slong len, slong digits, ulong flags)
5 changes: 2 additions & 3 deletions src/flint/flintlib/acb_dirichlet.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ from flint.flintlib.dirichlet cimport dirichlet_group_t, dirichlet_char_t
from flint.flintlib.flint cimport ulong, slong
from flint.flintlib.acb_poly cimport acb_poly_t
from flint.flintlib.fmpz cimport fmpz_t
from flint.flintlib.arb cimport arb_t, arb_ptr
from flint.flintlib.mag cimport mag_t, mag_struct
from flint.flintlib.acb cimport acb_struct, acb_srcptr
from flint.flintlib.arb_types cimport mag_t, mag_struct, arb_t, arb_ptr
from flint.flintlib.acb_types cimport acb_struct, acb_srcptr
from flint.flintlib.fmpq cimport fmpq_t
from flint.flintlib.arf cimport arf_t
from flint.flintlib.arb cimport arb_srcptr
Expand Down
11 changes: 8 additions & 3 deletions src/flint/flintlib/acb_hypgeom.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from flint.flintlib.acb cimport acb_t, acb_srcptr, acb_ptr
from flint.flintlib.acb_poly cimport acb_poly_t, acb_poly_struct
from flint.flintlib.mag cimport mag_t
from flint.flintlib.flint cimport ulong, slong
from flint.flintlib.acb_types cimport (
acb_t,
acb_srcptr,
acb_ptr,
acb_poly_t,
acb_poly_struct,
)
from flint.flintlib.arb_types cimport mag_t

cdef extern from "flint/acb_hypgeom.h":
# from here on is parsed
Expand Down
42 changes: 20 additions & 22 deletions src/flint/flintlib/acb_mat.pxd
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
from flint.flintlib.flint cimport ulong, flint_rand_t, slong
from flint.flintlib.fmpz_mat cimport fmpz_mat_t
from flint.flintlib.acb_types cimport acb_mat_t, acb_poly_t, acb_ptr, acb_srcptr, acb_t
from flint.flintlib.arb_types cimport arb_mat_t, arb_t, mag_t
from flint.flintlib.flint cimport flint_rand_t, slong, ulong
from flint.flintlib.fmpq_mat cimport fmpq_mat_t
from flint.flintlib.mag cimport mag_t
from flint.flintlib.fmpz cimport fmpz_t
from flint.flintlib.acb_poly cimport acb_poly_t
from flint.flintlib.arb cimport arb_t
from flint.flintlib.acb cimport acb_ptr, acb_struct, acb_t, acb_srcptr
from flint.flintlib.arb_mat cimport arb_mat_t
from flint.flintlib.fmpz_types cimport fmpz_mat_t, fmpz_t

cdef extern from "flint/acb_mat.h":
ctypedef struct acb_mat_struct:
acb_ptr entries
long r
long c
acb_ptr * rows
# unknown type FILE

ctypedef acb_mat_struct acb_mat_t[1]
#macros
acb_struct * acb_mat_entry(acb_mat_t mat, long i, long j)
# .. macro:: acb_mat_entry(mat, i, j)
# .. macro:: acb_mat_nrows(mat)
# .. macro:: acb_mat_ncols(mat)

long acb_mat_nrows(const acb_mat_t x)
long acb_mat_ncols(const acb_mat_t x)
# from here on is parsed
cdef extern from "flint/acb_mat.h":
void acb_mat_init(acb_mat_t mat, slong r, slong c)
void acb_mat_clear(acb_mat_t mat)
slong acb_mat_allocated_bytes(const acb_mat_t x)
Expand All @@ -33,9 +22,13 @@ cdef extern from "flint/acb_mat.h":
void acb_mat_set_fmpq_mat(acb_mat_t dest, const fmpq_mat_t src, slong prec)
void acb_mat_set_arb_mat(acb_mat_t dest, const arb_mat_t src)
void acb_mat_set_round_arb_mat(acb_mat_t dest, const arb_mat_t src, slong prec)
void acb_mat_get_real(arb_mat_t re, const arb_mat_t mat)
void acb_mat_get_imag(arb_mat_t im, const arb_mat_t mat)
void acb_mat_set_real_imag(acb_mat_t mat, const arb_mat_t re, const arb_mat_t im)
void acb_mat_randtest(acb_mat_t mat, flint_rand_t state, slong prec, slong mag_bits)
void acb_mat_randtest_eig(acb_mat_t mat, flint_rand_t state, acb_srcptr E, slong prec)
void acb_mat_printd(const acb_mat_t mat, slong digits)
# void acb_mat_fprintd(FILE * file, const acb_mat_t mat, slong digits)
int acb_mat_equal(const acb_mat_t mat1, const acb_mat_t mat2)
int acb_mat_overlaps(const acb_mat_t mat1, const acb_mat_t mat2)
int acb_mat_contains(const acb_mat_t mat1, const acb_mat_t mat2)
Expand All @@ -55,13 +48,14 @@ cdef extern from "flint/acb_mat.h":
void acb_mat_zero(acb_mat_t mat)
void acb_mat_one(acb_mat_t mat)
void acb_mat_ones(acb_mat_t mat)
void acb_mat_onei(acb_mat_t mat)
void acb_mat_indeterminate(acb_mat_t mat)
void acb_mat_dft(acb_mat_t mat, int type, slong prec)
void acb_mat_transpose(acb_mat_t dest, const acb_mat_t src)
void acb_mat_conjugate_transpose(acb_mat_t dest, const acb_mat_t src)
void acb_mat_conjugate(acb_mat_t dest, const acb_mat_t src)
void acb_mat_bound_inf_norm(mag_t b, const acb_mat_t A)
void acb_mat_frobenius_norm(acb_t res, const acb_mat_t A, slong prec)
void acb_mat_frobenius_norm(arb_t res, const acb_mat_t A, slong prec)
void acb_mat_bound_frobenius_norm(mag_t res, const acb_mat_t A)
void acb_mat_neg(acb_mat_t dest, const acb_mat_t src)
void acb_mat_add(acb_mat_t res, const acb_mat_t mat1, const acb_mat_t mat2, slong prec)
Expand All @@ -88,6 +82,10 @@ cdef extern from "flint/acb_mat.h":
void acb_mat_scalar_div_fmpz(acb_mat_t B, const acb_mat_t A, const fmpz_t c, slong prec)
void acb_mat_scalar_div_arb(acb_mat_t B, const acb_mat_t A, const arb_t c, slong prec)
void acb_mat_scalar_div_acb(acb_mat_t B, const acb_mat_t A, const acb_t c, slong prec)
void _acb_mat_vector_mul_row(acb_ptr res, acb_srcptr v, const acb_mat_t A, slong prec)
void _acb_mat_vector_mul_col(acb_ptr res, const acb_mat_t A, acb_srcptr v, slong prec)
void acb_mat_vector_mul_row(acb_ptr res, acb_srcptr v, const acb_mat_t A, slong prec)
void acb_mat_vector_mul_col(acb_ptr res, const acb_mat_t A, acb_srcptr v, slong prec)
int acb_mat_lu_classical(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
int acb_mat_lu_recursive(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
int acb_mat_lu(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
Expand Down Expand Up @@ -124,7 +122,7 @@ cdef extern from "flint/acb_mat.h":
void acb_mat_add_error_mag(acb_mat_t mat, const mag_t err)
int acb_mat_approx_eig_qr(acb_ptr E, acb_mat_t L, acb_mat_t R, const acb_mat_t A, const mag_t tol, slong maxiter, slong prec)
void acb_mat_eig_global_enclosure(mag_t eps, const acb_mat_t A, acb_srcptr E, const acb_mat_t R, slong prec)
void acb_mat_eig_enclosure_rump(acb_t l, acb_mat_t J, acb_mat_t R, const acb_mat_t A, const acb_t lambda_approx, const acb_mat_t R_approx, slong prec)
void acb_mat_eig_enclosure_rump(acb_t lambda_, acb_mat_t J, acb_mat_t R, const acb_mat_t A, const acb_t lambda_approx, const acb_mat_t R_approx, slong prec)
int acb_mat_eig_simple_rump(acb_ptr E, acb_mat_t L, acb_mat_t R, const acb_mat_t A, acb_srcptr E_approx, const acb_mat_t R_approx, slong prec)
int acb_mat_eig_simple_vdhoeven_mourrain(acb_ptr E, acb_mat_t L, acb_mat_t R, const acb_mat_t A, acb_srcptr E_approx, const acb_mat_t R_approx, slong prec)
int acb_mat_eig_simple(acb_ptr E, acb_mat_t L, acb_mat_t R, const acb_mat_t A, acb_srcptr E_approx, const acb_mat_t R_approx, slong prec)
Expand Down
Loading
Loading