Example #1
0
def test_manualintegrate_trigonometry():
    assert manualintegrate(sin(x), x) == -cos(x)
    assert manualintegrate(tan(x), x) == -log(cos(x))

    assert manualintegrate(sec(x), x) == log(sec(x) + tan(x))
    assert manualintegrate(csc(x), x) == -log(csc(x) + cot(x))

    assert manualintegrate(sin(x) * cos(x),
                           x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
    assert manualintegrate(-sec(x) * tan(x), x) == -sec(x)
    assert manualintegrate(csc(x) * cot(x), x) == -csc(x)
    assert manualintegrate(sec(x)**2, x) == tan(x)
    assert manualintegrate(csc(x)**2, x) == -cot(x)

    assert manualintegrate(x * sec(x**2), x) == log(tan(x**2) + sec(x**2)) / 2
    assert manualintegrate(cos(x) * csc(sin(x)),
                           x) == -log(cot(sin(x)) + csc(sin(x)))
    assert manualintegrate(cos(3 * x) * sec(x), x) == -x + sin(2 * x)
    assert manualintegrate(sin(3*x)*sec(x), x) == \
        -3*log(cos(x)) + 2*log(cos(x)**2) - 2*cos(x)**2

    assert_is_integral_of(sinh(2 * x), cosh(2 * x) / 2)
    assert_is_integral_of(x * cosh(x**2), sinh(x**2) / 2)
    assert_is_integral_of(tanh(x), log(cosh(x)))
    assert_is_integral_of(coth(x), log(sinh(x)))
    f, F = sech(x), 2 * atan(tanh(x / 2))
    assert manualintegrate(f, x) == F
    assert (F.diff(x) -
            f).rewrite(exp).simplify() == 0  # todo: equals returns None
    f, F = csch(x), log(tanh(x / 2))
    assert manualintegrate(f, x) == F
    assert (F.diff(x) - f).rewrite(exp).simplify() == 0
Example #2
0
def lstm_recursive(x, *limits):
    (W, ), (Wh, ), (b, ), (t, ) = limits
    hc = lstm[W, Wh, b, t - 1](x)

    xt = x[t]
    h = Indexed(hc, 0)
    c = Indexed(hc, 1)
    d = h.shape[-1]

    Wi = W[:, :d]
    Wf = W[:, d:2 * d]
    Wc = W[:, 2 * d:3 * d]
    Wo = W[:, -d:]

    Whi = Wh[:, :d]
    Whf = Wh[:, d:2 * d]
    Whc = Wh[:, 2 * d:3 * d]
    Who = Wh[:, -d:]

    bi = b[:d]
    bf = b[d:2 * d]
    bc = b[2 * d:3 * d]
    bo = b[-d:]

    i = sigmoid(xt @ Wi + h @ Whi + bi)
    f = sigmoid(xt @ Wf + h @ Whf + bf)
    c = f * c + i * tanh(xt @ Wc + h @ Whc + bc)
    o = sigmoid(xt @ Wo + h @ Who + bo)

    return Piecewise((BlockMatrix(o * tanh(c), c), t > 0),
                     (ZeroMatrix(*hc.shape), True))
Example #3
0
def test_simplifications():
    x = Symbol('x')
    assert sinh(asinh(x)) == x
    assert sinh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1)
    assert sinh(atanh(x)) == x / sqrt(1 - x**2)
    assert sinh(acoth(x)) == 1 / (sqrt(x - 1) * sqrt(x + 1))

    assert cosh(asinh(x)) == sqrt(1 + x**2)
    assert cosh(acosh(x)) == x
    assert cosh(atanh(x)) == 1 / sqrt(1 - x**2)
    assert cosh(acoth(x)) == x / (sqrt(x - 1) * sqrt(x + 1))

    assert tanh(asinh(x)) == x / sqrt(1 + x**2)
    assert tanh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1) / x
    assert tanh(atanh(x)) == x
    assert tanh(acoth(x)) == 1 / x

    assert coth(asinh(x)) == sqrt(1 + x**2) / x
    assert coth(acosh(x)) == x / (sqrt(x - 1) * sqrt(x + 1))
    assert coth(atanh(x)) == 1 / x
    assert coth(acoth(x)) == x

    assert csch(asinh(x)) == 1 / x
    assert csch(acosh(x)) == 1 / (sqrt(x - 1) * sqrt(x + 1))
    assert csch(atanh(x)) == sqrt(1 - x**2) / x
    assert csch(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)

    assert sech(asinh(x)) == 1 / sqrt(1 + x**2)
    assert sech(acosh(x)) == 1 / x
    assert sech(atanh(x)) == sqrt(1 - x**2)
    assert sech(acoth(x)) == sqrt(x - 1) * sqrt(x + 1) / x
