Skip to content

Commit 137e7a6

Browse files
Merge pull request #215 from oscarbenjamin/pr_auto_pxd
Automatically generate `fmpz_*` pxds from FLINT headers
2 parents 397d973 + a2212fd commit 137e7a6

40 files changed

+1025
-778
lines changed

bin/all_rst_to_pxd.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
FLINT_DOC_DIR=$1
4+
5+
set -e
6+
7+
modules="\
8+
fmpz\
9+
fmpz_factor\
10+
fmpz_poly\
11+
fmpz_poly_factor\
12+
fmpz_mat\
13+
fmpz_lll\
14+
arf\
15+
arb\
16+
arb_poly\
17+
arb_mat\
18+
acb\
19+
acb_poly\
20+
acb_mat\
21+
"
22+
23+
for module in $modules; do
24+
echo "Processing $module"
25+
bin/rst_to_pxd.py flint/$module --flint-doc-dir=$FLINT_DOC_DIR > src/flint/flintlib/$module.pxd
26+
done

bin/rst_to_pxd.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@
4040
# recognize a function definition in rst
4141
is_func = re.compile(r"\.\.( )+(c:)?function( )*::")
4242
# rename types to avoid python -- c name collisions
43-
rename_types = [(re.compile(r"\bfmpz\b"),"fmpz_struct"),(re.compile(r"\bfmpq\b"), "fmpq_struct")]
43+
rename_types = [
44+
(re.compile(r"\bfmpz\b"),"fmpz_struct"),
45+
(re.compile(r"\bfmpq\b"), "fmpq_struct"),
46+
(re.compile(r"\bin\b"), "in_"),
47+
(re.compile(r"\blambda\b"), "lambda_"),
48+
]
4449
# comment out functions which use these types
4550
comment_types = re.compile(r"(\bFILE\b)|(\bmpz_t\b)|(\bmpq_t\b)")
4651
comment_set = set(["FILE", "mpz_t", "mpq_t"])
47-
c_types = set(["char", "short", "long", "int", "float", "double"])
52+
c_types = set(["void", "char", "short", "long", "int", "float", "double"])
4853
type_modifers = re.compile(r"\*|(\bconst\b)|(\bunsigned\b)|(\bsigned\b)")
4954
import_dict = {}
5055

@@ -79,13 +84,15 @@ def undecorate(str):
7984
remove variable name, const, ``*``, etc. to just get types
8085
"""
8186
ret = str.strip()
82-
ret = ret[:ret.rfind(' ')]
87+
if ' ' in ret:
88+
ret = ret[:ret.rfind(' ')]
8389
ret = re.sub(type_modifers, '', ret)
8490
return ret.strip()
8591

8692
def get_parameter_types(str):
8793
params = str[str.find("(") + 1 : str.rfind(")")].split(",")
88-
return [undecorate(s) for s in params]
94+
params.append(str.split()[0])
95+
return [undecorate(s) for s in params if s]
8996

9097
def clean_types(function):
9198
ret = function.strip()
@@ -98,8 +105,15 @@ def get_functions(file):
98105
Get a list of functions from an rst file
99106
"""
100107
ret = []
108+
macros = []
101109
in_list = False
102110
for line in file:
111+
# Keep track of the macros
112+
# We want to give them types in cython...
113+
if line.startswith('.. macro'):
114+
macros.append(line.strip())
115+
continue
116+
103117
m = is_func.match(line)
104118
if m:
105119
ret.append( clean_types(line[m.end():]))
@@ -110,7 +124,7 @@ def get_functions(file):
110124
in_list = False
111125
else:
112126
ret.append(clean_types(line))
113-
return ret
127+
return ret, macros
114128

115129
def get_all_types(function_list):
116130
ret = set()
@@ -119,6 +133,11 @@ def get_all_types(function_list):
119133
ret.add(t)
120134
return ret
121135

