Ejemplo n.º 1
0
def test_sincos_rewrite_sqrt():
    for p in [1, 3, 5, 17]:
        for t in [1, 8]:
            n = t*p
            for i in range(1, (n + 1)//2 + 1):
                if 1 == gcd(i, n):
                    x = i*pi/n
                    s1 = sin(x).rewrite(sqrt)
                    c1 = cos(x).rewrite(sqrt)
                    assert not s1.has(cos, sin), f'fails for {i:d}*pi/{n:d}'
                    assert not c1.has(cos, sin), f'fails for {i:d}*pi/{n:d}'
                    assert 1e-3 > abs(sin(x.evalf(5)) - s1.evalf(2)), f'fails for {i:d}*pi/{n:d}'
                    assert 1e-3 > abs(cos(x.evalf(5)) - c1.evalf(2)), f'fails for {i:d}*pi/{n:d}'
Ejemplo n.º 2
0
def test_tancot_rewrite_sqrt():
    for p in [1, 3, 5, 17]:
        for t in [1, 8]:
            n = t*p
            for i in range(1, (n + 1)//2 + 1):
                if 1 == gcd(i, n):
                    x = i*pi/n
                    if 2*i != n and 3*i != 2*n:
                        t1 = tan(x).rewrite(sqrt)
                        assert not t1.has(cot, tan), f'fails for {i:d}*pi/{n:d}'
                        assert 1e-3 > abs( tan(x.evalf(7)) - t1.evalf(4) ), f'fails for {i:d}*pi/{n:d}'
                    if i != 0 and i != n:
                        c1 = cot(x).rewrite(sqrt)
                        assert not c1.has(cot, tan), f'fails for {i:d}*pi/{n:d}'
                        assert 1e-3 > abs( cot(x.evalf(7)) - c1.evalf(4) ), f'fails for {i:d}*pi/{n:d}'
Ejemplo n.º 3
0
 def t(m, n):
     x = Integer(m) / n
     r = polygamma(0, x)
     if r.has(polygamma):
         return False
     return abs(polygamma(0, x.evalf()).evalf(strict=False) -
                r.evalf()).evalf(strict=False) < 1e-10
Ejemplo n.º 4
0
def test_evalf_bugs():
    assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10)
    assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10)
    assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50'
    assert NS('log(10**100,10)', 10) == '100.0000000'
    assert NS('log(2)', 10) == '0.6931471806'
    assert NS(
        '(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667'
    assert NS(sin(1) + Rational(
        1, 10**100)*I, 15) == '0.841470984807897 + 1.00000000000000e-100*I'
    assert x.evalf() == x
    assert NS((1 + I)**2*I, 6) == '-2.00000'
    d = {n: (
        -1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)}
    assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I'
    assert NS(((-I - sqrt(2)*I)**2).evalf()) == '-5.82842712474619'
    assert NS((1 + I)**2*I, 15) == '-2.00000000000000'
    # issue sympy/sympy#4758 (1/2):
    assert NS(Float(pi.evalf(69), 100) - pi) == '-4.43863937855894e-71'
    assert NS(pi.evalf(69) - pi) == '-0.e-71'
    # issue sympy/sympy#4758 (2/2): With the bug present, this still only fails if the
    # terms are in the order given here. This is not generally the case,
    # because the order depends on the hashes of the terms.
    assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n,
              subs={n: .01}) == '19.8100000000000'
    assert NS(((x - 1)*((1 - x))**1000).n()
              ) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)'
    assert NS((-x).n()) == '-x'
    assert NS((-2*x).n()) == '-2.00000000000000*x'
    assert NS((-2*x*y).n()) == '-2.00000000000000*x*y'
    assert cos(x).n(subs={x: 1+I}) == cos(x).subs(x, 1+I).n()
    # issue sympy/sympy#6660. Also NaN != mpmath.nan
    # In this order:
    # 0*nan, 0/nan, 0*inf, 0/inf
    # 0+nan, 0-nan, 0+inf, 0-inf
    # >>> n = Some Number
    # n*nan, n/nan, n*inf, n/inf
    # n+nan, n-nan, n+inf, n-inf
    assert (0*sin(oo)).n() == S.Zero
    assert (0/sin(oo)).n() == S.Zero
    assert (0*E**(oo)).n() == S.NaN
    assert (0/E**(oo)).n() == S.Zero

    assert (0+sin(oo)).n() == S.NaN
    assert (0-sin(oo)).n() == S.NaN
    assert (0+E**(oo)).n() == S.Infinity
    assert (0-E**(oo)).n() == S.NegativeInfinity

    assert (5*sin(oo)).n() == S.NaN
    assert (5/sin(oo)).n() == S.NaN
    assert (5*E**(oo)).n() == S.Infinity
    assert (5/E**(oo)).n() == S.Zero

    assert (5+sin(oo)).n() == S.NaN
    assert (5-sin(oo)).n() == S.NaN
    assert (5+E**(oo)).n() == S.Infinity
    assert (5-E**(oo)).n() == S.NegativeInfinity

    # issue sympy/sympy#7416
    assert as_mpmath(0.0, 10, {'chop': True}) == 0
