コード例 #1
0
    def to_rational(ctx, s, limit=True):
        p, q = to_rational(s._mpf_)

        if not limit or q <= ctx.max_denom:
            return p, q

        p0, q0, p1, q1 = 0, 1, 1, 0
        n, d = p, q

        while True:
            a = n//d
            q2 = q0 + a*q1
            if q2 > ctx.max_denom:
                break
            p0, q0, p1, q1 = p1, q1, p0 + a*p1, q2
            n, d = d, n - a*d

        k = (ctx.max_denom - q0)//q1

        number = mpq(p, q)
        bound1 = mpq(p0 + k*p1, q0 + k*q1)
        bound2 = mpq(p1, q1)

        if not bound2 or not bound1:
            return p, q
        elif abs(bound2 - number) <= abs(bound1 - number):
            return bound2._mpq_
        else:
            return bound1._mpq_
コード例 #2
0
ファイル: mpelements.py プロジェクト: asmeurer/sympy
    def to_rational(ctx, s, limit=True):
        p, q = to_rational(s._mpf_)

        if not limit or q <= ctx.max_denom:
            return p, q

        p0, q0, p1, q1 = 0, 1, 1, 0
        n, d = p, q

        while True:
            a = n//d
            q2 = q0 + a*q1
            if q2 > ctx.max_denom:
                break
            p0, q0, p1, q1 = p1, q1, p0 + a*p1, q2
            n, d = d, n - a*d

        k = (ctx.max_denom - q0)//q1

        number = mpq(p, q)
        bound1 = mpq(p0 + k*p1, q0 + k*q1)
        bound2 = mpq(p1, q1)

        if not bound2 or not bound1:
            return p, q
        elif abs(bound2 - number) <= abs(bound1 - number):
            return bound2._mpq_
        else:
            return bound1._mpq_
コード例 #3
0
def test_sympify_mpmath():
    value = sympify(mpmath.mpf(1.0))
    assert value == Float(1.0) and type(value) is Float

    mpmath.mp.dps = 12
    assert sympify(mpmath.pi).epsilon_eq(Float("3.14159265359"),
                                         Float("1e-12")) == True
    assert sympify(mpmath.pi).epsilon_eq(Float("3.14159265359"),
                                         Float("1e-13")) == False

    mpmath.mp.dps = 6
    assert sympify(mpmath.pi).epsilon_eq(Float("3.14159"),
                                         Float("1e-5")) == True
    assert sympify(mpmath.pi).epsilon_eq(Float("3.14159"),
                                         Float("1e-6")) == False

    assert sympify(mpmath.mpc(1.0 + 2.0j)) == Float(1.0) + Float(2.0) * I

    assert sympify(mpq(1, 2)) == S.Half
コード例 #4
0
ファイル: test_sympify.py プロジェクト: KonstantinTogoi/sympy
def test_sympify_mpmath():
    value = sympify(mpmath.mpf(1.0))
    assert value == Float(1.0) and type(value) is Float

    mpmath.mp.dps = 12
    assert sympify(
        mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-12")) == True
    assert sympify(
        mpmath.pi).epsilon_eq(Float("3.14159265359"), Float("1e-13")) == False

    mpmath.mp.dps = 6
    assert sympify(
        mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-5")) == True
    assert sympify(
        mpmath.pi).epsilon_eq(Float("3.14159"), Float("1e-6")) == False

    assert sympify(mpmath.mpc(1.0 + 2.0j)) == Float(1.0) + Float(2.0)*I

    assert sympify(mpq(1, 2)) == S.Half
コード例 #5
0
def test_hash():
    for i in range(-256, 256):
        assert hash(mpf(i)) == hash(i)
    assert hash(mpf(0.5)) == hash(0.5)
    assert hash(mpc(2, 3)) == hash(2 + 3j)
    # Check that this doesn't fail
    assert hash(inf)
    # 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'))
    from mpmath.rational import mpq
    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)
    if sys.version >= "3.2":
        assert hash(mpf(1) * 2**2000) == hash(2**2000)
        assert hash(mpf(1) / 2**2000) == hash(mpq(1, 2**2000))
コード例 #6
0
def test_hash():
    for i in range(-256, 256):
        assert hash(mpf(i)) == hash(i)
    assert hash(mpf(0.5)) == hash(0.5)
    assert hash(mpc(2,3)) == hash(2+3j)
    # Check that this doesn't fail
    assert hash(inf)
    # 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'))
    from mpmath.rational import mpq
    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)
    if sys.version >= "3.2":
        assert hash(mpf(1)*2**2000) == hash(2**2000)
        assert hash(mpf(1)/2**2000) == hash(mpq(1,2**2000))
