예제 #1
0
def test_trigsimp_noncommutative():
    A, B = symbols('A,B', commutative=False)

    assert trigsimp(A - A*sin(x)**2) == A*cos(x)**2
    assert trigsimp(A - A*cos(x)**2) == A*sin(x)**2
    assert trigsimp(A*sin(x)**2 + A*cos(x)**2) == A
    assert trigsimp(A + A*tan(x)**2) == A/cos(x)**2
    assert trigsimp(A/cos(x)**2 - A) == A*tan(x)**2
    assert trigsimp(A/cos(x)**2 - A*tan(x)**2) == A
    assert trigsimp(A + A*cot(x)**2) == A/sin(x)**2
    assert trigsimp(A/sin(x)**2 - A) == A/tan(x)**2
    assert trigsimp(A/sin(x)**2 - A*cot(x)**2) == A

    assert trigsimp(y*A*cos(x)**2 + y*A*sin(x)**2) == y*A

    assert trigsimp(A*sin(x)/cos(x)) == A*tan(x)
    assert trigsimp(A*tan(x)*cos(x)) == A*sin(x)
    assert trigsimp(A*cot(x)**3*sin(x)**3) == A*cos(x)**3
    assert trigsimp(y*A*tan(x)**2/sin(x)**2) == y*A/cos(x)**2
    assert trigsimp(A*cot(x)/cos(x)) == A/sin(x)

    assert trigsimp(A*sin(x + y) + A*sin(x - y)) == 2*A*sin(x)*cos(y)
    assert trigsimp(A*sin(x + y) - A*sin(x - y)) == 2*A*sin(y)*cos(x)
    assert trigsimp(A*cos(x + y) + A*cos(x - y)) == 2*A*cos(x)*cos(y)
    assert trigsimp(A*cos(x + y) - A*cos(x - y)) == -2*A*sin(x)*sin(y)

    assert trigsimp(A*sinh(x + y) + A*sinh(x - y)) == 2*A*sinh(x)*cosh(y)
    assert trigsimp(A*sinh(x + y) - A*sinh(x - y)) == 2*A*sinh(y)*cosh(x)
    assert trigsimp(A*cosh(x + y) + A*cosh(x - y)) == 2*A*cosh(x)*cosh(y)
    assert trigsimp(A*cosh(x + y) - A*cosh(x - y)) == 2*A*sinh(x)*sinh(y)

    assert trigsimp(A*cos(0.12345)**2 + A*sin(0.12345)**2) == 1.0*A
예제 #2
0
def test_trigsimp_noncommutative():
    A, B = symbols('A,B', commutative=False)

    assert trigsimp(A - A*sin(x)**2) == A*cos(x)**2
    assert trigsimp(A - A*cos(x)**2) == A*sin(x)**2
    assert trigsimp(A*sin(x)**2 + A*cos(x)**2) == A
    assert trigsimp(A + A*tan(x)**2) == A/cos(x)**2
    assert trigsimp(A/cos(x)**2 - A) == A*tan(x)**2
    assert trigsimp(A/cos(x)**2 - A*tan(x)**2) == A
    assert trigsimp(A + A*cot(x)**2) == A/sin(x)**2
    assert trigsimp(A/sin(x)**2 - A) == A/tan(x)**2
    assert trigsimp(A/sin(x)**2 - A*cot(x)**2) == A

    assert trigsimp(y*A*cos(x)**2 + y*A*sin(x)**2) == y*A

    assert trigsimp(A*sin(x)/cos(x)) == A*tan(x)
    assert trigsimp(A*tan(x)*cos(x)) == A*sin(x)
    assert trigsimp(A*cot(x)**3*sin(x)**3) == A*cos(x)**3
    assert trigsimp(y*A*tan(x)**2/sin(x)**2) == y*A/cos(x)**2
    assert trigsimp(A*cot(x)/cos(x)) == A/sin(x)

    assert trigsimp(A*sin(x + y) + A*sin(x - y)) == 2*A*sin(x)*cos(y)
    assert trigsimp(A*sin(x + y) - A*sin(x - y)) == 2*A*sin(y)*cos(x)
    assert trigsimp(A*cos(x + y) + A*cos(x - y)) == 2*A*cos(x)*cos(y)
    assert trigsimp(A*cos(x + y) - A*cos(x - y)) == -2*A*sin(x)*sin(y)

    assert trigsimp(A*sinh(x + y) + A*sinh(x - y)) == 2*A*sinh(x)*cosh(y)
    assert trigsimp(A*sinh(x + y) - A*sinh(x - y)) == 2*A*sinh(y)*cosh(x)
    assert trigsimp(A*cosh(x + y) + A*cosh(x - y)) == 2*A*cosh(x)*cosh(y)
    assert trigsimp(A*cosh(x + y) - A*cosh(x - y)) == 2*A*sinh(x)*sinh(y)

    assert trigsimp(A*cos(0.12345)**2 + A*sin(0.12345)**2) == 1.0*A
예제 #3
0
def test_hyper_as_trig():
    from diofant.simplify.fu import _osborne as o, _osbornei as i, TR12

    eq = sinh(x)**2 + cosh(x)**2
    t, f = hyper_as_trig(eq)
    assert f(fu(t)) == cosh(2*x)
    e, f = hyper_as_trig(tanh(x + y))
    assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1)

    d = Dummy()
    assert o(sinh(x), d) == I*sin(x*d)
    assert o(tanh(x), d) == I*tan(x*d)
    assert o(coth(x), d) == cot(x*d)/I
    assert o(cosh(x), d) == cos(x*d)
    for func in (sinh, cosh, tanh, coth):
        h = func(pi)
        assert i(o(h, d), d) == h
    # /!\ the _osborne functions are not meant to work
    # in the o(i(trig, d), d) direction so we just check
    # that they work as they are supposed to work
    assert i(cos(x*y), y) == cosh(x)
    assert i(sin(x*y), y) == sinh(x)/I
    assert i(tan(x*y), y) == tanh(x)/I
    assert i(cot(x*y), y) == coth(x)*I
    assert i(sec(x*y), y) == 1/cosh(x)
    assert i(csc(x*y), y) == I/sinh(x)
예제 #4
0
def test_tan_rewrite():
    neg_exp, pos_exp = exp(-x*I), exp(x*I)
    assert tan(x).rewrite(exp) == I*(neg_exp - pos_exp)/(neg_exp + pos_exp)
    assert tan(x).rewrite(sin) == 2*sin(x)**2/sin(2*x)
    assert tan(x).rewrite(cos) == -cos(x + pi/2)/cos(x)
    assert tan(x).rewrite(cot) == 1/cot(x)
    assert (tan(sinh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: sinh(3)}).evalf())
    assert (tan(cosh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: cosh(3)}).evalf())
    assert (tan(tanh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: tanh(3)}).evalf())
    assert (tan(coth(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: coth(3)}).evalf())
    assert (tan(sin(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: sin(3)}).evalf())
    assert (tan(cos(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: cos(3)}).evalf())
    assert (tan(tan(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: tan(3)}).evalf())
    assert (tan(cot(x)).rewrite(exp).subs({x: 3}).evalf() ==
            tan(x).rewrite(exp).subs({x: cot(3)}).evalf())
    assert tan(log(x)).rewrite(Pow) == I*(x**-I - x**I)/(x**-I + x**I)
    assert tan(x).rewrite(Pow) == tan(x)
    assert 0 == (cos(pi/34)*tan(pi/34) - sin(pi/34)).rewrite(sqrt)
    assert 0 == (cos(pi/17)*tan(pi/17) - sin(pi/17)).rewrite(sqrt)
    assert tan(pi/19).rewrite(sqrt) == tan(pi/19)
    assert tan(8*pi/19).rewrite(sqrt) == tan(8*pi/19)
예제 #5
0
def test_has():
    assert cot(x).has(x)
    assert cot(x).has(cot)
    assert not cot(x).has(sin)
    assert sin(x).has(x)
    assert sin(x).has(sin)
    assert not sin(x).has(cot)
예제 #6
0
def test_sympyissue_21177():
    e1 = cot(pi * x) / ((x - 1) * (x - 2) + 1)
    e2 = cot(pi * x) / (x**2 - 3 * x + 3)
    pt = Rational(3, 2) - sqrt(3) * I / 2
    ans = -sqrt(3) * tanh(sqrt(3) * pi / 2) / 3

    assert residue(e1, x, pt) == ans
    assert residue(e2, x, pt) == ans