Example #4
0
def test_tanh_rewrite():
    x = Symbol('x')
    assert tanh(x).rewrite(exp) == (exp(x) - exp(-x))/(exp(x) + exp(-x)) \
        == tanh(x).rewrite('tractable')
    assert tanh(x).rewrite(sinh) == I * sinh(x) / sinh(I * pi / 2 - x)
    assert tanh(x).rewrite(cosh) == I * cosh(I * pi / 2 - x) / cosh(x)
    assert tanh(x).rewrite(coth) == 1 / coth(x)
Example #5
0
def test_gruntz_hyperbolic():
    assert gruntz(cosh(x), x, oo) == oo
    assert gruntz(cosh(x), x, -oo) == oo
    assert gruntz(sinh(x), x, oo) == oo
    assert gruntz(sinh(x), x, -oo) == -oo
    assert gruntz(2*cosh(x)*exp(x), x, oo) == oo
    assert gruntz(2*cosh(x)*exp(x), x, -oo) == 1
    assert gruntz(2*sinh(x)*exp(x), x, oo) == oo
    assert gruntz(2*sinh(x)*exp(x), x, -oo) == -1
    assert gruntz(tanh(x), x, oo) == 1
    assert gruntz(tanh(x), x, -oo) == -1
    assert gruntz(coth(x), x, oo) == 1
    assert gruntz(coth(x), x, -oo) == -1
def test_gruntz_hyperbolic():
    assert gruntz(cosh(x), x, oo) is oo
    assert gruntz(cosh(x), x, -oo) is oo
    assert gruntz(sinh(x), x, oo) is oo
    assert gruntz(sinh(x), x, -oo) is -oo
    assert gruntz(2 * cosh(x) * exp(x), x, oo) is oo
    assert gruntz(2 * cosh(x) * exp(x), x, -oo) == 1
    assert gruntz(2 * sinh(x) * exp(x), x, oo) is oo
    assert gruntz(2 * sinh(x) * exp(x), x, -oo) == -1
    assert gruntz(tanh(x), x, oo) == 1
    assert gruntz(tanh(x), x, -oo) == -1
    assert gruntz(coth(x), x, oo) == 1
    assert gruntz(coth(x), x, -oo) == -1
Example #7
0
def test_derivs():
    x = Symbol('x')
    assert coth(x).diff(x) == -sinh(x)**(-2)
    assert sinh(x).diff(x) == cosh(x)
    assert cosh(x).diff(x) == sinh(x)
    assert tanh(x).diff(x) == -tanh(x)**2 + 1
    assert csch(x).diff(x) == -coth(x) * csch(x)
    assert sech(x).diff(x) == -tanh(x) * sech(x)
    assert acoth(x).diff(x) == 1 / (-x**2 + 1)
    assert asinh(x).diff(x) == 1 / sqrt(x**2 + 1)
    assert acosh(x).diff(x) == 1 / sqrt(x**2 - 1)
    assert atanh(x).diff(x) == 1 / (-x**2 + 1)
    assert asech(x).diff(x) == -1 / (x * sqrt(1 - x**2))
    assert acsch(x).diff(x) == -1 / (x**2 * sqrt(1 + x**(-2)))
