def test_exptrigsimp(): def valid(a, b): from diofant.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))
def test_exptrigsimp(): def valid(a, b): 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))
def test_sympyissue_2827_trigsimp_methods(): def measure1(expr): return len(str(expr)) def measure2(expr): return -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
def test_sympyissue_2827_trigsimp_methods(): def measure1(expr): return len(str(expr)) def measure2(expr): return -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