예제 #7
0
def test_basic1():
    assert limit(x, x, oo) == oo
    assert limit(x, x, -oo) == -oo
    assert limit(-x, x, oo) == -oo
    assert limit(x**2, x, -oo) == oo
    assert limit(-x**2, x, oo) == -oo
    assert limit(x*log(x), x, 0, dir="+") == 0
    assert limit(1/x, x, oo) == 0
    assert limit(exp(x), x, oo) == oo
    assert limit(-exp(x), x, oo) == -oo
    assert limit(exp(x)/x, x, oo) == oo
    assert limit(1/x - exp(-x), x, oo) == 0
    assert limit(x + 1/x, x, oo) == oo
    assert limit(x - x**2, x, oo) == -oo
    assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
    assert limit((1 + x)**oo, x, 0) == oo
    assert limit((1 + x)**oo, x, 0, dir='-') == 0
    assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**oo
    assert limit(y/x/log(x), x, 0) == -oo*sign(y)
    assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
    limit(Sum(1/x, (x, 1, y)) - log(y), y, oo)
    limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo)
    assert limit(gamma(1/x + 3), x, oo) == 2
    assert limit(nan, x, -oo) == nan
    assert limit(O(2)*x, x, nan) == nan
    assert limit(sin(O(x)), x, 0) == 0
    assert limit(1/(x - 1), x, 1, dir="+") == oo
    assert limit(1/(x - 1), x, 1, dir="-") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="-") == oo
    assert limit(1/sin(x), x, pi, dir="+") == -oo
    assert limit(1/sin(x), x, pi, dir="-") == oo
    assert limit(1/cos(x), x, pi/2, dir="+") == -oo
    assert limit(1/cos(x), x, pi/2, dir="-") == oo
    assert limit(1/tan(x**3), x, cbrt(2*pi), dir="+") == oo
    assert limit(1/tan(x**3), x, cbrt(2*pi), dir="-") == -oo
    assert limit(1/cot(x)**3, x, 3*pi/2, dir="+") == -oo
    assert limit(1/cot(x)**3, x, 3*pi/2, dir="-") == oo

    # approaching 0
    # from dir="+"
    assert limit(1 + 1/x, x, 0) == oo
    # from dir='-'
    # Add
    assert limit(1 + 1/x, x, 0, dir='-') == -oo
    # Pow
    assert limit(x**(-2), x, 0, dir='-') == oo
    assert limit(x**(-3), x, 0, dir='-') == -oo
    assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
    assert limit(x**2, x, 0, dir='-') == 0
    assert limit(sqrt(x), x, 0, dir='-') == 0
    assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi))
    assert limit((1 + cos(x))**oo, x, 0) == oo

    assert limit(x**2, x, 0, dir='real') == 0
    assert limit(exp(x), x, 0, dir='real') == 1
    pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
예제 #8
0
def test_basic1():
    assert limit(x, x, oo) == oo
    assert limit(x, x, -oo) == -oo
    assert limit(-x, x, oo) == -oo
    assert limit(x**2, x, -oo) == oo
    assert limit(-x**2, x, oo) == -oo
    assert limit(x*log(x), x, 0, dir="+") == 0
    assert limit(1/x, x, oo) == 0
    assert limit(exp(x), x, oo) == oo
    assert limit(-exp(x), x, oo) == -oo
    assert limit(exp(x)/x, x, oo) == oo
    assert limit(1/x - exp(-x), x, oo) == 0
    assert limit(x + 1/x, x, oo) == oo
    assert limit(x - x**2, x, oo) == -oo
    assert limit((1 + x)**(1 + sqrt(2)), x, 0) == 1
    assert limit((1 + x)**oo, x, 0) == oo
    assert limit((1 + x)**oo, x, 0, dir='-') == 0
    assert limit((1 + x + y)**oo, x, 0, dir='-') == (1 + y)**(oo)
    assert limit(y/x/log(x), x, 0) == -oo*sign(y)
    assert limit(cos(x + y)/x, x, 0) == sign(cos(y))*oo
    limit(Sum(1/x, (x, 1, y)) - log(y), y, oo)
    limit(Sum(1/x, (x, 1, y)) - 1/y, y, oo)
    assert limit(gamma(1/x + 3), x, oo) == 2
    assert limit(S.NaN, x, -oo) == S.NaN
    assert limit(O(2)*x, x, S.NaN) == S.NaN
    assert limit(sin(O(x)), x, 0) == 0
    assert limit(1/(x - 1), x, 1, dir="+") == oo
    assert limit(1/(x - 1), x, 1, dir="-") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="+") == -oo
    assert limit(1/(5 - x)**3, x, 5, dir="-") == oo
    assert limit(1/sin(x), x, pi, dir="+") == -oo
    assert limit(1/sin(x), x, pi, dir="-") == oo
    assert limit(1/cos(x), x, pi/2, dir="+") == -oo
    assert limit(1/cos(x), x, pi/2, dir="-") == oo
    assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="+") == oo
    assert limit(1/tan(x**3), x, (2*pi)**Rational(1, 3), dir="-") == -oo
    assert limit(1/cot(x)**3, x, (3*pi/2), dir="+") == -oo
    assert limit(1/cot(x)**3, x, (3*pi/2), dir="-") == oo

    # approaching 0
    # from dir="+"
    assert limit(1 + 1/x, x, 0) == oo
    # from dir='-'
    # Add
    assert limit(1 + 1/x, x, 0, dir='-') == -oo
    # Pow
    assert limit(x**(-2), x, 0, dir='-') == oo
    assert limit(x**(-3), x, 0, dir='-') == -oo
    assert limit(1/sqrt(x), x, 0, dir='-') == (-oo)*I
    assert limit(x**2, x, 0, dir='-') == 0
    assert limit(sqrt(x), x, 0, dir='-') == 0
    assert limit(x**-pi, x, 0, dir='-') == oo*sign((-1)**(-pi))
    assert limit((1 + cos(x))**oo, x, 0) == oo

    assert limit(x**2, x, 0, dir='real') == 0
    assert limit(exp(x), x, 0, dir='real') == 1
    pytest.raises(PoleError, lambda: limit(1/x, x, 0, dir='real'))
예제 #9
0
def test_trigintegrate_mixed():
    assert trigintegrate(sin(x) * sec(x), x) == -log(sin(x)**2 - 1) / 2
    assert trigintegrate(sin(x) * csc(x), x) == x
    assert trigintegrate(sin(x) * cot(x), x) == sin(x)

    assert trigintegrate(cos(x) * sec(x), x) == x
    assert trigintegrate(cos(x) * csc(x), x) == log(cos(x)**2 - 1) / 2
    assert trigintegrate(cos(x) * tan(x), x) == -cos(x)
    assert trigintegrate(cos(x)*cot(x), x) == log(cos(x) - 1)/2 \
        - log(cos(x) + 1)/2 + cos(x)
예제 #10
0
def test_trigsimp1a():
    assert trigsimp(sin(2)**2*cos(3)*exp(2)/cos(2)**2) == tan(2)**2*cos(3)*exp(2)
    assert trigsimp(tan(2)**2*cos(3)*exp(2)*cos(2)**2) == sin(2)**2*cos(3)*exp(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*sin(2)) == cos(3)*exp(2)*cos(2)
    assert trigsimp(tan(2)*cos(3)*exp(2)/sin(2)) == cos(3)*exp(2)/cos(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)/cos(2)) == cos(3)*exp(2)/sin(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*tan(2)) == cos(3)*exp(2)
    assert trigsimp(sinh(2)*cos(3)*exp(2)/cosh(2)) == tanh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)*cosh(2)) == sinh(2)*cos(3)*exp(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*sinh(2)) == cosh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)/sinh(2)) == cos(3)*exp(2)/cosh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)/cosh(2)) == cos(3)*exp(2)/sinh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*tanh(2)) == cos(3)*exp(2)
예제 #11
0
def test_trigsimp1a():
    assert trigsimp(sin(2)**2*cos(3)*exp(2)/cos(2)**2) == tan(2)**2*cos(3)*exp(2)
    assert trigsimp(tan(2)**2*cos(3)*exp(2)*cos(2)**2) == sin(2)**2*cos(3)*exp(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*sin(2)) == cos(3)*exp(2)*cos(2)
    assert trigsimp(tan(2)*cos(3)*exp(2)/sin(2)) == cos(3)*exp(2)/cos(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)/cos(2)) == cos(3)*exp(2)/sin(2)
    assert trigsimp(cot(2)*cos(3)*exp(2)*tan(2)) == cos(3)*exp(2)
    assert trigsimp(sinh(2)*cos(3)*exp(2)/cosh(2)) == tanh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)*cosh(2)) == sinh(2)*cos(3)*exp(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*sinh(2)) == cosh(2)*cos(3)*exp(2)
    assert trigsimp(tanh(2)*cos(3)*exp(2)/sinh(2)) == cos(3)*exp(2)/cosh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)/cosh(2)) == cos(3)*exp(2)/sinh(2)
    assert trigsimp(coth(2)*cos(3)*exp(2)*tanh(2)) == cos(3)*exp(2)
