def test_manualintegrate_special(): f, F = 4*exp(-x**2/3), 2*sqrt(3)*sqrt(pi)*erf(sqrt(3)*x/3) assert_is_integral_of(f, F) f, F = 3*exp(4*x**2), 3*sqrt(pi)*erfi(2*x)/4 assert_is_integral_of(f, F) f, F = x**Rational(1, 3)*exp(-x/8), -16*uppergamma(Rational(4, 3), x/8) assert_is_integral_of(f, F) f, F = exp(2*x)/x, Ei(2*x) assert_is_integral_of(f, F) f, F = exp(1 + 2*x - x**2), sqrt(pi)*exp(2)*erf(x - 1)/2 assert_is_integral_of(f, F) f = sin(x**2 + 4*x + 1) F = (sqrt(2)*sqrt(pi)*(-sin(3)*fresnelc(sqrt(2)*(2*x + 4)/(2*sqrt(pi))) + cos(3)*fresnels(sqrt(2)*(2*x + 4)/(2*sqrt(pi))))/2) assert_is_integral_of(f, F) f, F = cos(4*x**2), sqrt(2)*sqrt(pi)*fresnelc(2*sqrt(2)*x/sqrt(pi))/4 assert_is_integral_of(f, F) f, F = sin(3*x + 2)/x, sin(2)*Ci(3*x) + cos(2)*Si(3*x) assert_is_integral_of(f, F) f, F = sinh(3*x - 2)/x, -sinh(2)*Chi(3*x) + cosh(2)*Shi(3*x) assert_is_integral_of(f, F) f, F = 5*cos(2*x - 3)/x, 5*cos(3)*Ci(2*x) + 5*sin(3)*Si(2*x) assert_is_integral_of(f, F) f, F = cosh(x/2)/x, Chi(x/2) assert_is_integral_of(f, F) f, F = cos(x**2)/x, Ci(x**2)/2 assert_is_integral_of(f, F) f, F = 1/log(2*x + 1), li(2*x + 1)/2 assert_is_integral_of(f, F) f, F = polylog(2, 5*x)/x, polylog(3, 5*x) assert_is_integral_of(f, F) f, F = 5/sqrt(3 - 2*sin(x)**2), 5*sqrt(3)*elliptic_f(x, Rational(2, 3))/3 assert_is_integral_of(f, F) f, F = sqrt(4 + 9*sin(x)**2), 2*elliptic_e(x, Rational(-9, 4)) assert_is_integral_of(f, F)
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
def test_csch(): x, y = symbols('x,y') k = Symbol('k', integer=True) n = Symbol('n', positive=True) assert csch(nan) is nan assert csch(zoo) is nan assert csch(oo) == 0 assert csch(-oo) == 0 assert csch(0) is zoo assert csch(-1) == -csch(1) assert csch(-x) == -csch(x) assert csch(-pi) == -csch(pi) assert csch(-2**1024 * E) == -csch(2**1024 * E) assert csch(pi * I) is zoo assert csch(-pi * I) is zoo assert csch(2 * pi * I) is zoo assert csch(-2 * pi * I) is zoo assert csch(-3 * 10**73 * pi * I) is zoo assert csch(7 * 10**103 * pi * I) is zoo assert csch(pi * I / 2) == -I assert csch(-pi * I / 2) == I assert csch(pi * I * Rational(5, 2)) == -I assert csch(pi * I * Rational(7, 2)) == I assert csch(pi * I / 3) == -2 / sqrt(3) * I assert csch(pi * I * Rational(-2, 3)) == 2 / sqrt(3) * I assert csch(pi * I / 4) == -sqrt(2) * I assert csch(-pi * I / 4) == sqrt(2) * I assert csch(pi * I * Rational(7, 4)) == sqrt(2) * I assert csch(pi * I * Rational(-3, 4)) == sqrt(2) * I assert csch(pi * I / 6) == -2 * I assert csch(-pi * I / 6) == 2 * I assert csch(pi * I * Rational(7, 6)) == 2 * I assert csch(pi * I * Rational(-7, 6)) == -2 * I assert csch(pi * I * Rational(-5, 6)) == 2 * I assert csch(pi * I / 105) == -1 / sin(pi / 105) * I assert csch(-pi * I / 105) == 1 / sin(pi / 105) * I assert csch(x * I) == -1 / sin(x) * I assert csch(k * pi * I) is zoo assert csch(17 * k * pi * I) is zoo assert csch(k * pi * I / 2) == -1 / sin(k * pi / 2) * I assert csch(n).is_real is True assert expand_trig(csch(x + y)) == 1 / (sinh(x) * cosh(y) + cosh(x) * sinh(y))
def test_cosh_expansion(): x, y = symbols('x,y') assert cosh(x + y).expand(trig=True) == cosh(x) * cosh(y) + sinh(x) * sinh(y) assert cosh(2 * x).expand(trig=True) == cosh(x)**2 + sinh(x)**2 assert cosh(3*x).expand(trig=True).expand() == \ 3*sinh(x)**2*cosh(x) + cosh(x)**3
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
def test_ode_solutions(): # only a few examples here, the rest will be tested in the actual dsolve tests assert constant_renumber(constantsimp(C1*exp(2*x) + exp(x)*(C2 + C3), [C1, C2, C3])) == \ constant_renumber(C1*exp(x) + C2*exp(2*x)) assert constant_renumber( constantsimp(Eq(f(x), I * C1 * sinh(x / 3) + C2 * cosh(x / 3)), [C1, C2])) == constant_renumber( Eq(f(x), C1 * sinh(x / 3) + C2 * cosh(x / 3))) assert constant_renumber(constantsimp(Eq(f(x), acos((-C1)/cos(x))), [C1])) == \ Eq(f(x), acos(C1/cos(x))) assert constant_renumber( constantsimp(Eq(log(f(x) / C1) + 2 * exp(x / f(x)), 0), [C1])) == Eq(log(C1 * f(x)) + 2 * exp(x / f(x)), 0) assert constant_renumber(constantsimp(Eq(log(x*sqrt(2)*sqrt(1/x)*sqrt(f(x)) /C1) + x**2/(2*f(x)**2), 0), [C1])) == \ Eq(log(C1*sqrt(x)*sqrt(f(x))) + x**2/(2*f(x)**2), 0) assert constant_renumber(constantsimp(Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(x/C1) - cos(f(x)/x)*exp(-f(x)/x)/2, 0), [C1])) == \ Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)* exp(-f(x)/x)/2, 0) assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2), (u2, _a, x/f(x))) + log(f(x)/C1), 0), [C1])) == \ Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) + log(C1*f(x)), 0) assert [constantsimp(i, [C1]) for i in [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x + x**2))]] == \ [Eq(f(x), sqrt(x*(C1 + x))), Eq(f(x), -sqrt(x*(C1 + x)))]
def test_coth_rewrite(): x = Symbol('x') 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_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
def test_expint(): """ Test various exponential integrals. """ from sympy.core.symbol import Symbol from sympy.functions.elementary.complexes import unpolarify from sympy.functions.elementary.hyperbolic import sinh from sympy.functions.special.error_functions import (Chi, Ci, Ei, Shi, Si, expint) assert simplify( unpolarify( integrate(exp(-z * x) / x**y, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand( func=True))) == expint(y, z) assert integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(1, z) assert integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(2, z).rewrite(Ei).rewrite(expint) assert integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True, conds='none').rewrite(expint).expand() == \ expint(3, z).rewrite(Ei).rewrite(expint).expand() t = Symbol('t', positive=True) assert integrate(-cos(x) / x, (x, t, oo), meijerg=True).expand() == Ci(t) assert integrate(-sin(x)/x, (x, t, oo), meijerg=True).expand() == \ Si(t) - pi/2 assert integrate(sin(x) / x, (x, 0, z), meijerg=True) == Si(z) assert integrate(sinh(x) / x, (x, 0, z), meijerg=True) == Shi(z) assert integrate(exp(-x)/x, x, meijerg=True).expand().rewrite(expint) == \ I*pi - expint(1, x) assert integrate(exp(-x)/x**2, x, meijerg=True).rewrite(expint).expand() \ == expint(1, x) - exp(-x)/x - I*pi u = Symbol('u', polar=True) assert integrate(cos(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Ci(u) assert integrate(cosh(u)/u, u, meijerg=True).expand().as_independent(u)[1] \ == Chi(u) assert integrate( expint(1, x), x, meijerg=True).rewrite(expint).expand() == x * expint(1, x) - exp(-x) assert integrate(expint(2, x), x, meijerg=True ).rewrite(expint).expand() == \ -x**2*expint(1, x)/2 + x*exp(-x)/2 - exp(-x)/2 assert simplify(unpolarify(integrate(expint(y, x), x, meijerg=True).rewrite(expint).expand(func=True))) == \ -expint(y + 1, x) assert integrate(Si(x), x, meijerg=True) == x * Si(x) + cos(x) assert integrate(Ci(u), u, meijerg=True).expand() == u * Ci(u) - sin(u) assert integrate(Shi(x), x, meijerg=True) == x * Shi(x) - cosh(x) assert integrate(Chi(u), u, meijerg=True).expand() == u * Chi(u) - sinh(u) assert integrate(Si(x) * exp(-x), (x, 0, oo), meijerg=True) == pi / 4 assert integrate(expint(1, x) * sin(x), (x, 0, oo), meijerg=True) == log(2) / 2
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)
def test_sech(): x, y = symbols('x, y') k = Symbol('k', integer=True) n = Symbol('n', positive=True) assert sech(nan) is nan assert sech(zoo) is nan assert sech(oo) == 0 assert sech(-oo) == 0 assert sech(0) == 1 assert sech(-1) == sech(1) assert sech(-x) == sech(x) assert sech(pi * I) == sec(pi) assert sech(-pi * I) == sec(pi) assert sech(-2**1024 * E) == sech(2**1024 * E) assert sech(pi * I / 2) is zoo assert sech(-pi * I / 2) is zoo assert sech((-3 * 10**73 + 1) * pi * I / 2) is zoo assert sech((7 * 10**103 + 1) * pi * I / 2) is zoo assert sech(pi * I) == -1 assert sech(-pi * I) == -1 assert sech(5 * pi * I) == -1 assert sech(8 * pi * I) == 1 assert sech(pi * I / 3) == 2 assert sech(pi * I * Rational(-2, 3)) == -2 assert sech(pi * I / 4) == sqrt(2) assert sech(-pi * I / 4) == sqrt(2) assert sech(pi * I * Rational(5, 4)) == -sqrt(2) assert sech(pi * I * Rational(-5, 4)) == -sqrt(2) assert sech(pi * I / 6) == 2 / sqrt(3) assert sech(-pi * I / 6) == 2 / sqrt(3) assert sech(pi * I * Rational(7, 6)) == -2 / sqrt(3) assert sech(pi * I * Rational(-5, 6)) == -2 / sqrt(3) assert sech(pi * I / 105) == 1 / cos(pi / 105) assert sech(-pi * I / 105) == 1 / cos(pi / 105) assert sech(x * I) == 1 / cos(x) assert sech(k * pi * I) == 1 / cos(k * pi) assert sech(17 * k * pi * I) == 1 / cos(17 * k * pi) assert sech(n).is_real is True assert expand_trig(sech(x + y)) == 1 / (cosh(x) * cosh(y) + sinh(x) * sinh(y))
def test_heurisch_hyperbolic(): assert heurisch(sinh(x), x) == cosh(x) assert heurisch(cosh(x), x) == sinh(x) assert heurisch(x * sinh(x), x) == x * cosh(x) - sinh(x) assert heurisch(x * cosh(x), x) == x * sinh(x) - cosh(x) assert heurisch( x * asinh(x / 2), x) == x**2 * asinh(x / 2) / 2 + asinh(x / 2) - x * sqrt(4 + x**2) / 4
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
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)))
def test_sinh_expansion(): x, y = symbols('x,y') assert sinh(x + y).expand(trig=True) == sinh(x) * cosh(y) + cosh(x) * sinh(y) assert sinh(2 * x).expand(trig=True) == 2 * sinh(x) * cosh(x) assert sinh(3*x).expand(trig=True).expand() == \ sinh(x)**3 + 3*sinh(x)*cosh(x)**2
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
def test_inverse_laplace_transform(): from sympy.core.exprtools import factor_terms from sympy.functions.special.delta_functions import DiracDelta from sympy.simplify.simplify import simplify ILT = inverse_laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') def simp_hyp(expr): return factor_terms(expand_mul(expr)).rewrite(sin) assert ILT(1, s, t) == DiracDelta(t) assert ILT(1/s, s, t) == Heaviside(t) assert ILT(a/(a + s), s, t) == a*exp(-a*t)*Heaviside(t) assert ILT(s/(a + s), s, t) == -a*exp(-a*t)*Heaviside(t) + DiracDelta(t) assert ILT((a + s)**(-2), s, t) == t*exp(-a*t)*Heaviside(t) assert ILT((a + s)**(-5), s, t) == t**4*exp(-a*t)*Heaviside(t)/24 assert ILT(a/(a**2 + s**2), s, t) == sin(a*t)*Heaviside(t) assert ILT(s/(s**2 + a**2), s, t) == cos(a*t)*Heaviside(t) assert ILT(b/(b**2 + (a + s)**2), s, t) == exp(-a*t)*sin(b*t)*Heaviside(t) assert ILT(b*s/(b**2 + (a + s)**2), s, t) +\ (a*sin(b*t) - b*cos(b*t))*exp(-a*t)*Heaviside(t) == 0 assert ILT(exp(-a*s)/s, s, t) == Heaviside(-a + t) assert ILT(exp(-a*s)/(b + s), s, t) == exp(b*(a - t))*Heaviside(-a + t) assert ILT((b + s)/(a**2 + (b + s)**2), s, t) == \ exp(-b*t)*cos(a*t)*Heaviside(t) assert ILT(exp(-a*s)/s**b, s, t) == \ (-a + t)**(b - 1)*Heaviside(-a + t)/gamma(b) assert ILT(exp(-a*s)/sqrt(s**2 + 1), s, t) == \ Heaviside(-a + t)*besselj(0, a - t) assert ILT(1/(s*sqrt(s + 1)), s, t) == Heaviside(t)*erf(sqrt(t)) assert ILT(1/(s**2*(s**2 + 1)), s, t) == (t - sin(t))*Heaviside(t) assert ILT(s**2/(s**2 + 1), s, t) == -sin(t)*Heaviside(t) + DiracDelta(t) assert ILT(1 - 1/(s**2 + 1), s, t) == -sin(t)*Heaviside(t) + DiracDelta(t) assert ILT(1/s**2, s, t) == t*Heaviside(t) assert ILT(1/s**5, s, t) == t**4*Heaviside(t)/24 assert simp_hyp(ILT(a/(s**2 - a**2), s, t)) == sinh(a*t)*Heaviside(t) assert simp_hyp(ILT(s/(s**2 - a**2), s, t)) == cosh(a*t)*Heaviside(t) # TODO sinh/cosh shifted come out a mess. also delayed trig is a mess # TODO should this simplify further? assert ILT(exp(-a*s)/s**b, s, t) == \ (t - a)**(b - 1)*Heaviside(t - a)/gamma(b) assert ILT(exp(-a*s)/sqrt(1 + s**2), s, t) == \ Heaviside(t - a)*besselj(0, a - t) # note: besselj(0, x) is even # XXX ILT turns these branch factor into trig functions ... assert simplify(ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2), s, t).rewrite(exp)) == \ Heaviside(t)*besseli(b, a*t) assert ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2), s, t).rewrite(exp) == \ Heaviside(t)*besselj(b, a*t) assert ILT(1/(s*sqrt(s + 1)), s, t) == Heaviside(t)*erf(sqrt(t)) # TODO can we make erf(t) work? assert ILT(1/(s**2*(s**2 + 1)),s,t) == (t - sin(t))*Heaviside(t) assert ILT( (s * eye(2) - Matrix([[1, 0], [0, 2]])).inv(), s, t) ==\ Matrix([[exp(t)*Heaviside(t), 0], [0, exp(2*t)*Heaviside(t)]])
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
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)
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)
def test_issue_3210(): eqs = ( sin(2) * cos(3) + sin(3) * cos(2), -sin(2) * sin(3) + cos(2) * cos(3), sin(2) * cos(3) - sin(3) * cos(2), sin(2) * sin(3) + cos(2) * cos(3), sin(2) * sin(3) + cos(2) * cos(3) + cos(2), sinh(2) * cosh(3) + sinh(3) * cosh(2), sinh(2) * sinh(3) + cosh(2) * cosh(3), ) assert [trigsimp(e) for e in eqs] == [ sin(5), cos(5), -sin(1), cos(1), cos(1) + cos(2), sinh(5), cosh(5), ]
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
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)
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()
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)
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)
def test_si(): assert Si(I*x) == I*Shi(x) assert Shi(I*x) == I*Si(x) assert Si(-I*x) == -I*Shi(x) assert Shi(-I*x) == -I*Si(x) assert Si(-x) == -Si(x) assert Shi(-x) == -Shi(x) assert Si(exp_polar(2*pi*I)*x) == Si(x) assert Si(exp_polar(-2*pi*I)*x) == Si(x) assert Shi(exp_polar(2*pi*I)*x) == Shi(x) assert Shi(exp_polar(-2*pi*I)*x) == Shi(x) assert Si(oo) == pi/2 assert Si(-oo) == -pi/2 assert Shi(oo) is oo assert Shi(-oo) is -oo assert mytd(Si(x), sin(x)/x, x) assert mytd(Shi(x), sinh(x)/x, x) assert mytn(Si(x), Si(x).rewrite(Ei), -I*(-Ei(x*exp_polar(-I*pi/2))/2 + Ei(x*exp_polar(I*pi/2))/2 - I*pi) + pi/2, x) assert mytn(Si(x), Si(x).rewrite(expint), -I*(-expint(1, x*exp_polar(-I*pi/2))/2 + expint(1, x*exp_polar(I*pi/2))/2) + pi/2, x) assert mytn(Shi(x), Shi(x).rewrite(Ei), Ei(x)/2 - Ei(x*exp_polar(I*pi))/2 + I*pi/2, x) assert mytn(Shi(x), Shi(x).rewrite(expint), expint(1, x)/2 - expint(1, x*exp_polar(I*pi))/2 - I*pi/2, x) assert tn_arg(Si) assert tn_arg(Shi) assert Si(x).nseries(x, n=8) == \ x - x**3/18 + x**5/600 - x**7/35280 + O(x**9) assert Shi(x).nseries(x, n=8) == \ x + x**3/18 + x**5/600 + x**7/35280 + O(x**9) assert Si(sin(x)).nseries(x, n=5) == x - 2*x**3/9 + 17*x**5/450 + O(x**6) assert Si(x).nseries(x, 1, n=3) == \ Si(1) + (x - 1)*sin(1) + (x - 1)**2*(-sin(1)/2 + cos(1)/2) + O((x - 1)**3, (x, 1)) assert Si(x).series(x, oo) == pi/2 - (- 6/x**3 + 1/x \ + O(x**(-7), (x, oo)))*sin(x)/x - (24/x**4 - 2/x**2 + 1 \ + O(x**(-7), (x, oo)))*cos(x)/x t = Symbol('t', Dummy=True) assert Si(x).rewrite(sinc) == Integral(sinc(t), (t, 0, x)) assert limit(Shi(x), x, S.NegativeInfinity) == -I*pi/2
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_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 test_trigsimp_old(): x, y = symbols('x,y') assert trigsimp(1 - sin(x)**2, old=True) == cos(x)**2 assert trigsimp(1 - cos(x)**2, old=True) == sin(x)**2 assert trigsimp(sin(x)**2 + cos(x)**2, old=True) == 1 assert trigsimp(1 + tan(x)**2, old=True) == 1 / cos(x)**2 assert trigsimp(1 / cos(x)**2 - 1, old=True) == tan(x)**2 assert trigsimp(1 / cos(x)**2 - tan(x)**2, old=True) == 1 assert trigsimp(1 + cot(x)**2, old=True) == 1 / sin(x)**2 assert trigsimp(1 / sin(x)**2 - cot(x)**2, old=True) == 1 assert trigsimp(5 * cos(x)**2 + 5 * sin(x)**2, old=True) == 5 assert trigsimp(sin(x) / cos(x), old=True) == tan(x) assert trigsimp(2 * tan(x) * cos(x), old=True) == 2 * sin(x) assert trigsimp(cot(x)**3 * sin(x)**3, old=True) == cos(x)**3 assert trigsimp(y * tan(x)**2 / sin(x)**2, old=True) == y / cos(x)**2 assert trigsimp(cot(x) / cos(x), old=True) == 1 / sin(x) assert trigsimp(sin(x + y) + sin(x - y), old=True) == 2 * sin(x) * cos(y) assert trigsimp(sin(x + y) - sin(x - y), old=True) == 2 * sin(y) * cos(x) assert trigsimp(cos(x + y) + cos(x - y), old=True) == 2 * cos(x) * cos(y) assert trigsimp(cos(x + y) - cos(x - y), old=True) == -2 * sin(x) * sin(y) assert trigsimp(sinh(x + y) + sinh(x - y), old=True) == 2 * sinh(x) * cosh(y) assert trigsimp(sinh(x + y) - sinh(x - y), old=True) == 2 * sinh(y) * cosh(x) assert trigsimp(cosh(x + y) + cosh(x - y), old=True) == 2 * cosh(x) * cosh(y) assert trigsimp(cosh(x + y) - cosh(x - y), old=True) == 2 * sinh(x) * sinh(y) assert trigsimp(cos(0.12345)**2 + sin(0.12345)**2, old=True) == 1 assert trigsimp(sin(x) / cos(x), old=True, method='combined') == tan(x) assert trigsimp(sin(x) / cos(x), old=True, method='groebner') == sin(x) / cos(x) assert trigsimp(sin(x) / cos(x), old=True, method='groebner', hints=[tan]) == tan(x) assert trigsimp(1 - sin(sin(x)**2 + cos(x)**2)**2, old=True, deep=True) == cos(1)**2
def test_sympy__functions__elementary__hyperbolic__sinh(): from sympy.functions.elementary.hyperbolic import sinh assert _test_args(sinh(2))
return abs(m1 * m2) elif formula == "trap" and der2_f is not None: m1 = ( ((b - a)**3) / (12 * n**2) ) m2 = brute_max(der2_f, [a, b]) return abs(m1 * m2) elif formula == "simp" and der4_f is not None: m1 = ( -((b - a)**3) / (2880 * n**4) ) m2 = brute_max(der4_f, [a, b]) return abs(m1 * m2) def runge(k, I_2n, I_n): return float(((1 << k) * I_2n - I_n)) / ((1 << (k)) - 1) if __name__ == '__main__': x = Symbol('x') f_expression = 1 / (0.3 + sinh(x)) print f_expression der2_f_expression = f_expression.diff(x, 2) der4_f_expression = f_expression.diff(x, 4) f = lambdify(x, f_expression) der2_f = lambdify(x, der2_f_expression) der4_f = lambdify(x, der4_f_expression) formula_ks = {"rect":2, "trap":2, "simp":4} values = dict() for formula in ("rect", "trap", "simp"): values[formula] = dict() for integral_n in (8, 16): values[formula][integral_n] = dict() I = integral(formula, f, integral_a, integral_b, integral_n) r = remainder(formula, f, integral_a, integral_b, integral_n, der2_f, der4_f) values[formula][integral_n]["value"] = I ; values[formula][integral_n]["remainder"] = r