Example #8
0
def test_sign_assumptions():
    p = Symbol('p', positive=True)
    n = Symbol('n', negative=True)
    assert sinh(n).is_negative is True
    assert sinh(p).is_positive is True
    assert cosh(n).is_positive is True
    assert cosh(p).is_positive is True
    assert tanh(n).is_negative is True
    assert tanh(p).is_positive is True
    assert csch(n).is_negative is True
    assert csch(p).is_positive is True
    assert sech(n).is_positive is True
    assert sech(p).is_positive is True
    assert coth(n).is_negative is True
    assert coth(p).is_positive is True
Example #9
0
def test_heurisch_trigonometric():
    assert heurisch(sin(x), x) == -cos(x)
    assert heurisch(pi * sin(x) + 1, x) == x - pi * cos(x)

    assert heurisch(cos(x), x) == sin(x)
    assert heurisch(tan(x), x) in [
        log(1 + tan(x)**2) / 2,
        log(tan(x) + I) + I * x,
        log(tan(x) - I) - I * x,
    ]

    assert heurisch(sin(x) * sin(y), x) == -cos(x) * sin(y)
    assert heurisch(sin(x) * sin(y), y) == -cos(y) * sin(x)

    # gives sin(x) in answer when run via setup.py and cos(x) when run via py.test
    assert heurisch(sin(x) * cos(x), x) in [sin(x)**2 / 2, -cos(x)**2 / 2]
    assert heurisch(cos(x) / sin(x), x) == log(sin(x))

    assert heurisch(x * sin(7 * x), x) == sin(7 * x) / 49 - x * cos(7 * x) / 7
    assert heurisch(
        1 / pi / 4 * x**2 * cos(x),
        x) == 1 / pi / 4 * (x**2 * sin(x) - 2 * sin(x) + 2 * x * cos(x))

    assert heurisch(acos(x/4) * asin(x/4), x) == 2*x - (sqrt(16 - x**2))*asin(x/4) \
        + (sqrt(16 - x**2))*acos(x/4) + x*asin(x/4)*acos(x/4)

    assert heurisch(sin(x) / (cos(x)**2 + 1),
                    x) == -atan(cos(x))  #fixes issue 13723
    assert heurisch(1 / (cos(x) + 2),
                    x) == 2 * sqrt(3) * atan(sqrt(3) * tan(x / 2) / 3) / 3
    assert heurisch(
        2 * sin(x) * cos(x) / (sin(x)**4 + 1),
        x) == atan(sqrt(2) * sin(x) - 1) - atan(sqrt(2) * sin(x) + 1)

    assert heurisch(1 / cosh(x), x) == 2 * atan(tanh(x / 2))
Example #10
0
def test_function_series3():
    """
    Test our easy "tanh" function.

    This test tests two things:
      * that the Function interface works as expected and it's easy to use
      * that the general algorithm for the series expansion works even when the
        derivative is defined recursively in terms of the original function,
        since tanh(x).diff(x) == 1-tanh(x)**2
    """

    class mytanh(Function):

        def fdiff(self, argindex=1):
            return 1 - mytanh(self.args[0])**2

        @classmethod
        def eval(cls, arg):
            arg = sympify(arg)
            if arg == 0:
                return sympify(0)

    e = tanh(x)
    f = mytanh(x)
    assert e.series(x, 0, 6) == f.series(x, 0, 6)
