Ejemplo n.º 1
0
def test_polylog_values():
    assert polylog(2, 2) == pi**2 / 4 - I * pi * log(2)
    assert polylog(2, S.Half) == pi**2 / 12 - log(2)**2 / 2
    for z in [
            S.Half, 2, (sqrt(5) - 1) / 2, -(sqrt(5) - 1) / 2,
            -(sqrt(5) + 1) / 2, (3 - sqrt(5)) / 2
    ]:
        assert Abs(
            polylog(2, z).evalf() -
            polylog(2, z, evaluate=False).evalf()) < 1e-15
    z = Symbol("z")
    for s in [-1, 0]:
        for _ in range(10):
            assert verify_numerically(polylog(s, z),
                                      polylog(s, z, evaluate=False),
                                      z,
                                      a=-3,
                                      b=-2,
                                      c=S.Half,
                                      d=2)
            assert verify_numerically(polylog(s, z),
                                      polylog(s, z, evaluate=False),
                                      z,
                                      a=2,
                                      b=-2,
                                      c=5,
                                      d=2)

    from sympy import Integral
    assert polylog(0, Integral(1, (x, 0, 1))) == -S.Half
Ejemplo n.º 2
0
def test_TRpower():
    assert TRpower(1 / sin(x)**2) == 1 / sin(x)**2
    assert TRpower(cos(x)**3*sin(x/2)**4) == \
        (3*cos(x)/4 + cos(3*x)/4)*(-cos(x)/2 + cos(2*x)/8 + Rational(3, 8))
    for k in range(2, 8):
        assert verify_numerically(sin(x)**k, TRpower(sin(x)**k))
        assert verify_numerically(cos(x)**k, TRpower(cos(x)**k))
Ejemplo n.º 3
0
def test_roots_quartic():
    assert roots_quartic(Poly(x**4, x)) == [0, 0, 0, 0]
    assert roots_quartic(Poly(x**4 + x**3, x)) in [
        [-1, 0, 0, 0],
        [0, -1, 0, 0],
        [0, 0, -1, 0],
        [0, 0, 0, -1]
    ]
    assert roots_quartic(Poly(x**4 - x**3, x)) in [
        [1, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]
    ]

    lhs = roots_quartic(Poly(x**4 + x, x))
    rhs = [S.Half + I*sqrt(3)/2, S.Half - I*sqrt(3)/2, S.Zero, -S.One]

    assert sorted(lhs, key=hash) == sorted(rhs, key=hash)

    # test of all branches of roots quartic
    for i, (a, b, c, d) in enumerate([(1, 2, 3, 0),
                                      (3, -7, -9, 9),
                                      (1, 2, 3, 4),
                                      (1, 2, 3, 4),
                                      (-7, -3, 3, -6),
                                      (-3, 5, -6, -4),
                                      (6, -5, -10, -3)]):
        if i == 2:
            c = -a*(a**2/S(8) - b/S(2))
        elif i == 3:
            d = a*(a*(a**2*Rational(3, 256) - b/S(16)) + c/S(4))
        eq = x**4 + a*x**3 + b*x**2 + c*x + d
        ans = roots_quartic(Poly(eq, x))
        assert all(eq.subs(x, ai).n(chop=True) == 0 for ai in ans)

    # not all symbolic quartics are unresolvable
    eq = Poly(q*x + q/4 + x**4 + x**3 + 2*x**2 - Rational(1, 3), x)
    sol = roots_quartic(eq)
    assert all(verify_numerically(eq.subs(x, i), 0) for i in sol)
    z = symbols('z', negative=True)
    eq = x**4 + 2*x**3 + 3*x**2 + x*(z + 11) + 5
    zans = roots_quartic(Poly(eq, x))
    assert all([verify_numerically(eq.subs(((x, i), (z, -1))), 0) for i in zans])
    # but some are (see also issue 4989)
    # it's ok if the solution is not Piecewise, but the tests below should pass
    eq = Poly(y*x**4 + x**3 - x + z, x)
    ans = roots_quartic(eq)
    assert all(type(i) == Piecewise for i in ans)
    reps = (
        dict(y=Rational(-1, 3), z=Rational(-1, 4)),  # 4 real
        dict(y=Rational(-1, 3), z=Rational(-1, 2)),  # 2 real
        dict(y=Rational(-1, 3), z=-2))  # 0 real
    for rep in reps:
        sol = roots_quartic(Poly(eq.subs(rep), x))
        assert all([verify_numerically(w.subs(rep) - s, 0) for w, s in zip(ans, sol)])
