Skip to content

Commit 027dd1f

Browse files
Merge pull request #35 from oscarbenjamin/pr_cython3
Add `__radd__` etc methods for Cython 3.x
2 parents 69b4737 + 0370688 commit 027dd1f

30 files changed

+1343
-962
lines changed

.github/workflows/buildwheel.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
CIBW_BEFORE_ALL_MACOS: bin/cibw_before_all_macosx_x86_64.sh
4444
CIBW_BEFORE_ALL_WINDOWS: msys2 -c bin/cibw_before_all_windows.sh
4545
CIBW_BEFORE_BUILD_WINDOWS: msys2 -c bin/cibw_before_build_windows.sh
46-
CIBW_BEFORE_BUILD: pip install numpy cython delvewheel
46+
CIBW_BEFORE_BUILD: pip install numpy cython==3.0.0b2 delvewheel
4747
CIBW_ENVIRONMENT: >
4848
C_INCLUDE_PATH=$(pwd)/.local/include/
4949
LIBRARY_PATH=$(pwd)/.local/lib/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ MANIFEST
1515
*.egg-info
1616
.coverage
1717
*.swp
18+
.python-version

bin/build_dependencies_unix.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ do
4848
PATCH_GMP_ARM64=yes
4949
shift
5050
;;
51+
--use-gmp-github-mirror)
52+
USE_GMP_GITHUB_MIRROR=yes
53+
shift
54+
;;
5155
*)
5256
2>&1 echo "unrecognised argument:" $key
5357
exit 1
@@ -82,17 +86,22 @@ if [ $USE_GMP = "gmp" ]; then
8286
# #
8387
# ----------------------------------------------------------------------- #
8488

85-
curl -O https://gmplib.org/download/gmp/gmp-$GMPVER.tar.xz
89+
if [ $USE_GMP_GITHUB_MIRROR = "yes" ]; then
90+
# Needed in GitHub Actions because it is blocked from gmplib.org
91+
git clone https://github.com/oscarbenjamin/gmp_mirror.git
92+
cp gmp_mirror/gmp-$GMPVER.tar.xz .
93+
else
94+
curl -O https://gmplib.org/download/gmp/gmp-$GMPVER.tar.xz
95+
fi
96+
8697
tar xf gmp-$GMPVER.tar.xz
8798
cd gmp-$GMPVER
8899

89100
#
90101
# See https://github.com/aleaxit/gmpy/issues/350
91102
#
92-
# We need to patch GMP for OSX arm64 (Apple M1) hardware. This patch is
93-
# from the GMP repo but was applied after the release of GMP 6.2.1.
94-
# Hopefully when a newer version of GMP is released we will not need to
95-
# apply this patch any more.
103+
# We need to patch GMP for OSX arm64 (Apple M1) hardware for GMP 6.2.1.
104+
# Now with GMP 6.3.0 this should not be needed any more.
96105
#
97106
if [ $PATCH_GMP_ARM64 = "yes" ]; then
98107
echo
@@ -103,6 +112,7 @@ if [ $USE_GMP = "gmp" ]; then
103112
fi
104113

105114
# Show the output of configfsf.guess
115+
chmod +x configfsf.guess
106116
./configfsf.guess
107117
./configure --prefix=$PREFIX\
108118
--enable-fat\

bin/build_variables.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
PREFIX=$(pwd)/.local
1414
mkdir -p $PREFIX
1515

16-
GMPVER=6.2.1
16+
GMPVER=6.3.0
1717
YASMVER=1.3.0
1818
MPIRVER=3.0.0
1919
MPFRVER=4.1.0

bin/cibw_before_all_linux.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
yum install -y xz
44
bin/build_dependencies_unix.sh\
55
--gmp gmp\
6-
--host x86_64-pc-linux-gnu
6+
--host x86_64-pc-linux-gnu\
7+
--use-gmp-github-mirror

bin/cibw_before_all_macosx_arm64.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export LDFLAGS=" -arch arm64"
66
bin/build_dependencies_unix.sh\
77
--gmp gmp\
88
--host aarch64-apple-darwin\
9-
--patch-gmp-arm64
9+
--use-gmp-github-mirror

bin/cibw_before_all_macosx_x86_64.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
bin/build_dependencies_unix.sh\
44
--gmp gmp\
5-
--host x86_64-apple-darwin
5+
--host x86_64-apple-darwin\
6+
--use-gmp-github-mirror

