Esempio n. 1
0
def test_exptrigsimp():
    def valid(a, b):
        from sympy.testing.randtest 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))
Esempio n. 2
0
def test_exptrigsimp():
    def valid(a, b):
        from sympy.utilities.randtest 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)
    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)
        s = simplify(eq)
        assert s == exptrigsimp(eq)
        res.append(s)
        sinv = simplify(1/eq)
        assert sinv == exptrigsimp(1/eq)
        res.append(sinv)
    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))
Esempio n. 3
0
def test_issue_2827_trigsimp_methods():
    measure1 = lambda expr: len(str(expr))
    measure2 = lambda expr: -count_ops(expr)
    # Return the most complicated result
    expr = (x + 1) / (x + sin(x)**2 + cos(x)**2)
    ans = Matrix([1])
    M = Matrix([expr])
    assert trigsimp(M, method='fu', measure=measure1) == ans
    assert trigsimp(M, method='fu', measure=measure2) != ans
    # all methods should work with Basic expressions even if they
    # aren't Expr
    M = Matrix.eye(1)
    assert all(
        trigsimp(M, method=m) == M for m in 'fu matching groebner old'.split())
    # watch for E in exptrigsimp, not only exp()
    eq = 1 / sqrt(E) + E
    assert exptrigsimp(eq) == eq
Esempio n. 4
0
def test_issue_2827_trigsimp_methods():
    measure1 = lambda expr: len(str(expr))
    measure2 = lambda expr: -count_ops(expr)
                                       # Return the most complicated result
    expr = (x + 1)/(x + sin(x)**2 + cos(x)**2)
    ans = Matrix([1])
    M = Matrix([expr])
    assert trigsimp(M, method='fu', measure=measure1) == ans
    assert trigsimp(M, method='fu', measure=measure2) != ans
    # all methods should work with Basic expressions even if they
    # aren't Expr
    M = Matrix.eye(1)
    assert all(trigsimp(M, method=m) == M for m in
        'fu matching groebner old'.split())
    # watch for E in exptrigsimp, not only exp()
    eq = 1/sqrt(E) + E
    assert exptrigsimp(eq) == eq
Esempio n. 5
0
    def _is_ac(self, expr):

        # Convert exp(-j*x*t) + exp(j*x*t) into 2 * cos(x) etc.
        expr = sym.exptrigsimp(expr.rewrite(cos))

        terms = expr.as_ordered_terms()
        if len(terms) > 1:
            return self._is_sum_ac(terms)

        factors = expr.as_ordered_factors()

        self.amp = 1
        for factor in factors:
            if factor.is_Function:
                if factor.func not in (cos, sin, exp):
                    return False
                if not self._find_freq_phase(factor):
                    return False
            elif is_dc(factor, self.var):
                self.amp *= factor
            else:
                return False
        return True
Esempio n. 6
0
def test_exptrigsimp_noncommutative():
    a, b = symbols('a b', commutative=False)
    x = Symbol('x', commutative=True)
    assert exp(a + x) == exptrigsimp(exp(a) * exp(x))
    p = exp(a) * exp(b) - exp(b) * exp(a)
    assert p == exptrigsimp(p) != 0
Esempio n. 7
0
def test_exptrigsimp_noncommutative():
    a,b = symbols('a b', commutative=False)
    x = Symbol('x', commutative=True)
    assert exp(a + x) == exptrigsimp(exp(a)*exp(x))
    p = exp(a)*exp(b) - exp(b)*exp(a)
    assert p == exptrigsimp(p) != 0