From 0efdfeb7cdb447fdf53ca7c2ac0d8570d0cff99c Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 12 May 2023 06:43:48 +0300 Subject: [PATCH] Drop ctx._mpq --- mpmath/ctx_fp.py | 2 -- mpmath/ctx_iv.py | 5 ----- mpmath/ctx_mp.py | 1 - mpmath/ctx_mp_python.py | 4 ++-- mpmath/functions/functions.py | 33 ++++++++++++++++----------------- mpmath/tests/test_basic_ops.py | 22 +++++++++++----------- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/mpmath/ctx_fp.py b/mpmath/ctx_fp.py index 14cd015d..f33a730a 100644 --- a/mpmath/ctx_fp.py +++ b/mpmath/ctx_fp.py @@ -21,8 +21,6 @@ class FPContext(StandardBaseContext): ctx._init_aliases() - _mpq = lambda cls, x, y: float(x)/y - NoConvergence = libmp.NoConvergence def _get_prec(ctx): return 53 diff --git a/mpmath/ctx_iv.py b/mpmath/ctx_iv.py index cf6028a3..c8a32a42 100644 --- a/mpmath/ctx_iv.py +++ b/mpmath/ctx_iv.py @@ -395,11 +395,6 @@ class MPIntervalContext(StandardBaseContext): a._mpci_ = v return a - def _mpq(ctx, p, q): - a = libmp.from_rational(p, q, ctx.prec, round_floor) - b = libmp.from_rational(p, q, ctx.prec, round_ceiling) - return ctx.make_mpf((a, b)) - def convert(ctx, x): if isinstance(x, (ctx.mpf, ctx.mpc)): return x diff --git a/mpmath/ctx_mp.py b/mpmath/ctx_mp.py index 7cd8b6cf..c17017e5 100644 --- a/mpmath/ctx_mp.py +++ b/mpmath/ctx_mp.py @@ -41,7 +41,6 @@ class MPContext(BaseMPContext, StandardBaseContext): ctx.trap_complex = False ctx.pretty = False ctx.types = [ctx.mpf, ctx.mpc, ctx.constant] - ctx._mpq = MPQ ctx.default() StandardBaseContext.__init__(ctx) diff --git a/mpmath/ctx_mp_python.py b/mpmath/ctx_mp_python.py index be61b6db..af5385fa 100644 --- a/mpmath/ctx_mp_python.py +++ b/mpmath/ctx_mp_python.py @@ -986,7 +986,7 @@ class PythonMPContext: if p is not None: if not p % q: return p // q, 'Z' - return ctx._mpq(p,q), 'Q' + return MPQ(p,q), 'Q' x = ctx.convert(x) if hasattr(x, "_mpc_"): v, im = x._mpc_ @@ -1004,7 +1004,7 @@ class PythonMPContext: if exp >= 0: return int(man) << exp, 'Z' p, q = int(man), (1<<(-exp)) - return ctx._mpq(p,q), 'Q' + return MPQ(p,q), 'Q' x = ctx.make_mpf(v) return x, 'R' if not exp: diff --git a/mpmath/functions/functions.py b/mpmath/functions/functions.py index 7aa7a08d..251aa4e6 100644 --- a/mpmath/functions/functions.py +++ b/mpmath/functions/functions.py @@ -19,23 +19,22 @@ class SpecialFunctions: f, wrap = cls.defined_functions[name] cls._wrap_specfun(name, f, wrap) - self._mpq_1 = self._mpq(1,1) - self._mpq_0 = self._mpq(0,1) - self._mpq_1_2 = self._mpq(1,2) - self._mpq_3_2 = self._mpq(3,2) - self._mpq_1_4 = self._mpq(1,4) - self._mpq_1_16 = self._mpq(1,16) - self._mpq_3_16 = self._mpq(3,16) - self._mpq_5_2 = self._mpq(5,2) - self._mpq_3_4 = self._mpq(3,4) - self._mpq_7_4 = self._mpq(7,4) - self._mpq_5_4 = self._mpq(5,4) - self._mpq_1_3 = self._mpq(1,3) - self._mpq_2_3 = self._mpq(2,3) - self._mpq_4_3 = self._mpq(4,3) - self._mpq_1_6 = self._mpq(1,6) - self._mpq_5_6 = self._mpq(5,6) - self._mpq_5_3 = self._mpq(5,3) + from ..libmp.backend import MPQ + + self._mpq_1 = MPQ(1,1) + self._mpq_1_2 = MPQ(1,2) + self._mpq_3_2 = MPQ(3,2) + self._mpq_1_4 = MPQ(1,4) + self._mpq_1_16 = MPQ(1,16) + self._mpq_3_16 = MPQ(3,16) + self._mpq_5_2 = MPQ(5,2) + self._mpq_3_4 = MPQ(3,4) + self._mpq_1_3 = MPQ(1,3) + self._mpq_2_3 = MPQ(2,3) + self._mpq_4_3 = MPQ(4,3) + self._mpq_1_6 = MPQ(1,6) + self._mpq_5_6 = MPQ(5,6) + self._mpq_5_3 = MPQ(5,3) self._misc_const_cache = {} diff --git a/mpmath/tests/test_basic_ops.py b/mpmath/tests/test_basic_ops.py index 0b917a83..e3daa1a3 100644 --- a/mpmath/tests/test_basic_ops.py +++ b/mpmath/tests/test_basic_ops.py @@ -179,12 +179,12 @@ def test_hash(): # Check that overflow doesn't assign equal hashes to large numbers assert hash(mpf('1e1000')) != hash('1e10000') assert hash(mpc(100,'1e1000')) != hash(mpc(200,'1e1000')) - assert hash(mp._mpq(1,3)) - assert hash(mp._mpq(0,1)) == 0 - assert hash(mp._mpq(-1,1)) == hash(-1) - assert hash(mp._mpq(1,1)) == hash(1) - assert hash(mp._mpq(5,1)) == hash(5) - assert hash(mp._mpq(1,2)) == hash(0.5) + assert hash(MPQ(1,3)) + assert hash(MPQ(0,1)) == 0 + assert hash(MPQ(-1,1)) == hash(-1) + assert hash(MPQ(1,1)) == hash(1) + assert hash(MPQ(5,1)) == hash(5) + assert hash(MPQ(1,2)) == hash(0.5) assert hash(mpf(1)*2**2000) == hash(2**2000) assert hash(mpf(1)/2**2000) == hash(MPQ(1,2**2000)) @@ -465,11 +465,11 @@ def test_isnan_etc(): assert mp.isnpint(-1) is True assert mp.isnpint(-1.1) is False assert mp.isnpint(-1.0) is True - assert mp.isnpint(mp._mpq(1, 2)) is False - assert mp.isnpint(mp._mpq(-1, 2)) is False - assert mp.isnpint(mp._mpq(-3, 1)) is True - assert mp.isnpint(mp._mpq(0, 1)) is True - assert mp.isnpint(mp._mpq(1, 1)) is False + assert mp.isnpint(MPQ(1, 2)) is False + assert mp.isnpint(MPQ(-1, 2)) is False + assert mp.isnpint(MPQ(-3, 1)) is True + assert mp.isnpint(MPQ(0, 1)) is True + assert mp.isnpint(MPQ(1, 1)) is False assert mp.isnpint(0 + 0j) is True assert mp.isnpint(-1 + 0j) is True assert mp.isnpint(-1.1 + 0j) is False