コード例 #7
0
def test_isnan_etc():
    from mpmath.rational import mpq
    assert isnan(nan) == True
    assert isnan(3) == False
    assert isnan(mpf(3)) == False
    assert isnan(inf) == False
    assert isnan(mpc(2, nan)) == True
    assert isnan(mpc(2, nan)) == True
    assert isnan(mpc(nan, nan)) == True
    assert isnan(mpc(2, 2)) == False
    assert isnan(mpc(nan, inf)) == True
    assert isnan(mpc(inf, inf)) == False
    assert isnan(mpq((3, 2))) == False
    assert isnan(mpq((0, 1))) == False
    assert isinf(inf) == True
    assert isinf(-inf) == True
    assert isinf(3) == False
    assert isinf(nan) == False
    assert isinf(3 + 4j) == False
    assert isinf(mpc(inf)) == True
    assert isinf(mpc(3, inf)) == True
    assert isinf(mpc(inf, 3)) == True
    assert isinf(mpc(inf, inf)) == True
    assert isinf(mpc(nan, inf)) == True
    assert isinf(mpc(inf, nan)) == True
    assert isinf(mpc(nan, nan)) == False
    assert isinf(mpq((3, 2))) == False
    assert isinf(mpq((0, 1))) == False
    assert isnormal(3) == True
    assert isnormal(3.5) == True
    assert isnormal(mpf(3.5)) == True
    assert isnormal(0) == False
    assert isnormal(mpf(0)) == False
    assert isnormal(0.0) == False
    assert isnormal(inf) == False
    assert isnormal(-inf) == False
    assert isnormal(nan) == False
    assert isnormal(float(inf)) == False
    assert isnormal(mpc(0, 0)) == False
    assert isnormal(mpc(3, 0)) == True
    assert isnormal(mpc(0, 3)) == True
    assert isnormal(mpc(3, 3)) == True
    assert isnormal(mpc(0, nan)) == False
    assert isnormal(mpc(0, inf)) == False
    assert isnormal(mpc(3, nan)) == False
    assert isnormal(mpc(3, inf)) == False
    assert isnormal(mpc(3, -inf)) == False
    assert isnormal(mpc(nan, 0)) == False
    assert isnormal(mpc(inf, 0)) == False
    assert isnormal(mpc(nan, 3)) == False
    assert isnormal(mpc(inf, 3)) == False
    assert isnormal(mpc(inf, nan)) == False
    assert isnormal(mpc(nan, inf)) == False
    assert isnormal(mpc(nan, nan)) == False
    assert isnormal(mpc(inf, inf)) == False
    assert isnormal(mpq((3, 2))) == True
    assert isnormal(mpq((0, 1))) == False
    assert isint(3) == True
    assert isint(0) == True
    assert isint(long(3)) == True
    assert isint(long(0)) == True
    assert isint(mpf(3)) == True
    assert isint(mpf(0)) == True
    assert isint(mpf(-3)) == True
    assert isint(mpf(3.2)) == False
    assert isint(3.2) == False
    assert isint(nan) == False
    assert isint(inf) == False
    assert isint(-inf) == False
    assert isint(mpc(0)) == True
    assert isint(mpc(3)) == True
    assert isint(mpc(3.2)) == False
    assert isint(mpc(3, inf)) == False
    assert isint(mpc(inf)) == False
    assert isint(mpc(3, 2)) == False
    assert isint(mpc(0, 2)) == False
    assert isint(mpc(3, 2), gaussian=True) == True
    assert isint(mpc(3, 0), gaussian=True) == True
    assert isint(mpc(0, 3), gaussian=True) == True
    assert isint(3 + 4j) == False
    assert isint(3 + 4j, gaussian=True) == True
    assert isint(3 + 0j) == True
    assert isint(mpq((3, 2))) == False
    assert isint(mpq((3, 9))) == False
    assert isint(mpq((9, 3))) == True
    assert isint(mpq((0, 4))) == True
    assert isint(mpq((1, 1))) == True
    assert isint(mpq((-1, 1))) == True
    assert mp.isnpint(0) == True
    assert mp.isnpint(1) == False
    assert mp.isnpint(-1) == True
    assert mp.isnpint(-1.1) == False
    assert mp.isnpint(-1.0) == True
    assert mp.isnpint(mp.mpq(1, 2)) == False
    assert mp.isnpint(mp.mpq(-1, 2)) == False
    assert mp.isnpint(mp.mpq(-3, 1)) == True
    assert mp.isnpint(mp.mpq(0, 1)) == True
    assert mp.isnpint(mp.mpq(1, 1)) == False
    assert mp.isnpint(0 + 0j) == True
    assert mp.isnpint(-1 + 0j) == True
    assert mp.isnpint(-1.1 + 0j) == False
    assert mp.isnpint(-1 + 0.1j) == False
    assert mp.isnpint(0 + 0.1j) == False