Example #11
0
def gru_recursive(x, *limits):
    (Wx, ), (Wh, ), (b, ), (t, ) = limits
    h = gru[Wx, Wh, b, t - 1](x)

    d = h.shape[-1]

    xt = x[t]
    Wxz = Wx[:, :d]
    Wxr = Wx[:, d:2 * d]
    Wxh = Wx[:, -d:]

    Whz = Wh[:, :d]
    Whr = Wh[:, d:2 * d]
    Whh = Wh[:, -d:]

    bz = b[:d]
    br = b[d:2 * d]
    bh = b[-d:]

    z = sigmoid(xt @ Wxz + h @ Whz + bz)
    r = sigmoid(xt @ Wxr + h @ Whr + br)
    gh = tanh(xt @ Wxh + (r * h) @ Whh + bh)

    return Piecewise(((1 - z) * gh + z * h, t > 0),
                     (ZeroMatrix(*h.shape), True))
Example #12
0
def test_unpolarify():
    from sympy.functions.elementary.complexes import (polar_lift,
                                                      principal_branch,
                                                      unpolarify)
    from sympy.core.relational import Ne
    from sympy.functions.elementary.hyperbolic import tanh
    from sympy.functions.special.error_functions import erf
    from sympy.functions.special.gamma_functions import (gamma, uppergamma)
    from sympy.abc import x
    p = exp_polar(7 * I) + 1
    u = exp(7 * I) + 1

    assert unpolarify(1) == 1
    assert unpolarify(p) == u
    assert unpolarify(p**2) == u**2
    assert unpolarify(p**x) == p**x
    assert unpolarify(p * x) == u * x
    assert unpolarify(p + x) == u + x
    assert unpolarify(sqrt(sin(p))) == sqrt(sin(u))

    # Test reduction to principal branch 2*pi.
    t = principal_branch(x, 2 * pi)
    assert unpolarify(t) == x
    assert unpolarify(sqrt(t)) == sqrt(t)

    # Test exponents_only.
    assert unpolarify(p**p, exponents_only=True) == p**u
    assert unpolarify(uppergamma(x, p**p)) == uppergamma(x, p**u)

    # Test functions.
    assert unpolarify(sin(p)) == sin(u)
    assert unpolarify(tanh(p)) == tanh(u)
    assert unpolarify(gamma(p)) == gamma(u)
    assert unpolarify(erf(p)) == erf(u)
    assert unpolarify(uppergamma(x, p)) == uppergamma(x, p)

    assert unpolarify(uppergamma(sin(p), sin(p + exp_polar(0)))) == \
        uppergamma(sin(u), sin(u + 1))
    assert unpolarify(uppergamma(polar_lift(0), 2*exp_polar(0))) == \
        uppergamma(0, 2)

    assert unpolarify(Eq(p, 0)) == Eq(u, 0)
    assert unpolarify(Ne(p, 0)) == Ne(u, 0)
    assert unpolarify(polar_lift(x) > 0) == (x > 0)

    # Test bools
    assert unpolarify(True) is True
Example #13
0
def test_real_assumptions():
    z = Symbol('z', real=False)
    assert sinh(z).is_real is None
    assert cosh(z).is_real is None
    assert tanh(z).is_real is None
    assert sech(z).is_real is None
    assert csch(z).is_real is None
    assert coth(z).is_real is None
Example #14
0
def test_issue_6999():
    s = tanh(x).lseries(x, 1)
    assert next(s) == tanh(1)
    assert next(s) == x - (x - 1) * tanh(1)**2 - 1
    assert next(s) == -(x - 1)**2 * tanh(1) + (x - 1)**2 * tanh(1)**3
    assert next(s) == -(x - 1)**3*tanh(1)**4 - (x - 1)**3/3 + \
        4*(x - 1)**3*tanh(1)**2/3