136+
137+
def has_types(line, types):
138+
return any(t in types for t in get_parameter_types(line))
139+
140+
122141
def gen_imports(function_list):
123142
"""
124143
Generate import statements for known functions.
@@ -132,10 +151,12 @@ def gen_imports(function_list):
132151
imports[import_dict[t]].append(t)
133152
else:
134153
ret.add(t)
135-
for k,v in imports.items():
136-
types = ", ".join(v)
154+
for k,v in sorted(imports.items()):
155+
types = ", ".join(sorted(v))
137156
print("from flint.flintlib." + k + " cimport " + types)
138-
return ret
157+
return sorted(ret)
158+
159+
139160

140161
def generate_pxd_file(h_name, opts):
141162
fill_import_dict(opts.flint_lib_dir)
@@ -146,14 +167,18 @@ def generate_pxd_file(h_name, opts):
146167
docdir = opts.flint_doc_dir
147168
name = name[6:]
148169
with open(os.path.join(docdir, name + ".rst")) as f:
149-
l = get_functions(f)
150-
s = gen_imports(l)
170+
l, macros = get_functions(f)
171+
unknown_types = gen_imports(l)
172+
print()
173+
for t in unknown_types:
174+
print("# unknown type " + t)
151175
print()
152-
print ("\n# unimported types ", s - comment_set)
176+
for m in macros:
177+
print("# " + m)
153178
print()
154179
print(r'cdef extern from "' + h_name +r'.h":')
155180
for f in l:
156-
if comment_types.search(f):
181+
if has_types(f, unknown_types):
157182
print(" # " + f)
158183
else:
159184
print(" " + f)

src/flint/flint_base/flint_context.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flint.flintlib.arf cimport (
1+
from flint.flintlib.arf_types cimport (
22
arf_rnd_t,
33
)
44

src/flint/flint_base/flint_context.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flint.flintlib.arf cimport ARF_RND_DOWN
1+
from flint.flintlib.arf_types cimport arf_rnd_t
22
from flint.flintlib.flint cimport (
33
flint_cleanup,
44
flint_get_num_threads,
@@ -12,7 +12,7 @@ cdef class FlintContext:
1212

1313
def default(self):
1414
self.pretty = True
15-
self.rnd = ARF_RND_DOWN
15+
self.rnd = arf_rnd_t.ARF_RND_DOWN
1616
self.prec = 53
1717
self.unicode = False
1818
self.threads = 1

src/flint/flintlib/acb.pxd

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
from flint.flintlib.flint cimport ulong, slong, flint_rand_t
2-
from flint.flintlib.arb cimport arb_struct, arb_t, arb_ptr
1+
from flint.flintlib.acb_types cimport acb_ptr, acb_srcptr, acb_t
2+
from flint.flintlib.arb_types cimport arb_ptr, arb_srcptr, arb_t, mag_srcptr, mag_t
3+
from flint.flintlib.arf_types cimport arf_srcptr, arf_t
4+
from flint.flintlib.flint cimport flint_rand_t, fmpz_struct, slong, ulong
35
from flint.flintlib.fmpq cimport fmpq_t
4-
from flint.flintlib.fmpz cimport fmpz_t, fmpz_struct
5-
from flint.flintlib.arf cimport arf_t, arf_srcptr
6-
from flint.flintlib.mag cimport mag_t, mag_srcptr
6+
from flint.flintlib.fmpz_types cimport fmpz_t
77

8-
cdef extern from "flint/acb.h":
9-
ctypedef struct acb_struct:
10-
arb_struct real
11-
arb_struct imag
12-
13-
ctypedef acb_struct * acb_ptr
14-
ctypedef const acb_struct * acb_srcptr
15-
ctypedef acb_struct acb_t[1]
8+
# unknown type FILE
169

17-
arb_ptr acb_realref(const acb_t x)
18-
arb_ptr acb_imagref(const acb_t x)
10+
# .. macro:: acb_realref(x)
11+
# .. macro:: acb_imagref(x)
1912

13+
cdef extern from "flint/acb.h":
2014
void acb_init(acb_t x)
2115
void acb_clear(acb_t x)
2216
acb_ptr _acb_vec_init(slong n)
@@ -47,12 +41,16 @@ cdef extern from "flint/acb.h":
4741
void acb_add_error_arb(acb_t x, const arb_t err)
4842
void acb_get_mid(acb_t m, const acb_t x)
4943
void acb_print(const acb_t x)
44+
# void acb_fprint(FILE * file, const acb_t x)
5045
void acb_printd(const acb_t x, slong digits)
46+
# void acb_fprintd(FILE * file, const acb_t x, slong digits)
5147
void acb_printn(const acb_t x, slong digits, ulong flags)
48+
# void acb_fprintn(FILE * file, const acb_t x, slong digits, ulong flags)
5249
void acb_randtest(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
5350
void acb_randtest_special(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
5451
void acb_randtest_precise(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
5552
void acb_randtest_param(acb_t z, flint_rand_t state, slong prec, slong mag_bits)
53+
void acb_urandom(acb_t z, flint_rand_t state, slong prec)
5654
int acb_is_zero(const acb_t z)
5755
int acb_is_one(const acb_t z)
5856
int acb_is_finite(const acb_t z)
@@ -105,6 +103,7 @@ cdef extern from "flint/acb.h":
105103
void acb_sub(acb_t z, const acb_t x, const acb_t y, slong prec)
106104
void acb_mul_onei(acb_t z, const acb_t x)
107105
void acb_div_onei(acb_t z, const acb_t x)
106+
void acb_mul_i_pow_si(acb_t z, const acb_t x, slong k)
108107
void acb_mul_ui(acb_t z, const acb_t x, ulong y, slong prec)
109108
void acb_mul_si(acb_t z, const acb_t x, slong y, slong prec)
110109
void acb_mul_fmpz(acb_t z, const acb_t x, const fmpz_t y, slong prec)
@@ -144,6 +143,7 @@ cdef extern from "flint/acb.h":
144143
void acb_sqrt_analytic(acb_t r, const acb_t z, int analytic, slong prec)
145144
void acb_rsqrt(acb_t r, const acb_t z, slong prec)
146145
void acb_rsqrt_analytic(acb_t r, const acb_t z, int analytic, slong prec)
146+
void acb_sqrts(acb_t y1, acb_t y2, const acb_t x, slong prec)
147147
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)
148148
void acb_root_ui(acb_t r, const acb_t z, ulong k, slong prec)
149149
void acb_pow_fmpz(acb_t y, const acb_t b, const fmpz_t e, slong prec)
@@ -227,9 +227,16 @@ cdef extern from "flint/acb.h":
227227
void _acb_vec_zero(acb_ptr A, slong n)
228228
int _acb_vec_is_zero(acb_srcptr vec, slong len)
229229
int _acb_vec_is_real(acb_srcptr v, slong len)
230+
int _acb_vec_is_finite(acb_srcptr vec, slong len)
231+
int _acb_vec_equal(acb_srcptr vec1, acb_srcptr vec2, slong len)
232+
int _acb_vec_overlaps(acb_srcptr vec1, acb_srcptr vec2, slong len)
233+
int _acb_vec_contains(acb_srcptr vec1, acb_srcptr vec2, slong len)
230234
void _acb_vec_set(acb_ptr res, acb_srcptr vec, slong len)
231235
void _acb_vec_set_round(acb_ptr res, acb_srcptr vec, slong len, slong prec)
232236
void _acb_vec_swap(acb_ptr vec1, acb_ptr vec2, slong len)
237+
void _acb_vec_get_real(arb_ptr re, acb_srcptr vec, slong len)
238+
void _acb_vec_get_imag(arb_ptr im, acb_srcptr vec, slong len)
239+
void _acb_vec_set_real_imag(acb_ptr vec, arb_srcptr re, arb_srcptr im, slong len)
233240
void _acb_vec_neg(acb_ptr res, acb_srcptr vec, slong len)
234241
void _acb_vec_add(acb_ptr res, acb_srcptr vec1, acb_srcptr vec2, slong len, slong prec)
235242
void _acb_vec_sub(acb_ptr res, acb_srcptr vec1, acb_srcptr vec2, slong len, slong prec)
@@ -245,6 +252,7 @@ cdef extern from "flint/acb.h":
245252
void _acb_vec_scalar_div_arb(acb_ptr res, acb_srcptr vec, slong len, const arb_t c, slong prec)
246253
void _acb_vec_scalar_mul_fmpz(acb_ptr res, acb_srcptr vec, slong len, const fmpz_t c, slong prec)
247254
void _acb_vec_scalar_div_fmpz(acb_ptr res, acb_srcptr vec, slong len, const fmpz_t c, slong prec)
255+
void _acb_vec_sqr(acb_ptr res, acb_srcptr vec, slong len, slong prec)
248256
slong _acb_vec_bits(acb_srcptr vec, slong len)
249257
void _acb_vec_set_powers(acb_ptr xs, const acb_t x, slong len, slong prec)
250258
void _acb_vec_unit_roots(acb_ptr z, slong order, slong len, slong prec)
@@ -254,3 +262,5 @@ cdef extern from "flint/acb.h":
254262
void _acb_vec_trim(acb_ptr res, acb_srcptr vec, slong len)
255263
int _acb_vec_get_unique_fmpz_vec(fmpz_struct * res, acb_srcptr vec, slong len)
256264
void _acb_vec_sort_pretty(acb_ptr vec, slong len)
265+
void _acb_vec_printd(acb_srcptr vec, slong len, slong digits)
266+
void _acb_vec_printn(acb_srcptr vec, slong len, slong digits, ulong flags)

src/flint/flintlib/acb_dirichlet.pxd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ from flint.flintlib.dirichlet cimport dirichlet_group_t, dirichlet_char_t
33
from flint.flintlib.flint cimport ulong, slong
44
from flint.flintlib.acb_poly cimport acb_poly_t
55
from flint.flintlib.fmpz cimport fmpz_t
6-
from flint.flintlib.arb cimport arb_t, arb_ptr
7-
from flint.flintlib.mag cimport mag_t, mag_struct
8-
from flint.flintlib.acb cimport acb_struct, acb_srcptr
6+
from flint.flintlib.arb_types cimport mag_t, mag_struct, arb_t, arb_ptr
7+
from flint.flintlib.acb_types cimport acb_struct, acb_srcptr
98
from flint.flintlib.fmpq cimport fmpq_t
109
from flint.flintlib.arf cimport arf_t
1110
from flint.flintlib.arb cimport arb_srcptr

src/flint/flintlib/acb_hypgeom.pxd

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
from flint.flintlib.acb cimport acb_t, acb_srcptr, acb_ptr
2-
from flint.flintlib.acb_poly cimport acb_poly_t, acb_poly_struct
3-
from flint.flintlib.mag cimport mag_t
41
from flint.flintlib.flint cimport ulong, slong
2+
from flint.flintlib.acb_types cimport (
3+
acb_t,
4+
acb_srcptr,
5+
acb_ptr,
6+
acb_poly_t,
7+
acb_poly_struct,
8+
)
9+
from flint.flintlib.arb_types cimport mag_t
510

611
cdef extern from "flint/acb_hypgeom.h":
712
# from here on is parsed

src/flint/flintlib/acb_mat.pxd

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
from flint.flintlib.flint cimport ulong, flint_rand_t, slong
2-
from flint.flintlib.fmpz_mat cimport fmpz_mat_t
1+
from flint.flintlib.acb_types cimport acb_mat_t, acb_poly_t, acb_ptr, acb_srcptr, acb_t
2+
from flint.flintlib.arb_types cimport arb_mat_t, arb_t, mag_t
3+
from flint.flintlib.flint cimport flint_rand_t, slong, ulong
34
from flint.flintlib.fmpq_mat cimport fmpq_mat_t
4-
from flint.flintlib.mag cimport mag_t
5-
from flint.flintlib.fmpz cimport fmpz_t
6-
from flint.flintlib.acb_poly cimport acb_poly_t
7-
from flint.flintlib.arb cimport arb_t
8-
from flint.flintlib.acb cimport acb_ptr, acb_struct, acb_t, acb_srcptr
9-
from flint.flintlib.arb_mat cimport arb_mat_t
5+
from flint.flintlib.fmpz_types cimport fmpz_mat_t, fmpz_t
106

11-
cdef extern from "flint/acb_mat.h":
12-
ctypedef struct acb_mat_struct:
13-
acb_ptr entries
14-
long r
15-
long c
16-
acb_ptr * rows
7+
# unknown type FILE
178

18-
ctypedef acb_mat_struct acb_mat_t[1]
19-
#macros
20-
acb_struct * acb_mat_entry(acb_mat_t mat, long i, long j)
9+
# .. macro:: acb_mat_entry(mat, i, j)
10+
# .. macro:: acb_mat_nrows(mat)
11+
# .. macro:: acb_mat_ncols(mat)
2112

22-
long acb_mat_nrows(const acb_mat_t x)
23-
long acb_mat_ncols(const acb_mat_t x)
24-
# from here on is parsed
13+
cdef extern from "flint/acb_mat.h":
2514
void acb_mat_init(acb_mat_t mat, slong r, slong c)
2615
void acb_mat_clear(acb_mat_t mat)
2716
slong acb_mat_allocated_bytes(const acb_mat_t x)
@@ -33,9 +22,13 @@ cdef extern from "flint/acb_mat.h":
3322
void acb_mat_set_fmpq_mat(acb_mat_t dest, const fmpq_mat_t src, slong prec)
3423
void acb_mat_set_arb_mat(acb_mat_t dest, const arb_mat_t src)
3524
void acb_mat_set_round_arb_mat(acb_mat_t dest, const arb_mat_t src, slong prec)
25+
void acb_mat_get_real(arb_mat_t re, const arb_mat_t mat)
26+
void acb_mat_get_imag(arb_mat_t im, const arb_mat_t mat)
27+
void acb_mat_set_real_imag(acb_mat_t mat, const arb_mat_t re, const arb_mat_t im)
3628
void acb_mat_randtest(acb_mat_t mat, flint_rand_t state, slong prec, slong mag_bits)
3729
void acb_mat_randtest_eig(acb_mat_t mat, flint_rand_t state, acb_srcptr E, slong prec)
3830
void acb_mat_printd(const acb_mat_t mat, slong digits)
31+
# void acb_mat_fprintd(FILE * file, const acb_mat_t mat, slong digits)
3932
int acb_mat_equal(const acb_mat_t mat1, const acb_mat_t mat2)
4033
int acb_mat_overlaps(const acb_mat_t mat1, const acb_mat_t mat2)
4134
int acb_mat_contains(const acb_mat_t mat1, const acb_mat_t mat2)
@@ -55,13 +48,14 @@ cdef extern from "flint/acb_mat.h":
5548
void acb_mat_zero(acb_mat_t mat)
5649
void acb_mat_one(acb_mat_t mat)
5750
void acb_mat_ones(acb_mat_t mat)
51+
void acb_mat_onei(acb_mat_t mat)
5852
void acb_mat_indeterminate(acb_mat_t mat)
5953
void acb_mat_dft(acb_mat_t mat, int type, slong prec)
6054
void acb_mat_transpose(acb_mat_t dest, const acb_mat_t src)
6155
void acb_mat_conjugate_transpose(acb_mat_t dest, const acb_mat_t src)
6256
void acb_mat_conjugate(acb_mat_t dest, const acb_mat_t src)
6357
void acb_mat_bound_inf_norm(mag_t b, const acb_mat_t A)
64-
void acb_mat_frobenius_norm(acb_t res, const acb_mat_t A, slong prec)
58+
void acb_mat_frobenius_norm(arb_t res, const acb_mat_t A, slong prec)
6559
void acb_mat_bound_frobenius_norm(mag_t res, const acb_mat_t A)
6660
void acb_mat_neg(acb_mat_t dest, const acb_mat_t src)
6761
void acb_mat_add(acb_mat_t res, const acb_mat_t mat1, const acb_mat_t mat2, slong prec)
@@ -88,6 +82,10 @@ cdef extern from "flint/acb_mat.h":
8882
void acb_mat_scalar_div_fmpz(acb_mat_t B, const acb_mat_t A, const fmpz_t c, slong prec)
8983
void acb_mat_scalar_div_arb(acb_mat_t B, const acb_mat_t A, const arb_t c, slong prec)
9084
void acb_mat_scalar_div_acb(acb_mat_t B, const acb_mat_t A, const acb_t c, slong prec)
85+
void _acb_mat_vector_mul_row(acb_ptr res, acb_srcptr v, const acb_mat_t A, slong prec)
86+
void _acb_mat_vector_mul_col(acb_ptr res, const acb_mat_t A, acb_srcptr v, slong prec)
87+
void acb_mat_vector_mul_row(acb_ptr res, acb_srcptr v, const acb_mat_t A, slong prec)
88+
void acb_mat_vector_mul_col(acb_ptr res, const acb_mat_t A, acb_srcptr v, slong prec)
9189
int acb_mat_lu_classical(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
9290
int acb_mat_lu_recursive(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
9391
int acb_mat_lu(slong * perm, acb_mat_t LU, const acb_mat_t A, slong prec)
@@ -124,7 +122,7 @@ cdef extern from "flint/acb_mat.h":
124122
void acb_mat_add_error_mag(acb_mat_t mat, const mag_t err)
125123
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)
126124
void acb_mat_eig_global_enclosure(mag_t eps, const acb_mat_t A, acb_srcptr E, const acb_mat_t R, slong prec)
127-
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)
125+
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)
128126
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)
129127
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)
130128
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)

0 commit comments

Comments
 (0)