Ejemplo n.º 5
0
def test_subs():
    assert NS('besseli(-x, y) - besseli(x, y)', subs={x: 3.5, y: 20.0}) == \
        '-4.92535585957223e-10'
    assert NS('Piecewise((x, x>0)) + Piecewise((1-x, x>0))', subs={x: 0.1}) == \
        '1.00000000000000'
    pytest.raises(TypeError, lambda: x.evalf(subs=(x, 1)))
Ejemplo n.º 6
0
def test_evalf_bugs():
    assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10)
    assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10)
    assert NS('log(1+1/10**50)', 20) == '1.0000000000000000000e-50'
    assert NS('log(10**100,10)', 10) == '100.0000000'
    assert NS('log(2)', 10) == '0.6931471806'
    assert NS(
        '(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667'
    assert NS(sin(1) + I/10**100, 15) == '0.841470984807897 + 1.00000000000000e-100*I'
    assert x.evalf(strict=False) == x
    assert NS((1 + I)**2*I, 6) == '-2.00000'
    d = {n: (
        -1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)}
    assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I'
    assert NS(((-I - sqrt(2)*I)**2).evalf(), strict=False) == '-5.82842712474619'
    assert NS((1 + I)**2*I, 15) == '-2.00000000000000'
    # issue sympy/sympy#4758 (1/2):
    assert NS(Float(pi.evalf(69), 100) - pi) == '-4.43863937855894e-71'
    assert NS(pi.evalf(69) - pi, strict=False) == '-0.e-71'
    # issue sympy/sympy#4758 (2/2): With the bug present, this still only fails if the
    # terms are in the order given here. This is not generally the case,
    # because the order depends on the hashes of the terms.
    assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n,
              subs={n: .01}, strict=False) == '19.8100000000000'
    assert NS(((x - 1)*((1 - x))**1000).evalf(strict=False),
              strict=False) == '(-x + 1.00000000000000)**1000*(x - 1.00000000000000)'
    assert NS((-x).evalf(strict=False)) == '-x'
    assert NS((-2*x).evalf(strict=False), strict=False) == '-2.00000000000000*x'
    assert NS((-2*x*y).evalf(strict=False), strict=False) == '-2.00000000000000*x*y'
    assert cos(x).evalf(subs={x: 1+I}) == cos(x).subs({x: 1 + I}).evalf()
    # issue sympy/sympy#6660. Also NaN != mpmath.nan
    # In this order:
    # 0*nan, 0/nan, 0*inf, 0/inf
    # 0+nan, 0-nan, 0+inf, 0-inf
    # >>> n = Some Number
    # n*nan, n/nan, n*inf, n/inf
    # n+nan, n-nan, n+inf, n-inf
    assert (0*sin(oo)).evalf() == 0
    assert (0/sin(oo)).evalf() == 0
    assert (0*E**oo).evalf() == nan
    assert (0/E**oo).evalf() == 0

    assert (0+sin(oo)).evalf() == nan
    assert (0-sin(oo)).evalf() == nan
    assert (0+E**oo).evalf() == +oo
    assert (0-E**oo).evalf() == -oo

    assert (5*sin(oo)).evalf() == nan
    assert (5/sin(oo)).evalf() == nan
    assert (5*E**oo).evalf() == oo
    assert (5/E**oo).evalf() == 0

    assert (5+sin(oo)).evalf() == nan
    assert (5-sin(oo)).evalf() == nan
    assert (5+E**oo).evalf() == +oo
    assert (5-E**oo).evalf() == -oo

    # issue sympy/sympy#7416
    assert as_mpmath(0.0, 10, {'chop': True}) == 0

    # issue sympy/sympy#5412
    assert (oo*I).evalf() == oo*I
    assert (oo + oo*I).evalf() == oo + oo*I
Ejemplo n.º 7
0
def test_subs():
    assert NS('besseli(-x, y) - besseli(x, y)', subs={x: 3.5, y: 20.0}) == \
        '-4.92535585957223e-10'
    assert NS('Piecewise((x, x>0)) + Piecewise((1-x, x>0))', subs={x: 0.1}) == \
        '1.00000000000000'
    pytest.raises(TypeError, lambda: x.evalf(subs=(x, 1)))