Example #15
0
def test_hyper_as_trig():
    from sympy.simplify.fu import _osborne, _osbornei

    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 _osborne(sinh(x), d) == I * sin(x * d)
    assert _osborne(tanh(x), d) == I * tan(x * d)
    assert _osborne(coth(x), d) == cot(x * d) / I
    assert _osborne(cosh(x), d) == cos(x * d)
    assert _osborne(sech(x), d) == sec(x * d)
    assert _osborne(csch(x), d) == csc(x * d) / I
    for func in (sinh, cosh, tanh, coth, sech, csch):
        h = func(pi)
        assert _osbornei(_osborne(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 _osbornei(cos(x * y + z), y) == cosh(x + z * I)
    assert _osbornei(sin(x * y + z), y) == sinh(x + z * I) / I
    assert _osbornei(tan(x * y + z), y) == tanh(x + z * I) / I
    assert _osbornei(cot(x * y + z), y) == coth(x + z * I) * I
    assert _osbornei(sec(x * y + z), y) == sech(x + z * I)
    assert _osbornei(csc(x * y + z), y) == csch(x + z * I) * I
Example #16
0
def test_sech_rewrite():
    x = Symbol('x')
    assert sech(x).rewrite(exp) == 1 / (exp(x)/2 + exp(-x)/2) \
        == sech(x).rewrite('tractable')
    assert sech(x).rewrite(sinh) == I / sinh(x + I * pi / 2)
    tanh_half = tanh(S.Half * x)**2
    assert sech(x).rewrite(tanh) == (1 - tanh_half) / (1 + tanh_half)
    coth_half = coth(S.Half * x)**2
    assert sech(x).rewrite(coth) == (coth_half - 1) / (coth_half + 1)
Example #17
0
def test_csch_rewrite():
    x = Symbol('x')
    assert csch(x).rewrite(exp) == 1 / (exp(x)/2 - exp(-x)/2) \
        == csch(x).rewrite('tractable')
    assert csch(x).rewrite(cosh) == I / cosh(x + I * pi / 2)
    tanh_half = tanh(S.Half * x)
    assert csch(x).rewrite(tanh) == (1 - tanh_half**2) / (2 * tanh_half)
    coth_half = coth(S.Half * x)
    assert csch(x).rewrite(coth) == (coth_half**2 - 1) / (2 * coth_half)
Example #18
0
def test_cosh_rewrite():
    x = Symbol('x')
    assert cosh(x).rewrite(exp) == (exp(x) + exp(-x))/2 \
        == cosh(x).rewrite('tractable')
    assert cosh(x).rewrite(sinh) == -I * sinh(x + I * pi / 2)
    tanh_half = tanh(S.Half * x)**2
    assert cosh(x).rewrite(tanh) == (1 + tanh_half) / (1 - tanh_half)
    coth_half = coth(S.Half * x)**2
    assert cosh(x).rewrite(coth) == (coth_half + 1) / (coth_half - 1)
Example #19
0
def test_sinh_rewrite():
    x = Symbol('x')
    assert sinh(x).rewrite(exp) == (exp(x) - exp(-x))/2 \
        == sinh(x).rewrite('tractable')
    assert sinh(x).rewrite(cosh) == -I * cosh(x + I * pi / 2)
    tanh_half = tanh(S.Half * x)
    assert sinh(x).rewrite(tanh) == 2 * tanh_half / (1 - tanh_half**2)
    coth_half = coth(S.Half * x)
    assert sinh(x).rewrite(coth) == 2 * coth_half / (coth_half**2 - 1)
Example #20
0
def test_issue_21177():
    r = -sqrt(3) * tanh(sqrt(3) * pi / 2) / 3
    a = residue(
        cot(pi * x) / ((x - 1) * (x - 2) + 1), x,
        S(3) / 2 - sqrt(3) * I / 2)
    b = residue(
        cot(pi * x) / (x**2 - 3 * x + 3), x,
        S(3) / 2 - sqrt(3) * I / 2)
    assert a == r
    assert (b - a).cancel() == 0
Example #21
0
def test_exptrigsimp():
    def valid(a, b):
        from sympy.core.random import verify_numerically as tn
        if not (tn(a, b) and a == b):
            return False
        return True

    assert exptrigsimp(exp(x) + exp(-x)) == 2 * cosh(x)
    assert exptrigsimp(exp(x) - exp(-x)) == 2 * sinh(x)
    assert exptrigsimp(
        (2 * exp(x) - 2 * exp(-x)) / (exp(x) + exp(-x))) == 2 * tanh(x)
    assert exptrigsimp((2 * exp(2 * x) - 2) / (exp(2 * x) + 1)) == 2 * tanh(x)
    e = [
        cos(x) + I * sin(x),
        cos(x) - I * sin(x),
        cosh(x) - sinh(x),
        cosh(x) + sinh(x)
    ]
    ok = [exp(I * x), exp(-I * x), exp(-x), exp(x)]
    assert all(valid(i, j) for i, j in zip([exptrigsimp(ei) for ei in e], ok))

    ue = [
        cos(x) + sin(x),
        cos(x) - sin(x),
        cosh(x) + I * sinh(x),
        cosh(x) - I * sinh(x)
    ]
    assert [exptrigsimp(ei) == ei for ei in ue]

    res = []
    ok = [
        y * tanh(1), 1 / (y * tanh(1)), I * y * tan(1), -I / (y * tan(1)),
        y * tanh(x), 1 / (y * tanh(x)), I * y * tan(x), -I / (y * tan(x)),
        y * tanh(1 + I), 1 / (y * tanh(1 + I))
    ]
    for a in (1, I, x, I * x, 1 + I):
        w = exp(a)
        eq = y * (w - 1 / w) / (w + 1 / w)
        res.append(simplify(eq))
        res.append(simplify(1 / eq))
    assert all(valid(i, j) for i, j in zip(res, ok))

    for a in range(1, 3):
        w = exp(a)
        e = w + 1 / w
        s = simplify(e)
        assert s == exptrigsimp(e)
        assert valid(s, 2 * cosh(a))
        e = w - 1 / w
        s = simplify(e)
        assert s == exptrigsimp(e)
        assert valid(s, 2 * sinh(a))
Example #22
0
def test_inverses():
    x = Symbol('x')
    assert sinh(x).inverse() == asinh
    raises(AttributeError, lambda: cosh(x).inverse())
    assert tanh(x).inverse() == atanh
    assert coth(x).inverse() == acoth
    assert asinh(x).inverse() == sinh
    assert acosh(x).inverse() == cosh
    assert atanh(x).inverse() == tanh
    assert acoth(x).inverse() == coth
    assert asech(x).inverse() == sech
    assert acsch(x).inverse() == csch
Example #23
0
def test_hyperbolic():
    assert sinh(x).nseries(x, n=6) == x + x**3 / 6 + x**5 / 120 + O(x**6)
    assert cosh(x).nseries(x, n=5) == 1 + x**2 / 2 + x**4 / 24 + O(x**5)
    assert tanh(x).nseries(x, n=6) == x - x**3 / 3 + 2 * x**5 / 15 + O(x**6)
    assert coth(x).nseries(x, n=6) == \
        1/x - x**3/45 + x/3 + 2*x**5/945 + O(x**6)
    assert asinh(x).nseries(x, n=6) == x - x**3 / 6 + 3 * x**5 / 40 + O(x**6)
    assert acosh(x).nseries(x, n=6) == \
        pi*I/2 - I*x - 3*I*x**5/40 - I*x**3/6 + O(x**6)
    assert atanh(x).nseries(x, n=6) == x + x**3 / 3 + x**5 / 5 + O(x**6)
    assert acoth(x).nseries(
        x, n=6) == x + x**3 / 3 + x**5 / 5 + pi * I / 2 + O(x**6)
Example #24
0
def test_trigsimp1():
    x, y = symbols('x,y')

    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)
Example #25
0
def test_trigsimp_groebner():
    from sympy.simplify.trigsimp import trigsimp_groebner

    c = cos(x)
    s = sin(x)
    ex = (4 * s * c + 12 * s + 5 * c**3 + 21 * c**2 + 23 * c + 15) / (
        -s * c**2 + 2 * s * c + 15 * s + 7 * c**3 + 31 * c**2 + 37 * c + 21)
    resnum = (5 * s - 5 * c + 1)
    resdenom = (8 * s - 6 * c)
    results = [resnum / resdenom, (-resnum) / (-resdenom)]
    assert trigsimp_groebner(ex) in results
    assert trigsimp_groebner(s / c, hints=[tan]) == tan(x)
    assert trigsimp_groebner(c * s) == c * s
    assert trigsimp((-s + 1) / c + c / (-s + 1), method='groebner') == 2 / c
    assert trigsimp((-s + 1) / c + c / (-s + 1),
                    method='groebner',
                    polynomial=True) == 2 / c

    # Test quick=False works
    assert trigsimp_groebner(ex, hints=[2]) in results
    assert trigsimp_groebner(ex, hints=[int(2)]) in results

    # test "I"
    assert trigsimp_groebner(sin(I * x) / cos(I * x),
                             hints=[tanh]) == I * tanh(x)

    # test hyperbolic / sums
    assert trigsimp_groebner((tanh(x) + tanh(y)) / (1 + tanh(x) * tanh(y)),
                             hints=[(tanh, x, y)]) == tanh(x + y)
Example #26
0
def test_evalc():
    x = Symbol("x", real=True)
    y = Symbol("y", real=True)
    z = Symbol("z")
    assert ((x + I * y)**2).expand(complex=True) == x**2 + 2 * I * x * y - y**2
    assert expand_complex(z**(2 * I)) == (re(
        (re(z) + I * im(z))**(2 * I)) + I * im((re(z) + I * im(z))**(2 * I)))
    assert expand_complex(z**(2 * I),
                          deep=False) == I * im(z**(2 * I)) + re(z**(2 * I))

    assert exp(I * x) != cos(x) + I * sin(x)
    assert exp(I * x).expand(complex=True) == cos(x) + I * sin(x)
    assert exp(I * x +
               y).expand(complex=True) == exp(y) * cos(x) + I * sin(x) * exp(y)

    assert sin(I * x).expand(complex=True) == I * sinh(x)
    assert sin(x + I*y).expand(complex=True) == sin(x)*cosh(y) + \
        I * sinh(y) * cos(x)

    assert cos(I * x).expand(complex=True) == cosh(x)
    assert cos(x + I*y).expand(complex=True) == cos(x)*cosh(y) - \
        I * sinh(y) * sin(x)

    assert tan(I * x).expand(complex=True) == tanh(x) * I
    assert tan(x + I * y).expand(
        complex=True) == (sin(2 * x) / (cos(2 * x) + cosh(2 * y)) +
                          I * sinh(2 * y) / (cos(2 * x) + cosh(2 * y)))

    assert sinh(I * x).expand(complex=True) == I * sin(x)
    assert sinh(x + I*y).expand(complex=True) == sinh(x)*cos(y) + \
        I * sin(y) * cosh(x)

    assert cosh(I * x).expand(complex=True) == cos(x)
    assert cosh(x + I*y).expand(complex=True) == cosh(x)*cos(y) + \
        I * sin(y) * sinh(x)

    assert tanh(I * x).expand(complex=True) == tan(x) * I
    assert tanh(x + I * y).expand(
        complex=True) == ((sinh(x) * cosh(x) + I * cos(y) * sin(y)) /
                          (sinh(x)**2 + cos(y)**2)).expand()
Example #27
0
def test_conjugate():
    a = Symbol("a", real=True)
    b = Symbol("b", 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)
Example #28
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)
Example #29
0
def test_exp_rewrite():
    assert exp(x).rewrite(sin) == sinh(x) + cosh(x)
    assert exp(x * I).rewrite(cos) == cos(x) + I * sin(x)
    assert exp(1).rewrite(cos) == sinh(1) + cosh(1)
    assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
    assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
    assert exp(x).rewrite(tanh) == (1 + tanh(x / 2)) / (1 - tanh(x / 2))
    assert exp(pi * I / 4).rewrite(sqrt) == sqrt(2) / 2 + sqrt(2) * I / 2
    assert exp(pi * I / 3).rewrite(sqrt) == S.Half + sqrt(3) * I / 2
    if not global_parameters.exp_is_pow:
        assert exp(x * log(y)).rewrite(Pow) == y**x
        assert exp(log(x) * log(y)).rewrite(Pow) in [x**log(y), y**log(x)]
        assert exp(log(log(x)) * y).rewrite(Pow) == log(x)**y

    n = Symbol('n', integer=True)

    assert Sum((exp(pi * I / 2) / 2)**n,
               (n, 0, oo)).rewrite(sqrt).doit() == Rational(4, 5) + I * 2 / 5
    assert Sum(
        (exp(pi * I / 4) / 2)**n,
        (n, 0, oo)).rewrite(sqrt).doit() == 1 / (1 - sqrt(2) * (1 + I) / 4)
    assert (Sum(
        (exp(pi * I / 3) / 2)**n,
        (n, 0, oo)).rewrite(sqrt).doit().cancel() == 4 * I / (sqrt(3) + 3 * I))
 def f(rv):
     if not isinstance(rv, TrigonometricFunction):
         return rv
     const, x = rv.args[0].as_independent(d, as_Add=True)
     a = x.xreplace({d: S.One}) + const * I
     if isinstance(rv, sin):
         return sinh(a) / I
     elif isinstance(rv, cos):
         return cosh(a)
     elif isinstance(rv, tan):
         return tanh(a) / I
     elif isinstance(rv, cot):
         return coth(a) * I
     elif isinstance(rv, sec):
         return sech(a)
     elif isinstance(rv, csc):
         return csch(a) * I
     else:
         raise NotImplementedError('unhandled %s' % rv.func)