예제 #12
0
def test_trigsimp_old(capsys):
    e = 2 * sin(x)**2 + 2 * cos(x)**2
    assert trigsimp(e, old=True) == 2
    e = 3 * tanh(x)**7 - 2 / coth(x)**7
    assert trigsimp(e, method='old') == e
    e = (-sin(x) + 1) / cos(x) + cos(x) / (-sin(x) + 1)
    assert (trigsimp(e, method='old') == (-sin(x) + 1) / cos(x) - cos(x) /
            (sin(x) - 1))
    e = (-sin(x) + 1) / cos(x) + cos(x) / (-sin(x) + 1)
    assert trigsimp(e, method='groebner', old=True) == 2 / cos(x)

    assert trigsimp(1 / cot(x)**2, compare=True, old=True) == cot(x)**(-2)
    assert capsys.readouterr().out == '\tfutrig: tan(x)**2\n'
예제 #13
0
def test_hyperbolic_simp():
    assert trigsimp(sinh(x)**2 + 1) == cosh(x)**2
    assert trigsimp(cosh(x)**2 - 1) == sinh(x)**2
    assert trigsimp(cosh(x)**2 - sinh(x)**2) == 1
    assert trigsimp(1 - tanh(x)**2) == 1 / cosh(x)**2
    assert trigsimp(1 - 1 / cosh(x)**2) == tanh(x)**2
    assert trigsimp(tanh(x)**2 + 1 / cosh(x)**2) == 1
    assert trigsimp(coth(x)**2 - 1) == 1 / sinh(x)**2
    assert trigsimp(1 / sinh(x)**2 + 1) == 1 / tanh(x)**2
    assert trigsimp(coth(x)**2 - 1 / sinh(x)**2) == 1

    assert trigsimp(5 * cosh(x)**2 - 5 * sinh(x)**2) == 5
    assert trigsimp(5 * cosh(x / 2)**2 -
                    2 * sinh(x / 2)**2) == 3 * cosh(x) / 2 + Rational(7, 2)

    assert trigsimp(sinh(x) / cosh(x)) == tanh(x)
    assert trigsimp(tanh(x)) == trigsimp(sinh(x) / cosh(x))
    assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x)
    assert trigsimp(2 * tanh(x) * cosh(x)) == 2 * sinh(x)
    assert trigsimp(coth(x)**3 * sinh(x)**3) == cosh(x)**3
    assert trigsimp(y * tanh(x)**2 / sinh(x)**2) == y / cosh(x)**2
    assert trigsimp(coth(x) / cosh(x)) == 1 / sinh(x)

    e = 2 * cosh(x)**2 - 2 * sinh(x)**2
    assert trigsimp(log(e)) == log(2)

    assert trigsimp(cosh(x)**2 * cosh(y)**2 - cosh(x)**2 * sinh(y)**2 -
                    sinh(x)**2,
                    recursive=True) == 1
    assert trigsimp(sinh(x)**2 * sinh(y)**2 - sinh(x)**2 * cosh(y)**2 +
                    cosh(x)**2,
                    recursive=True) == 1

    assert abs(trigsimp(2.0 * cosh(x)**2 - 2.0 * sinh(x)**2) - 2.0) < 1e-10

    assert trigsimp(sinh(x)**2 / cosh(x)**2) == tanh(x)**2
    assert trigsimp(sinh(x)**3 / cosh(x)**3) == tanh(x)**3
    assert trigsimp(sinh(x)**10 / cosh(x)**10) == tanh(x)**10
    assert trigsimp(cosh(x)**3 / sinh(x)**3) == 1 / tanh(x)**3

    assert trigsimp(cosh(x) / sinh(x)) == 1 / tanh(x)
    assert trigsimp(cosh(x)**2 / sinh(x)**2) == 1 / tanh(x)**2
    assert trigsimp(cosh(x)**10 / sinh(x)**10) == 1 / tanh(x)**10

    assert trigsimp(x * cosh(x) * tanh(x)) == x * sinh(x)
    assert trigsimp(-sinh(x) + cosh(x) * tanh(x)) == 0

    assert tan(x) != 1 / cot(x)  # cot doesn't auto-simplify

    assert trigsimp(tan(x) - 1 / cot(x)) == 0
    assert trigsimp(3 * tanh(x)**7 - 2 / coth(x)**7) == tanh(x)**7
예제 #14
0
def test_TR13():
    assert TR13(tan(3)*tan(2)) == -tan(2)/tan(5) - tan(3)/tan(5) + 1
    assert TR13(cot(3)*cot(2)) == 1 + cot(3)*cot(5) + cot(2)*cot(5)
    assert TR13(tan(1)*tan(2)*tan(3)) == \
        (-tan(2)/tan(5) - tan(3)/tan(5) + 1)*tan(1)
    assert TR13(tan(1)*tan(2)*cot(3)) == \
        (-tan(2)/tan(3) + 1 - tan(1)/tan(3))*cot(3)
예제 #15
0
def test_TR13():
    assert TR13(tan(3) * tan(2)) == -tan(2) / tan(5) - tan(3) / tan(5) + 1
    assert TR13(cot(3) * cot(2)) == 1 + cot(3) * cot(5) + cot(2) * cot(5)
    assert TR13(tan(1)*tan(2)*tan(3)) == \
        (-tan(2)/tan(5) - tan(3)/tan(5) + 1)*tan(1)
    assert TR13(tan(1)*tan(2)*cot(3)) == \
        (-tan(2)/tan(3) + 1 - tan(1)/tan(3))*cot(3)
예제 #16
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}'
예제 #17
0
def test_hyperbolic_simp():
    assert trigsimp(sinh(x)**2 + 1) == cosh(x)**2
    assert trigsimp(cosh(x)**2 - 1) == sinh(x)**2
    assert trigsimp(cosh(x)**2 - sinh(x)**2) == 1
    assert trigsimp(1 - tanh(x)**2) == 1/cosh(x)**2
    assert trigsimp(1 - 1/cosh(x)**2) == tanh(x)**2
    assert trigsimp(tanh(x)**2 + 1/cosh(x)**2) == 1
    assert trigsimp(coth(x)**2 - 1) == 1/sinh(x)**2
    assert trigsimp(1/sinh(x)**2 + 1) == 1/tanh(x)**2
    assert trigsimp(coth(x)**2 - 1/sinh(x)**2) == 1

    assert trigsimp(5*cosh(x)**2 - 5*sinh(x)**2) == 5
    assert trigsimp(5*cosh(x/2)**2 - 2*sinh(x/2)**2) == 3*cosh(x)/2 + Rational(7, 2)

    assert trigsimp(sinh(x)/cosh(x)) == tanh(x)
    assert trigsimp(tanh(x)) == trigsimp(sinh(x)/cosh(x))
    assert trigsimp(cosh(x)/sinh(x)) == 1/tanh(x)
    assert trigsimp(2*tanh(x)*cosh(x)) == 2*sinh(x)
    assert trigsimp(coth(x)**3*sinh(x)**3) == cosh(x)**3
    assert trigsimp(y*tanh(x)**2/sinh(x)**2) == y/cosh(x)**2
    assert trigsimp(coth(x)/cosh(x)) == 1/sinh(x)

    e = 2*cosh(x)**2 - 2*sinh(x)**2
    assert trigsimp(log(e)) == log(2)

    assert trigsimp(cosh(x)**2*cosh(y)**2 - cosh(x)**2*sinh(y)**2 - sinh(x)**2,
                    recursive=True) == 1
    assert trigsimp(sinh(x)**2*sinh(y)**2 - sinh(x)**2*cosh(y)**2 + cosh(x)**2,
                    recursive=True) == 1

    assert abs(trigsimp(2.0*cosh(x)**2 - 2.0*sinh(x)**2) - 2.0) < 1e-10

    assert trigsimp(sinh(x)**2/cosh(x)**2) == tanh(x)**2
    assert trigsimp(sinh(x)**3/cosh(x)**3) == tanh(x)**3
    assert trigsimp(sinh(x)**10/cosh(x)**10) == tanh(x)**10
    assert trigsimp(cosh(x)**3/sinh(x)**3) == 1/tanh(x)**3

    assert trigsimp(cosh(x)/sinh(x)) == 1/tanh(x)
    assert trigsimp(cosh(x)**2/sinh(x)**2) == 1/tanh(x)**2
    assert trigsimp(cosh(x)**10/sinh(x)**10) == 1/tanh(x)**10

    assert trigsimp(x*cosh(x)*tanh(x)) == x*sinh(x)
    assert trigsimp(-sinh(x) + cosh(x)*tanh(x)) == 0

    assert tan(x) != 1/cot(x)  # cot doesn't auto-simplify

    assert trigsimp(tan(x) - 1/cot(x)) == 0
    assert trigsimp(3*tanh(x)**7 - 2/coth(x)**7) == tanh(x)**7