Ejemplo n.º 4
0
def test_rewriting():
    assert dirichlet_eta(x).rewrite(zeta) == (1 - 2**(1 - x)) * zeta(x)
    assert zeta(x).rewrite(dirichlet_eta) == dirichlet_eta(x) / (1 -
                                                                 2**(1 - x))
    assert zeta(x).rewrite(dirichlet_eta, a=2) == zeta(x)
    assert verify_numerically(dirichlet_eta(x),
                              dirichlet_eta(x).rewrite(zeta), x)
    assert verify_numerically(zeta(x), zeta(x).rewrite(dirichlet_eta), x)

    assert zeta(x, a).rewrite(lerchphi) == lerchphi(1, x, a)
    assert polylog(s, z).rewrite(lerchphi) == lerchphi(z, s, 1) * z

    assert lerchphi(1, x, a).rewrite(zeta) == zeta(x, a)
    assert z * lerchphi(z, s, 1).rewrite(polylog) == polylog(s, z)
Ejemplo n.º 5
0
def test_TR9():
    a = S.Half
    b = 3 * a
    assert TR9(a) == a
    assert TR9(cos(1) + cos(2)) == 2 * cos(a) * cos(b)
    assert TR9(cos(1) - cos(2)) == 2 * sin(a) * sin(b)
    assert TR9(sin(1) - sin(2)) == -2 * sin(a) * cos(b)
    assert TR9(sin(1) + sin(2)) == 2 * sin(b) * cos(a)
    assert TR9(cos(1) + 2 * sin(1) +
               2 * sin(2)) == cos(1) + 4 * sin(b) * cos(a)
    assert TR9(cos(4) + cos(2) + 2 * cos(1) * cos(3)) == 4 * cos(1) * cos(3)
    assert TR9((cos(4) + cos(2)) / cos(3) / 2 + cos(3)) == 2 * cos(1) * cos(2)
    assert TR9(cos(3) + cos(4) + cos(5) + cos(6)) == \
        4*cos(S.Half)*cos(1)*cos(Rational(9, 2))
    assert TR9(cos(3) + cos(3) * cos(2)) == cos(3) + cos(2) * cos(3)
    assert TR9(-cos(y) +
               cos(x *
                   y)) == -2 * sin(x * y / 2 - y / 2) * sin(x * y / 2 + y / 2)
    assert TR9(-sin(y) + sin(x * y)) == 2 * sin(x * y / 2 -
                                                y / 2) * cos(x * y / 2 + y / 2)
    c = cos(x)
    s = sin(x)
    for si in ((1, 1), (1, -1), (-1, 1), (-1, -1)):
        for a in ((c, s), (s, c), (cos(x), cos(x * y)), (sin(x), sin(x * y))):
            args = zip(si, a)
            ex = Add(*[Mul(*ai) for ai in args])
            t = TR9(ex)
            assert not (a[0].func == a[1].func and
                        (not verify_numerically(ex, t.expand(trig=True))
                         or t.is_Add) or a[1].func != a[0].func and ex != t)
Ejemplo n.º 6
0
 def t(a, b, arg, n):
     from sympy import Mul
     m1 = meijerg(a, b, arg)
     m2 = Mul(*_inflate_g(m1, n))
     # NOTE: (the random number)**9 must still be on the principal sheet.
     # Thus make b&d small to create random numbers of small imaginary part.
     return verify_numerically(m1.subs(subs), m2.subs(subs), x, b=0.1, d=-0.1)