bin/cibw_before_all_windows.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ cat setup.cfg
1717
pacman -S --noconfirm mingw-w64-x86_64-gcc m4 make mingw-w64-x86_64-tools-git
1818

1919
# This takes ~30mins
20-
bin/build_dependencies_unix.sh
20+
bin/build_dependencies_unix.sh --use-gmp-github-mirror

bin/cibw_before_build_windows.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ mv libpython${VER}.a libs
4040
# Install build dependencies #
4141
###################################################
4242

43-
pip install Cython numpy delvewheel
43+
pip install cython numpy delvewheel

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545

4646

4747
define_macros = []
48-
compiler_directives = {'language_level':3}
48+
compiler_directives = {
49+
'language_level': 3,
50+
'binding': False,
51+
}
4952

5053

5154
# Enable coverage tracing

src/flint/acb.pyx

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,18 @@ cdef class acb(flint_scalar):
156156
return (self.real._mpf_, self.imag._mpf_)
157157

158158
def __richcmp__(s, t, int op):
159-
cdef acb_struct sval[1]
160159
cdef acb_struct tval[1]
161160
cdef bint res
162-
cdef int stype, ttype
161+
cdef int ttype
163162
if not (op == 2 or op == 3):
164163
raise ValueError("comparing complex numbers")
165-
stype = acb_set_any_ref(sval, s)
166-
if stype == FMPZ_UNKNOWN:
167-
return NotImplemented
168164
ttype = acb_set_any_ref(tval, t)
169165
if ttype == FMPZ_UNKNOWN:
170166
return NotImplemented
171167
if op == 2:
172-
res = acb_eq(sval, tval)
168+
res = acb_eq(s.val, tval)
173169
else:
174-
res = acb_ne(sval, tval)
175-
if stype == FMPZ_TMP: acb_clear(sval)
170+
res = acb_ne(s.val, tval)
176171
if ttype == FMPZ_TMP: acb_clear(tval)
177172
return res
178173

@@ -363,92 +358,116 @@ cdef class acb(flint_scalar):
363358
return res
364359

365360
def __add__(s, t):
366-
cdef acb_struct sval[1]
367361
cdef acb_struct tval[1]
368-
cdef int stype, ttype
369-
stype = acb_set_any_ref(sval, s)
370-
if stype == FMPZ_UNKNOWN:
362+
cdef int ttype
363+
ttype = acb_set_any_ref(tval, t)
364+
if ttype == FMPZ_UNKNOWN:
371365
return NotImplemented
366+
u = acb.__new__(acb)
367+
acb_add((<acb>u).val, (<acb>s).val, tval, getprec())
368+
if ttype == FMPZ_TMP: acb_clear(tval)
369+
return u
370+
371+
def __radd__(s, t):
372+
cdef acb_struct tval[1]
373+
cdef int ttype
372374
ttype = acb_set_any_ref(tval, t)
373375
if ttype == FMPZ_UNKNOWN:
374376
return NotImplemented
375377
u = acb.__new__(acb)
376-
acb_add((<acb>u).val, sval, tval, getprec())
377-
if stype == FMPZ_TMP: acb_clear(sval)
378+
acb_add((<acb>u).val, tval, s.val, getprec())
378379
if ttype == FMPZ_TMP: acb_clear(tval)
379380
return u
380381

381382
def __sub__(s, t):
382-
cdef acb_struct sval[1]
383383
cdef acb_struct tval[1]
384-
cdef int stype, ttype
385-
stype = acb_set_any_ref(sval, s)
386-
if stype == FMPZ_UNKNOWN:
384+
cdef int ttype
385+
ttype = acb_set_any_ref(tval, t)
386+
if ttype == FMPZ_UNKNOWN:
387387
return NotImplemented
388+
u = acb.__new__(acb)
389+
acb_sub((<acb>u).val, (<acb>s).val, tval, getprec())
390+
if ttype == FMPZ_TMP: acb_clear(tval)
391+
return u
392+
393+
def __rsub__(s, t):
394+
cdef acb_struct tval[1]
395+
cdef int ttype
388396
ttype = acb_set_any_ref(tval, t)
389397
if ttype == FMPZ_UNKNOWN:
390398
return NotImplemented
391399
u = acb.__new__(acb)
392-
acb_sub((<acb>u).val, sval, tval, getprec())
393-
if stype == FMPZ_TMP: acb_clear(sval)
400+
acb_sub((<acb>u).val, tval, s.val, getprec())
394401
if ttype == FMPZ_TMP: acb_clear(tval)
395402
return u
396403