コード例 #8
0
ファイル: test_basic_ops.py プロジェクト: hitej/meta-core
def test_isnan_etc():
    from mpmath.rational import mpq

    assert isnan(nan) == True
    assert isnan(3) == False
    assert isnan(mpf(3)) == False
    assert isnan(inf) == False
    assert isnan(mpc(2, nan)) == True
    assert isnan(mpc(2, nan)) == True
    assert isnan(mpc(nan, nan)) == True
    assert isnan(mpc(2, 2)) == False
    assert isnan(mpc(nan, inf)) == True
    assert isnan(mpc(inf, inf)) == False
    assert isnan(mpq((3, 2))) == False
    assert isnan(mpq((0, 1))) == False
    assert isinf(inf) == True
    assert isinf(-inf) == True
    assert isinf(3) == False
    assert isinf(nan) == False
    assert isinf(3 + 4j) == False
    assert isinf(mpc(inf)) == True
    assert isinf(mpc(3, inf)) == True
    assert isinf(mpc(inf, 3)) == True
    assert isinf(mpc(inf, inf)) == True
    assert isinf(mpc(nan, inf)) == True
    assert isinf(mpc(inf, nan)) == True
    assert isinf(mpc(nan, nan)) == False
    assert isinf(mpq((3, 2))) == False
    assert isinf(mpq((0, 1))) == False
    assert isnormal(3) == True
    assert isnormal(3.5) == True
    assert isnormal(mpf(3.5)) == True
    assert isnormal(0) == False
    assert isnormal(mpf(0)) == False
    assert isnormal(0.0) == False
    assert isnormal(inf) == False
    assert isnormal(-inf) == False
    assert isnormal(nan) == False
    assert isnormal(float(inf)) == False
    assert isnormal(mpc(0, 0)) == False
    assert isnormal(mpc(3, 0)) == True
    assert isnormal(mpc(0, 3)) == True
    assert isnormal(mpc(3, 3)) == True
    assert isnormal(mpc(0, nan)) == False
    assert isnormal(mpc(0, inf)) == False
    assert isnormal(mpc(3, nan)) == False
    assert isnormal(mpc(3, inf)) == False
    assert isnormal(mpc(3, -inf)) == False
    assert isnormal(mpc(nan, 0)) == False
    assert isnormal(mpc(inf, 0)) == False
    assert isnormal(mpc(nan, 3)) == False
    assert isnormal(mpc(inf, 3)) == False
    assert isnormal(mpc(inf, nan)) == False
    assert isnormal(mpc(nan, inf)) == False
    assert isnormal(mpc(nan, nan)) == False
    assert isnormal(mpc(inf, inf)) == False
    assert isnormal(mpq((3, 2))) == True
    assert isnormal(mpq((0, 1))) == False
    assert isint(3) == True
    assert isint(0) == True
    assert isint(long(3)) == True
    assert isint(long(0)) == True
    assert isint(mpf(3)) == True
    assert isint(mpf(0)) == True
    assert isint(mpf(-3)) == True
    assert isint(mpf(3.2)) == False
    assert isint(3.2) == False
    assert isint(nan) == False
    assert isint(inf) == False
    assert isint(-inf) == False
    assert isint(mpc(0)) == True
    assert isint(mpc(3)) == True
    assert isint(mpc(3.2)) == False
    assert isint(mpc(3, inf)) == False
    assert isint(mpc(inf)) == False
    assert isint(mpc(3, 2)) == False
    assert isint(mpc(0, 2)) == False
    assert isint(mpc(3, 2), gaussian=True) == True
    assert isint(mpc(3, 0), gaussian=True) == True
    assert isint(mpc(0, 3), gaussian=True) == True
    assert isint(3 + 4j) == False
    assert isint(3 + 4j, gaussian=True) == True
    assert isint(3 + 0j) == True
    assert isint(mpq((3, 2))) == False
    assert isint(mpq((3, 9))) == False
    assert isint(mpq((9, 3))) == True
    assert isint(mpq((0, 4))) == True
    assert isint(mpq((1, 1))) == True
    assert isint(mpq((-1, 1))) == True
    assert mp.isnpint(0) == True
    assert mp.isnpint(1) == False
    assert mp.isnpint(-1) == True
    assert mp.isnpint(-1.1) == False
    assert mp.isnpint(-1.0) == True
    assert mp.isnpint(mp.mpq(1, 2)) == False
    assert mp.isnpint(mp.mpq(-1, 2)) == False
    assert mp.isnpint(mp.mpq(-3, 1)) == True
    assert mp.isnpint(mp.mpq(0, 1)) == True
    assert mp.isnpint(mp.mpq(1, 1)) == False
    assert mp.isnpint(0 + 0j) == True
    assert mp.isnpint(-1 + 0j) == True
    assert mp.isnpint(-1.1 + 0j) == False
    assert mp.isnpint(-1 + 0.1j) == False
    assert mp.isnpint(0 + 0.1j) == False
コード例 #9
0
 def _mpmath_(self, p, r):
     return rational.mpq(self.numerator, self.denominator)