예제 #18
0
def test_real_imag():
    a, b = symbols('a b', extended_real=True)
    z = a + b*I
    for deep in [True, False]:
        assert sin(
            z).as_real_imag(deep=deep) == (sin(a)*cosh(b), cos(a)*sinh(b))
        assert cos(
            z).as_real_imag(deep=deep) == (cos(a)*cosh(b), -sin(a)*sinh(b))
        assert tan(z).as_real_imag(deep=deep) == (sin(2*a)/(cos(2*a) +
                                                            cosh(2*b)), sinh(2*b)/(cos(2*a) + cosh(2*b)))
        assert cot(z).as_real_imag(deep=deep) == (-sin(2*a)/(cos(2*a) -
                                                             cosh(2*b)), -sinh(2*b)/(cos(2*a) - cosh(2*b)))
        assert sin(a).as_real_imag(deep=deep) == (sin(a), 0)
        assert cos(a).as_real_imag(deep=deep) == (cos(a), 0)
        assert tan(a).as_real_imag(deep=deep) == (tan(a), 0)
        assert cot(a).as_real_imag(deep=deep) == (cot(a), 0)
예제 #19
0
def test_sympyissue_21176():
    e = x**2 * cot(pi * x) / (x**4 + 1)
    pt = -sqrt(2) / 2 - sqrt(2) * I / 2
    assert residue(
        e, x,
        pt) == sqrt(2) * I / Mul(2,
                                 -2 + 2 * I,
                                 tan(sqrt(2) * pi / 2 + sqrt(2) * I * pi / 2),
                                 evaluate=False)