Example #31
0
def test_complex():
    a, b = symbols('a,b', real=True)
    z = a + b * I
    for func in [sinh, cosh, tanh, coth, sech, csch]:
        assert func(z).conjugate() == func(a - b * I)
    for deep in [True, False]:
        assert sinh(z).expand(
            complex=True, deep=deep) == sinh(a) * cos(b) + I * cosh(a) * sin(b)
        assert cosh(z).expand(
            complex=True, deep=deep) == cosh(a) * cos(b) + I * sinh(a) * sin(b)
        assert tanh(z).expand(
            complex=True, deep=deep) == sinh(a) * cosh(a) / (cos(b)**2 + sinh(
                a)**2) + I * sin(b) * cos(b) / (cos(b)**2 + sinh(a)**2)
        assert coth(z).expand(
            complex=True, deep=deep) == sinh(a) * cosh(a) / (sin(b)**2 + sinh(
                a)**2) - I * sin(b) * cos(b) / (sin(b)**2 + sinh(a)**2)
        assert csch(z).expand(complex=True, deep=deep) == cos(b) * sinh(a) / (sin(b)**2\
            *cosh(a)**2 + cos(b)**2 * sinh(a)**2) - I*sin(b) * cosh(a) / (sin(b)**2\
            *cosh(a)**2 + cos(b)**2 * sinh(a)**2)
        assert sech(z).expand(complex=True, deep=deep) == cos(b) * cosh(a) / (sin(b)**2\
            *sinh(a)**2 + cos(b)**2 * cosh(a)**2) - I*sin(b) * sinh(a) / (sin(b)**2\
            *sinh(a)**2 + cos(b)**2 * cosh(a)**2)
Example #32
0
def test_sympy__functions__elementary__hyperbolic__tanh():
    from sympy.functions.elementary.hyperbolic import tanh
    assert _test_args(tanh(2))