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_issue_1572_1364_1368(): assert solve((sqrt(x**2 - 1) - 2)) in ([sqrt(5), -sqrt(5)], [-sqrt(5), sqrt(5)]) assert set(solve((2**exp(y**2/x) + 2)/(x**2 + 15), y)) == set([ -sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi)), sqrt(x)*sqrt(-log(log(2)) + log(log(2) + I*pi))]) C1, C2 = symbols('C1 C2') f = Function('f') assert solve(C1 + C2/x**2 - exp(-f(x)), f(x)) == [log(x**2/(C1*x**2 + C2))] a = Symbol('a') E = S.Exp1 assert solve(1 - log(a + 4*x**2), x) in ( [-sqrt(-a + E)/2, sqrt(-a + E)/2], [sqrt(-a + E)/2, -sqrt(-a + E)/2] ) assert solve(log(a**(-3) - x**2)/a, x) in ( [-sqrt(-1 + a**(-3)), sqrt(-1 + a**(-3))], [sqrt(-1 + a**(-3)), -sqrt(-1 + a**(-3))],) assert solve(1 - log(a + 4*x**2), x) in ( [-sqrt(-a + E)/2, sqrt(-a + E)/2], [sqrt(-a + E)/2, -sqrt(-a + E)/2],) assert set(solve(( a**2 + 1) * (sin(a*x) + cos(a*x)), x)) == set([-pi/(4*a), 3*pi/(4*a)]) assert solve(3 - (sinh(a*x) + cosh(a*x)), x) == [2*atanh(S.Half)/a] assert set(solve(3 - (sinh(a*x) + cosh(a*x)**2), x)) == \ set([ 2*atanh(-1 + sqrt(2))/a, 2*atanh(S(1)/2 + sqrt(5)/2)/a, 2*atanh(-sqrt(2) - 1)/a, 2*atanh(-sqrt(5)/2 + S(1)/2)/a ]) assert solve(atan(x) - 1) == [tan(1)]
def _w(self, section): q0 = self.q0 a = self.a aa = self.alpha bb = self.beta A11 = self.laminate.abd[0,0] B11 = self.laminate.abd[0,3] D11 = self.laminate.abd[3,3] n0 = self.n0 x = self._x if ((section is 'left') or (section is 'right')): res = ((1/(4.*n0**2))*q0* (-4*(1/sympy.cosh((1/2.)*a*bb))* sympy.sinh((1/2.)*(a-2*(-x))*bb)* sympy.sinh((1/2.)*a*aa*bb)*D11+a*(a-2*(-x))*aa*n0)) if section is 'right': res = res.subs(self._x, -self._x) elif section is 'mid': res = ((1/(8.*n0**2))*q0* (8*(-1+ sympy.cosh(x*bb)*sympy.cosh((1/2.)*a*(-1+aa)*bb)* (1/sympy.cosh((1/2.)*a*bb)))*D11- (4*x**2+a**2*(-2+aa)*aa)*n0)) return res
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 Lorentz_Tranformation_in_Geometric_Algebra(): Print_Function() (alpha, beta, gamma) = symbols('alpha beta gamma') (x, t, xp, tp) = symbols("x t x' t'") (g0, g1) = MV.setup('gamma*t|x', metric='[1,-1]') from sympy import sinh, cosh R = cosh(alpha/2) + sinh(alpha/2)*(g0 ^ g1) X = t*g0 + x*g1 Xp = tp*g0 + xp*g1 print('R =', R) print(r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}") Xpp = R*Xp*R.rev() Xpp = Xpp.collect([xp, tp]) Xpp = Xpp.subs({2*sinh(alpha/2)*cosh(alpha/2): sinh(alpha), sinh(alpha/2)**2 + cosh(alpha/2)**2: cosh(alpha)}) print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp) Xpp = Xpp.subs({sinh(alpha): gamma*beta, cosh(alpha): gamma}) print(r'%\f{\sinh}{\alpha} = \gamma\beta') print(r'%\f{\cosh}{\alpha} = \gamma') print(r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =", Xpp.collect(gamma)) return
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))
def test_issue_6920(): 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)] # wrap in f to show that the change happens wherever ei occurs f = Function('f') assert [simplify(f(ei)).args[0] for ei in e] == ok
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 + S.Pi/2)/cos(x) assert tan(x).rewrite(cot) == 1/cot(x) assert tan(sinh(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, sinh(3)).n() assert tan(cosh(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cosh(3)).n() assert tan(tanh(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, tanh(3)).n() assert tan(coth(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, coth(3)).n() assert tan(sin(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, sin(3)).n() assert tan(cos(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cos(3)).n() assert tan(tan(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, tan(3)).n() assert tan(cot(x)).rewrite( exp).subs(x, 3).n() == tan(x).rewrite(exp).subs(x, cot(3)).n() assert tan(log(x)).rewrite(Pow) == I*(x**-I - x**I)/(x**-I + x**I) assert 0 == (cos(pi/15)*tan(pi/15) - sin(pi/15)).rewrite(pow) assert tan(pi/19).rewrite(pow) == tan(pi/19) assert tan(8*pi/19).rewrite(sqrt) == tan(8*pi/19)
def Lorentz_Tranformation_in_Geog_Algebra(): Print_Function() (alpha,beta,gamma) = symbols('alpha beta gamma') (x,t,xp,tp) = symbols("x t x' t'",real=True) (st2d,g0,g1) = Ga.build('gamma*t|x',g=[1,-1]) from sympy import sinh,cosh R = cosh(alpha/2)+sinh(alpha/2)*(g0^g1) X = t*g0+x*g1 Xp = tp*g0+xp*g1 print 'R =',R print r"#%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} = t'\bm{\gamma'_{t}}+x'\bm{\gamma'_{x}} = R\lp t'\bm{\gamma_{t}}+x'\bm{\gamma_{x}}\rp R^{\dagger}" Xpp = R*Xp*R.rev() Xpp = Xpp.collect() Xpp = Xpp.trigsimp() print r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =",Xpp Xpp = Xpp.subs({sinh(alpha):gamma*beta,cosh(alpha):gamma}) print r'%\f{\sinh}{\alpha} = \gamma\beta' print r'%\f{\cosh}{\alpha} = \gamma' print r"%t\bm{\gamma_{t}}+x\bm{\gamma_{x}} =",Xpp.collect() return
def test_issue_8368(): assert integrate(exp(-s*x)*cosh(x), (x, 0, oo)) == \ Piecewise( ( pi*Piecewise( ( -s/(pi*(-s**2 + 1)), Abs(s**2) < 1), ( 1/(pi*s*(1 - 1/s**2)), Abs(s**(-2)) < 1), ( meijerg( ((S(1)/2,), (0, 0)), ((0, S(1)/2), (0,)), polar_lift(s)**2), True) ), And( Abs(periodic_argument(polar_lift(s)**2, oo)) < pi, cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0, Ne(s**2, 1)) ), ( Integral(exp(-s*x)*cosh(x), (x, 0, oo)), True)) assert integrate(exp(-s*x)*sinh(x), (x, 0, oo)) == \ Piecewise( ( -1/(s + 1)/2 - 1/(-s + 1)/2, And( Ne(1/s, 1), Abs(periodic_argument(s, oo)) < pi/2, Abs(periodic_argument(s, oo)) <= pi/2, cos(Abs(periodic_argument(s, oo)))*Abs(s) - 1 > 0)), ( Integral(exp(-s*x)*sinh(x), (x, 0, oo)), True))
def test_ode_solutions(): # only a few examples here, the rest will be tested in the actual dsolve tests assert ode_renumber(constantsimp(C1*exp(2*x)+exp(x)*(C2+C3), x, 3), 'C', 1, 3) == \ ode_renumber(C1*exp(x)+C2*exp(2*x), 'C', 1, 2) assert ode_renumber(constantsimp(Eq(f(x),I*C1*sinh(x/3) + C2*cosh(x/3)), x, 2), 'C', 1, 2) == ode_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2) assert ode_renumber(constantsimp(Eq(f(x),acos((-C1)/cos(x))), x, 1), 'C', 1, 1) == \ Eq(f(x),acos(C1/cos(x))) assert ode_renumber(constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), x, 1), 'C', 1, 1) == Eq(log(C1*f(x)) + 2*exp(x/f(x)), 0) assert ode_renumber(constantsimp(Eq(log(x*2**Rational(1,2)*(1/x)**Rational(1,2)*f(x)\ **Rational(1,2)/C1) + x**2/(2*f(x)**2), 0), x, 1), 'C', 1, 1) == \ Eq(log(C1*x*(1/x)**Rational(1,2)*f(x)**Rational(1,2)) + x**2/(2*f(x)**2), 0) assert ode_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), x, 1), 'C', 1, 1) == \ Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)*exp(-f(x)/x)/2, 0) u2 = Symbol('u2') _a = Symbol('_a') assert ode_renumber(constantsimp(Eq(-Integral(-1/((1 - u2**2)**Rational(1,2)*u2), \ (u2, _a, x/f(x))) + log(f(x)/C1), 0), x, 1), 'C', 1, 1) == \ Eq(-Integral(-1/(u2*(1 - u2**2)**Rational(1,2)), (u2, _a, x/f(x))) + \ log(C1*f(x)), 0) assert map(lambda i: ode_renumber(constantsimp(i, x, 1), 'C', 1, 1), [Eq(f(x), (-C1*x + x**2)**Rational(1,2)), Eq(f(x), -(-C1*x + x**2)**Rational(1,2))]) == [Eq(f(x), (C1*x + x**2)**Rational(1,2)), Eq(f(x), -(C1*x + x**2)**Rational(1,2))]
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), x, 3), 'C', 1, 3) == \ constant_renumber(C1*exp(x)+C2*exp(2*x), 'C', 1, 2) assert constant_renumber(constantsimp(Eq(f(x),I*C1*sinh(x/3) + C2*cosh(x/3)), x, 2), 'C', 1, 2) == constant_renumber(Eq(f(x), C1*sinh(x/3) + C2*cosh(x/3)), 'C', 1, 2) assert constant_renumber(constantsimp(Eq(f(x),acos((-C1)/cos(x))), x, 1), 'C', 1, 1) == \ Eq(f(x),acos(C1/cos(x))) assert constant_renumber(constantsimp(Eq(log(f(x)/C1) + 2*exp(x/f(x)), 0), x, 1), 'C', 1, 1) == 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), x, 1), 'C', 1, 1) == \ Eq(log(C1*x*sqrt(1/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), x, 1), 'C', 1, 1) == \ Eq(-exp(-f(x)/x)*sin(f(x)/x)/2 + log(C1*x) - cos(f(x)/x)*exp(-f(x)/x)/2, 0) u2 = Symbol('u2') _a = Symbol('_a') assert constant_renumber(constantsimp(Eq(-Integral(-1/(sqrt(1 - u2**2)*u2), \ (u2, _a, x/f(x))) + log(f(x)/C1), 0), x, 1), 'C', 1, 1) == \ Eq(-Integral(-1/(u2*sqrt(1 - u2**2)), (u2, _a, x/f(x))) + \ log(C1*f(x)), 0) assert [constant_renumber(constantsimp(i, x, 1), 'C', 1, 1) for i in [Eq(f(x), sqrt(-C1*x + x**2)), Eq(f(x), -sqrt(-C1*x + x**2))]] == [Eq(f(x), sqrt(C1*x + x**2)), Eq(f(x), -sqrt(C1*x + x**2))]
def test_manualintegrate_special(): f, F = 4*exp(-x**2/3), 2*sqrt(3)*sqrt(pi)*erf(sqrt(3)*x/3) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = 3*exp(4*x**2), 3*sqrt(pi)*erfi(2*x)/4 assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = x**(S(1)/3)*exp(-x/8), -16*uppergamma(S(4)/3, x/8) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = exp(2*x)/x, Ei(2*x) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = exp(1 + 2*x - x**2), sqrt(pi)*exp(2)*erf(x - 1)/2 assert manualintegrate(f, x) == F and F.diff(x).equals(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 manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = cos(4*x**2), sqrt(2)*sqrt(pi)*fresnelc(2*sqrt(2)*x/sqrt(pi))/4 assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = sin(3*x + 2)/x, sin(2)*Ci(3*x) + cos(2)*Si(3*x) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = sinh(3*x - 2)/x, -sinh(2)*Chi(3*x) + cosh(2)*Shi(3*x) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = 5*cos(2*x - 3)/x, 5*cos(3)*Ci(2*x) + 5*sin(3)*Si(2*x) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = cosh(x/2)/x, Chi(x/2) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = cos(x**2)/x, Ci(x**2)/2 assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = 1/log(2*x + 1), li(2*x + 1)/2 assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = polylog(2, 5*x)/x, polylog(3, 5*x) assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = 5/sqrt(3 - 2*sin(x)**2), 5*sqrt(3)*elliptic_f(x, S(2)/3)/3 assert manualintegrate(f, x) == F and F.diff(x).equals(f) f, F = sqrt(4 + 9*sin(x)**2), 2*elliptic_e(x, -S(9)/4) assert manualintegrate(f, x) == F and F.diff(x).equals(f)
def test_hyper_as_trig(): from sympy.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_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_cosh_rewrite(): x = Symbol('x') assert cosh(x).rewrite(exp) == (exp(x)+exp(-x))/2 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_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_csc(): x = symbols('x', real=True) z = symbols('z') # https://github.com/sympy/sympy/issues/6707 cosecant = csc('x') alternate = 1/sin('x') assert cosecant.equals(alternate) == True assert alternate.equals(cosecant) == True assert csc.nargs == FiniteSet(1) assert csc(0) == zoo assert csc(pi) == zoo assert csc(pi/2) == 1 assert csc(-pi/2) == -1 assert csc(pi/6) == 2 assert csc(pi/3) == 2*sqrt(3)/3 assert csc(5*pi/2) == 1 assert csc(9*pi/7) == -csc(2*pi/7) assert csc(I) == -I/sinh(1) assert csc(x*I) == -I/sinh(x) assert csc(-x) == -csc(x) assert csc(x).rewrite(exp) == 2*I/(exp(I*x) - exp(-I*x)) assert csc(x).rewrite(sin) == 1/sin(x) assert csc(x).rewrite(cos) == csc(x) assert csc(x).rewrite(tan) == (tan(x/2)**2 + 1)/(2*tan(x/2)) assert csc(z).conjugate() == csc(conjugate(z)) assert (csc(z).as_real_imag() == (sin(re(z))*cosh(im(z))/(sin(re(z))**2*cosh(im(z))**2 + cos(re(z))**2*sinh(im(z))**2), -cos(re(z))*sinh(im(z))/(sin(re(z))**2*cosh(im(z))**2 + cos(re(z))**2*sinh(im(z))**2))) assert csc(x).expand(trig=True) == 1/sin(x) assert csc(2*x).expand(trig=True) == 1/(2*sin(x)*cos(x)) assert csc(x).is_real == True assert csc(z).is_real == None assert csc(x).as_leading_term() == csc(x) assert csc(0).is_bounded == False assert csc(x).is_bounded == None assert csc(pi/2).is_bounded == True assert series(csc(x), x, x0=pi/2, n=6) == \ 1 + (x - pi/2)**2/2 + 5*(x - pi/2)**4/24 + O((x - pi/2)**6, (x, pi/2)) assert series(csc(x), x, x0=0, n=6) == \ 1/x + x/6 + 7*x**3/360 + 31*x**5/15120 + O(x**6) assert csc(x).diff(x) == -cot(x)*csc(x)
def test_sec(): x = symbols('x', real=True) z = symbols('z') assert sec.nargs == FiniteSet(1) assert sec(0) == 1 assert sec(pi) == -1 assert sec(pi/2) == zoo assert sec(-pi/2) == zoo assert sec(pi/6) == 2*sqrt(3)/3 assert sec(pi/3) == 2 assert sec(5*pi/2) == zoo assert sec(9*pi/7) == -sec(2*pi/7) assert sec(I) == 1/cosh(1) assert sec(x*I) == 1/cosh(x) assert sec(-x) == sec(x) assert sec(x).rewrite(exp) == 1/(exp(I*x)/2 + exp(-I*x)/2) assert sec(x).rewrite(sin) == sec(x) assert sec(x).rewrite(cos) == 1/cos(x) assert sec(x).rewrite(tan) == (tan(x/2)**2 + 1)/(-tan(x/2)**2 + 1) assert sec(x).rewrite(pow) == sec(x) assert sec(x).rewrite(sqrt) == sec(x) assert sec(z).conjugate() == sec(conjugate(z)) assert (sec(z).as_real_imag() == (cos(re(z))*cosh(im(z))/(sin(re(z))**2*sinh(im(z))**2 + cos(re(z))**2*cosh(im(z))**2), sin(re(z))*sinh(im(z))/(sin(re(z))**2*sinh(im(z))**2 + cos(re(z))**2*cosh(im(z))**2))) assert sec(x).expand(trig=True) == 1/cos(x) assert sec(2*x).expand(trig=True) == 1/(2*cos(x)**2 - 1) assert sec(x).is_real == True assert sec(z).is_real == None assert sec(x).as_leading_term() == sec(x) assert sec(0).is_bounded == True assert sec(x).is_bounded == None assert sec(pi/2).is_bounded == False assert series(sec(x), x, x0=0, n=6) == 1 + x**2/2 + 5*x**4/24 + O(x**6) # https://github.com/sympy/sympy/issues/7166 assert series(sqrt(sec(x))) == 1 + x**2/4 + 7*x**4/96 + O(x**6) assert sec(x).diff(x) == tan(x)*sec(x) # Taylor Term checks assert sec(z).taylor_term(4, z) == 5*z**4/24 assert sec(z).taylor_term(6, z) == 61*z**6/720 assert sec(z).taylor_term(5, z) == 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 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_complex(): a,b = symbols('a,b', real=True) z = a + b*I for func in [sinh, cosh, tanh, coth]: 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)
def test_issue_8368_7173(): LT = laplace_transform # hyperbolic assert LT(sinh(x), x, s) == (1 / (s ** 2 - 1), 1, True) assert LT(cosh(x), x, s) == (s / (s ** 2 - 1), 1, True) assert LT(sinh(x + 3), x, s) == ((-s + (s + 1) * exp(6) + 1) * exp(-3) / (s - 1) / (s + 1) / 2, 1, True) assert LT(sinh(x) * cosh(x), x, s) == (1 / (s ** 2 - 4), 2, Ne(s / 2, 1)) # trig (make sure they are not being rewritten in terms of exp) assert LT(cos(x + 3), x, s) == ((s * cos(3) - sin(3)) / (s ** 2 + 1), 0, True)
def test_sec(): x = symbols('x', real=True) z = symbols('z') assert sec.nargs == 1 assert sec(0) == 1 assert sec(pi) == -1 assert sec(pi/2) == oo assert sec(-pi/2) == oo assert sec(pi/6) == 2*sqrt(3)/3 assert sec(pi/3) == 2 assert sec(5*pi/2) == oo assert sec(9*pi/7) == -sec(2*pi/7) assert sec(I) == 1/cosh(1) assert sec(x*I) == 1/cosh(x) assert sec(-x) == sec(x) assert sec(x).rewrite(exp) == 1/(exp(I*x)/2 + exp(-I*x)/2) assert sec(x).rewrite(sin) == sec(x) assert sec(x).rewrite(cos) == 1/cos(x) assert sec(x).rewrite(tan) == (tan(x/2)**2 + 1)/(-tan(x/2)**2 + 1) assert sec(x).rewrite(pow) == sec(x) assert sec(x).rewrite(sqrt) == sec(x) assert sec(z).conjugate() == sec(conjugate(z)) assert (sec(z).as_real_imag() == (cos(re(z))*cosh(im(z))/(sin(re(z))**2*sinh(im(z))**2 + cos(re(z))**2*cosh(im(z))**2), sin(re(z))*sinh(im(z))/(sin(re(z))**2*sinh(im(z))**2 + cos(re(z))**2*cosh(im(z))**2))) assert sec(x).expand(trig=True) == 1/cos(x) assert sec(2*x).expand(trig=True) == 1/(2*cos(x)**2 - 1) assert sec(x).is_real == True assert sec(z).is_real == None assert sec(x).as_leading_term() == sec(x) assert sec(0).is_bounded == True assert sec(x).is_bounded == None assert sec(pi/2).is_bounded == False assert series(sec(x), x, x0=0, n=6) == 1 + x**2/2 + 5*x**4/24 + O(x**6) # https://code.google.com/p/sympy/issues/detail?id=4067 assert series(sqrt(sec(x))) == 1 + x**2/4 + 7*x**4/96 + O(x**6) # https://code.google.com/p/sympy/issues/detail?id=4068 assert (series(sqrt(sec(x)), x, x0=pi*3/2, n=4) == 1/sqrt(x) +x**(S(3)/2)/12 + x**(S(7)/2)/160 + O(x**4)) assert sec(x).diff(x) == tan(x)*sec(x)
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)) == 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(x)*cos(x) + I*cosh(y)*sinh(y)) / (cos(x)**2 + sinh(y)**2)).expand() 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_real_imag(): a,b = symbols('a,b', real=True) z = a+b*I for deep in [True, False]: assert sin(z).as_real_imag(deep=deep) == (sin(a)*cosh(b), cos(a)*sinh(b)) assert cos(z).as_real_imag(deep=deep) == (cos(a)*cosh(b), -sin(a)*sinh(b)) assert tan(z).as_real_imag(deep=deep) == (sin(a)*cos(a)/(cos(a)**2+sinh(b)**2), sinh(b)*cosh(b)/(cos(a)**2+sinh(b)**2)) assert cot(z).as_real_imag(deep=deep) == (sin(a)*cos(a)/(sin(a)**2+sinh(b)**2), -sinh(b)*cosh(b)/(sin(a)**2+sinh(b)**2)) assert sin(a).as_real_imag(deep=deep) == (sin(a), 0) assert cos(a).as_real_imag(deep=deep) == (cos(a), 0) assert tan(a).as_real_imag(deep=deep) == (tan(a), 0) assert cot(a).as_real_imag(deep=deep) == (cot(a), 0)
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).n() == sin(x).rewrite(exp).subs(x, sinh(3)).n() assert sin(cosh(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cosh(3)).n() assert sin(tanh(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, tanh(3)).n() assert sin(coth(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, coth(3)).n() assert sin(sin(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, sin(3)).n() assert sin(cos(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cos(3)).n() assert sin(tan(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, tan(3)).n() assert sin(cot(x)).rewrite(exp).subs(x, 3).n() == sin(x).rewrite(exp).subs(x, cot(3)).n() assert sin(log(x)).rewrite(Pow) == I * x ** -I / 2 - I * x ** I / 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_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).n() == cos(x).rewrite(exp).subs(x, sinh(3)).n() assert cos(cosh(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cosh(3)).n() assert cos(tanh(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tanh(3)).n() assert cos(coth(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, coth(3)).n() assert cos(sin(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, sin(3)).n() assert cos(cos(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cos(3)).n() assert cos(tan(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, tan(3)).n() assert cos(cot(x)).rewrite(exp).subs(x, 3).n() == cos(x).rewrite(exp).subs(x, cot(3)).n() assert cos(log(x)).rewrite(Pow) == x ** I / 2 + x ** -I / 2
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 cosh(asinh(x)) == sqrt(1 + x ** 2) assert cosh(acosh(x)) == x assert cosh(atanh(x)) == 1 / sqrt(1 - x ** 2) 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
def test_expint(): """ Test various exponential integrals. """ from sympy import (expint, unpolarify, Symbol, Ci, Si, Shi, Chi, sin, cos, sinh, cosh, Ei) 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_cosh_fdiff(): x = Symbol('x') raises(ArgumentIndexError, lambda: cosh(x).fdiff(2))
def test_coth(): x, y = symbols('x,y') k = Symbol('k', integer=True) assert coth(nan) is nan assert coth(zoo) is nan assert coth(oo) == 1 assert coth(-oo) == -1 assert coth(0) is zoo assert unchanged(coth, 1) assert coth(-1) == -coth(1) assert unchanged(coth, x) assert coth(-x) == -coth(x) assert coth(pi * I) == -I * cot(pi) assert coth(-pi * I) == cot(pi) * I assert unchanged(coth, 2**1024 * E) assert coth(-2**1024 * E) == -coth(2**1024 * E) assert coth(pi * I) == -I * cot(pi) assert coth(-pi * I) == I * cot(pi) assert coth(2 * pi * I) == -I * cot(2 * pi) assert coth(-2 * pi * I) == I * cot(2 * pi) assert coth(-3 * 10**73 * pi * I) == I * cot(3 * 10**73 * pi) assert coth(7 * 10**103 * pi * I) == -I * cot(7 * 10**103 * pi) assert coth(pi * I / 2) == 0 assert coth(-pi * I / 2) == 0 assert coth(pi * I * Rational(5, 2)) == 0 assert coth(pi * I * Rational(7, 2)) == 0 assert coth(pi * I / 3) == -I / sqrt(3) assert coth(pi * I * Rational(-2, 3)) == -I / sqrt(3) assert coth(pi * I / 4) == -I assert coth(-pi * I / 4) == I assert coth(pi * I * Rational(17, 4)) == -I assert coth(pi * I * Rational(-3, 4)) == -I assert coth(pi * I / 6) == -sqrt(3) * I assert coth(-pi * I / 6) == sqrt(3) * I assert coth(pi * I * Rational(7, 6)) == -sqrt(3) * I assert coth(pi * I * Rational(-5, 6)) == -sqrt(3) * I assert coth(pi * I / 105) == -cot(pi / 105) * I assert coth(-pi * I / 105) == cot(pi / 105) * I assert unchanged(coth, 2 + 3 * I) assert coth(x * I) == -cot(x) * I assert coth(k * pi * I) == -cot(k * pi) * I assert coth(17 * k * pi * I) == -cot(17 * k * pi) * I assert coth(k * pi * I) == -cot(k * pi) * I assert coth(log(tan(2))) == coth(log(-tan(2))) assert coth(1 + I * pi / 2) == tanh(1) assert coth(x).as_real_imag( deep=False) == (sinh(re(x)) * cosh(re(x)) / (sin(im(x))**2 + sinh(re(x))**2), -sin(im(x)) * cos(im(x)) / (sin(im(x))**2 + sinh(re(x))**2)) x = Symbol('x', extended_real=True) assert coth(x).as_real_imag(deep=False) == (coth(x), 0)
def test_expand(): from sympy import besselsimp, Symbol, exp, exp_polar, I assert expand_func(besselj(S(1)/2, z).rewrite(jn)) == \ sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z)) assert expand_func(bessely(S(1)/2, z).rewrite(yn)) == \ -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z)) # XXX: teach sin/cos to work around arguments like # x*exp_polar(I*pi*n/2). Then change besselsimp -> expand_func assert besselsimp(besselj(S(1) / 2, z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besselj(S(-1) / 2, z)) == sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besselj(S(5)/2, z)) == \ -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(besselj(-S(5)/2, z)) == \ -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(bessely(S(1)/2, z)) == \ -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z)) assert besselsimp(bessely(S(-1) / 2, z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(bessely(S(5)/2, z)) == \ sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(bessely(S(-5)/2, z)) == \ -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(besseli(S(1) / 2, z)) == sqrt(2) * sinh(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besseli(S(-1)/2, z)) == \ sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z)) assert besselsimp(besseli(S(5)/2, z)) == \ sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(besseli(S(-5)/2, z)) == \ sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**(S(5)/2)) assert besselsimp(besselk(S(1)/2, z)) == \ besselsimp(besselk(S(-1)/2, z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z)) assert besselsimp(besselk(S(5)/2, z)) == \ besselsimp(besselk(S(-5)/2, z)) == \ sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**(S(5)/2)) def check(eq, ans): return tn(eq, ans) and eq == ans rn = randcplx(a=1, b=0, d=0, c=2) for besselx in [besselj, bessely, besseli, besselk]: ri = S(2 * randint(-11, 10) + 1) / 2 # half integer in [-21/2, 21/2] assert tn(besselsimp(besselx(ri, z)), besselx(ri, z)) assert check(expand_func(besseli(rn, x)), besseli(rn - 2, x) - 2 * (rn - 1) * besseli(rn - 1, x) / x) assert check(expand_func(besseli(-rn, x)), besseli(-rn + 2, x) + 2 * (-rn + 1) * besseli(-rn + 1, x) / x) assert check(expand_func(besselj(rn, x)), -besselj(rn - 2, x) + 2 * (rn - 1) * besselj(rn - 1, x) / x) assert check( expand_func(besselj(-rn, x)), -besselj(-rn + 2, x) + 2 * (-rn + 1) * besselj(-rn + 1, x) / x) assert check(expand_func(besselk(rn, x)), besselk(rn - 2, x) + 2 * (rn - 1) * besselk(rn - 1, x) / x) assert check(expand_func(besselk(-rn, x)), besselk(-rn + 2, x) - 2 * (-rn + 1) * besselk(-rn + 1, x) / x) assert check(expand_func(bessely(rn, x)), -bessely(rn - 2, x) + 2 * (rn - 1) * bessely(rn - 1, x) / x) assert check( expand_func(bessely(-rn, x)), -bessely(-rn + 2, x) + 2 * (-rn + 1) * bessely(-rn + 1, x) / x) n = Symbol('n', integer=True, positive=True) assert expand_func(besseli(n + 2, z)) == \ besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z assert expand_func(besselj(n + 2, z)) == \ -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z assert expand_func(besselk(n + 2, z)) == \ besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z assert expand_func(bessely(n + 2, z)) == \ -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z assert expand_func(besseli(n + S(1)/2, z).rewrite(jn)) == \ (sqrt(2)*sqrt(z)*exp(-I*pi*(n + S(1)/2)/2) * exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi)) assert expand_func(besselj(n + S(1)/2, z).rewrite(jn)) == \ sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi) r = Symbol('r', real=True) p = Symbol('p', positive=True) i = Symbol('i', integer=True) for besselx in [besselj, bessely, besseli, besselk]: assert besselx(i, p).is_real assert besselx(i, x).is_real is None assert besselx(x, z).is_real is None for besselx in [besselj, besseli]: assert besselx(i, r).is_real for besselx in [bessely, besselk]: assert besselx(i, r).is_real is None
def test_exp_rewrite(): x = symbols('x') 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)
diff = 10 * accuracy h /= 4 counter += 1 return derivative, counter def simpson(func, a, b, n): h = (b - a) / n return h / 3 * sum([ func(a + h * i) + 4 * func(a + h * (i + 1)) + func(a + h * (i + 2)) for i in range(0, n - 1, 2) ]) print("Первая производная") print(first_diff(lambda x: sp.cosh(x), 1, 1e-6)) print("Вторая производная") print(second_diff(lambda x: sp.cosh(x), 1, 1e-6)) print("Метод Трапеций") print(integrate(lambda x: sp.cosh(x), 0, 2, 1e-6, "trapezium")) print('Метод Симпcона:') n = error_estimation(simpson, lambda x: sp.cosh(x), 0, 2, 3, 1e-6) s = simpson(lambda x: sp.cosh(x), 0, 2, n) print('n: ', s) s2 = simpson(lambda x: sp.cosh(x), 0, 2, n // 2) print('n/2:', s2)
def test_sinh(): x, y = symbols('x,y') k = Symbol('k', integer=True) assert sinh(nan) is nan assert sinh(zoo) is nan assert sinh(oo) is oo assert sinh(-oo) is -oo assert sinh(0) == 0 assert unchanged(sinh, 1) assert sinh(-1) == -sinh(1) assert unchanged(sinh, x) assert sinh(-x) == -sinh(x) assert unchanged(sinh, pi) assert sinh(-pi) == -sinh(pi) assert unchanged(sinh, 2**1024 * E) assert sinh(-2**1024 * E) == -sinh(2**1024 * E) assert sinh(pi * I) == 0 assert sinh(-pi * I) == 0 assert sinh(2 * pi * I) == 0 assert sinh(-2 * pi * I) == 0 assert sinh(-3 * 10**73 * pi * I) == 0 assert sinh(7 * 10**103 * pi * I) == 0 assert sinh(pi * I / 2) == I assert sinh(-pi * I / 2) == -I assert sinh(pi * I * Rational(5, 2)) == I assert sinh(pi * I * Rational(7, 2)) == -I assert sinh(pi * I / 3) == S.Half * sqrt(3) * I assert sinh(pi * I * Rational(-2, 3)) == Rational(-1, 2) * sqrt(3) * I assert sinh(pi * I / 4) == S.Half * sqrt(2) * I assert sinh(-pi * I / 4) == Rational(-1, 2) * sqrt(2) * I assert sinh(pi * I * Rational(17, 4)) == S.Half * sqrt(2) * I assert sinh(pi * I * Rational(-3, 4)) == Rational(-1, 2) * sqrt(2) * I assert sinh(pi * I / 6) == S.Half * I assert sinh(-pi * I / 6) == Rational(-1, 2) * I assert sinh(pi * I * Rational(7, 6)) == Rational(-1, 2) * I assert sinh(pi * I * Rational(-5, 6)) == Rational(-1, 2) * I assert sinh(pi * I / 105) == sin(pi / 105) * I assert sinh(-pi * I / 105) == -sin(pi / 105) * I assert unchanged(sinh, 2 + 3 * I) assert sinh(x * I) == sin(x) * I assert sinh(k * pi * I) == 0 assert sinh(17 * k * pi * I) == 0 assert sinh(k * pi * I / 2) == sin(k * pi / 2) * I assert sinh(x).as_real_imag(deep=False) == (cos(im(x)) * sinh(re(x)), sin(im(x)) * cosh(re(x))) x = Symbol('x', extended_real=True) assert sinh(x).as_real_imag(deep=False) == (sinh(x), 0) x = Symbol('x', real=True) assert sinh(I * x).is_finite is True
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_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)
def test_cosh_series(): x = Symbol('x') assert cosh(x).series(x, 0, 10) == \ 1 + x**2/2 + x**4/24 + x**6/720 + x**8/40320 + O(x**10)
def test_hyperbolic_simp(): x, y = symbols('x,y') 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 + S(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) for a in (pi / 6 * I, pi / 4 * I, pi / 3 * I): assert trigsimp(sinh(a) * cosh(x) + cosh(a) * sinh(x)) == sinh(x + a) assert trigsimp(-sinh(a) * cosh(x) + cosh(a) * sinh(x)) == sinh(x - a) 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_trigsimp_issue_7761(): assert trigsimp(cosh(pi / 4)) == cosh(pi / 4)
def test_cos(): x, y = symbols('x y') assert cos(nan) == nan assert cos(oo*I) == oo assert cos(-oo*I) == oo assert cos(0) == 1 assert cos(acos(x)) == x assert cos(atan(x)) == 1 / sqrt(1 + x**2) assert cos(asin(x)) == sqrt(1 - x**2) assert cos(acot(x)) == 1 / sqrt(1 + 1 / x**2) assert cos(atan2(y, x)) == x / sqrt(x**2 + y**2) assert cos(pi*I) == cosh(pi) assert cos(-pi*I) == cosh(pi) assert cos(-2*I) == cosh(2) assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos(pi/2) == 0 assert cos(-pi/2) == 0 assert cos((-3*10**73 + 1)*pi/2) == 0 assert cos((7*10**103 + 1)*pi/2) == 0 assert cos(pi) == -1 assert cos(-pi) == -1 assert cos(2*pi) == 1 assert cos(5*pi) == -1 assert cos(8*pi) == 1 assert cos(pi/3) == S.Half assert cos(-2*pi/3) == -S.Half assert cos(pi/4) == S.Half*sqrt(2) assert cos(-pi/4) == S.Half*sqrt(2) assert cos(11*pi/4) == -S.Half*sqrt(2) assert cos(-3*pi/4) == -S.Half*sqrt(2) assert cos(pi/6) == S.Half*sqrt(3) assert cos(-pi/6) == S.Half*sqrt(3) assert cos(7*pi/6) == -S.Half*sqrt(3) assert cos(-5*pi/6) == -S.Half*sqrt(3) assert cos(1*pi/5) == (sqrt(5) + 1)/4 assert cos(2*pi/5) == (sqrt(5) - 1)/4 assert cos(3*pi/5) == -cos(2*pi/5) assert cos(4*pi/5) == -cos(1*pi/5) assert cos(6*pi/5) == -cos(1*pi/5) assert cos(8*pi/5) == cos(2*pi/5) assert cos(-1273*pi/5) == -cos(2*pi/5) assert cos(pi/8) == sqrt((2 + sqrt(2))/4) assert cos(104*pi/105) == -cos(pi/105) assert cos(106*pi/105) == -cos(pi/105) assert cos(-104*pi/105) == -cos(pi/105) assert cos(-106*pi/105) == -cos(pi/105) assert cos(x*I) == cosh(x) assert cos(k*pi*I) == cosh(k*pi) assert cos(r).is_real is True assert cos(k*pi) == (-1)**k assert cos(2*k*pi) == 1 for d in range(1, 22) + [60, 85]: for n in xrange(0, 2*d + 1): x = n*pi/d e = abs( float(cos(x)) - cos(float(x)) ) assert e < 1e-12
def test_issue1037(): assert cosh(asinh(Integer(3)/2)) == sqrt(Integer(13)/4)
def test_expr_to_holonomic(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') p = expr_to_holonomic((sin(x) / x)**2) q = HolonomicFunction(8*x + (4*x**2 + 6)*Dx + 6*x*Dx**2 + x**2*Dx**3, x, 0, \ [1, 0, -2/3]) assert p == q p = expr_to_holonomic(1 / (1 + x**2)**2) q = HolonomicFunction(4 * x + (x**2 + 1) * Dx, x, 0, [1]) assert p == q p = expr_to_holonomic(exp(x) * sin(x) + x * log(1 + x)) q = HolonomicFunction((2*x**3 + 10*x**2 + 20*x + 18) + (-2*x**4 - 10*x**3 - 20*x**2 \ - 18*x)*Dx + (2*x**5 + 6*x**4 + 7*x**3 + 8*x**2 + 10*x - 4)*Dx**2 + \ (-2*x**5 - 5*x**4 - 2*x**3 + 2*x**2 - x + 4)*Dx**3 + (x**5 + 2*x**4 - x**3 - \ 7*x**2/2 + x + 5/2)*Dx**4, x, 0, [0, 1, 4, -1]) assert p == q p = expr_to_holonomic(x * exp(x) + cos(x) + 1) q = HolonomicFunction((-x - 3)*Dx + (x + 2)*Dx**2 + (-x - 3)*Dx**3 + (x + 2)*Dx**4, x, \ 0, [2, 1, 1, 3]) assert p == q assert (x * exp(x) + cos(x) + 1).series(n=10) == p.series(n=10) p = expr_to_holonomic(log(1 + x)**2 + 1) q = HolonomicFunction( Dx + (3 * x + 3) * Dx**2 + (x**2 + 2 * x + 1) * Dx**3, x, 0, [1, 0, 2]) assert p == q p = expr_to_holonomic(erf(x)**2 + x) q = HolonomicFunction((8*x**4 - 2*x**2 + 2)*Dx**2 + (6*x**3 - x/2)*Dx**3 + \ (x**2+ 1/4)*Dx**4, x, 0, [0, 1, 8/pi, 0]) assert p == q p = expr_to_holonomic(cosh(x) * x) q = HolonomicFunction((-x**2 + 2) - 2 * x * Dx + x**2 * Dx**2, x, 0, [0, 1]) assert p == q p = expr_to_holonomic(besselj(2, x)) q = HolonomicFunction((x**2 - 4) + x * Dx + x**2 * Dx**2, x, 0, [0, 0]) assert p == q p = expr_to_holonomic(besselj(0, x) + exp(x)) q = HolonomicFunction((-x**2 - x/2 + 1/2) + (x**2 - x/2 - 3/2)*Dx + (-x**2 + x/2 + 1)*Dx**2 +\ (x**2 + x/2)*Dx**3, x, 0, [2, 1, 1/2]) assert p == q p = expr_to_holonomic(sin(x)**2 / x) q = HolonomicFunction(4 + 4 * x * Dx + 3 * Dx**2 + x * Dx**3, x, 0, [0, 1, 0]) assert p == q p = expr_to_holonomic(sin(x)**2 / x, x0=2) q = HolonomicFunction((4) + (4 * x) * Dx + (3) * Dx**2 + (x) * Dx**3, x, 2, [ sin(2)**2 / 2, sin(2) * cos(2) - sin(2)**2 / 4, -3 * sin(2)**2 / 4 + cos(2)**2 - sin(2) * cos(2) ]) assert p == q p = expr_to_holonomic(log(x) / 2 - Ci(2 * x) / 2 + Ci(2) / 2) q = HolonomicFunction(4*Dx + 4*x*Dx**2 + 3*Dx**3 + x*Dx**4, x, 0, \ [-log(2)/2 - EulerGamma/2 + Ci(2)/2, 0, 1, 0]) assert p == q p = p.to_expr() q = log(x) / 2 - Ci(2 * x) / 2 + Ci(2) / 2 assert p == q p = expr_to_holonomic(x**(S(1) / 2), x0=1) q = HolonomicFunction(x * Dx - 1 / 2, x, 1, [1]) assert p == q p = expr_to_holonomic(sqrt(1 + x**2)) q = HolonomicFunction((-x) + (x**2 + 1) * Dx, x, 0, [1]) assert p == q assert (expr_to_holonomic(sqrt(x) + sqrt(2*x)).to_expr()-\ (sqrt(x) + sqrt(2*x))).simplify() == 0 assert expr_to_holonomic(3 * x + 2 * sqrt(x)).to_expr() == 3 * x + 2 * sqrt(x) p = expr_to_holonomic((x**4 + x**3 + 5 * x**2 + 3 * x + 2) / x**2, lenics=3) q = HolonomicFunction((-2*x**4 - x**3 + 3*x + 4) + (x**5 + x**4 + 5*x**3 + 3*x**2 + \ 2*x)*Dx, x, 0, {-2: [2, 3, 5]}) assert p == q p = expr_to_holonomic(1 / (x - 1)**2, lenics=3, x0=1) q = HolonomicFunction((2) + (x - 1) * Dx, x, 1, {-2: [1, 0, 0]}) assert p == q
def test_coth_rewrite(): x = Symbol('x') assert coth(x).rewrite(exp) == (exp(x)+exp(-x))/(exp(x)-exp(-x)) 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_issue_8368(): assert meijerint_indefinite(cosh(x)*exp(-x*t), x) == ( (-t - 1)*exp(x) + (-t + 1)*exp(-x))*exp(-t*x)/2/(t**2 - 1)
def sech(x): return (sympy.cosh(x)**(-1))
def Product_of_Rotors(): Print_Function() (na,nb,nm,alpha,th,th_a,th_b) = symbols('n_a n_b n_m alpha theta theta_a theta_b',\ real = True) g = [[na, 0, alpha],[0, nm, 0],[alpha, 0, nb]] #metric tensor """ Values of metric tensor components [na,nm,nb] = [+1/-1,+1/-1,+1/-1] alpha = ea|eb """ (g3d, ea, em, eb) = Ga.build('e_a e_m e_b', g=g) print('g =',g3d.g) print(r'%n_{a} = \bm{e}_{a}^{2}\;\;n_{b} = \bm{e}_{b}^{2}\;\;n_{m} = \bm{e}_{m}^{2}'+\ r'\;\;\alpha = \bm{e}_{a}\cdot\bm{e}_{b}') (ca,cb,sa,sb) = symbols('c_a c_b s_a s_b',real=True) Ra = ca + sa*ea*em # Rotor for ea^em plane Rb = cb + sb*em*eb # Rotor for em^eb plane print(r'%\mbox{Rotor in }\bm{e}_{a}\bm{e}_{m}\mbox{ plane } R_{a} =',Ra) print(r'%\mbox{Rotor in }\bm{e}_{m}\bm{e}_{b}\mbox{ plane } R_{b} =',Rb) Rab = Ra*Rb # Compound Rotor """ Show that compound rotor is scalar plus bivector """ print(r'%R_{a}R_{b} = S+\bm{B} =', Rab) Rab2 = Rab.get_grade(2) print(r'%\bm{B} =',Rab2) Rab2sq = Rab2*Rab2 # Square of compound rotor bivector part Ssq = (Rab.scalar())**2 # Square of compound rotor scalar part Bsq = Rab2sq.scalar() print(r'%S^{2} =',Ssq) print(r'%\bm{B}^{2} =',Bsq) Dsq = (Ssq-Bsq).expand().simplify() print('%S^{2}-B^{2} =', Dsq) Dsq = Dsq.subs(nm**2,S(1)) # (e_m)**4 = 1 print('%S^{2}-B^{2} =', Dsq) Cases = [S(-1),S(1)] # -1/+1 squares for each basis vector print(r'#Consider all combinations of $\bm{e}_{a}^{2}$, $\bm{e}_{b}^{2}$'+\ r' and $\bm{e}_{m}^2$:') for Na in Cases: for Nb in Cases: for Nm in Cases: Ba_sq = -Na*Nm Bb_sq = -Nb*Nm if Ba_sq < 0: Ca_th = cos(th_a) Sa_th = sin(th_a) else: Ca_th = cosh(th_a) Sa_th = sinh(th_a) if Bb_sq < 0: Cb_th = cos(th_b) Sb_th = sin(th_b) else: Cb_th = cosh(th_b) Sb_th = sinh(th_b) print(r'%\left [ \bm{e}_{a}^{2},\bm{e}_{b}^{2},\bm{e}_{m}^2\right ] =',\ [Na,Nb,Nm]) Dsq_tmp = Dsq.subs({ca:Ca_th,sa:Sa_th,cb:Cb_th,sb:Sb_th,na:Na,nb:Nb,nm:Nm}) print(r'%S^{2}-\bm{B}^{2} =',Dsq_tmp,' =',trigsimp(Dsq_tmp)) print(r'#Thus we have shown that $R_{a}R_{b} = S+\bm{D} = e^{\bm{C}}$ where $\bm{C}$'+\ r' is a bivector blade.') return
def test_Airy(): """Compare finite depth Airy wave expression with results from analytical expression""" try: import sympy as sp from sympy.abc import t from sympy.physics.vector import ReferenceFrame from sympy.physics.vector import gradient, divergence R = ReferenceFrame('R') x, y, z = R[0], R[1], R[2] Phi, k, h, g, rho = sp.symbols("Phi, k, h, g, rho") omega = sp.sqrt(g * k * sp.tanh(k * h)) phi = g / omega * sp.cosh(k * (z + h)) / sp.cosh( k * h) * sp.sin(k * x - omega * t) u = gradient(phi, R) p = -rho * Phi.diff(t) for depth in np.linspace(100.0, 10.0, 2): for omega in np.linspace(0.5, 4.0, 2): dp = DiffractionProblem(dummy, free_surface=0.0, sea_bottom=-depth, omega=omega) for t_val, x_val, y_val, z_val in product( np.linspace(0.0, 1.0, 2), np.linspace(-10, 10, 3), np.linspace(-10, 10, 3), np.linspace(-10, 0, 3)): parameters = { t: t_val, x: x_val, y: y_val, z: z_val, omega: dp.omega, k: dp.wavenumber, h: dp.depth, g: dp.g, rho: dp.rho } phi_num = dp.Airy_wave_potential( np.array((x_val, y_val, z_val))) assert np.isclose(float(phi.subs(parameters)), np.real(phi_num * np.exp(-1j * dp.omega * t_val)), rtol=1e-3) u_num = dp.Airy_wave_velocity( np.array((x_val, y_val, z_val))) assert np.isclose(float(u.dot(R.x).subs(parameters)), np.real(u_num[0] * np.exp(-1j * dp.omega * t_val)), rtol=1e-3) assert np.isclose(float(u.dot(R.y).subs(parameters)), np.real(u_num[1] * np.exp(-1j * dp.omega * t_val)), rtol=1e-3) assert np.isclose(float(u.dot(R.z).subs(parameters)), np.real(u_num[2] * np.exp(-1j * dp.omega * t_val)), rtol=1e-3) except ImportError: print("Not tested with sympy.")
def test_intrinsic_math_codegen(): # not included: log10 from sympy import (acos, asin, atan, ceiling, cos, cosh, floor, log, ln, sin, sinh, sqrt, tan, tanh, N, Abs) x = symbols('x') name_expr = [ ("test_abs", 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, "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_issue_17421(): assert N(acos(-I + acosh(cosh(cosh(1) + I)))) == 1.0 * I
def test_issue_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_cos(): x, y = symbols('x,y') r = Symbol('r', real=True) k = Symbol('k', integer=True) assert cos(nan) == nan assert cos(oo * I) == oo assert cos(-oo * I) == oo assert cos(0) == 1 assert cos(acos(x)) == x assert cos(atan(x)) == 1 / sqrt(1 + x**2) assert cos(asin(x)) == sqrt(1 - x**2) assert cos(acot(x)) == 1 / sqrt(1 + 1 / x**2) assert cos(pi * I) == cosh(pi) assert cos(-pi * I) == cosh(pi) assert cos(-2 * I) == cosh(2) assert cos(pi / 2) == 0 assert cos(-pi / 2) == 0 assert cos(pi / 2) == 0 assert cos(-pi / 2) == 0 assert cos((-3 * 10**73 + 1) * pi / 2) == 0 assert cos((7 * 10**103 + 1) * pi / 2) == 0 assert cos(pi) == -1 assert cos(-pi) == -1 assert cos(2 * pi) == 1 assert cos(5 * pi) == -1 assert cos(8 * pi) == 1 assert cos(pi / 3) == S.Half assert cos(-2 * pi / 3) == -S.Half assert cos(pi / 4) == S.Half * sqrt(2) assert cos(-pi / 4) == S.Half * sqrt(2) assert cos(11 * pi / 4) == -S.Half * sqrt(2) assert cos(-3 * pi / 4) == -S.Half * sqrt(2) assert cos(pi / 6) == S.Half * sqrt(3) assert cos(-pi / 6) == S.Half * sqrt(3) assert cos(7 * pi / 6) == -S.Half * sqrt(3) assert cos(-5 * pi / 6) == -S.Half * sqrt(3) assert cos(1 * pi / 5) == (sqrt(5) + 1) / 4 assert cos(2 * pi / 5) == (sqrt(5) - 1) / 4 assert cos(3 * pi / 5) == -cos(2 * pi / 5) assert cos(4 * pi / 5) == -cos(1 * pi / 5) assert cos(6 * pi / 5) == -cos(1 * pi / 5) assert cos(8 * pi / 5) == cos(2 * pi / 5) assert cos(-1273 * pi / 5) == -cos(2 * pi / 5) assert cos(104 * pi / 105) == -cos(pi / 105) assert cos(106 * pi / 105) == -cos(pi / 105) assert cos(-104 * pi / 105) == -cos(pi / 105) assert cos(-106 * pi / 105) == -cos(pi / 105) assert cos(x * I) == cosh(x) assert cos(k * pi * I) == cosh(k * pi) assert cos(r).is_real == True assert cos(k * pi) == (-1)**k assert cos(2 * k * pi) == 1
def test_cosh(): x, y = symbols('x,y') k = Symbol('k', integer=True) assert cosh(nan) == nan assert cosh(zoo) == nan assert cosh(oo) == oo assert cosh(-oo) == oo assert cosh(0) == 1 assert cosh(1) == cosh(1) assert cosh(-1) == cosh(1) assert cosh(x) == cosh(x) assert cosh(-x) == cosh(x) assert cosh(pi*I) == cos(pi) assert cosh(-pi*I) == cos(pi) assert cosh(2**1024 * E) == cosh(2**1024 * E) assert cosh(-2**1024 * E) == cosh(2**1024 * E) assert cosh(pi*I/2) == 0 assert cosh(-pi*I/2) == 0 assert cosh(pi*I/2) == 0 assert cosh(-pi*I/2) == 0 assert cosh((-3*10**73+1)*pi*I/2) == 0 assert cosh((7*10**103+1)*pi*I/2) == 0 assert cosh(pi*I) == -1 assert cosh(-pi*I) == -1 assert cosh(5*pi*I) == -1 assert cosh(8*pi*I) == 1 assert cosh(pi*I/3) == S.Half assert cosh(-2*pi*I/3) == -S.Half assert cosh(pi*I/4) == S.Half*sqrt(2) assert cosh(-pi*I/4) == S.Half*sqrt(2) assert cosh(11*pi*I/4) == -S.Half*sqrt(2) assert cosh(-3*pi*I/4) == -S.Half*sqrt(2) assert cosh(pi*I/6) == S.Half*sqrt(3) assert cosh(-pi*I/6) == S.Half*sqrt(3) assert cosh(7*pi*I/6) == -S.Half*sqrt(3) assert cosh(-5*pi*I/6) == -S.Half*sqrt(3) assert cosh(pi*I/105) == cos(pi/105) assert cosh(-pi*I/105) == cos(pi/105) assert cosh(2 + 3*I) == cosh(2 + 3*I) assert cosh(x*I) == cos(x) assert cosh(k*pi*I) == cos(k*pi) assert cosh(17*k*pi*I) == cos(17*k*pi) assert cosh(k*pi) == cosh(k*pi)
def test_tanh(): x, y = symbols('x,y') k = Symbol('k', integer=True) assert tanh(nan) is nan assert tanh(zoo) is nan assert tanh(oo) == 1 assert tanh(-oo) == -1 assert tanh(0) == 0 assert unchanged(tanh, 1) assert tanh(-1) == -tanh(1) assert unchanged(tanh, x) assert tanh(-x) == -tanh(x) assert unchanged(tanh, pi) assert tanh(-pi) == -tanh(pi) assert unchanged(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) is zoo assert tanh(-pi * I / 2) is zoo assert tanh(pi * I * Rational(5, 2)) is zoo assert tanh(pi * I * Rational(7, 2)) is zoo assert tanh(pi * I / 3) == sqrt(3) * I assert tanh(pi * I * Rational(-2, 3)) == sqrt(3) * I assert tanh(pi * I / 4) == I assert tanh(-pi * I / 4) == -I assert tanh(pi * I * Rational(17, 4)) == I assert tanh(pi * I * Rational(-3, 4)) == I assert tanh(pi * I / 6) == I / sqrt(3) assert tanh(-pi * I / 6) == -I / sqrt(3) assert tanh(pi * I * Rational(7, 6)) == I / sqrt(3) assert tanh(pi * I * Rational(-5, 6)) == I / sqrt(3) assert tanh(pi * I / 105) == tan(pi / 105) * I assert tanh(-pi * I / 105) == -tan(pi / 105) * I assert unchanged(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 assert tanh(x).as_real_imag( deep=False) == (sinh(re(x)) * cosh(re(x)) / (cos(im(x))**2 + sinh(re(x))**2), sin(im(x)) * cos(im(x)) / (cos(im(x))**2 + sinh(re(x))**2)) x = Symbol('x', extended_real=True) assert tanh(x).as_real_imag(deep=False) == (tanh(x), 0) assert tanh(I * pi / 3 + 1).is_real is False assert tanh(x).is_real is True assert tanh(I * pi * x / 2).is_real is None
def test_cosh(): x, y = symbols('x,y') k = Symbol('k', integer=True) assert cosh(nan) is nan assert cosh(zoo) is nan assert cosh(oo) is oo assert cosh(-oo) is oo assert cosh(0) == 1 assert unchanged(cosh, 1) assert cosh(-1) == cosh(1) assert unchanged(cosh, x) assert cosh(-x) == cosh(x) assert cosh(pi * I) == cos(pi) assert cosh(-pi * I) == cos(pi) assert unchanged(cosh, 2**1024 * E) assert cosh(-2**1024 * E) == cosh(2**1024 * E) assert cosh(pi * I / 2) == 0 assert cosh(-pi * I / 2) == 0 assert cosh((-3 * 10**73 + 1) * pi * I / 2) == 0 assert cosh((7 * 10**103 + 1) * pi * I / 2) == 0 assert cosh(pi * I) == -1 assert cosh(-pi * I) == -1 assert cosh(5 * pi * I) == -1 assert cosh(8 * pi * I) == 1 assert cosh(pi * I / 3) == S.Half assert cosh(pi * I * Rational(-2, 3)) == Rational(-1, 2) assert cosh(pi * I / 4) == S.Half * sqrt(2) assert cosh(-pi * I / 4) == S.Half * sqrt(2) assert cosh(pi * I * Rational(11, 4)) == Rational(-1, 2) * sqrt(2) assert cosh(pi * I * Rational(-3, 4)) == Rational(-1, 2) * sqrt(2) assert cosh(pi * I / 6) == S.Half * sqrt(3) assert cosh(-pi * I / 6) == S.Half * sqrt(3) assert cosh(pi * I * Rational(7, 6)) == Rational(-1, 2) * sqrt(3) assert cosh(pi * I * Rational(-5, 6)) == Rational(-1, 2) * sqrt(3) assert cosh(pi * I / 105) == cos(pi / 105) assert cosh(-pi * I / 105) == cos(pi / 105) assert unchanged(cosh, 2 + 3 * I) assert cosh(x * I) == cos(x) assert cosh(k * pi * I) == cos(k * pi) assert cosh(17 * k * pi * I) == cos(17 * k * pi) assert unchanged(cosh, k * pi) assert cosh(x).as_real_imag(deep=False) == (cos(im(x)) * cosh(re(x)), sin(im(x)) * sinh(re(x))) x = Symbol('x', extended_real=True) assert cosh(x).as_real_imag(deep=False) == (cosh(x), 0) x = Symbol('x', real=True) assert cosh(I * x).is_finite is True
def test_issue_2137(): assert (cos(1+I)**3).as_real_imag() == (-3*sin(1)**2*sinh(1)**2*cos(1)*cosh(1) + cos(1)**3*cosh(1)**3, -3*cos(1)**2*cosh(1)**2*sin(1)*sinh(1) + sin(1)**3*sinh(1)**3)
def test_acosh(): x = Symbol('x') assert unchanged(acosh, -x) #at specific points assert acosh(1) == 0 assert acosh(-1) == pi * I assert acosh(0) == I * pi / 2 assert acosh(S.Half) == I * pi / 3 assert acosh(Rational(-1, 2)) == pi * I * Rational(2, 3) assert acosh(nan) is nan # at infinites assert acosh(oo) is oo assert acosh(-oo) is oo assert acosh(I * oo) == oo + I * pi / 2 assert acosh(-I * oo) == oo - I * pi / 2 assert acosh(zoo) is zoo assert acosh(I) == log(I * (1 + sqrt(2))) assert acosh(-I) == log(-I * (1 + sqrt(2))) assert acosh((sqrt(3) - 1) / (2 * sqrt(2))) == pi * I * Rational(5, 12) assert acosh(-(sqrt(3) - 1) / (2 * sqrt(2))) == pi * I * Rational(7, 12) assert acosh(sqrt(2) / 2) == I * pi / 4 assert acosh(-sqrt(2) / 2) == I * pi * Rational(3, 4) assert acosh(sqrt(3) / 2) == I * pi / 6 assert acosh(-sqrt(3) / 2) == I * pi * Rational(5, 6) assert acosh(sqrt(2 + sqrt(2)) / 2) == I * pi / 8 assert acosh(-sqrt(2 + sqrt(2)) / 2) == I * pi * Rational(7, 8) assert acosh(sqrt(2 - sqrt(2)) / 2) == I * pi * Rational(3, 8) assert acosh(-sqrt(2 - sqrt(2)) / 2) == I * pi * Rational(5, 8) assert acosh((1 + sqrt(3)) / (2 * sqrt(2))) == I * pi / 12 assert acosh(-(1 + sqrt(3)) / (2 * sqrt(2))) == I * pi * Rational(11, 12) assert acosh((sqrt(5) + 1) / 4) == I * pi / 5 assert acosh(-(sqrt(5) + 1) / 4) == I * pi * Rational(4, 5) assert str(acosh(5 * I).n(6)) == '2.31244 + 1.5708*I' assert str(acosh(-5 * I).n(6)) == '2.31244 - 1.5708*I' # inverse composition assert unchanged(acosh, Symbol('v1')) assert acosh(cosh(-3, evaluate=False)) == 3 assert acosh(cosh(3, evaluate=False)) == 3 assert acosh(cosh(0, evaluate=False)) == 0 assert acosh(cosh(I, evaluate=False)) == I assert acosh(cosh(-I, evaluate=False)) == I assert acosh(cosh(7 * I, evaluate=False)) == -2 * I * pi + 7 * I assert acosh(cosh(1 + I)) == 1 + I assert acosh(cosh(3 - 3 * I)) == 3 - 3 * I assert acosh(cosh(-3 + 2 * I)) == 3 - 2 * I assert acosh(cosh(-5 - 17 * I)) == 5 - 6 * I * pi + 17 * I assert acosh(cosh(-21 + 11 * I)) == 21 - 11 * I + 4 * I * pi assert acosh(cosh(cosh(1) + I)) == cosh(1) + I
def test_expand(): assert expand_func(besselj(S.Half, z).rewrite(jn)) == \ sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z)) assert expand_func(bessely(S.Half, z).rewrite(yn)) == \ -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z)) # XXX: teach sin/cos to work around arguments like # x*exp_polar(I*pi*n/2). Then change besselsimp -> expand_func assert besselsimp(besselj(S.Half, z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besselj(Rational(-1, 2), z)) == sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besselj(Rational(5, 2), z)) == \ -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(besselj(Rational(-5, 2), z)) == \ -sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(bessely(S.Half, z)) == \ -(sqrt(2)*cos(z))/(sqrt(pi)*sqrt(z)) assert besselsimp(bessely(Rational(-1, 2), z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(bessely(Rational(5, 2), z)) == \ sqrt(2)*(z**2*cos(z) - 3*z*sin(z) - 3*cos(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(bessely(Rational(-5, 2), z)) == \ -sqrt(2)*(z**2*sin(z) + 3*z*cos(z) - 3*sin(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(besseli(S.Half, z)) == sqrt(2) * sinh(z) / (sqrt(pi) * sqrt(z)) assert besselsimp(besseli(Rational(-1, 2), z)) == \ sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z)) assert besselsimp(besseli(Rational(5, 2), z)) == \ sqrt(2)*(z**2*sinh(z) - 3*z*cosh(z) + 3*sinh(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(besseli(Rational(-5, 2), z)) == \ sqrt(2)*(z**2*cosh(z) - 3*z*sinh(z) + 3*cosh(z))/(sqrt(pi)*z**Rational(5, 2)) assert besselsimp(besselk(S.Half, z)) == \ besselsimp(besselk(Rational(-1, 2), z)) == sqrt(pi)*exp(-z)/(sqrt(2)*sqrt(z)) assert besselsimp(besselk(Rational(5, 2), z)) == \ besselsimp(besselk(Rational(-5, 2), z)) == \ sqrt(2)*sqrt(pi)*(z**2 + 3*z + 3)*exp(-z)/(2*z**Rational(5, 2)) n = Symbol('n', integer=True, positive=True) assert expand_func(besseli(n + 2, z)) == \ besseli(n, z) + (-2*n - 2)*(-2*n*besseli(n, z)/z + besseli(n - 1, z))/z assert expand_func(besselj(n + 2, z)) == \ -besselj(n, z) + (2*n + 2)*(2*n*besselj(n, z)/z - besselj(n - 1, z))/z assert expand_func(besselk(n + 2, z)) == \ besselk(n, z) + (2*n + 2)*(2*n*besselk(n, z)/z + besselk(n - 1, z))/z assert expand_func(bessely(n + 2, z)) == \ -bessely(n, z) + (2*n + 2)*(2*n*bessely(n, z)/z - bessely(n - 1, z))/z assert expand_func(besseli(n + S.Half, z).rewrite(jn)) == \ (sqrt(2)*sqrt(z)*exp(-I*pi*(n + S.Half)/2) * exp_polar(I*pi/4)*jn(n, z*exp_polar(I*pi/2))/sqrt(pi)) assert expand_func(besselj(n + S.Half, z).rewrite(jn)) == \ sqrt(2)*sqrt(z)*jn(n, z)/sqrt(pi) r = Symbol('r', real=True) p = Symbol('p', positive=True) i = Symbol('i', integer=True) for besselx in [besselj, bessely, besseli, besselk]: assert besselx(i, p).is_extended_real is True assert besselx(i, x).is_extended_real is None assert besselx(x, z).is_extended_real is None for besselx in [besselj, besseli]: assert besselx(i, r).is_extended_real is True for besselx in [bessely, besselk]: assert besselx(i, r).is_extended_real is None for besselx in [besselj, bessely, besseli, besselk]: assert expand_func(besselx(oo, x)) == besselx(oo, x, evaluate=False) assert expand_func(besselx(-oo, x)) == besselx(-oo, x, evaluate=False)
def test_moment_generating_function(): t = symbols('t', positive=True) # Symbolic tests a, b, c = symbols('a b c') mgf = moment_generating_function(Beta('x', a, b))(t) assert mgf == hyper((a, ), (a + b, ), t) mgf = moment_generating_function(Chi('x', a))(t) assert mgf == sqrt(2)*t*gamma(a/2 + S.Half)*\ hyper((a/2 + S.Half,), (Rational(3, 2),), t**2/2)/gamma(a/2) +\ hyper((a/2,), (S.Half,), t**2/2) mgf = moment_generating_function(ChiSquared('x', a))(t) assert mgf == (1 - 2 * t)**(-a / 2) mgf = moment_generating_function(Erlang('x', a, b))(t) assert mgf == (1 - t / b)**(-a) mgf = moment_generating_function(ExGaussian("x", a, b, c))(t) assert mgf == exp(a * t + b**2 * t**2 / 2) / (1 - t / c) mgf = moment_generating_function(Exponential('x', a))(t) assert mgf == a / (a - t) mgf = moment_generating_function(Gamma('x', a, b))(t) assert mgf == (-b * t + 1)**(-a) mgf = moment_generating_function(Gumbel('x', a, b))(t) assert mgf == exp(b * t) * gamma(-a * t + 1) mgf = moment_generating_function(Gompertz('x', a, b))(t) assert mgf == b * exp(b) * expint(t / a, b) mgf = moment_generating_function(Laplace('x', a, b))(t) assert mgf == exp(a * t) / (-b**2 * t**2 + 1) mgf = moment_generating_function(Logistic('x', a, b))(t) assert mgf == exp(a * t) * beta(-b * t + 1, b * t + 1) mgf = moment_generating_function(Normal('x', a, b))(t) assert mgf == exp(a * t + b**2 * t**2 / 2) mgf = moment_generating_function(Pareto('x', a, b))(t) assert mgf == b * (-a * t)**b * uppergamma(-b, -a * t) mgf = moment_generating_function(QuadraticU('x', a, b))(t) assert str(mgf) == ( "(3*(t*(-4*b + (a + b)**2) + 4)*exp(b*t) - " "3*(t*(a**2 + 2*a*(b - 2) + b**2) + 4)*exp(a*t))/(t**2*(a - b)**3)") mgf = moment_generating_function(RaisedCosine('x', a, b))(t) assert mgf == pi**2 * exp(a * t) * sinh(b * t) / (b * t * (b**2 * t**2 + pi**2)) mgf = moment_generating_function(Rayleigh('x', a))(t) assert mgf == sqrt(2)*sqrt(pi)*a*t*(erf(sqrt(2)*a*t/2) + 1)\ *exp(a**2*t**2/2)/2 + 1 mgf = moment_generating_function(Triangular('x', a, b, c))(t) assert str(mgf) == ("(-2*(-a + b)*exp(c*t) + 2*(-a + c)*exp(b*t) + " "2*(b - c)*exp(a*t))/(t**2*(-a + b)*(-a + c)*(b - c))") mgf = moment_generating_function(Uniform('x', a, b))(t) assert mgf == (-exp(a * t) + exp(b * t)) / (t * (-a + b)) mgf = moment_generating_function(UniformSum('x', a))(t) assert mgf == ((exp(t) - 1) / t)**a mgf = moment_generating_function(WignerSemicircle('x', a))(t) assert mgf == 2 * besseli(1, a * t) / (a * t) # Numeric tests mgf = moment_generating_function(Beta('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == hyper((2, ), (3, ), 1) / 2 mgf = moment_generating_function(Chi('x', 1))(t) assert mgf.diff(t).subs(t, 1) == sqrt(2) * hyper( (1, ), (Rational(3, 2), ), S.Half) / sqrt(pi) + hyper( (Rational(3, 2), ), (Rational(3, 2), ), S.Half) + 2 * sqrt(2) * hyper( (2, ), (Rational(5, 2), ), S.Half) / (3 * sqrt(pi)) mgf = moment_generating_function(ChiSquared('x', 1))(t) assert mgf.diff(t).subs(t, 1) == I mgf = moment_generating_function(Erlang('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(ExGaussian("x", 0, 1, 1))(t) assert mgf.diff(t).subs(t, 2) == -exp(2) mgf = moment_generating_function(Exponential('x', 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Gamma('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Gumbel('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == EulerGamma + 1 mgf = moment_generating_function(Gompertz('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == -e * meijerg(((), (1, 1)), ((0, 0, 0), ()), 1) mgf = moment_generating_function(Laplace('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Logistic('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == beta(1, 1) mgf = moment_generating_function(Normal('x', 0, 1))(t) assert mgf.diff(t).subs(t, 1) == exp(S.Half) mgf = moment_generating_function(Pareto('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == expint(1, 0) mgf = moment_generating_function(QuadraticU('x', 1, 2))(t) assert mgf.diff(t).subs(t, 1) == -12 * e - 3 * exp(2) mgf = moment_generating_function(RaisedCosine('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == -2*e*pi**2*sinh(1)/\ (1 + pi**2)**2 + e*pi**2*cosh(1)/(1 + pi**2) mgf = moment_generating_function(Rayleigh('x', 1))(t) assert mgf.diff(t).subs(t, 0) == sqrt(2) * sqrt(pi) / 2 mgf = moment_generating_function(Triangular('x', 1, 3, 2))(t) assert mgf.diff(t).subs(t, 1) == -e + exp(3) mgf = moment_generating_function(Uniform('x', 0, 1))(t) assert mgf.diff(t).subs(t, 1) == 1 mgf = moment_generating_function(UniformSum('x', 1))(t) assert mgf.diff(t).subs(t, 1) == 1 mgf = moment_generating_function(WignerSemicircle('x', 1))(t) assert mgf.diff(t).subs(t, 1) == -2*besseli(1, 1) + besseli(2, 1) +\ besseli(0, 1)