예제 #20
0
def test_Ynm():
    # http://en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi",
                                                         extended_real=True)
    from diofant.abc import n, m

    assert Ynm(0, 0, th, ph).expand(func=True) == 1 / (2 * sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2 * I * ph) * Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(
        func=True) == sqrt(6) * sin(th) * exp(-I * ph) / (4 * sqrt(pi))
    assert Ynm(1, 0, th,
               ph).expand(func=True) == sqrt(3) * cos(th) / (2 * sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(
        func=True) == -sqrt(6) * sin(th) * exp(I * ph) / (4 * sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(
        func=True
    ) == 3 * sqrt(5) * cos(th)**2 / (4 * sqrt(pi)) - sqrt(5) / (4 * sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30) * sin(th) * exp(
        I * ph) * cos(th) / (4 * sqrt(pi))
    assert Ynm(
        2, -2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(-2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) +
                                  sqrt(30) * exp(-2 * I * ph) / (8 * sqrt(pi)))
    assert Ynm(
        2, 2, th,
        ph).expand(func=True) == (-sqrt(30) * exp(2 * I * ph) * cos(th)**2 /
                                  (8 * sqrt(pi)) + sqrt(30) * exp(2 * I * ph) /
                                  (8 * sqrt(pi)))

    assert diff(Ynm(
        n, m, th, ph), th) == (m * cot(th) * Ynm(n, m, th, ph) + sqrt(
            (-m + n) * (m + n + 1)) * exp(-I * ph) * Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I * m * Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(
        Ynm(n, m, th,
            ph)) == (-1)**(2 * m) * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2 * I * m * ph) * Ynm(n, m, th, ph)
    assert Ynm(n, -m, th,
               ph) == (-1)**m * exp(-2 * I * m * ph) * Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) == Ynm(
        n, m, th, ph).rewrite(cos) == exp(I * m * ph) * sqrt(
            (2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
            assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() == (
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        cos(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi)),
        sqrt((2 * n + 1) * factorial(-m + n) / factorial(m + n)) *
        sin(m * ph) * assoc_legendre(n, m, cos(th)) / (2 * sqrt(pi))))
예제 #21
0
def test_trigsimp_issues():
    # issue sympy/sympy#4625 - factor_terms works, too
    assert trigsimp(sin(x)**3 + cos(x)**2 * sin(x)) == sin(x)

    # issue sympy/sympy#5948
    assert trigsimp(diff(integrate(cos(x)/sin(x)**3, x), x)) == \
        cos(x)/sin(x)**3
    assert trigsimp(diff(integrate(sin(x)/cos(x)**3, x), x)) == \
        sin(x)/cos(x)**3

    # check integer exponents
    e = sin(x)**y / cos(x)**y
    assert trigsimp(e) == e
    assert trigsimp(e.subs(y, 2)) == tan(x)**2
    assert trigsimp(e.subs(x, 1)) == tan(1)**y

    # check for multiple patterns
    assert (cos(x)**2/sin(x)**2*cos(y)**2/sin(y)**2).trigsimp() == \
        1/tan(x)**2/tan(y)**2
    assert trigsimp(cos(x)/sin(x)*cos(x+y)/sin(x+y)) == \
        1/(tan(x)*tan(x + y))

    eq = cos(2) * (cos(3) + 1)**2 / (cos(3) - 1)**2
    assert trigsimp(eq) == eq.factor()  # factor makes denom (-1 + cos(3))**2
    assert trigsimp(cos(2)*(cos(3) + 1)**2*(cos(3) - 1)**2) == \
        cos(2)*sin(3)**4

    # issue sympy/sympy#6789; this generates an expression that formerly caused
    # trigsimp to hang
    assert cot(x).equals(tan(x)) is False

    # nan or the unchanged expression is ok, but not sin(1)
    z = cos(x)**2 + sin(x)**2 - 1
    z1 = tan(x)**2 - 1 / cot(x)**2
    n = (1 + z1 / z)
    assert trigsimp(sin(n)) != sin(1)
    eq = x * (n - 1) - x * n
    assert trigsimp(eq) is nan
    assert trigsimp(eq, recursive=True) is nan
    assert trigsimp(1).is_Integer

    assert trigsimp(-sin(x)**4 - 2 * sin(x)**2 * cos(x)**2 - cos(x)**4) == -1
예제 #22
0
def test_trigsimp_issues():
    # issue sympy/sympy#4625 - factor_terms works, too
    assert trigsimp(sin(x)**3 + cos(x)**2*sin(x)) == sin(x)

    # issue sympy/sympy#5948
    assert trigsimp(diff(integrate(cos(x)/sin(x)**3, x), x)) == \
        cos(x)/sin(x)**3
    assert trigsimp(diff(integrate(sin(x)/cos(x)**3, x), x)) == \
        sin(x)/cos(x)**3

    # check integer exponents
    e = sin(x)**y/cos(x)**y
    assert trigsimp(e) == e
    assert trigsimp(e.subs({y: 2})) == tan(x)**2
    assert trigsimp(e.subs({x: 1})) == tan(1)**y

    # check for multiple patterns
    assert (cos(x)**2/sin(x)**2*cos(y)**2/sin(y)**2).trigsimp() == \
        1/tan(x)**2/tan(y)**2
    assert trigsimp(cos(x)/sin(x)*cos(x+y)/sin(x+y)) == \
        1/(tan(x)*tan(x + y))

    eq = cos(2)*(cos(3) + 1)**2/(cos(3) - 1)**2
    assert trigsimp(eq) == eq.factor()  # factor makes denom (-1 + cos(3))**2
    assert trigsimp(cos(2)*(cos(3) + 1)**2*(cos(3) - 1)**2) == \
        cos(2)*sin(3)**4

    # issue sympy/sympy#6789; this generates an expression that formerly caused
    # trigsimp to hang
    assert cot(x).equals(tan(x)) is False

    # nan or the unchanged expression is ok, but not sin(1)
    z = cos(x)**2 + sin(x)**2 - 1
    z1 = tan(x)**2 - 1/cot(x)**2
    n = (1 + z1/z)
    assert trigsimp(sin(n)) != sin(1)
    eq = x*(n - 1) - x*n
    assert trigsimp(eq) is nan
    assert trigsimp(eq, recursive=True) is nan
    assert trigsimp(1).is_Integer

    assert trigsimp(-sin(x)**4 - 2*sin(x)**2*cos(x)**2 - cos(x)**4) == -1
예제 #23
0
def test_inverses():
    pytest.raises(AttributeError, lambda: sin(x).inverse())
    pytest.raises(AttributeError, lambda: cos(x).inverse())
    assert tan(x).inverse() == atan
    assert cot(x).inverse() == acot
    pytest.raises(AttributeError, lambda: csc(x).inverse())
    pytest.raises(AttributeError, lambda: sec(x).inverse())
    assert asin(x).inverse() == sin
    assert acos(x).inverse() == cos
    assert atan(x).inverse() == tan
    assert acot(x).inverse() == cot
예제 #24
0
def test_trigonometric():
    n3 = Integer(3)
    e = (sin(x)**2).diff(x)
    assert e == 2 * sin(x) * cos(x)
    e = e.subs({x: n3})
    assert e == 2 * cos(n3) * sin(n3)

    e = (sin(x)**2).diff(x)
    assert e == 2 * sin(x) * cos(x)
    e = e.subs({sin(x): cos(x)})
    assert e == 2 * cos(x)**2

    i = Symbol('i', integer=True)
    assert tan(x).subs({x: pi / 2}) is zoo
    assert cot(x).subs({x: pi}) is zoo
    assert cot(i * x).subs({x: pi}) is zoo
    assert tan(i * x).subs({x: pi / 2}) == tan(i * pi / 2)
    assert tan(i * x).subs({x: pi / 2}).subs({i: 1}) is zoo
    o = Symbol('o', odd=True)
    assert tan(o * x).subs({x: pi / 2}) == tan(o * pi / 2)
예제 #25
0
def test_trigonometric():
    n3 = Rational(3)
    e = (sin(x)**2).diff(x)
    assert e == 2 * sin(x) * cos(x)
    e = e.subs(x, n3)
    assert e == 2 * cos(n3) * sin(n3)

    e = (sin(x)**2).diff(x)
    assert e == 2 * sin(x) * cos(x)
    e = e.subs(sin(x), cos(x))
    assert e == 2 * cos(x)**2

    i = Symbol('i', integer=True)
    assert tan(x).subs(x, pi / 2) is zoo
    assert cot(x).subs(x, pi) is zoo
    assert cot(i * x).subs(x, pi) is zoo
    assert tan(i * x).subs(x, pi / 2) == tan(i * pi / 2)
    assert tan(i * x).subs(x, pi / 2).subs(i, 1) is zoo
    o = Symbol('o', odd=True)
    assert tan(o * x).subs(x, pi / 2) == tan(o * pi / 2)
예제 #26
0
def test_trigonometric():
    n3 = Integer(3)
    e = (sin(x)**2).diff(x)
    assert e == 2*sin(x)*cos(x)
    e = e.subs({x: n3})
    assert e == 2*cos(n3)*sin(n3)

    e = (sin(x)**2).diff(x)
    assert e == 2*sin(x)*cos(x)
    e = e.subs({sin(x): cos(x)})
    assert e == 2*cos(x)**2

    i = Symbol('i', integer=True)
    assert tan(x).subs({x: pi/2}) is zoo
    assert cot(x).subs({x: pi}) is zoo
    assert cot(i*x).subs({x: pi}) is zoo
    assert tan(i*x).subs({x: pi/2}) == tan(i*pi/2)
    assert tan(i*x).subs({x: pi/2}).subs({i: 1}) is zoo
    o = Symbol('o', odd=True)
    assert tan(o*x).subs({x: pi/2}) == tan(o*pi/2)
예제 #27
0
def test_conjugate():
    a = Symbol('a', extended_real=True)
    b = Symbol('b', extended_real=True)
    c = Symbol('c', imaginary=True)
    d = Symbol('d', imaginary=True)
    z = a + I * b + c + I * d
    zc = a - I * b - c + I * d
    assert conjugate(z) == zc
    assert conjugate(exp(z)) == exp(zc)
    assert conjugate(exp(I * x)) == exp(-I * conjugate(x))
    assert conjugate(z**5) == zc**5
    assert conjugate(abs(x)) == abs(x)
    assert conjugate(sign(z)) == sign(zc)
    assert conjugate(sin(z)) == sin(zc)
    assert conjugate(cos(z)) == cos(zc)
    assert conjugate(tan(z)) == tan(zc)
    assert conjugate(cot(z)) == cot(zc)
    assert conjugate(sinh(z)) == sinh(zc)
    assert conjugate(cosh(z)) == cosh(zc)
    assert conjugate(tanh(z)) == tanh(zc)
    assert conjugate(coth(z)) == coth(zc)
예제 #28
0
def test_conjugate():
    a = Symbol("a", extended_real=True)
    b = Symbol("b", extended_real=True)
    c = Symbol("c", imaginary=True)
    d = Symbol("d", imaginary=True)
    x = Symbol('x')
    z = a + I*b + c + I*d
    zc = a - I*b - c + I*d
    assert conjugate(z) == zc
    assert conjugate(exp(z)) == exp(zc)
    assert conjugate(exp(I*x)) == exp(-I*conjugate(x))
    assert conjugate(z**5) == zc**5
    assert conjugate(abs(x)) == abs(x)
    assert conjugate(sign(z)) == sign(zc)
    assert conjugate(sin(z)) == sin(zc)
    assert conjugate(cos(z)) == cos(zc)
    assert conjugate(tan(z)) == tan(zc)
    assert conjugate(cot(z)) == cot(zc)
    assert conjugate(sinh(z)) == sinh(zc)
    assert conjugate(cosh(z)) == cosh(zc)
    assert conjugate(tanh(z)) == tanh(zc)
    assert conjugate(coth(z)) == coth(zc)
예제 #29
0
def test_trigsimp1():
    assert trigsimp(1 - sin(x)**2) == cos(x)**2
    assert trigsimp(1 - cos(x)**2) == sin(x)**2
    assert trigsimp(sin(x)**2 + cos(x)**2) == 1
    assert trigsimp(1 + tan(x)**2) == 1 / cos(x)**2
    assert trigsimp(1 / cos(x)**2 - 1) == tan(x)**2
    assert trigsimp(1 / cos(x)**2 - tan(x)**2) == 1
    assert trigsimp(1 + cot(x)**2) == 1 / sin(x)**2
    assert trigsimp(1 / sin(x)**2 - 1) == 1 / tan(x)**2
    assert trigsimp(1 / sin(x)**2 - cot(x)**2) == 1

    assert trigsimp(5 * cos(x)**2 + 5 * sin(x)**2) == 5
    assert trigsimp(5 * cos(x / 2)**2 +
                    2 * sin(x / 2)**2) == 3 * cos(x) / 2 + Rational(7, 2)

    assert trigsimp(sin(x) / cos(x)) == tan(x)
    assert trigsimp(2 * tan(x) * cos(x)) == 2 * sin(x)
    assert trigsimp(cot(x)**3 * sin(x)**3) == cos(x)**3
    assert trigsimp(y * tan(x)**2 / sin(x)**2) == y / cos(x)**2
    assert trigsimp(cot(x) / cos(x)) == 1 / sin(x)

    assert trigsimp(sin(x + y) + sin(x - y)) == 2 * sin(x) * cos(y)
    assert trigsimp(sin(x + y) - sin(x - y)) == 2 * sin(y) * cos(x)
    assert trigsimp(cos(x + y) + cos(x - y)) == 2 * cos(x) * cos(y)
    assert trigsimp(cos(x + y) - cos(x - y)) == -2 * sin(x) * sin(y)
    assert trigsimp(tan(x + y) - tan(x)/(1 - tan(x)*tan(y))) == \
        sin(y)/(-sin(y)*tan(x) + cos(y))  # -tan(y)/(tan(x)*tan(y) - 1)

    assert trigsimp(sinh(x + y) + sinh(x - y)) == 2 * sinh(x) * cosh(y)
    assert trigsimp(sinh(x + y) - sinh(x - y)) == 2 * sinh(y) * cosh(x)
    assert trigsimp(cosh(x + y) + cosh(x - y)) == 2 * cosh(x) * cosh(y)
    assert trigsimp(cosh(x + y) - cosh(x - y)) == 2 * sinh(x) * sinh(y)
    assert trigsimp(tanh(x + y) - tanh(x)/(1 + tanh(x)*tanh(y))) == \
        sinh(y)/(sinh(y)*tanh(x) + cosh(y))

    assert trigsimp(cos(0.12345)**2 + sin(0.12345)**2) == 1
    e = 2 * sin(x)**2 + 2 * cos(x)**2
    assert trigsimp(log(e)) == log(2)
예제 #30
0
def test_cos_rewrite():
    assert cos(x).rewrite(exp) == exp(I*x)/2 + exp(-I*x)/2
    assert cos(x).rewrite(tan) == (1 - tan(x/2)**2)/(1 + tan(x/2)**2)
    assert cos(x).rewrite(cot) == -(1 - cot(x/2)**2)/(1 + cot(x/2)**2)
    assert (cos(sinh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: sinh(3)}).evalf())
    assert (cos(cosh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: cosh(3)}).evalf())
    assert (cos(tanh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: tanh(3)}).evalf())
    assert (cos(coth(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: coth(3)}).evalf())
    assert (cos(sin(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: sin(3)}).evalf())
    assert (cos(cos(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: cos(3)}).evalf())
    assert (cos(tan(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: tan(3)}).evalf())
    assert (cos(cot(x)).rewrite(exp).subs({x: 3}).evalf() ==
            cos(x).rewrite(exp).subs({x: cot(3)}).evalf())
    assert cos(log(x)).rewrite(Pow) == x**I/2 + x**-I/2
    assert cos(x).rewrite(Pow) == cos(x)
    assert cos(x).rewrite(sec) == 1/sec(x)
