def test_simplifications(): 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
def test_simplifications(): 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
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)
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)
def test_derivs(): 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)
def test_derivs(): 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)
def test_gruntz_hyperbolic(): assert limit(cosh(x), x, oo) == oo assert limit(cosh(-x), x, oo) == oo assert limit(sinh(x), x, oo) == oo assert limit(sinh(-x), x, oo) == -oo assert limit(2*cosh(x)*exp(x), x, oo) == oo assert limit(2*cosh(-x)*exp(-x), x, oo) == 1 assert limit(2*sinh(x)*exp(x), x, oo) == oo assert limit(2*sinh(-x)*exp(-x), x, oo) == -1 assert limit(tanh(x), x, oo) == 1 assert limit(tanh(-x), x, oo) == -1 assert limit(coth(x), x, oo) == 1 assert limit(coth(-x), x, oo) == -1
def test_gruntz_hyperbolic(): assert gruntz(cosh(x), x) == oo assert gruntz(cosh(-x), x) == oo assert gruntz(sinh(x), x) == oo assert gruntz(sinh(-x), x) == -oo assert gruntz(2*cosh(x)*exp(x), x) == oo assert gruntz(2*cosh(-x)*exp(-x), x) == 1 assert gruntz(2*sinh(x)*exp(x), x) == oo assert gruntz(2*sinh(-x)*exp(-x), x) == -1 assert gruntz(tanh(x), x) == 1 assert gruntz(tanh(-x), x) == -1 assert gruntz(coth(x), x) == 1 assert gruntz(coth(-x), x) == -1
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)
def test_gruntz_hyperbolic(): assert gruntz(cosh(x), x) == oo assert gruntz(cosh(-x), x) == oo assert gruntz(sinh(x), x) == oo assert gruntz(sinh(-x), x) == -oo assert gruntz(2 * cosh(x) * exp(x), x) == oo assert gruntz(2 * cosh(-x) * exp(-x), x) == 1 assert gruntz(2 * sinh(x) * exp(x), x) == oo assert gruntz(2 * sinh(-x) * exp(-x), x) == -1 assert gruntz(tanh(x), x) == 1 assert gruntz(tanh(-x), x) == -1 assert gruntz(coth(x), x) == 1 assert gruntz(coth(-x), x) == -1
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs({x: xval}), strict=False) numerical_tests.append((name, (xval,), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ('test_fabs', abs(x)), ('test_acos', acos(x)), ('test_asin', asin(x)), ('test_atan', atan(x)), ('test_cos', cos(x)), ('test_cosh', cosh(x)), ('test_log', log(x)), ('test_ln', ln(x)), ('test_sin', sin(x)), ('test_sinh', sinh(x)), ('test_sqrt', sqrt(x)), ('test_tan', tan(x)), ('test_tanh', tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs({x: xval}), strict=False) numerical_tests.append((name, (xval, ), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == 'C': name_expr_C = [('test_floor', floor(x)), ('test_ceil', ceiling(x))] else: name_expr_C = [] run_test('intrinsic_math1', name_expr + name_expr_C, numerical_tests, lang, commands)
def test_intrinsic_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] numerical_tests = [] for name, expr in name_expr: for xval in 0.2, 0.5, 0.8: expected = N(expr.subs(x, xval), strict=False) numerical_tests.append((name, (xval, ), expected, 1e-14)) for lang, commands in valid_lang_commands: if lang == "C": name_expr_C = [("test_floor", floor(x)), ("test_ceil", ceiling(x))] else: name_expr_C = [] run_test("intrinsic_math1", name_expr + name_expr_C, numerical_tests, lang, commands)
def test_csch_rewrite(): 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)
def test_sech_rewrite(): 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)
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
def test_cosh_rewrite(): 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)
def test_sinh_rewrite(): 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)
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)
def test_sympyissue_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
def test_inverses(): assert sinh(x).inverse() == asinh pytest.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
def test_tanh(): # issue sympy/sympy#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
def test_hyperbolic(): assert sinh(x).series(x, n=7) == x + x**3/6 + x**5/120 + O(x**7) assert cosh(x).series(x) == 1 + x**2/2 + x**4/24 + O(x**6) assert tanh(x).series(x, n=7) == x - x**3/3 + 2*x**5/15 + O(x**7) assert coth(x).series(x, n=7) == \ 1/x - x**3/45 + x/3 + 2*x**5/945 + O(x**7) assert asinh(x).series(x, n=7) == x - x**3/6 + 3*x**5/40 + O(x**7) assert acosh(x).series(x, n=7) == \ pi*I/2 - I*x - 3*I*x**5/40 - I*x**3/6 + O(x**7) assert atanh(x).series(x, n=7) == x + x**3/3 + x**5/5 + O(x**7) assert acoth(x).series(x, n=7) == -I*pi/2 + x + x**3/3 + x**5/5 + O(x**7)
def test_hyperbolic(): assert sinh(x).nseries(x, n=6) == x + x**3/6 + x**5/120 + O(x**7) assert cosh(x).nseries(x, n=5) == 1 + x**2/2 + x**4/24 + O(x**6) assert tanh(x).nseries(x, n=6) == x - x**3/3 + 2*x**5/15 + O(x**7) assert coth(x).nseries(x, n=6) == \ 1/x - x**3/45 + x/3 + 2*x**5/945 + O(x**7) assert asinh(x).nseries(x, n=6) == x - x**3/6 + 3*x**5/40 + O(x**7) assert acosh(x).nseries(x, n=6) == \ pi*I/2 - I*x - 3*I*x**5/40 - I*x**3/6 + O(x**7) assert atanh(x).nseries(x, n=6) == x + x**3/3 + x**5/5 + O(x**7) assert acoth(x).nseries(x, n=6) == x + x**3/3 + x**5/5 + pi*I/2 + O(x**7)
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)
def test_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((-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 # 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) # issue sympy/sympy#11062 trigsimp_groebner(csc(x) * sin(x)) # not raises
def test_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((-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 # 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) # issue sympy/sympy#11062 trigsimp_groebner(csc(x) * sin(x)) # not raises
def test_evalc(): x = Symbol('x', extended_real=True) y = Symbol('y', extended_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()
def test_cot_rewrite(): neg_exp, pos_exp = exp(-x*I), exp(x*I) assert cot(x).rewrite(exp) == I*(pos_exp + neg_exp)/(pos_exp - neg_exp) assert cot(x).rewrite(sin) == 2*sin(2*x)/sin(x)**2 assert cot(x).rewrite(cos) == -cos(x)/cos(x + pi/2) assert cot(x).rewrite(tan) == 1/tan(x) assert (cot(sinh(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: sinh(3)}).evalf()) assert (cot(cosh(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: cosh(3)}).evalf()) assert (cot(tanh(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: tanh(3)}).evalf()) assert (cot(coth(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: coth(3)}).evalf()) assert (cot(sin(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: sin(3)}).evalf()) assert (cot(tan(x)).rewrite(exp).subs({x: 3}).evalf() == cot(x).rewrite(exp).subs({x: tan(3)}).evalf()) assert cot(log(x)).rewrite(Pow) == -I*(x**-I + x**I)/(x**-I - x**I) assert cot(4*pi/34).rewrite(sqrt).ratsimp() == (cos(4*pi/34)/sin(4*pi/34)).rewrite(sqrt).ratsimp() assert cot(4*pi/17).rewrite(sqrt) == (cos(4*pi/17)/sin(4*pi/17)).rewrite(sqrt) assert cot(pi/19).rewrite(sqrt) == cot(pi/19)
def test_evalc(): x = Symbol("x", extended_real=True) y = Symbol("y", extended_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()
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)
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'
def test_unpolarify(): 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
def test_ansi_math1_codegen(): # not included: log10 name_expr = [ ("test_fabs", Abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_ceil", ceiling(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_floor", floor(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] result = codegen(name_expr, "C", "file", header=False, empty=False) assert result[0][0] == "file.c" assert result[0][1] == ( '#include "file.h"\n#include <math.h>\n' 'double test_fabs(double x) {\n double test_fabs_result;\n test_fabs_result = fabs(x);\n return test_fabs_result;\n}\n' 'double test_acos(double x) {\n double test_acos_result;\n test_acos_result = acos(x);\n return test_acos_result;\n}\n' 'double test_asin(double x) {\n double test_asin_result;\n test_asin_result = asin(x);\n return test_asin_result;\n}\n' 'double test_atan(double x) {\n double test_atan_result;\n test_atan_result = atan(x);\n return test_atan_result;\n}\n' 'double test_ceil(double x) {\n double test_ceil_result;\n test_ceil_result = ceil(x);\n return test_ceil_result;\n}\n' 'double test_cos(double x) {\n double test_cos_result;\n test_cos_result = cos(x);\n return test_cos_result;\n}\n' 'double test_cosh(double x) {\n double test_cosh_result;\n test_cosh_result = cosh(x);\n return test_cosh_result;\n}\n' 'double test_floor(double x) {\n double test_floor_result;\n test_floor_result = floor(x);\n return test_floor_result;\n}\n' 'double test_log(double x) {\n double test_log_result;\n test_log_result = log(x);\n return test_log_result;\n}\n' 'double test_ln(double x) {\n double test_ln_result;\n test_ln_result = log(x);\n return test_ln_result;\n}\n' 'double test_sin(double x) {\n double test_sin_result;\n test_sin_result = sin(x);\n return test_sin_result;\n}\n' 'double test_sinh(double x) {\n double test_sinh_result;\n test_sinh_result = sinh(x);\n return test_sinh_result;\n}\n' 'double test_sqrt(double x) {\n double test_sqrt_result;\n test_sqrt_result = sqrt(x);\n return test_sqrt_result;\n}\n' 'double test_tan(double x) {\n double test_tan_result;\n test_tan_result = tan(x);\n return test_tan_result;\n}\n' 'double test_tanh(double x) {\n double test_tanh_result;\n test_tanh_result = tanh(x);\n return test_tanh_result;\n}\n' ) assert result[1][0] == "file.h" assert result[1][1] == ( '#ifndef PROJECT__FILE__H\n#define PROJECT__FILE__H\n' 'double test_fabs(double x);\ndouble test_acos(double x);\n' 'double test_asin(double x);\ndouble test_atan(double x);\n' 'double test_ceil(double x);\ndouble test_cos(double x);\n' 'double test_cosh(double x);\ndouble test_floor(double x);\n' 'double test_log(double x);\ndouble test_ln(double x);\n' 'double test_sin(double x);\ndouble test_sinh(double x);\n' 'double test_sqrt(double x);\ndouble test_tan(double x);\n' 'double test_tanh(double x);\n#endif\n' )
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)
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 tanh(x).series(x, 0, 6) == mytanh(x).series(x, 0, 6)
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)
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)
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)
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_complex(): a, b = symbols('a,b', extended_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)
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) assert tanh(x).series(x, 0, 6) == mytanh(x).series(x, 0, 6)
def test_mathml_trig(): mml = mp._print(sin(x)) assert mml.childNodes[0].nodeName == 'sin' mml = mp._print(cos(x)) assert mml.childNodes[0].nodeName == 'cos' mml = mp._print(tan(x)) assert mml.childNodes[0].nodeName == 'tan' mml = mp._print(asin(x)) assert mml.childNodes[0].nodeName == 'arcsin' mml = mp._print(acos(x)) assert mml.childNodes[0].nodeName == 'arccos' mml = mp._print(atan(x)) assert mml.childNodes[0].nodeName == 'arctan' mml = mp._print(sinh(x)) assert mml.childNodes[0].nodeName == 'sinh' mml = mp._print(cosh(x)) assert mml.childNodes[0].nodeName == 'cosh' mml = mp._print(tanh(x)) assert mml.childNodes[0].nodeName == 'tanh' mml = mp._print(asinh(x)) assert mml.childNodes[0].nodeName == 'arcsinh' mml = mp._print(atanh(x)) assert mml.childNodes[0].nodeName == 'arctanh' mml = mp._print(acosh(x)) assert mml.childNodes[0].nodeName == 'arccosh'
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_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)
def test_coth_rewrite(): assert coth(x).rewrite(exp) == (exp(x) + exp(-x))/(exp(x) - exp(-x)) \ == coth(x).rewrite('tractable') assert coth(x).rewrite(sinh) == -I*sinh(I*pi/2 - x)/sinh(x) assert coth(x).rewrite(cosh) == -I*cosh(x)/cosh(I*pi/2 - x) assert coth(x).rewrite(tanh) == 1/tanh(x)
def test_csch_rewrite(): 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) assert csch(x).rewrite(tanh) == (1 - tanh(x/2)**2)/(2*tanh(x/2)) assert csch(x).rewrite(coth) == (coth(x/2)**2 - 1)/(2*coth(x/2))
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
def test_sympyissue_8901(): assert integrate(sinh(1.0*x)) == 1.0*cosh(1.0*x) assert integrate(tanh(1.0*x)) == 1.0*x - 1.0*log(tanh(1.0*x) + 1) assert integrate(tanh(x)) == x - log(tanh(x) + 1)
def test_sech_rewrite(): 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) assert sech(x).rewrite(tanh) == (1 - tanh(x/2)**2)/(1 + tanh(x/2)**2) assert sech(x).rewrite(coth) == (coth(x/2)**2 - 1)/(coth(x/2)**2 + 1)
def test_intrinsic_math_codegen(): # not included: log10 name_expr = [ ("test_abs", Abs(x)), ("test_acos", acos(x)), ("test_asin", asin(x)), ("test_atan", atan(x)), ("test_cos", cos(x)), ("test_cosh", cosh(x)), ("test_log", log(x)), ("test_ln", ln(x)), ("test_sin", sin(x)), ("test_sinh", sinh(x)), ("test_sqrt", sqrt(x)), ("test_tan", tan(x)), ("test_tanh", tanh(x)), ] result = codegen(name_expr, "F95", "file", header=False, empty=False) assert result[0][0] == "file.f90" expected = ( 'REAL*8 function test_abs(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_abs = Abs(x)\n' 'end function\n' 'REAL*8 function test_acos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_acos = acos(x)\n' 'end function\n' 'REAL*8 function test_asin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_asin = asin(x)\n' 'end function\n' 'REAL*8 function test_atan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_atan = atan(x)\n' 'end function\n' 'REAL*8 function test_cos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_cos = cos(x)\n' 'end function\n' 'REAL*8 function test_cosh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_cosh = cosh(x)\n' 'end function\n' 'REAL*8 function test_log(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_log = log(x)\n' 'end function\n' 'REAL*8 function test_ln(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_ln = log(x)\n' 'end function\n' 'REAL*8 function test_sin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sin = sin(x)\n' 'end function\n' 'REAL*8 function test_sinh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sinh = sinh(x)\n' 'end function\n' 'REAL*8 function test_sqrt(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_sqrt = sqrt(x)\n' 'end function\n' 'REAL*8 function test_tan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_tan = tan(x)\n' 'end function\n' 'REAL*8 function test_tanh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'test_tanh = tanh(x)\n' 'end function\n' ) assert result[0][1] == expected assert result[1][0] == "file.h" expected = ( 'interface\n' 'REAL*8 function test_abs(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_acos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_asin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_atan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_cos(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_cosh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_log(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_ln(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sin(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sinh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_sqrt(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_tan(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' 'interface\n' 'REAL*8 function test_tanh(x)\n' 'implicit none\n' 'REAL*8, intent(in) :: x\n' 'end function\n' 'end interface\n' ) assert result[1][1] == expected
def test_tanh(): k = Symbol('k', integer=True) assert tanh(nan) == nan assert tanh(zoo) == nan assert tanh(oo) == 1 assert tanh(-oo) == -1 assert tanh(0) == 0 assert tanh(1) == tanh(1) assert tanh(-1) == -tanh(1) assert tanh(x) == tanh(x) assert tanh(-x) == -tanh(x) assert tanh(pi) == tanh(pi) assert tanh(-pi) == -tanh(pi) assert tanh(2**1024 * E) == tanh(2**1024 * E) assert tanh(-2**1024 * E) == -tanh(2**1024 * E) assert tanh(pi*I) == 0 assert tanh(-pi*I) == 0 assert tanh(2*pi*I) == 0 assert tanh(-2*pi*I) == 0 assert tanh(-3*10**73*pi*I) == 0 assert tanh(7*10**103*pi*I) == 0 assert tanh(pi*I/2) == tanh(pi*I/2) assert tanh(-pi*I/2) == -tanh(pi*I/2) assert tanh(5*pi*I/2) == tanh(5*pi*I/2) assert tanh(7*pi*I/2) == tanh(7*pi*I/2) assert tanh(pi*I/3) == sqrt(3)*I assert tanh(-2*pi*I/3) == sqrt(3)*I assert tanh(pi*I/4) == I assert tanh(-pi*I/4) == -I assert tanh(17*pi*I/4) == I assert tanh(-3*pi*I/4) == I assert tanh(pi*I/6) == I/sqrt(3) assert tanh(-pi*I/6) == -I/sqrt(3) assert tanh(7*pi*I/6) == I/sqrt(3) assert tanh(-5*pi*I/6) == I/sqrt(3) assert tanh(pi*I/105) == tan(pi/105)*I assert tanh(-pi*I/105) == -tan(pi/105)*I assert tanh(2 + 3*I) == tanh(2 + 3*I) assert tanh(x*I) == tan(x)*I assert tanh(k*pi*I) == 0 assert tanh(17*k*pi*I) == 0 assert tanh(k*pi*I/2) == tan(k*pi/2)*I r = Symbol('r', extended_real=True) assert tanh(r).is_extended_real assert tanh(x).is_extended_real is None assert tanh(r).is_finite assert tanh(x).is_finite is None pytest.raises(ArgumentIndexError, lambda: tanh(x).fdiff(2)) a, b = symbols('a b', extended_real=True) z = a + b*I for deep in [True, False]: d = sinh(a)**2 + cos(b)**2 assert tanh(z).as_real_imag(deep=deep) == (sinh(a)*cosh(a)/d, sin(b)*cos(b)/d) assert tanh(a).as_real_imag(deep=deep) == (tanh(a), 0)
def test_tanh_series(): assert tanh(x).series(x, 0, 10) == \ x - x**3/3 + 2*x**5/15 - 17*x**7/315 + 62*x**9/2835 + O(x**10)
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(x).rewrite(tanh) == (1 + tanh(x/2))/(1 - tanh(x/2))
def test_cosh_rewrite(): 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) assert cosh(x).rewrite(tanh) == (1 + tanh(x/2)**2)/(1 - tanh(x/2)**2) assert cosh(x).rewrite(coth) == (coth(x/2)**2 + 1)/(coth(x/2)**2 - 1)
def test_sinh_rewrite(): 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) assert sinh(x).rewrite(tanh) == 2*tanh(x/2)/(1 - tanh(x/2)**2) assert sinh(x).rewrite(coth) == 2*coth(x/2)/(coth(x/2)**2 - 1)