Skip to content

Commit 4c9a2ef

Browse files
committed
Simplify backend handling in libintmath.py/libmpf.py
1 parent 5e57cb8 commit 4c9a2ef

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

mpmath/libmp/backend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
try:
2727
import gmpy2 as gmpy
2828
BACKEND = 'gmpy'
29-
MPZ = gmpy.mpz
30-
MPQ = gmpy.mpq
3129
except ImportError:
3230
pass
3331

32+
if gmpy:
33+
MPZ = gmpy.mpz
34+
MPQ = gmpy.mpq
35+
3436
MPZ_ZERO = MPZ(0)
3537
MPZ_ONE = MPZ(1)
3638
MPZ_TWO = MPZ(2)

mpmath/libmp/libintmath.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from functools import lru_cache
1212

13-
from .backend import BACKEND, MPZ, MPZ_ONE, MPZ_ZERO, gmpy
13+
from .backend import MPZ, MPZ_ONE, MPZ_ZERO, gmpy
1414

1515

1616
small_trailing = [0] * 256
@@ -73,8 +73,7 @@ def bitcount(n):
7373
"""Calculate bit size of abs(n)."""
7474
return MPZ(n).bit_length()
7575

76-
if BACKEND == 'gmpy':
77-
bitcount = gmpy.bit_length
76+
if gmpy and hasattr(MPZ, 'bit_scan1'):
7877
def trailing(n):
7978
return MPZ(n).bit_scan1() if n else MPZ(0)
8079

@@ -146,10 +145,10 @@ def numeral_gmpy(n, base=10, size=0, digits=stddigits):
146145
bd = numeral(B, base, half, digits).rjust(half, "0")
147146
return ad + bd
148147

149-
if BACKEND == "gmpy":
148+
numeral = numeral_python
149+
150+
if gmpy:
150151
numeral = numeral_gmpy
151-
else:
152-
numeral = numeral_python
153152

154153
_1_800 = 1<<800
155154
_1_600 = 1<<600
@@ -257,7 +256,7 @@ def sqrt_fixed(x, prec):
257256

258257
sqrt_fixed2 = sqrt_fixed
259258

260-
if BACKEND == 'gmpy':
259+
if gmpy:
261260
isqrt_small = isqrt_fast = isqrt = gmpy.isqrt
262261
sqrtrem = gmpy.isqrt_rem
263262
else:
@@ -270,10 +269,8 @@ def sqrt_fixed(x, prec):
270269
sqrtrem = sqrtrem_python
271270
_gcd2 = math.gcd
272271

273-
274-
if BACKEND == 'python':
275-
gcd = math.gcd
276-
elif BACKEND == 'gmpy':
272+
gcd = math.gcd
273+
if gmpy:
277274
gcd = gmpy.gcd
278275

279276

@@ -318,13 +315,12 @@ def ifac2(n, memo_pair=[{0:1}, {1:1}]):
318315
memo[k] = p
319316
return p
320317
ifac2_python = ifac2
318+
ifac = math.factorial
321319

322-
if BACKEND == 'gmpy':
320+
if gmpy:
323321
ifac = gmpy.fac
324322
ifac2 = gmpy.double_fac
325323
ifib = gmpy.fib
326-
else:
327-
ifac = math.factorial
328324

329325
ifac = lru_cache(maxsize=1024)(ifac)
330326

@@ -386,7 +382,7 @@ def test(a):
386382
return True
387383
isprime_python = isprime
388384

389-
if BACKEND == 'gmpy':
385+
if gmpy and hasattr(gmpy, 'is_prime'):
390386
isprime = gmpy.is_prime
391387

392388
def moebius(n):

mpmath/libmp/libmpf.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _normalize(sign, man, exp, bc, prec, rnd):
186186

187187
_exp_types = (int,)
188188

189-
if BACKEND == 'gmpy':
189+
if gmpy:
190190
_normalize = gmpy._mpmath_normalize
191191

192192
def normalize(sign, man, exp, bc, prec, rnd):
@@ -234,7 +234,7 @@ def from_man_exp(man, exp, prec=0, rnd=round_fast):
234234

235235
int_cache = dict((n, from_man_exp(n, 0)) for n in range(-10, 257))
236236

237-
if BACKEND == 'gmpy':
237+
if gmpy:
238238
from_man_exp = gmpy._mpmath_create
239239

240240
def from_int(n, prec=0, rnd=round_fast):
@@ -804,11 +804,10 @@ def python_mpf_mul_int(s, n, prec, rnd=round_fast):
804804
bc += man>>bc
805805
return normalize(sign, man, exp, bc, prec, rnd)
806806

807+
mpf_mul_int = python_mpf_mul_int
807808

808-
if BACKEND == 'gmpy':
809+
if gmpy:
809810
mpf_mul_int = gmpy_mpf_mul_int
810-
else:
811-
mpf_mul_int = python_mpf_mul_int
812811

813812
def mpf_shift(s, n):
814813
"""Quickly multiply the raw mpf s by 2**n without rounding."""

0 commit comments

Comments
 (0)