예제 #31
0
def test_sin_rewrite():
    assert sin(x).rewrite(exp) == -I*(exp(I*x) - exp(-I*x))/2
    assert sin(x).rewrite(tan) == 2*tan(x/2)/(1 + tan(x/2)**2)
    assert sin(x).rewrite(cot) == 2*cot(x/2)/(1 + cot(x/2)**2)
    assert (sin(sinh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: sinh(3)}).evalf())
    assert (sin(cosh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: cosh(3)}).evalf())
    assert (sin(tanh(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: tanh(3)}).evalf())
    assert (sin(coth(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: coth(3)}).evalf())
    assert (sin(sin(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: sin(3)}).evalf())
    assert (sin(cos(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: cos(3)}).evalf())
    assert (sin(tan(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: tan(3)}).evalf())
    assert (sin(cot(x)).rewrite(exp).subs({x: 3}).evalf() ==
            sin(x).rewrite(exp).subs({x: cot(3)}).evalf())
    assert sin(log(x)).rewrite(Pow) == I*x**-I / 2 - I*x**I / 2
    assert sin(x).rewrite(Pow) == sin(x)  # issue sympy/sympy#7171
    assert sin(x).rewrite(csc) == 1/csc(x)
예제 #32
0
def test_trigsimp1():
    assert trigsimp(1 - sin(x)**2) == cos(x)**2
    assert trigsimp(1 - cos(x)**2) == sin(x)**2
    assert trigsimp(sin(x)**2 + cos(x)**2) == 1
    assert trigsimp(1 + tan(x)**2) == 1/cos(x)**2
    assert trigsimp(1/cos(x)**2 - 1) == tan(x)**2
    assert trigsimp(1/cos(x)**2 - tan(x)**2) == 1
    assert trigsimp(1 + cot(x)**2) == 1/sin(x)**2
    assert trigsimp(1/sin(x)**2 - 1) == 1/tan(x)**2
    assert trigsimp(1/sin(x)**2 - cot(x)**2) == 1

    assert trigsimp(5*cos(x)**2 + 5*sin(x)**2) == 5
    assert trigsimp(5*cos(x/2)**2 + 2*sin(x/2)**2) == 3*cos(x)/2 + Rational(7, 2)

    assert trigsimp(sin(x)/cos(x)) == tan(x)
    assert trigsimp(2*tan(x)*cos(x)) == 2*sin(x)
    assert trigsimp(cot(x)**3*sin(x)**3) == cos(x)**3
    assert trigsimp(y*tan(x)**2/sin(x)**2) == y/cos(x)**2
    assert trigsimp(cot(x)/cos(x)) == 1/sin(x)

    assert trigsimp(sin(x + y) + sin(x - y)) == 2*sin(x)*cos(y)
    assert trigsimp(sin(x + y) - sin(x - y)) == 2*sin(y)*cos(x)
    assert trigsimp(cos(x + y) + cos(x - y)) == 2*cos(x)*cos(y)
    assert trigsimp(cos(x + y) - cos(x - y)) == -2*sin(x)*sin(y)
    assert trigsimp(tan(x + y) - tan(x)/(1 - tan(x)*tan(y))) == \
        sin(y)/(-sin(y)*tan(x) + cos(y))  # -tan(y)/(tan(x)*tan(y) - 1)

    assert trigsimp(sinh(x + y) + sinh(x - y)) == 2*sinh(x)*cosh(y)
    assert trigsimp(sinh(x + y) - sinh(x - y)) == 2*sinh(y)*cosh(x)
    assert trigsimp(cosh(x + y) + cosh(x - y)) == 2*cosh(x)*cosh(y)
    assert trigsimp(cosh(x + y) - cosh(x - y)) == 2*sinh(x)*sinh(y)
    assert trigsimp(tanh(x + y) - tanh(x)/(1 + tanh(x)*tanh(y))) == \
        sinh(y)/(sinh(y)*tanh(x) + cosh(y))

    assert trigsimp(cos(0.12345)**2 + sin(0.12345)**2) == 1
    e = 2*sin(x)**2 + 2*cos(x)**2
    assert trigsimp(log(e)) == log(2)
예제 #33
0
def test_hyper_as_trig():
    eq = sinh(x)**2 + cosh(x)**2
    t, f = hyper_as_trig(eq)
    assert f(fu(t)) == cosh(2*x)
    e, f = hyper_as_trig(tanh(x + y))
    assert f(TR12(e)) == (tanh(x) + tanh(y))/(tanh(x)*tanh(y) + 1)

    d = Dummy()
    assert o(sinh(x), d) == I*sin(x*d)
    assert o(tanh(x), d) == I*tan(x*d)
    assert o(coth(x), d) == cot(x*d)/I
    assert o(cosh(x), d) == cos(x*d)
    for func in (sinh, cosh, tanh, coth):
        h = func(pi)
        assert i(o(h, d), d) == h
    # /!\ the _osborne functions are not meant to work
    # in the o(i(trig, d), d) direction so we just check
    # that they work as they are supposed to work
    assert i(cos(x*y), y) == cosh(x)
    assert i(sin(x*y), y) == sinh(x)/I
    assert i(tan(x*y), y) == tanh(x)/I
    assert i(cot(x*y), y) == coth(x)*I
    assert i(sec(x*y), y) == 1/cosh(x)
    assert i(csc(x*y), y) == I/sinh(x)
예제 #34
0
def test_leading_terms():
    for func in [sin, cos, tan, cot, asin, acos, atan, acot]:
        for arg in (1/x, Rational(1, 2)):
            eq = func(arg)
            assert eq.as_leading_term(x) == eq

    # issue sympy/sympy#5272
    assert sin(x).as_leading_term(x) == x
    assert cos(x).as_leading_term(x) == 1
    assert tan(x).as_leading_term(x) == x
    assert cot(x).as_leading_term(x) == 1/x
    assert asin(x).as_leading_term(x) == x
    assert acos(x).as_leading_term(x) == x
    assert atan(x).as_leading_term(x) == x
    assert acot(x).as_leading_term(x) == x
예제 #35
0
def test_diff3():
    p = Integer(5)
    e = a*b + sin(b**p)
    assert e == a*b + sin(b**5)
    assert e.diff(a) == b
    assert e.diff(b) == a + 5*b**4*cos(b**5)
    e = tan(c)
    assert e == tan(c)
    assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2]
    e = c*log(c) - c
    assert e == -c + c*log(c)
    assert e.diff(c) == log(c)
    e = log(sin(c))
    assert e == log(sin(c))
    assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)]
    e = 2**a/log(2)
    assert e.diff(a) == 2**a
예제 #36
0
def test_diff3():
    a, b, c = map(Symbol, 'abc')
    p = Integer(5)
    e = a*b + sin(b**p)
    assert e == a*b + sin(b**5)
    assert e.diff(a) == b
    assert e.diff(b) == a + 5*b**4*cos(b**5)
    e = tan(c)
    assert e == tan(c)
    assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2]
    e = c*log(c) - c
    assert e == -c + c*log(c)
    assert e.diff(c) == log(c)
    e = log(sin(c))
    assert e == log(sin(c))
    assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)]
    e = 2**a/log(2)
    assert e.diff(a) == 2**a