397404
def __mul__(s, t):
398-
cdef acb_struct sval[1]
399405
cdef acb_struct tval[1]
400-
cdef int stype, ttype
401-
stype = acb_set_any_ref(sval, s)
402-
if stype == FMPZ_UNKNOWN:
403-
return NotImplemented
406+
cdef int ttype
404407
ttype = acb_set_any_ref(tval, t)
405408
if ttype == FMPZ_UNKNOWN:
406409
return NotImplemented
407410
u = acb.__new__(acb)
408-
acb_mul((<acb>u).val, sval, tval, getprec())
409-
if stype == FMPZ_TMP: acb_clear(sval)
411+
acb_mul((<acb>u).val, (<acb>s).val, tval, getprec())
410412
if ttype == FMPZ_TMP: acb_clear(tval)
411413
return u
412414

413-
# important: must not be cdef because of cython magic
414-
@staticmethod
415-
def _div_(s, t):
416-
cdef acb_struct sval[1]
415+
def __rmul__(s, t):
417416
cdef acb_struct tval[1]
418-
cdef int stype, ttype
419-
stype = acb_set_any_ref(sval, s)
420-
if stype == FMPZ_UNKNOWN:
421-
return NotImplemented
417+
cdef int ttype
422418
ttype = acb_set_any_ref(tval, t)
423419
if ttype == FMPZ_UNKNOWN:
424420
return NotImplemented
425421
u = acb.__new__(acb)
426-
acb_div((<acb>u).val, sval, tval, getprec())
427-
if stype == FMPZ_TMP: acb_clear(sval)
422+
acb_mul((<acb>u).val, tval, s.val, getprec())
428423
if ttype == FMPZ_TMP: acb_clear(tval)
429424
return u
430425

431426
def __truediv__(s, t):
432-
return acb._div_(s, t)
427+
cdef acb_struct tval[1]
428+
cdef int ttype
429+
ttype = acb_set_any_ref(tval, t)
430+
if ttype == FMPZ_UNKNOWN:
431+
return NotImplemented
432+
u = acb.__new__(acb)
433+
acb_div((<acb>u).val, (<acb>s).val, tval, getprec())
434+
if ttype == FMPZ_TMP: acb_clear(tval)
435+
return u
433436

434-
def __div__(s, t):
435-
return acb._div_(s, t)
437+
def __rtruediv__(s, t):
438+
cdef acb_struct tval[1]
439+
cdef int ttype
440+
ttype = acb_set_any_ref(tval, t)
441+
if ttype == FMPZ_UNKNOWN:
442+
return NotImplemented
443+
u = acb.__new__(acb)
444+
acb_div((<acb>u).val, tval, s.val, getprec())
445+
if ttype == FMPZ_TMP: acb_clear(tval)
446+
return u
436447

437448
def __pow__(s, t, u):
438-
cdef acb_struct sval[1]
439449
cdef acb_struct tval[1]
440-
cdef int stype, ttype
450+
cdef int ttype
441451
if u is not None:
442452
raise ValueError("modular exponentiation of complex number")
443-
stype = acb_set_any_ref(sval, s)
444-
if stype == FMPZ_UNKNOWN:
453+
ttype = acb_set_any_ref(tval, t)
454+
if ttype == FMPZ_UNKNOWN:
445455
return NotImplemented
456+
u = acb.__new__(acb)
457+
acb_pow((<acb>u).val, (<acb>s).val, tval, getprec())
458+
if ttype == FMPZ_TMP: acb_clear(tval)
459+
return u
460+
461+
def __rpow__(s, t, u):
462+
cdef acb_struct tval[1]
463+
cdef int ttype
464+
if u is not None:
465+
raise ValueError("modular exponentiation of complex number")
446466
ttype = acb_set_any_ref(tval, t)
447467
if ttype == FMPZ_UNKNOWN:
448468
return NotImplemented
449469
u = acb.__new__(acb)
450-
acb_pow((<acb>u).val, sval, tval, getprec())
451-
if stype == FMPZ_TMP: acb_clear(sval)
470+
acb_pow((<acb>u).val, tval, s.val, getprec())
452471
if ttype == FMPZ_TMP: acb_clear(tval)
453472
return u
454473

@@ -2560,4 +2579,3 @@ cdef class acb(flint_scalar):
25602579
acb_hypgeom_coulomb(NULL, (<acb>G).val, NULL, NULL,
25612580
(<acb>l).val, (<acb>eta).val, (<acb>self).val, getprec())
25622581
return G
2563-

0 commit comments

Comments
 (0)