Ejemplo n.º 7
0
 def t(fac, arg):
     g = meijerg([a], [b], [c], [d], arg)*fac
     subs = {a: randcplx()/10, b: randcplx()/10 + I,
             c: randcplx(), d: randcplx()}
     integral = meijerint_indefinite(g, x)
     assert integral is not None
     assert verify_numerically(g.subs(subs), integral.diff(x).subs(subs), x)
Ejemplo n.º 8
0
def mytn(expr1, expr2, expr3, x, d=0):
    from sympy.testing.randtest import verify_numerically, random_complex_number
    subs = {}
    for a in expr1.free_symbols:
        if a != x:
            subs[a] = random_complex_number()
    return expr2 == expr3 and verify_numerically(
        expr1.subs(subs), expr2.subs(subs), x, d=d)
Ejemplo n.º 9
0
def test_TR3():
    assert TR3(cos(y - x * (y - x))) == cos(x * (x - y) + y)
    assert cos(pi / 2 + x) == -sin(x)
    assert cos(30 * pi / 2 + x) == -cos(x)

    for f in (cos, sin, tan, cot, csc, sec):
        i = f(pi * Rational(3, 7))
        j = TR3(i)
        assert verify_numerically(i, j) and i.func != j.func
Ejemplo n.º 10
0
def test_reflect():
    x = Symbol('x', real=True)
    y = Symbol('y', real=True)
    b = Symbol('b')
    m = Symbol('m')
    l = Line((0, b), slope=m)
    p = Point(x, y)
    r = p.reflect(l)
    dp = l.perpendicular_segment(p).length
    dr = l.perpendicular_segment(r).length

    assert verify_numerically(dp, dr)

    assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=oo)) \
        == Triangle(Point(5, 0), Point(4, 0), Point(4, 2))
    assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=oo)) \
        == Triangle(Point(-1, 0), Point(-2, 0), Point(-2, 2))
    assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((0, 3), slope=0)) \
        == Triangle(Point(1, 6), Point(2, 6), Point(2, 4))
    assert Polygon((1, 0), (2, 0), (2, 2)).reflect(Line((3, 0), slope=0)) \
        == Triangle(Point(1, 0), Point(2, 0), Point(2, -2))
Ejemplo n.º 11
0
 def ok(a, b, n):
     e = (a + I*b)**n
     return verify_numerically(e, expand_multinomial(e))