예제 #37
0
def test_diff3():
    a, b, c = map(Symbol, 'abc')
    p = Rational(5)
    e = a * b + sin(b**p)
    assert e == a * b + sin(b**5)
    assert e.diff(a) == b
    assert e.diff(b) == a + 5 * b**4 * cos(b**5)
    e = tan(c)
    assert e == tan(c)
    assert e.diff(c) in [
        cos(c)**(-2), 1 + sin(c)**2 / cos(c)**2, 1 + tan(c)**2
    ]
    e = c * log(c) - c
    assert e == -c + c * log(c)
    assert e.diff(c) == log(c)
    e = log(sin(c))
    assert e == log(sin(c))
    assert e.diff(c) in [sin(c)**(-1) * cos(c), cot(c)]
    e = (Rational(2)**a / log(Rational(2)))
    assert e == 2**a * log(Rational(2))**(-1)
    assert e.diff(a) == 2**a
예제 #38
0
def test_Ynm():
    # https//en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi", extended_real=True)

    assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
    assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                    + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
    assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                   + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))

    assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
                                           + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
    assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) ==
            Ynm(n, m, th, ph).rewrite(cos) ==
            exp(I*m*ph)*sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n)) *
            assoc_legendre(n, m, cos(th))/(2*sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() ==
            (sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*cos(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi)),
             sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*sin(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi))))
예제 #39
0
def test_sympyissue_4547():
    assert limit(cot(x), x, 0, dir='+') == oo
    assert limit(cot(x), x, pi/2, dir='+') == 0
예제 #40
0
def test_sympyissue_4329():
    assert tan(x).series(x, pi/2, n=3).removeO() == \
        -pi/6 + x/3 - 1/(x - pi/2)
    assert cot(x).series(x, pi, n=3).removeO() == \
        -x/3 + pi/3 + 1/(x - pi)
    assert limit(tan(x)**tan(2 * x), x, pi / 4) == exp(-1)
예제 #41
0
def test_sympyissue_4329():
    assert tan(x).series(x, pi/2, n=3).removeO() == \
        -pi/6 + x/3 - 1/(x - pi/2)
    assert cot(x).series(x, pi, n=3).removeO() == \
        -x/3 + pi/3 + 1/(x - pi)
    assert limit(tan(x)**tan(2*x), x, pi/4) == exp(-1)
예제 #42
0
def test_TR15_16_17():
    assert TR15(1 - 1 / sin(x)**2) == -cot(x)**2
    assert TR16(1 - 1 / cos(x)**2) == -tan(x)**2
    assert TR111(1 - 1 / tan(x)**2) == 1 - cot(x)**2
예제 #43
0
def test_TR2():
    assert TR2(tan(x)) == sin(x)/cos(x)
    assert TR2(cot(x)) == cos(x)/sin(x)
    assert TR2(tan(tan(x) - sin(x)/cos(x))) == 0
예제 #44
0
def test_TR15_16_17():
    assert TR15(1 - 1/sin(x)**2) == -cot(x)**2
    assert TR16(1 - 1/cos(x)**2) == -tan(x)**2
    assert TR111(1 - 1/tan(x)**2) == 1 - cot(x)**2
예제 #45
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe / qe)
    Aeee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(13944145, 4720968))

    Heeo = harmonic(ne + pe / qo)
    Aeeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(2422020029, 702257080))

    Heoe = harmonic(ne + po / qe)
    Aeoe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(11818877030, 4286604231) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Heoo = harmonic(ne + po / qo)
    Aeoo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(11669332571, 3628714320))

    Hoee = harmonic(no + pe / qe)
    Aoee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(779405, 277704))

    Hoeo = harmonic(no + pe / qo)
    Aoeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(53857323, 16331560))

    Hooe = harmonic(no + po / qe)
    Aooe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(486853480, 186374097) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Hooo = harmonic(no + po / qo)
    Aooo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(383693479, 125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e / a) == 1
        assert h.evalf() == a.evalf()
예제 #46
0
def test_newissue():
    assert limit(exp(1/sin(x))/exp(cot(x)), x, 0) == 1
예제 #47
0
def test_coth():
    k = Symbol('k', integer=True)

    assert coth(nan) == nan
    assert coth(zoo) == nan

    assert coth(oo) == 1
    assert coth(-oo) == -1

    assert coth(0) == coth(0)
    assert coth(0) == zoo
    assert coth(1) == coth(1)
    assert coth(-1) == -coth(1)

    assert coth(x) == coth(x)
    assert coth(-x) == -coth(x)

    assert coth(pi*I) == -I*cot(pi)
    assert coth(-pi*I) == cot(pi)*I

    assert coth(2**1024 * E) == coth(2**1024 * E)
    assert coth(-2**1024 * E) == -coth(2**1024 * E)

    assert coth(pi*I) == -I*cot(pi)
    assert coth(-pi*I) == I*cot(pi)
    assert coth(2*pi*I) == -I*cot(2*pi)
    assert coth(-2*pi*I) == I*cot(2*pi)
    assert coth(-3*10**73*pi*I) == I*cot(3*10**73*pi)
    assert coth(7*10**103*pi*I) == -I*cot(7*10**103*pi)

    assert coth(pi*I/2) == 0
    assert coth(-pi*I/2) == 0
    assert coth(5*pi*I/2) == 0
    assert coth(7*pi*I/2) == 0

    assert coth(pi*I/3) == -I/sqrt(3)
    assert coth(-2*pi*I/3) == -I/sqrt(3)

    assert coth(pi*I/4) == -I
    assert coth(-pi*I/4) == I
    assert coth(17*pi*I/4) == -I
    assert coth(-3*pi*I/4) == -I

    assert coth(pi*I/6) == -sqrt(3)*I
    assert coth(-pi*I/6) == sqrt(3)*I
    assert coth(7*pi*I/6) == -sqrt(3)*I
    assert coth(-5*pi*I/6) == -sqrt(3)*I

    assert coth(pi*I/105) == -cot(pi/105)*I
    assert coth(-pi*I/105) == cot(pi/105)*I

    assert coth(2 + 3*I) == coth(2 + 3*I)

    assert coth(x*I) == -cot(x)*I

    assert coth(k*pi*I) == -cot(k*pi)*I
    assert coth(17*k*pi*I) == -cot(17*k*pi)*I

    assert coth(k*pi*I) == -cot(k*pi)*I

    pytest.raises(ArgumentIndexError, lambda: coth(x).fdiff(2))

    a, b = symbols('a b', extended_real=True)
    z = a + b*I
    for deep in [True, False]:
        d = sinh(a)**2 + sin(b)**2
        assert coth(z).as_real_imag(deep=deep) == (sinh(a)*cosh(a)/d,
                                                   -sin(b)*cos(b)/d)
        assert coth(a).as_real_imag(deep=deep) == (coth(a), 0)