Ejemplo n.º 12
0
def test_fresnel():
    assert fresnels(0) == 0
    assert fresnels(oo) == S.Half
    assert fresnels(-oo) == Rational(-1, 2)
    assert fresnels(I * oo) == -I * S.Half

    assert unchanged(fresnels, z)
    assert fresnels(-z) == -fresnels(z)
    assert fresnels(I * z) == -I * fresnels(z)
    assert fresnels(-I * z) == I * fresnels(z)

    assert conjugate(fresnels(z)) == fresnels(conjugate(z))

    assert fresnels(z).diff(z) == sin(pi * z**2 / 2)

    assert fresnels(z).rewrite(erf) == (S.One + I) / 4 * (erf(
        (S.One + I) / 2 * sqrt(pi) * z) - I * erf(
            (S.One - I) / 2 * sqrt(pi) * z))

    assert fresnels(z).rewrite(hyper) == \
        pi*z**3/6 * hyper([Rational(3, 4)], [Rational(3, 2), Rational(7, 4)], -pi**2*z**4/16)

    assert fresnels(z).series(z, n=15) == \
        pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15)

    assert fresnels(w).is_extended_real is True
    assert fresnels(w).is_finite is True

    assert fresnels(z).is_extended_real is None
    assert fresnels(z).is_finite is None

    assert fresnels(z).as_real_imag() == (
        fresnels(re(z) - I * im(z)) / 2 + fresnels(re(z) + I * im(z)) / 2,
        -I * (-fresnels(re(z) - I * im(z)) + fresnels(re(z) + I * im(z))) / 2)

    assert fresnels(z).as_real_imag(deep=False) == (
        fresnels(re(z) - I * im(z)) / 2 + fresnels(re(z) + I * im(z)) / 2,
        -I * (-fresnels(re(z) - I * im(z)) + fresnels(re(z) + I * im(z))) / 2)

    assert fresnels(w).as_real_imag() == (fresnels(w), 0)
    assert fresnels(w).as_real_imag(deep=True) == (fresnels(w), 0)

    assert fresnels(2 + 3 * I).as_real_imag() == (
        fresnels(2 + 3 * I) / 2 + fresnels(2 - 3 * I) / 2,
        -I * (fresnels(2 + 3 * I) - fresnels(2 - 3 * I)) / 2)

    assert expand_func(integrate(fresnels(z), z)) == \
        z*fresnels(z) + cos(pi*z**2/2)/pi

    assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(9, 4) * \
        meijerg(((), (1,)), ((Rational(3, 4),),
        (Rational(1, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(3, 4)*(z**2)**Rational(3, 4))

    assert fresnelc(0) == 0
    assert fresnelc(oo) == S.Half
    assert fresnelc(-oo) == Rational(-1, 2)
    assert fresnelc(I * oo) == I * S.Half

    assert unchanged(fresnelc, z)
    assert fresnelc(-z) == -fresnelc(z)
    assert fresnelc(I * z) == I * fresnelc(z)
    assert fresnelc(-I * z) == -I * fresnelc(z)

    assert conjugate(fresnelc(z)) == fresnelc(conjugate(z))

    assert fresnelc(z).diff(z) == cos(pi * z**2 / 2)

    assert fresnelc(z).rewrite(erf) == (S.One - I) / 4 * (erf(
        (S.One + I) / 2 * sqrt(pi) * z) + I * erf(
            (S.One - I) / 2 * sqrt(pi) * z))

    assert fresnelc(z).rewrite(hyper) == \
        z * hyper([Rational(1, 4)], [S.Half, Rational(5, 4)], -pi**2*z**4/16)

    assert fresnelc(w).is_extended_real is True

    assert fresnelc(z).as_real_imag() == \
        (fresnelc(re(z) - I*im(z))/2 + fresnelc(re(z) + I*im(z))/2,
         -I*(-fresnelc(re(z) - I*im(z)) + fresnelc(re(z) + I*im(z)))/2)

    assert fresnelc(z).as_real_imag(deep=False) == \
        (fresnelc(re(z) - I*im(z))/2 + fresnelc(re(z) + I*im(z))/2,
         -I*(-fresnelc(re(z) - I*im(z)) + fresnelc(re(z) + I*im(z)))/2)

    assert fresnelc(2 + 3 * I).as_real_imag() == (
        fresnelc(2 - 3 * I) / 2 + fresnelc(2 + 3 * I) / 2,
        -I * (fresnelc(2 + 3 * I) - fresnelc(2 - 3 * I)) / 2)

    assert expand_func(integrate(fresnelc(z), z)) == \
        z*fresnelc(z) - sin(pi*z**2/2)/pi

    assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(3, 4) * \
        meijerg(((), (1,)), ((Rational(1, 4),),
        (Rational(3, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(1, 4)*(z**2)**Rational(1, 4))

    from sympy.testing.randtest import verify_numerically

    verify_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z)
    verify_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(hyper), z)
    verify_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z)

    verify_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z)
    verify_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z)
    verify_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z)

    raises(ArgumentIndexError, lambda: fresnels(z).fdiff(2))
    raises(ArgumentIndexError, lambda: fresnelc(z).fdiff(2))

    assert fresnels(x).taylor_term(-1, x) is S.Zero
    assert fresnelc(x).taylor_term(-1, x) is S.Zero
    assert fresnelc(x).taylor_term(1, x) == -pi**2 * x**5 / 40
Ejemplo n.º 13
0
    def u(expr, x):
        from sympy import Add, exp, exp_polar

        r = _rewrite_single(expr, x)
        e = Add(*[res[0] * res[2] for res in r[0]]).replace(exp_polar, exp)  # XXX Hack?
        assert verify_numerically(e, expr, x)