예제 #48
0
def test_inverse_mellin_transform():
    from diofant import (sin, simplify, Max, Min, expand, powsimp, exp_polar,
                         cos, cot)
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x)
    assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \
        (x**2 + 1)*Heaviside(1 - x)/(4*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x

    # test factorisation of polys
    r = symbols('r', extended_real=True)
    assert IMT(1/(s**2 + 1), s, exp(-x), (None, oo)
              ).subs(x, r).rewrite(sin).simplify() \
        == sin(r)*Heaviside(1 - exp(-r))

    # test multiplicative substitution
    _a, _b = symbols('a b', positive=True)
    assert IMT(_b**(-s / _a) * factorial(s / _a) / s, s, x,
               (0, oo)) == exp(-_b * x**_a)
    assert IMT(factorial(_a / _b + s / _b) / (_a + s), s, x,
               (-_a, oo)) == x**_a * exp(-x**_b)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False),
                                force=True)).replace(exp_polar, exp)

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    nu = symbols('nu', extended_real=True, finite=True)
    assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1)
    assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
        == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s),
                         s, x, (-oo, None))) \
        == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \
        == (1/(x + 1))**rho
    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s)
                        * gamma(-c/2 - s)/gamma(1 - c - s),
                        s, x, (0, -re(c)/2))) == \
        (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s)
                        / gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
        b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) +
        b**2 + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s)
                        / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
        b**c*(sqrt(1 + x/b**2) + 1)**c

    # Section 8.4.5
    assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
        log(x)**3*Heaviside(x - 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi / (s * sin(2 * pi * s)), s, x,
               (-Rational(1, 2), 0)) == log(sqrt(x) + 1)
    assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x)

    # TODO
    def mysimp(expr):
        from diofant import expand, logcombine, powsimp
        return expand(powsimp(logcombine(expr, force=True),
                              force=True,
                              deep=True),
                      force=True).replace(exp_polar, exp)

    assert mysimp(mysimp(IMT(pi / (s * tan(pi * s)), s, x, (-1, 0)))) in [
        log(1 - x) * Heaviside(1 - x) + log(x - 1) * Heaviside(x - 1),
        log(x) * Heaviside(x - 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1)
    ]
    # test passing cot
    assert mysimp(IMT(pi * cot(pi * s) / s, s, x, (0, 1))) in [
        log(1 / x - 1) * Heaviside(1 - x) + log(1 - 1 / x) * Heaviside(x - 1),
        -log(x) * Heaviside(-x + 1) + log(1 - 1 / x) * Heaviside(x - 1) +
        log(-x + 1) * Heaviside(-x + 1),
    ]

    # 8.4.14
    assert IMT(-gamma(s + Rational(1, 2))/(sqrt(pi)*s), s, x, (-Rational(1, 2), 0)) == \
        erf(sqrt(x))

    # 8.4.19
    assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, Rational(3, 4)))) \
        == besselj(a, 2*sqrt(x))
    assert simplify(IMT(2**a*gamma(Rational(1, 2) - 2*s)*gamma(s + (a + 1)/2)
                      / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-(re(a) + 1)/2, Rational(1, 4)))) == \
        sin(sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(Rational(1, 2) - 2*s)
                      / (gamma(Rational(1, 2) - s - a/2)*gamma(1 - 2*s + a)),
                      s, x, (-re(a)/2, Rational(1, 4)))) == \
        cos(sqrt(x))*besselj(a, sqrt(x))
    # TODO this comes out as an amazing mess, but simplifies nicely
    assert simplify(IMT(gamma(a + s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                      s, x, (-re(a), Rational(1, 2)))) == \
        besselj(a, sqrt(x))**2
    assert simplify(IMT(gamma(s)*gamma(Rational(1, 2) - s)
                      / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                      s, x, (0, Rational(1, 2)))) == \
        besselj(-a, sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
                      / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
                         * gamma(a/2 + b/2 - s + 1)),
                      s, x, (-(re(a) + re(b))/2, Rational(1, 2)))) == \
        besselj(a, sqrt(x))*besselj(b, sqrt(x))

    # Section 8.4.20
    # TODO this can be further simplified!
    assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) *
                    gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) /
                    (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
                    s, x,
                    (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), Rational(1, 2)))) == \
                    besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) -
                    besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b)
    # TODO more

    # for coverage

    assert IMT(pi / cos(pi * s), s, x,
               (0, Rational(1, 2))) == sqrt(x) / (x + 1)
예제 #49
0
def test_inverse_mellin_transform():
    IMT = inverse_mellin_transform

    assert IMT(gamma(s), s, x, (0, oo)) == exp(-x)
    assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1/x)
    assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \
        (x**2 + 1)*Heaviside(1 - x)/(4*x)

    # test passing "None"
    assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \
        -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x)
    assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \
        (-x/2 + 1/(2*x))*Heaviside(-x + 1)

    # test expansion of sums
    assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1)*exp(-x)/x

    # test factorisation of polys
    r = symbols('r', extended_real=True)
    assert (IMT(1/(s**2 + 1), s, exp(-x),
            (None, oo)).subs({x: r}).rewrite(sin).simplify() ==
            sin(r)*Heaviside(1 - exp(-r)))

    # test multiplicative substitution
    _a, _b = symbols('a b', positive=True)
    assert IMT(_b**(-s/_a)*factorial(s/_a)/s, s, x, (0, oo)) == exp(-_b*x**_a)
    assert IMT(factorial(_a/_b + s/_b)/(_a + s), s, x, (-_a, oo)) == x**_a*exp(-x**_b)

    def simp_pows(expr):
        return simplify(powsimp(expand_mul(expr, deep=False), force=True)).replace(exp_polar, exp)

    # Now test the inverses of all direct transforms tested above

    # Section 8.4.2
    nu = symbols('nu', real=True)
    assert IMT(-1/(nu + s), s, x, (-oo, None)) == x**nu*Heaviside(x - 1)
    assert IMT(1/(nu + s), s, x, (None, oo)) == x**nu*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \
        == (1 - x)**(beta - 1)*Heaviside(1 - x)
    assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s),
                         s, x, (-oo, None))) \
        == (x - 1)**(beta - 1)*Heaviside(x - 1)
    assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \
        == (1/(x + 1))**rho
    assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c)
                         * gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi,
                         s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \
        == (x**c - d**c)/(x - d)

    assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s)
                        * gamma(-c/2 - s)/gamma(1 - c - s),
                        s, x, (0, -re(c)/2))) == \
        (1 + sqrt(x + 1))**c
    assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s)
                        / gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \
        b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) +
                                                    b**2 + x)/(b**2 + x)
    assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s)
                        / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \
        b**c*(sqrt(1 + x/b**2) + 1)**c

    # Section 8.4.5
    assert IMT(24/s**5, s, x, (0, oo)) == log(x)**4*Heaviside(1 - x)
    assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \
        log(x)**3*Heaviside(x - 1)
    assert IMT(pi/(s*sin(pi*s)), s, x, (-1, 0)) == log(x + 1)
    assert IMT(pi/(s*sin(pi*s/2)), s, x, (-2, 0)) == log(x**2 + 1)
    assert IMT(pi/(s*sin(2*pi*s)), s, x, (-Rational(1, 2), 0)) == log(sqrt(x) + 1)
    assert IMT(pi/(s*sin(pi*s)), s, x, (0, 1)) == log(1 + 1/x)

    # TODO
    def mysimp(expr):
        return expand(
            powsimp(logcombine(expr, force=True), force=True, deep=True),
            force=True).replace(exp_polar, exp)

    assert mysimp(mysimp(IMT(pi/(s*tan(pi*s)), s, x, (-1, 0)))) in [
        log(1 - x)*Heaviside(1 - x) + log(x - 1)*Heaviside(x - 1),
        log(x)*Heaviside(x - 1) + log(1 - 1/x)*Heaviside(x - 1) + log(-x +
                                                                      1)*Heaviside(-x + 1)]
    # test passing cot
    assert mysimp(IMT(pi*cot(pi*s)/s, s, x, (0, 1))) in [
        log(1/x - 1)*Heaviside(1 - x) + log(1 - 1/x)*Heaviside(x - 1),
        -log(x)*Heaviside(-x + 1) + log(1 - 1/x)*Heaviside(x - 1) + log(-x +
                                                                        1)*Heaviside(-x + 1), ]

    # 8.4.14
    assert IMT(-gamma(s + Rational(1, 2))/(sqrt(pi)*s), s, x, (-Rational(1, 2), 0)) == \
        erf(sqrt(x))

    # 8.4.19
    assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, Rational(3, 4)))) \
        == besselj(a, 2*sqrt(x))
    assert simplify(IMT(2**a*gamma(Rational(1, 2) - 2*s)*gamma(s + (a + 1)/2)
                        / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)),
                        s, x, (-(re(a) + 1)/2, Rational(1, 4)))) == \
        sin(sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(Rational(1, 2) - 2*s)
                        / (gamma(Rational(1, 2) - s - a/2)*gamma(1 - 2*s + a)),
                        s, x, (-re(a)/2, Rational(1, 4)))) == \
        cos(sqrt(x))*besselj(a, sqrt(x))
    # TODO this comes out as an amazing mess, but simplifies nicely
    assert simplify(IMT(gamma(a + s)*gamma(Rational(1, 2) - s)
                        / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)),
                        s, x, (-re(a), Rational(1, 2)))) == \
        besselj(a, sqrt(x))**2
    assert simplify(IMT(gamma(s)*gamma(Rational(1, 2) - s)
                        / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)),
                        s, x, (0, Rational(1, 2)))) == \
        besselj(-a, sqrt(x))*besselj(a, sqrt(x))
    assert simplify(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s)
                        / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1)
                           * gamma(a/2 + b/2 - s + 1)),
                        s, x, (-(re(a) + re(b))/2, Rational(1, 2)))) == \
        besselj(a, sqrt(x))*besselj(b, sqrt(x))

    # Section 8.4.20
    # TODO this can be further simplified!
    assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) *
                        gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) /
                        (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)),
                        s, x,
                        (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), Rational(1, 2)))) == \
        besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) -
                              besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b)
    # TODO more

    # for coverage

    assert IMT(pi/cos(pi*s), s, x, (0, Rational(1, 2))) == sqrt(x)/(x + 1)