def test_airybiprime(): z = Symbol('z', real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airybiprime(z), airybiprime) assert airybiprime(0) == 3**(S(1)/6)/gamma(S(1)/3) assert airybiprime(oo) == oo assert airybiprime(-oo) == 0 assert diff(airybiprime(z), z) == z*airybi(z) assert series(airybiprime(z), z, 0, 3) == ( 3**(S(1)/6)/gamma(S(1)/3) + 3**(S(5)/6)*z**2/(6*gamma(S(2)/3)) + O(z**3)) assert airybiprime(z).rewrite(hyper) == ( 3**(S(5)/6)*z**2*hyper((), (S(5)/3,), z**S(3)/9)/(6*gamma(S(2)/3)) + 3**(S(1)/6)*hyper((), (S(1)/3,), z**S(3)/9)/gamma(S(1)/3)) assert isinstance(airybiprime(z).rewrite(besselj), airybiprime) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-S(1)/3, 2*(-t)**(S(3)/2)/3) + besselj(S(1)/3, 2*(-t)**(S(3)/2)/3))/3) assert airybiprime(z).rewrite(besseli) == ( sqrt(3)*(z**2*besseli(S(2)/3, 2*z**(S(3)/2)/3)/(z**(S(3)/2))**(S(2)/3) + (z**(S(3)/2))**(S(2)/3)*besseli(-S(2)/3, 2*z**(S(3)/2)/3))/3) assert airybiprime(p).rewrite(besseli) == ( sqrt(3)*p*(besseli(-S(2)/3, 2*p**(S(3)/2)/3) + besseli(S(2)/3, 2*p**(S(3)/2)/3))/3) assert expand_func(airybiprime(2*(3*z**5)**(S(1)/3))) == ( sqrt(3)*(z**(S(5)/3)/(z**5)**(S(1)/3) - 1)*airyaiprime(2*3**(S(1)/3)*z**(S(5)/3))/2 + (z**(S(5)/3)/(z**5)**(S(1)/3) + 1)*airybiprime(2*3**(S(1)/3)*z**(S(5)/3))/2)
def test_diff(): assert besselj(n, z).diff(z) == besselj(n - 1, z)/2 - besselj(n + 1, z)/2 assert bessely(n, z).diff(z) == bessely(n - 1, z)/2 - bessely(n + 1, z)/2 assert besseli(n, z).diff(z) == besseli(n - 1, z)/2 + besseli(n + 1, z)/2 assert besselk(n, z).diff(z) == -besselk(n - 1, z)/2 - besselk(n + 1, z)/2 assert hankel1(n, z).diff(z) == hankel1(n - 1, z)/2 - hankel1(n + 1, z)/2 assert hankel2(n, z).diff(z) == hankel2(n - 1, z)/2 - hankel2(n + 1, z)/2
def test_to_hyper(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') p = HolonomicFunction(Dx - 2, x, 0, [3]).to_hyper() q = 3 * hyper([], [], 2*x) assert p == q p = hyperexpand(HolonomicFunction((1 + x) * Dx - 3, x, 0, [2]).to_hyper()).expand() q = 2*x**3 + 6*x**2 + 6*x + 2 assert p == q p = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1]).to_hyper() q = -x**2*hyper((2, 2, 1), (2, 3), -x)/2 + x assert p == q p = HolonomicFunction(2*x*Dx + Dx**2, x, 0, [0, 2/sqrt(pi)]).to_hyper() q = 2*x*hyper((1/2,), (3/2,), -x**2)/sqrt(pi) assert p == q p = hyperexpand(HolonomicFunction(2*x*Dx + Dx**2, x, 0, [1, -2/sqrt(pi)]).to_hyper()) q = erfc(x) assert p.rewrite(erfc) == q p = hyperexpand(HolonomicFunction((x**2 - 1) + x*Dx + x**2*Dx**2, x, 0, [0, S(1)/2]).to_hyper()) q = besselj(1, x) assert p == q p = hyperexpand(HolonomicFunction(x*Dx**2 + Dx + x, x, 0, [1, 0]).to_hyper()) q = besselj(0, x) assert p == q
def test_airyai(): z = Symbol('z', real=False) t = Symbol('t', negative=True) p = Symbol('p', positive=True) assert isinstance(airyai(z), airyai) assert airyai(0) == 3**(S(1)/3)/(3*gamma(S(2)/3)) assert airyai(oo) == 0 assert airyai(-oo) == 0 assert diff(airyai(z), z) == airyaiprime(z) assert series(airyai(z), z, 0, 3) == ( 3**(S(5)/6)*gamma(S(1)/3)/(6*pi) - 3**(S(1)/6)*z*gamma(S(2)/3)/(2*pi) + O(z**3)) assert airyai(z).rewrite(hyper) == ( -3**(S(2)/3)*z*hyper((), (S(4)/3,), z**S(3)/9)/(3*gamma(S(1)/3)) + 3**(S(1)/3)*hyper((), (S(2)/3,), z**S(3)/9)/(3*gamma(S(2)/3))) assert isinstance(airyai(z).rewrite(besselj), airyai) assert airyai(t).rewrite(besselj) == ( sqrt(-t)*(besselj(-S(1)/3, 2*(-t)**(S(3)/2)/3) + besselj(S(1)/3, 2*(-t)**(S(3)/2)/3))/3) assert airyai(z).rewrite(besseli) == ( -z*besseli(S(1)/3, 2*z**(S(3)/2)/3)/(3*(z**(S(3)/2))**(S(1)/3)) + (z**(S(3)/2))**(S(1)/3)*besseli(-S(1)/3, 2*z**(S(3)/2)/3)/3) assert airyai(p).rewrite(besseli) == ( sqrt(p)*(besseli(-S(1)/3, 2*p**(S(3)/2)/3) - besseli(S(1)/3, 2*p**(S(3)/2)/3))/3) assert expand_func(airyai(2*(3*z**5)**(S(1)/3))) == ( -sqrt(3)*(-1 + (z**5)**(S(1)/3)/z**(S(5)/3))*airybi(2*3**(S(1)/3)*z**(S(5)/3))/6 + (1 + (z**5)**(S(1)/3)/z**(S(5)/3))*airyai(2*3**(S(1)/3)*z**(S(5)/3))/2)
def test_messy(): from sympy import (laplace_transform, Si, Shi, Chi, atan, Piecewise, acoth, E1, besselj, acosh, asin, And, re, fourier_transform, sqrt) assert laplace_transform(Si(x), x, s) == ((-atan(s) + pi/2)/s, 0, True) assert laplace_transform(Shi(x), x, s) == (acoth(s)/s, 1, True) # where should the logs be simplified? assert laplace_transform(Chi(x), x, s) == \ ((log(s**(-2)) - log((s**2 - 1)/s**2))/(2*s), 1, True) # TODO maybe simplify the inequalities? assert laplace_transform(besselj(a, x), x, s)[1:] == \ (0, And(S(0) < re(a/2) + S(1)/2, S(0) < re(a/2) + 1)) # NOTE s < 0 can be done, but argument reduction is not good enough yet assert fourier_transform(besselj(1, x)/x, x, s, noconds=False) == \ (Piecewise((0, 4*abs(pi**2*s**2) > 1), (2*sqrt(-4*pi**2*s**2 + 1), True)), s > 0) # TODO FT(besselj(0,x)) - conditions are messy (but for acceptable reasons) # - folding could be better assert integrate(E1(x)*besselj(0, x), (x, 0, oo), meijerg=True) == \ log(1 + sqrt(2)) assert integrate(E1(x)*besselj(1, x), (x, 0, oo), meijerg=True) == \ log(S(1)/2 + sqrt(2)/2) assert integrate(1/x/sqrt(1 - x**2), x, meijerg=True) == \ Piecewise((-acosh(1/x), 1 < abs(x**(-2))), (I*asin(1/x), True))
def test_from_sympy(): x = symbols("x") R, Dx = DifferentialOperators(QQ.old_poly_ring(x), "Dx") p = from_sympy((sin(x) / x) ** 2) q = HolonomicFunction( 8 * x + (4 * x ** 2 + 6) * Dx + 6 * x * Dx ** 2 + x ** 2 * Dx ** 3, x, 1, [sin(1) ** 2, -2 * sin(1) ** 2 + 2 * sin(1) * cos(1), -8 * sin(1) * cos(1) + 2 * cos(1) ** 2 + 4 * sin(1) ** 2], ) assert p == q p = from_sympy(1 / (1 + x ** 2) ** 2) q = HolonomicFunction(4 * x + (x ** 2 + 1) * Dx, x, 0, 1) assert p == q p = from_sympy(exp(x) * sin(x) + x * log(1 + x)) q = HolonomicFunction( (4 * x ** 3 + 20 * x ** 2 + 40 * x + 36) + (-4 * x ** 4 - 20 * x ** 3 - 40 * x ** 2 - 36 * x) * Dx + (4 * x ** 5 + 12 * x ** 4 + 14 * x ** 3 + 16 * x ** 2 + 20 * x - 8) * Dx ** 2 + (-4 * x ** 5 - 10 * x ** 4 - 4 * x ** 3 + 4 * x ** 2 - 2 * x + 8) * Dx ** 3 + (2 * x ** 5 + 4 * x ** 4 - 2 * x ** 3 - 7 * x ** 2 + 2 * x + 5) * Dx ** 4, x, 0, [0, 1, 4, -1], ) assert p == q p = from_sympy(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 = from_sympy(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 = from_sympy(erf(x) ** 2 + x) q = HolonomicFunction( (32 * x ** 4 - 8 * x ** 2 + 8) * Dx ** 2 + (24 * x ** 3 - 2 * x) * Dx ** 3 + (4 * x ** 2 + 1) * Dx ** 4, x, 0, [0, 1, 8 / pi, 0], ) assert p == q p = from_sympy(cosh(x) * x) q = HolonomicFunction((-x ** 2 + 2) - 2 * x * Dx + x ** 2 * Dx ** 2, x, 0, [0, 1]) assert p == q p = from_sympy(besselj(2, x)) q = HolonomicFunction((x ** 2 - 4) + x * Dx + x ** 2 * Dx ** 2, x, 0, [0, 0]) assert p == q p = from_sympy(besselj(0, x) + exp(x)) q = HolonomicFunction( (-2 * x ** 2 - x + 1) + (2 * x ** 2 - x - 3) * Dx + (-2 * x ** 2 + x + 2) * Dx ** 2 + (2 * x ** 2 + x) * Dx ** 3, x, 0, [2, 1, 1 / 2], ) assert p == q
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
def test_rewrite(): from sympy import polar_lift, exp, I assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - S(1)/2, z) assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - S(1)/2, z) assert besseli(n, z).rewrite(besselj) == \ exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z) assert besselj(n, z).rewrite(besseli) == \ exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z) nu = randcplx() assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z)
def test_laplace_transform(): LT = laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') w = Symbol("w") f = Function("f") # Test unevaluated form assert laplace_transform(f(t), t, w) == LaplaceTransform(f(t), t, w) assert inverse_laplace_transform(f(w), w, t, plane=0) == InverseLaplaceTransform(f(w), w, t, 0) # test a bug spos = symbols('s', positive=True) assert LT(exp(t), t, spos)[:2] == (1/(spos - 1), True) # basic tests from wikipedia assert LT((t-a)**b*exp(-c*(t-a))*Heaviside(t-a), t, s) \ == ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1)*gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1/s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a*s)/s, 0, True) assert LT(1 - exp(-a*t), t, s) == (a/(s*(a + s)), 0, True) assert LT((exp(2*t)-1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[:2] == (1/(s-1), 1) assert LT(exp(2*t), t, s)[:2] == (1/(s-2), 2) assert LT(exp(a*t), t, s)[:2] == (1/(s-a), a) assert LT(log(t/a), t, s) == ((log(a) + log(s) + EulerGamma)/(-s), 0, True) assert LT(erf(t), t, s) == ((-erf(s/2) + 1)*exp(s**2/4)/s, 0, True) assert LT(sin(a*t), t, s) == (a/(a**2 + s**2), 0, True) assert LT(cos(a*t), t, s) == (s/(a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a*t)*sin(b*t), t, s) == (1/b/(1 + (a + s)**2/b**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ (1/(s + a)/(1 + b**2/(a + s)**2), -a, True) # TODO sinh, cosh have delicate cancellation assert LT(besselj(0, t), t, s) == (1/sqrt(1 + s**2), 0, True) assert LT(besselj(1, t), t, s) == (1 - 1/sqrt(1 + 1/s**2), 0, True) # TODO general order works, but is a *mess* # TODO besseli also works, but is an even greater mess # test a bug in conditions processing # TODO the auxiliary condition should be recognised/simplified assert LT(exp(t)*cos(t), t, s)[:-1] in [ ((s - 1)/(s**2 - 2*s + 2), -oo), ((s - 1)/((s - 1)**2 + 1), -oo), ]
def test_besselsimp(): from sympy import besselj, besseli, besselk, bessely, jn, yn, exp_polar, cosh assert besselsimp(exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))) == \ besselj(y, z) assert besselsimp(exp(-I*pi*a/2)*besseli(a, 2*sqrt(x)*exp_polar(I*pi/2))) == \ besselj(a, 2*sqrt(x)) assert besselsimp(sqrt(2)*sqrt(pi)*x**(S(1)/4)*exp(I*pi/4)*exp(-I*pi*a/2) * \ besseli(-S(1)/2, sqrt(x)*exp_polar(I*pi/2)) * \ besseli(a, sqrt(x)*exp_polar(I*pi/2))/2) == \ besselj(a, sqrt(x)) * cos(sqrt(x)) assert besselsimp(besseli(S(-1)/2, z)) == sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z)) assert besselsimp(besseli(a, z*exp_polar(-I*pi/2))) == exp(-I*pi*a/2)*besselj(a, z)
def test_branching(): from sympy import exp_polar, polar_lift, Symbol, I, exp assert besselj(polar_lift(k), x) == besselj(k, x) assert besseli(polar_lift(k), x) == besseli(k, x) n = Symbol('n', integer=True) assert besselj(n, exp_polar(2*pi*I)*x) == besselj(n, x) assert besselj(n, polar_lift(x)) == besselj(n, x) assert besseli(n, exp_polar(2*pi*I)*x) == besseli(n, x) assert besseli(n, polar_lift(x)) == besseli(n, x) def tn(func, s): from random import uniform c = uniform(1, 5) expr = func(s, c*exp_polar(I*pi)) - func(s, c*exp_polar(-I*pi)) eps = 1e-15 expr2 = func(s + eps, -c + eps*I) - func(s + eps, -c - eps*I) return abs(expr.n() - expr2.n()).n() < 1e-10 nu = Symbol('nu') assert besselj(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besselj(nu, x) assert besseli(nu, exp_polar(2*pi*I)*x) == exp(2*pi*I*nu)*besseli(nu, x) assert tn(besselj, 2) assert tn(besselj, pi) assert tn(besselj, I) assert tn(besseli, 2) assert tn(besseli, pi) assert tn(besseli, I)
def test_rewrite(): from sympy import polar_lift, exp, I assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - S(1)/2, z) assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - S(1)/2, z) assert besseli(n, z).rewrite(besselj) == \ exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z) assert besselj(n, z).rewrite(besseli) == \ exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z) nu = randcplx() assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z) assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z) # check that a rewrite was triggered, when the order is set to a generic # symbol 'nu' assert yn(nu, z) != yn(nu, z).rewrite(jn) assert hn1(nu, z) != hn1(nu, z).rewrite(jn) assert hn2(nu, z) != hn2(nu, z).rewrite(jn) assert jn(nu, z) != jn(nu, z).rewrite(yn) assert hn1(nu, z) != hn1(nu, z).rewrite(yn) assert hn2(nu, z) != hn2(nu, z).rewrite(yn) # rewriting spherical bessel functions (SBFs) w.r.t. besselj, bessely is # not allowed if a generic symbol 'nu' is used as the order of the SBFs # to avoid inconsistencies (the order of bessel[jy] is allowed to be # complex-valued, whereas SBFs are defined only for integer orders) order = nu for f in (besselj, bessely): assert hn1(order, z) == hn1(order, z).rewrite(f) assert hn2(order, z) == hn2(order, z).rewrite(f) assert jn(order, z).rewrite(besselj) == sqrt(2)*sqrt(pi)*sqrt(1/z)*besselj(order + S(1)/2, z)/2 assert jn(order, z).rewrite(bessely) == (-1)**nu*sqrt(2)*sqrt(pi)*sqrt(1/z)*bessely(-order - S(1)/2, z)/2 # for integral orders rewriting SBFs w.r.t bessel[jy] is allowed N = Symbol('n', integer=True) ri = randint(-11, 10) for order in (ri, N): for f in (besselj, bessely): assert yn(order, z) != yn(order, z).rewrite(f) assert jn(order, z) != jn(order, z).rewrite(f) assert hn1(order, z) != hn1(order, z).rewrite(f) assert hn2(order, z) != hn2(order, z).rewrite(f) for func, refunc in product((yn, jn, hn1, hn2), (jn, yn, besselj, bessely)): assert tn(func(ri, z), func(ri, z).rewrite(refunc), z)
def test_hyper(): for x in sorted(exparg): test("erf", x, N(sp.erf(x))) for x in sorted(exparg): test("erfc", x, N(sp.erfc(x))) gamarg = FiniteSet(*(x+S(1)/12 for x in exparg)) betarg = ProductSet(gamarg, gamarg) for x in sorted(gamarg): test("lgamma", x, N(sp.log(abs(sp.gamma(x))))) for x in sorted(gamarg): test("gamma", x, N(sp.gamma(x))) for x, y in sorted(betarg, key=lambda (x, y): (y, x)): test("beta", x, y, N(sp.beta(x, y))) pgamarg = FiniteSet(S(1)/12, S(1)/3, S(3)/2, 5) pgamargp = ProductSet(gamarg & Interval(0, oo, True), pgamarg) for a, x in sorted(pgamargp): test("pgamma", a, x, N(sp.lowergamma(a, x))) for a, x in sorted(pgamargp): test("pgammac", a, x, N(sp.uppergamma(a, x))) for a, x in sorted(pgamargp): test("pgammar", a, x, N(sp.lowergamma(a, x)/sp.gamma(a))) for a, x in sorted(pgamargp): test("pgammarc", a, x, N(sp.uppergamma(a, x)/sp.gamma(a))) for a, x in sorted(pgamargp): test("ipgammarc", a, N(sp.uppergamma(a, x)/sp.gamma(a)), x) pbetargp = [(a, b, x) for a, b, x in ProductSet(betarg, pgamarg) if a > 0 and b > 0 and x < 1] pbetargp.sort(key=lambda (a, b, x): (b, a, x)) for a, b, x in pbetargp: test("pbeta", a, b, x, mp.betainc(mpf(a), mpf(b), x2=mpf(x))) for a, b, x in pbetargp: test("pbetar", a, b, x, mp.betainc(mpf(a), mpf(b), x2=mpf(x), regularized=True)) for a, b, x in pbetargp: test("ipbetar", a, b, mp.betainc(mpf(a), mpf(b), x2=mpf(x), regularized=True), x) for x in sorted(posarg): test("j0", x, N(sp.besselj(0, x))) for x in sorted(posarg): test("j1", x, N(sp.besselj(1, x))) for x in sorted(posarg-FiniteSet(0)): test("y0", x, N(sp.bessely(0, x))) for x in sorted(posarg-FiniteSet(0)): test("y1", x, N(sp.bessely(1, x)))
def test_besselsimp(): from sympy import besselj, besseli, exp_polar, cosh, cosine_transform assert besselsimp(exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))) == \ besselj(y, z) assert besselsimp(exp(-I*pi*a/2)*besseli(a, 2*sqrt(x)*exp_polar(I*pi/2))) == \ besselj(a, 2*sqrt(x)) assert besselsimp(sqrt(2)*sqrt(pi)*x**(S(1)/4)*exp(I*pi/4)*exp(-I*pi*a/2) * besseli(-S(1)/2, sqrt(x)*exp_polar(I*pi/2)) * besseli(a, sqrt(x)*exp_polar(I*pi/2))/2) == \ besselj(a, sqrt(x)) * cos(sqrt(x)) assert besselsimp(besseli(S(-1)/2, z)) == \ sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z)) assert besselsimp(besseli(a, z*exp_polar(-I*pi/2))) == \ exp(-I*pi*a/2)*besselj(a, z) assert cosine_transform(1/t*sin(a/t), t, y) == \ sqrt(2)*sqrt(pi)*besselj(0, 2*sqrt(a)*sqrt(y))/2
def test_inverse_laplace_transform(): from sympy import sinh, cosh, besselj, besseli, simplify, factor_terms ILT = inverse_laplace_transform a, b, c, = symbols("a b c", positive=True, finite=True) t = symbols("t") def simp_hyp(expr): return factor_terms(expand_mul(expr)).rewrite(sin) # just test inverses of all of the above assert ILT(1 / s, s, t) == Heaviside(t) assert ILT(1 / s ** 2, s, t) == t * Heaviside(t) assert ILT(1 / s ** 5, s, t) == t ** 4 * Heaviside(t) / 24 assert ILT(exp(-a * s) / s, s, t) == Heaviside(t - a) assert ILT(exp(-a * s) / (s + b), s, t) == exp(b * (a - t)) * Heaviside(-a + t) assert ILT(a / (s ** 2 + a ** 2), s, t) == sin(a * t) * Heaviside(t) assert ILT(s / (s ** 2 + a ** 2), s, t) == cos(a * t) * Heaviside(t) # TODO is there a way around simp_hyp? 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) assert ILT(a / ((s + b) ** 2 + a ** 2), s, t) == exp(-b * t) * sin(a * t) * Heaviside(t) assert ILT((s + b) / ((s + b) ** 2 + a ** 2), s, t) == exp(-b * t) * cos(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_bessel_rand(): assert td(besselj(randcplx(), z), z) assert td(bessely(randcplx(), z), z) assert td(besseli(randcplx(), z), z) assert td(besselk(randcplx(), z), z) assert td(hankel1(randcplx(), z), z) assert td(hankel2(randcplx(), z), z) assert td(jn(randcplx(), z), z) assert td(yn(randcplx(), z), z)
def test_inverse_laplace_transform(): from sympy import (expand, sinh, cosh, besselj, besseli, exp_polar, unpolarify, simplify) ILT = inverse_laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') def simp_hyp(expr): return expand(expand(expr).rewrite(sin)) # just test inverses of all of the above assert ILT(1/s, s, t) == Heaviside(t) assert ILT(1/s**2, s, t) == t*Heaviside(t) assert ILT(1/s**5, s, t) == t**4*Heaviside(t)/24 assert ILT(exp(-a*s)/s, s, t) == Heaviside(t - a) assert ILT(exp(-a*s)/(s + b), s, t) == exp(b*(a - t))*Heaviside(-a + t) assert ILT(a/(s**2 + a**2), s, t) == sin(a*t)*Heaviside(t) assert ILT(s/(s**2 + a**2), s, t) == cos(a*t)*Heaviside(t) # TODO is there a way around simp_hyp? 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) assert ILT(a/((s + b)**2 + a**2), s, t) == exp(-b*t)*sin(a*t)*Heaviside(t) assert ILT( (s + b)/((s + b)**2 + a**2), s, t) == exp(-b*t)*cos(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))
def test_inversion(): from sympy import piecewise_fold, besselj, sqrt, I, sin, cos, Heaviside def inv(f): return piecewise_fold(meijerint_inversion(f, s, t)) assert inv(1/(s**2 + 1)) == sin(t)*Heaviside(t) assert inv(s/(s**2 + 1)) == cos(t)*Heaviside(t) assert inv(exp(-s)/s) == Heaviside(t - 1) assert inv(1/sqrt(1 + s**2)) == besselj(0, t)*Heaviside(t) # Test some antcedents checking. assert meijerint_inversion(sqrt(s)/sqrt(1 + s**2), s, t) is None assert inv(exp(s**2)) is None assert meijerint_inversion(exp(-s**2), s, t) is None
def test_bessel_eval(): from sympy import I, Symbol n, m, k = Symbol('n', integer=True), Symbol('m'), Symbol('k', integer=True, zero=False) for f in [besselj, besseli]: assert f(0, 0) == S.One assert f(2.1, 0) == S.Zero assert f(-3, 0) == S.Zero assert f(-10.2, 0) == S.ComplexInfinity assert f(1 + 3*I, 0) == S.Zero assert f(-3 + I, 0) == S.ComplexInfinity assert f(-2*I, 0) == S.NaN assert f(n, 0) != S.One and f(n, 0) != S.Zero assert f(m, 0) != S.One and f(m, 0) != S.Zero assert f(k, 0) == S.Zero assert bessely(0, 0) == S.NegativeInfinity assert besselk(0, 0) == S.Infinity for f in [bessely, besselk]: assert f(1 + I, 0) == S.ComplexInfinity assert f(I, 0) == S.NaN for f in [besselj, bessely]: assert f(m, S.Infinity) == S.Zero assert f(m, S.NegativeInfinity) == S.Zero for f in [besseli, besselk]: assert f(m, I*S.Infinity) == S.Zero assert f(m, I*S.NegativeInfinity) == S.Zero for f in [besseli, besselk]: assert f(-4, z) == f(4, z) assert f(-3, z) == f(3, z) assert f(-n, z) == f(n, z) assert f(-m, z) != f(m, z) for f in [besselj, bessely]: assert f(-4, z) == f(4, z) assert f(-3, z) == -f(3, z) assert f(-n, z) == (-1)**n*f(n, z) assert f(-m, z) != (-1)**m*f(m, z) for f in [besselj, besseli]: assert f(m, -z) == (-z)**m*z**(-m)*f(m, z) assert besseli(2, -z) == besseli(2, z) assert besseli(3, -z) == -besseli(3, z) assert besselj(0, -z) == besselj(0, z) assert besselj(1, -z) == -besselj(1, z) assert besseli(0, I*z) == besselj(0, z) assert besseli(1, I*z) == I*besselj(1, z) assert besselj(3, I*z) == -I*besseli(3, z)
def test_laplace_transform(): LT = laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') # test a bug spos = symbols('s', positive=True) assert LT(exp(t), t, spos)[:2] == (1/(spos - 1), True) # basic tests from wikipedia assert LT((t-a)**b*exp(-c*(t-a))*Heaviside(t-a), t, s) \ == ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1)*gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1/s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a*s)/s, 0, True) assert LT(1 - exp(-a*t), t, s) == (a/(s*(a + s)), 0, True) assert LT((exp(2*t)-1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[0:2] == (1/(s-1), 1) assert LT(exp(2*t), t, s)[0:2] == (1/(s-2), 2) assert LT(exp(a*t), t, s)[0:2] == (1/(s-a), a) assert LT(log(t/a), t, s) == ((log(a) + log(s) + EulerGamma)/(-s), 0, True) assert LT(erf(t), t, s) == ((-erf(s/2) + 1)*exp(s**2/4)/s, 0, True) assert LT(sin(a*t), t, s) == (a/(a**2 + s**2), 0, True) assert LT(cos(a*t), t, s) == (s/(a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a*t)*sin(b*t), t, s) == (1/b/(1 + (a + s)**2/b**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ (1/(s + a)/(1 + b**2/(a + s)**2), -a, True) # TODO sinh, cosh have delicate cancellation assert LT(besselj(0, t), t, s) == (1/sqrt(1 + s**2), 0, True) assert LT(besselj(1, t), t, s) == (1 - 1/sqrt(1 + 1/s**2), 0, True)
def test_laplace_transform(): LT = laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') # basic tests from wikipedia assert LT((t-a)**b*exp(-c*(t-a))*Heaviside(t-a), t, s) \ == ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1)*gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1/s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a*s)/s, 0, True) assert LT(1 - exp(-a*t), t, s) == (a/(s*(a + s)), 0, True) assert LT((exp(2*t)-1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[0:2] == (1/(s-1), 1) assert LT(exp(2*t), t, s)[0:2] == (1/(s-2), 2) assert LT(exp(a*t), t, s)[0:2] == (1/(s-a), a) lt = LT(log(t/a), t, s) assert lt[1:] == (0, True) # TODO hyperexpand is not clever enough to recognise this on its own assert hyperexpand(lt[0], allow_hyper=True) == (-log(a*s) - EulerGamma)/s assert LT(erf(t), t, s) == ((-erf(s/2) + 1)*exp(s**2/4)/s, 0, True) assert LT(sin(a*t), t, s) == (a/(a**2 + s**2), 0, True) assert LT(cos(a*t), t, s) == (s/(a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a*t)*sin(b*t), t, s) == (1/b/(1 + (a + s)**2/b**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ (sqrt((a + s)**2)/b**2/(1 + (a + s)**2/b**2), -a, True) # TODO sinh, cosh have delicate cancellation # TODO conditions are a mess assert LT(besselj(0, t), t, s, noconds=True) == 1/sqrt(1 + s**2) assert LT(besselj(1, t), t, s, noconds=True) == 1 - 1/sqrt(1 + 1/s**2)
def __init__(self, **kwargs): a = self.a nu = self.nu k = self.k r, th = sympy.symbols('r th') v_asympt01 = get_v_asympt0(0, k, nu) v_asympt03 = get_v_asympt0(1, k, nu) self.v_asympt = ( v_asympt01 * sympy.sin(nu/2*(th-a)) + v_asympt03 * sympy.sin(3*nu/2*(th-2*np.pi)) ) * get_tapering_func(self.R) self.v_series = ( get_v_asympt0(0, k, nu, nterms=10) * sympy.sin(nu/2*(th-a)) + get_v_asympt0(1, k, nu, nterms=10) * sympy.sin(3*nu/2*(th-2*np.pi)) ) self.g = ( (th - a) / (2*np.pi - a) * sympy.besselj(nu/2, k*r) + (th - 2*np.pi) / (a - 2*np.pi) * sympy.besselj(3*nu/2, k*r) )
def test_pmint_besselj(): f = besselj(nu + 1, x)/besselj(nu, x) g = nu*log(x) - log(besselj(nu, x)) assert heurisch(f, x) == g f = (nu*besselj(nu, x) - x*besselj(nu + 1, x))/x g = besselj(nu, x) assert heurisch(f, x) == g f = jn(nu + 1, x)/jn(nu, x) g = nu*log(x) - log(jn(nu, x)) assert heurisch(f, x) == g
def test_pmint_bessel_products(): # Note: Derivatives of Bessel functions have many forms. # Recurrence relations are needed for comparisons. if ON_TRAVIS: skip("Too slow for travis.") f = x*besselj(nu, x)*bessely(nu, 2*x) g = -2*x*besselj(nu, x)*bessely(nu - 1, 2*x)/3 + x*besselj(nu - 1, x)*bessely(nu, 2*x)/3 assert heurisch(f, x) == g f = x*besselj(nu, x)*besselk(nu, 2*x) g = -2*x*besselj(nu, x)*besselk(nu - 1, 2*x)/5 - x*besselj(nu - 1, x)*besselk(nu, 2*x)/5 assert heurisch(f, x) == g
def __init__(self, **kwargs): a = self.a nu = self.nu k = self.k K = self.K R = self.R r, th = sympy.symbols('r th') v_asympt01 = get_v_asympt0(K, k, nu) self.v_asympt = ( get_v_asympt0(K, k, nu) * sympy.sin((K+1/2)*nu*(th-a)) ) * get_tapering_func(R) self.v_series = get_v_asympt0(K, k, nu, nterms=10)* sympy.sin((K+1/2)*nu*(th-a)) phi1 = sympy.besselj((K+1/2)*nu, k*r) if alt_phi1_ext: phi1 += sympy.Piecewise((0, r<R), ((r-R)**5, True)) self.g = (th - a) / (2*np.pi - a) * (phi1)
def test_inverse_laplace_transform(): from sympy import expand, sinh, cosh, besselj, besseli, exp_polar, unpolarify ILT = inverse_laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') def simp_hyp(expr): return expand(expand(expr).rewrite(sin)) # just test inverses of all of the above assert ILT(1/s, s, t) == Heaviside(t) assert ILT(1/s**2, s, t) == t*Heaviside(t) assert ILT(1/s**5, s, t) == t**4*Heaviside(t)/factorial(4) assert ILT(exp(-a*s)/s, s, t) == Heaviside(t-a) assert ILT(exp(-a*s)/(s+b), s, t) == exp(b*(a - t))*Heaviside(t - a) assert ILT(a/(s**2 + a**2), s, t) == sin(a*t)*Heaviside(t) assert ILT(s/(s**2 + a**2), s, t) == cos(a*t)*Heaviside(t) # TODO is there a way around simp_hyp? 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) assert ILT(a/((s+b)**2 + a**2), s, t) == exp(-b*t)*sin(a*t)*Heaviside(t) assert ILT((s+b)/((s+b)**2 + a**2), s, t) == exp(-b*t)*cos(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 # TODO besselsimp would be good to have # these are re-enabled in a later commit #assert ILT(a**b*(s + sqrt(s**2 - a**2))**(-b)/sqrt(s**2 - a**2), s, t) == \ # (t**2)**(b/2 + S(1)/2)*Heaviside(t)*besseli(b, a*t*exp_polar(I*pi))*exp_polar(-I*pi*b)/(t*sqrt((t**2)**b)) #assert unpolarify(ILT(a**b*(s + sqrt(s**2 + a**2))**(-b)/sqrt(s**2 + a**2), # s, t).rewrite(besselj)) == \ # (t**2)**(b/2 + S(1)/2)*exp(-I*pi*b)*Heaviside(t)*besselj(b, a*t*exp_polar(I*pi))/(t*sqrt((t**2)**b)) assert ILT(1/(s*sqrt(s+1)), s, t) == Heaviside(t)*erf(sqrt(t))
def test_pmint_bessel_products(): # Note: Derivatives of Bessel functions have many forms. # Recurrence relations are needed for comparisons. if ON_TRAVIS: skip("Too slow for travis.") f = x * besselj(nu, x) * bessely(nu, 2 * x) g = -2 * x * besselj(nu, x) * bessely(nu - 1, 2 * x) / 3 + x * besselj( nu - 1, x) * bessely(nu, 2 * x) / 3 assert heurisch(f, x) == g f = x * besselj(nu, x) * besselk(nu, 2 * x) g = -2 * x * besselj(nu, x) * besselk(nu - 1, 2 * x) / 5 - x * besselj( nu - 1, x) * besselk(nu, 2 * x) / 5 assert heurisch(f, x) == g
def fourier_transform(self) -> sy.Expr: r""" The 2D Fourier transform of :math:`Z^m_n`. This function essentially implements eq. (7) of [Tatulli_2013]_. .. note:: Compared to [Tatulli_2013]_, we are using a slightly different notation. Instead of indexing the dimensions of the Fourier space (or :math:`k`-space) using :math:`\kappa` and :math:`\alpha`, we use :math:`k_1` and :math:`k_2`. Returns: The 2D Fourier transform of :math:`Z^m_n`, that is, :math:`\mathcal{F}\lbrace Z^m_n \rbrace (k_1, k_2)`, as a `sy.Expr`. """ # Define symbols for k1 and k2 k1 = sy.Symbol('k1') k2 = sy.Symbol('k2') # Define the first factor, which only depends on n factor_1 = (sy.Pow(-1, self.n) * sy.sqrt(self.n + 1) / (sy.pi * k1) * sy.besselj(2 * sy.pi * k1, self.n + 1)) # Define the second factor that also depends on m if self.m == 0: factor_2 = sy.Pow(-1, self.n / 2) elif self.m > 0: factor_2 = (sy.sqrt(2) * sy.Pow(-1, (self.n - self.m) / 2) * sy.Pow(sy.I, self.m) * sy.cos(self.m * k2)) else: factor_2 = (sy.sqrt(2) * sy.Pow(-1, (self.n + self.m) / 2) * sy.Pow(sy.I, -self.m) * sy.sin(-self.m * k2)) return sy.nsimplify(sy.simplify(factor_1 * factor_2))
def test_meromorphic(): assert besselj(2, x).is_meromorphic(x, 1) == True assert besselj(2, x).is_meromorphic(x, 0) == True assert besselj(2, x).is_meromorphic(x, oo) == False assert besselj(S(2) / 3, x).is_meromorphic(x, 1) == True assert besselj(S(2) / 3, x).is_meromorphic(x, 0) == False assert besselj(S(2) / 3, x).is_meromorphic(x, oo) == False assert besselj(x, 2 * x).is_meromorphic(x, 2) == False assert besselk(0, x).is_meromorphic(x, 1) == True assert besselk(2, x).is_meromorphic(x, 0) == True assert besseli(0, x).is_meromorphic(x, 1) == True assert besseli(2, x).is_meromorphic(x, 0) == True assert bessely(0, x).is_meromorphic(x, 1) == True assert bessely(0, x).is_meromorphic(x, 0) == False assert bessely(2, x).is_meromorphic(x, 0) == True assert hankel1(3, x**2 + 2 * x).is_meromorphic(x, 1) == True assert hankel1(0, x).is_meromorphic(x, 0) == False assert hankel2(11, 4).is_meromorphic(x, 5) == True assert hn1(6, 7 * x**3 + 4).is_meromorphic(x, 7) == True assert hn2(3, 2 * x).is_meromorphic(x, 9) == True assert jn(5, 2 * x + 7).is_meromorphic(x, 4) == True assert yn(8, x**2 + 11).is_meromorphic(x, 6) == True
def test_rewrite(): from sympy import polar_lift, exp, I assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - S(1)/2, z) assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - S(1)/2, z) assert besseli(n, z).rewrite(besselj) == \ exp(-I*n*pi/2)*besselj(n, polar_lift(I)*z) assert besselj(n, z).rewrite(besseli) == \ exp(I*n*pi/2)*besseli(n, polar_lift(-I)*z) nu = randcplx() assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z) assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z)
def test_to_meijerg(): x = symbols('x') assert hyperexpand(expr_to_holonomic(sin(x)).to_meijerg()) == sin(x) assert hyperexpand(expr_to_holonomic(cos(x)).to_meijerg()) == cos(x) assert hyperexpand(expr_to_holonomic(exp(x)).to_meijerg()) == exp(x) assert hyperexpand(expr_to_holonomic( log(x)).to_meijerg()).simplify() == log(x) assert expr_to_holonomic(4 * x**2 / 3 + 7).to_meijerg() == 4 * x**2 / 3 + 7 assert hyperexpand( expr_to_holonomic(besselj(2, x), lenics=3).to_meijerg()) == besselj(2, x) p = hyper((-S(1) / 2, -3), (), x) assert from_hyper(p).to_meijerg() == hyperexpand(p) p = hyper((S(1), S(3)), (S(2), ), x) assert (hyperexpand(from_hyper(p).to_meijerg()) - hyperexpand(p)).expand() == 0 p = from_hyper(hyper((-2, -3), (S(1) / 2, ), x)) s = hyperexpand(hyper((-2, -3), (S(1) / 2, ), x)) C_0 = Symbol('C_0') C_1 = Symbol('C_1') D_0 = Symbol('D_0') assert (hyperexpand(p.to_meijerg()).subs({ C_0: 1, D_0: 0 }) - s).simplify() == 0 p.singular_ics = [(0, [1]), (S(1) / 2, [0])] assert (hyperexpand(p.to_meijerg()) - s).simplify() == 0 p = expr_to_holonomic(besselj(S(1) / 2, x), initcond=False) assert ( p.to_expr() - (D_0 * sin(x) + C_0 * cos(x) + C_1 * sin(x)) / sqrt(x)).simplify() == 0 p = expr_to_holonomic( besselj(S(1) / 2, x), singular_ics=((S(-1) / 2, [sqrt(2) / sqrt(pi), sqrt(2) / sqrt(pi)]), )) assert (p.to_expr() - besselj(S(1) / 2, x) - besselj(S(-1) / 2, x)).simplify() == 0
def test_to_meijerg(): x = symbols('x') assert hyperexpand(expr_to_holonomic(sin(x)).to_meijerg()) == sin(x) assert hyperexpand(expr_to_holonomic(cos(x)).to_meijerg()) == cos(x) assert hyperexpand(expr_to_holonomic(exp(x)).to_meijerg()) == exp(x) assert hyperexpand(expr_to_holonomic( log(x)).to_meijerg()).simplify() == log(x) assert expr_to_holonomic(4 * x**2 / 3 + 7).to_meijerg() == 4 * x**2 / 3 + 7 assert hyperexpand( expr_to_holonomic(besselj(2, x), lenics=3).to_meijerg()) == besselj(2, x) p = hyper((Rational(-1, 2), -3), (), x) assert from_hyper(p).to_meijerg() == hyperexpand(p) p = hyper((S.One, S(3)), (S(2), ), x) assert (hyperexpand(from_hyper(p).to_meijerg()) - hyperexpand(p)).expand() == 0 p = from_hyper(hyper((-2, -3), (S.Half, ), x)) s = hyperexpand(hyper((-2, -3), (S.Half, ), x)) C_0 = Symbol('C_0') C_1 = Symbol('C_1') D_0 = Symbol('D_0') assert (hyperexpand(p.to_meijerg()).subs({ C_0: 1, D_0: 0 }) - s).simplify() == 0 p.y0 = {0: [1], S.Half: [0]} assert (hyperexpand(p.to_meijerg()) - s).simplify() == 0 p = expr_to_holonomic(besselj(S.Half, x), initcond=False) assert ( p.to_expr() - (D_0 * sin(x) + C_0 * cos(x) + C_1 * sin(x)) / sqrt(x)).simplify() == 0 p = expr_to_holonomic( besselj(S.Half, x), y0={Rational(-1, 2): [sqrt(2) / sqrt(pi), sqrt(2) / sqrt(pi)]}) assert (p.to_expr() - besselj(S.Half, x) - besselj(Rational(-1, 2), x)).simplify() == 0
def test_to_meijerg(): x = symbols('x') assert hyperexpand(expr_to_holonomic(sin(x)).to_meijerg()) == sin(x) assert hyperexpand(expr_to_holonomic(cos(x)).to_meijerg()) == cos(x) assert hyperexpand(expr_to_holonomic(exp(x)).to_meijerg()) == exp(x) assert hyperexpand(expr_to_holonomic(log(x)).to_meijerg()).simplify() == log(x) assert expr_to_holonomic(4*x**2/3 + 7).to_meijerg() == 4*x**2/3 + 7 assert hyperexpand(expr_to_holonomic(besselj(2, x), lenics=3).to_meijerg()) == besselj(2, x) p = hyper((-S(1)/2, -3), (), x) assert from_hyper(p).to_meijerg() == hyperexpand(p) p = hyper((S(1), S(3)), (S(2), ), x) assert (hyperexpand(from_hyper(p).to_meijerg()) - hyperexpand(p)).expand() == 0 p = from_hyper(hyper((-2, -3), (S(1)/2, ), x)) s = hyperexpand(hyper((-2, -3), (S(1)/2, ), x)) C_0 = Symbol('C_0') C_1 = Symbol('C_1') D_0 = Symbol('D_0') assert (hyperexpand(p.to_meijerg()).subs({C_0:1, D_0:0}) - s).simplify() == 0 p.y0 = {0: [1], S(1)/2: [0]} assert (hyperexpand(p.to_meijerg()) - s).simplify() == 0 p = expr_to_holonomic(besselj(S(1)/2, x), initcond=False) assert (p.to_expr() - (D_0*sin(x) + C_0*cos(x) + C_1*sin(x))/sqrt(x)).simplify() == 0 p = expr_to_holonomic(besselj(S(1)/2, x), y0={S(-1)/2: [sqrt(2)/sqrt(pi), sqrt(2)/sqrt(pi)]}) assert (p.to_expr() - besselj(S(1)/2, x) - besselj(S(-1)/2, x)).simplify() == 0
from sympy import symbols, lambdify, besselj, I from scipy.special import jv as scipy_besselj from dolfin import * mesh = UnitCubeMesh(20, 20, 20) # Space where the exact solution is represented Ve = FunctionSpace(mesh, 'DG', 4) dof_x = Ve.dofmap().tabulate_all_coordinates(mesh).reshape((-1, 3)) X, Y, Z = dof_x[:, 0], dof_x[:, 1], dof_x[:, 2] # Suppose the solution is besselj(1, x[0]) + besselj(2, x[1]) + besselj(3, x[2]) x, y, z = symbols('x, y, z') u = (0.00073 - 0.00052 * I) * besselj(0, (1.84 - 1.84 * I) * x) u_lambda = lambdify(x, u, ['numpy', {'besselj': scipy_besselj}]) # Interpolate manually # timer = Timer('eval') # timer.start() # u_values = u_lambda(X, Y, Z) # print 'Evaluated %d dofs in % s' % (Ve.dim(), timer.stop()) # Exact solution in Ve # u = Function(Ve) # u.vector()[:] = u_values # Solution space and the hypot. numerical solution # V = FunctionSpace(mesh, 'CG', 1) # uh = interpolate(Constant(1), V) # print errornorm(u, uh) print u_lambda(1.0)
def test_laplace_transform(): from sympy import fresnels, fresnelc LT = laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') w = Symbol("w") f = Function("f") # Test unevaluated form assert laplace_transform(f(t), t, w) == LaplaceTransform(f(t), t, w) assert inverse_laplace_transform(f(w), w, t, plane=0) == InverseLaplaceTransform( f(w), w, t, 0) # test a bug spos = symbols('s', positive=True) assert LT(exp(t), t, spos)[:2] == (1 / (spos - 1), True) # basic tests from wikipedia assert LT((t - a)**b*exp(-c*(t - a))*Heaviside(t - a), t, s) == \ ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1) * gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1 / s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a * s) / s, 0, True) assert LT(1 - exp(-a * t), t, s) == (a / (s * (a + s)), 0, True) assert LT((exp(2*t) - 1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[:2] == (1 / (s - 1), 1) assert LT(exp(2 * t), t, s)[:2] == (1 / (s - 2), 2) assert LT(exp(a * t), t, s)[:2] == (1 / (s - a), a) assert LT(log(t / a), t, s) == ((log(a * s) + EulerGamma) / s / -1, 0, True) assert LT(erf(t), t, s) == ((erfc(s / 2)) * exp(s**2 / 4) / s, 0, True) assert LT(sin(a * t), t, s) == (a / (a**2 + s**2), 0, True) assert LT(cos(a * t), t, s) == (s / (a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a * t) * sin(b * t), t, s) == (b / (b**2 + (a + s)**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ ((a + s)/(b**2 + (a + s)**2), -a, True) assert LT(besselj(0, t), t, s) == (1 / sqrt(1 + s**2), 0, True) assert LT(besselj(1, t), t, s) == (1 - 1 / sqrt(1 + 1 / s**2), 0, True) # TODO general order works, but is a *mess* # TODO besseli also works, but is an even greater mess # test a bug in conditions processing # TODO the auxiliary condition should be recognised/simplified assert LT(exp(t) * cos(t), t, s)[:-1] in [ ((s - 1) / (s**2 - 2 * s + 2), -oo), ((s - 1) / ((s - 1)**2 + 1), -oo), ] # Fresnel functions assert laplace_transform(fresnels(t), t, s) == \ ((-sin(s**2/(2*pi))*fresnels(s/pi) + sin(s**2/(2*pi))/2 - cos(s**2/(2*pi))*fresnelc(s/pi) + cos(s**2/(2*pi))/2)/s, 0, True) assert laplace_transform( fresnelc(t), t, s) == (((2 * sin(s**2 / (2 * pi)) * fresnelc(s / pi) - 2 * cos(s**2 / (2 * pi)) * fresnels(s / pi) + sqrt(2) * cos(s**2 / (2 * pi) + pi / 4)) / (2 * s), 0, True)) assert LT(Matrix([[exp(t), t*exp(-t)], [t*exp(-t), exp(t)]]), t, s) ==\ Matrix([ [(1/(s - 1), 1, True), ((s + 1)**(-2), 0, True)], [((s + 1)**(-2), 0, True), (1/(s - 1), 1, True)] ])
def test_inverse_mellin_transform(): from sympy import (sin, simplify, Max, Min, expand, powsimp, exp_polar, cos, cot) IMT = inverse_mellin_transform assert IMT(gamma(s), s, x, (0, oo)) == exp(-x) assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x) assert simplify(IMT(s/(2*s**2 - 2), s, x, (2, oo))) == \ (x**2 + 1)*Heaviside(1 - x)/(4*x) # test passing "None" assert IMT(1/(s**2 - 1), s, x, (-1, None)) == \ -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x) assert IMT(1/(s**2 - 1), s, x, (None, 1)) == \ -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x) # test expansion of sums assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x # test factorisation of polys r = symbols('r', real=True) assert IMT(1/(s**2 + 1), s, exp(-x), (None, oo) ).subs(x, r).rewrite(sin).simplify() \ == sin(r)*Heaviside(1 - exp(-r)) # test multiplicative substitution _a, _b = symbols('a b', positive=True) assert IMT(_b**(-s / _a) * factorial(s / _a) / s, s, x, (0, oo)) == exp(-_b * x**_a) assert IMT(factorial(_a / _b + s / _b) / (_a + s), s, x, (-_a, oo)) == x**_a * exp(-x**_b) def simp_pows(expr): return simplify(powsimp(expand_mul(expr, deep=False), force=True)).replace(exp_polar, exp) # Now test the inverses of all direct transforms tested above # Section 8.4.2 nu = symbols('nu', real=True, finite=True) assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1) assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \ == (1 - x)**(beta - 1)*Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(1 - beta - s)/gamma(1 - s), s, x, (-oo, None))) \ == (x - 1)**(beta - 1)*Heaviside(x - 1) assert simp_pows(IMT(gamma(s)*gamma(rho - s)/gamma(rho), s, x, (0, None))) \ == (1/(x + 1))**rho assert simp_pows(IMT(d**c*d**(s - 1)*sin(pi*c) *gamma(s)*gamma(s + c)*gamma(1 - s)*gamma(1 - s - c)/pi, s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \ == (x**c - d**c)/(x - d) assert simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1 - c)/2 - s) *gamma(-c/2 - s)/gamma(1 - c - s), s, x, (0, -re(c)/2))) == \ (1 + sqrt(x + 1))**c assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s) /gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \ b**(a - 1)*(sqrt(1 + x/b**2) + 1)**(a - 1)*(b**2*sqrt(1 + x/b**2) + b**2 + x)/(b**2 + x) assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s) / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \ b**c*(sqrt(1 + x/b**2) + 1)**c # Section 8.4.5 assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x) assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \ log(x)**3*Heaviside(x - 1) assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1) assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1) assert IMT(pi / (s * sin(2 * pi * s)), s, x, (-S(1) / 2, 0)) == log(sqrt(x) + 1) assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x) # TODO def mysimp(expr): from sympy import expand, logcombine, powsimp return expand(powsimp(logcombine(expr, force=True), force=True, deep=True), force=True).replace(exp_polar, exp) assert mysimp(mysimp(IMT(pi / (s * tan(pi * s)), s, x, (-1, 0)))) in [ log(1 - x) * Heaviside(1 - x) + log(x - 1) * Heaviside(x - 1), log(x) * Heaviside(x - 1) + log(1 - 1 / x) * Heaviside(x - 1) + log(-x + 1) * Heaviside(-x + 1) ] # test passing cot assert mysimp(IMT(pi * cot(pi * s) / s, s, x, (0, 1))) in [ log(1 / x - 1) * Heaviside(1 - x) + log(1 - 1 / x) * Heaviside(x - 1), -log(x) * Heaviside(-x + 1) + log(1 - 1 / x) * Heaviside(x - 1) + log(-x + 1) * Heaviside(-x + 1), ] # 8.4.14 assert IMT(-gamma(s + S(1)/2)/(sqrt(pi)*s), s, x, (-S(1)/2, 0)) == \ erf(sqrt(x)) # 8.4.19 assert simplify(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, S(3)/4))) \ == besselj(a, 2*sqrt(x)) assert simplify(IMT(2**a*gamma(S(1)/2 - 2*s)*gamma(s + (a + 1)/2) / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)), s, x, (-(re(a) + 1)/2, S(1)/4))) == \ sin(sqrt(x))*besselj(a, sqrt(x)) assert simplify(IMT(2**a*gamma(a/2 + s)*gamma(S(1)/2 - 2*s) / (gamma(S(1)/2 - s - a/2)*gamma(1 - 2*s + a)), s, x, (-re(a)/2, S(1)/4))) == \ cos(sqrt(x))*besselj(a, sqrt(x)) # TODO this comes out as an amazing mess, but simplifies nicely assert simplify(IMT(gamma(a + s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), s, x, (-re(a), S(1)/2))) == \ besselj(a, sqrt(x))**2 assert simplify(IMT(gamma(s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)), s, x, (0, S(1)/2))) == \ besselj(-a, sqrt(x))*besselj(a, sqrt(x)) assert simplify(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) *gamma(a/2 + b/2 - s + 1)), s, x, (-(re(a) + re(b))/2, S(1)/2))) == \ besselj(a, sqrt(x))*besselj(b, sqrt(x)) # Section 8.4.20 # TODO this can be further simplified! assert simplify(IMT(-2**(2*s)*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(-2*s + 1) * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)), s, x, (Max(-re(a)/2 - re(b)/2, -re(a)/2 + re(b)/2), S(1)/2))) == \ besselj(a, sqrt(x))*-(besselj(-b, sqrt(x)) - besselj(b, sqrt(x))*cos(pi*b))/sin(pi*b) # TODO more # for coverage assert IMT(pi / cos(pi * s), s, x, (0, S(1) / 2)) == sqrt(x) / (x + 1)
def test_rewrite(): from sympy import polar_lift, exp, I assert besselj(n, z).rewrite(jn) == sqrt(2 * z / pi) * jn(n - S.Half, z) assert bessely(n, z).rewrite(yn) == sqrt(2 * z / pi) * yn(n - S.Half, z) assert besseli(n, z).rewrite(besselj) == exp(-I * n * pi / 2) * besselj( n, polar_lift(I) * z) assert besselj(n, z).rewrite(besseli) == exp(I * n * pi / 2) * besseli( n, polar_lift(-I) * z) nu = randcplx() assert tn(besselj(nu, z), besselj(nu, z).rewrite(besseli), z) assert tn(besselj(nu, z), besselj(nu, z).rewrite(bessely), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(besselj), z) assert tn(besseli(nu, z), besseli(nu, z).rewrite(bessely), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besselj), z) assert tn(bessely(nu, z), bessely(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besselj), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(besseli), z) assert tn(besselk(nu, z), besselk(nu, z).rewrite(bessely), z) # check that a rewrite was triggered, when the order is set to a generic # symbol 'nu' assert yn(nu, z) != yn(nu, z).rewrite(jn) assert hn1(nu, z) != hn1(nu, z).rewrite(jn) assert hn2(nu, z) != hn2(nu, z).rewrite(jn) assert jn(nu, z) != jn(nu, z).rewrite(yn) assert hn1(nu, z) != hn1(nu, z).rewrite(yn) assert hn2(nu, z) != hn2(nu, z).rewrite(yn) # rewriting spherical bessel functions (SBFs) w.r.t. besselj, bessely is # not allowed if a generic symbol 'nu' is used as the order of the SBFs # to avoid inconsistencies (the order of bessel[jy] is allowed to be # complex-valued, whereas SBFs are defined only for integer orders) order = nu for f in (besselj, bessely): assert hn1(order, z) == hn1(order, z).rewrite(f) assert hn2(order, z) == hn2(order, z).rewrite(f) assert (jn(order, z).rewrite(besselj) == sqrt(2) * sqrt(pi) * sqrt(1 / z) * besselj(order + S.Half, z) / 2) assert (jn(order, z).rewrite(bessely) == (-1)**nu * sqrt(2) * sqrt(pi) * sqrt(1 / z) * bessely(-order - S.Half, z) / 2) # for integral orders rewriting SBFs w.r.t bessel[jy] is allowed N = Symbol("n", integer=True) ri = randint(-11, 10) for order in (ri, N): for f in (besselj, bessely): assert yn(order, z) != yn(order, z).rewrite(f) assert jn(order, z) != jn(order, z).rewrite(f) assert hn1(order, z) != hn1(order, z).rewrite(f) assert hn2(order, z) != hn2(order, z).rewrite(f) for func, refunc in product((yn, jn, hn1, hn2), (jn, yn, besselj, bessely)): assert tn(func(ri, z), func(ri, z).rewrite(refunc), z)
def test_bessel(): from sympy import besselj, besseli assert simplify(integrate(besselj(a, z)*besselj(b, z)/z, (z, 0, oo), meijerg=True, conds='none')) == \ 2*sin(pi*(a/2 - b/2))/(pi*(a - b)*(a + b)) assert simplify( integrate(besselj(a, z) * besselj(a, z) / z, (z, 0, oo), meijerg=True, conds='none')) == 1 / (2 * a) # TODO more orthogonality integrals assert simplify(integrate(sin(z*x)*(x**2 - 1)**(-(y + S(1)/2)), (x, 1, oo), meijerg=True, conds='none') *2/((z/2)**y*sqrt(pi)*gamma(S(1)/2 - y))) == \ besselj(y, z) # Werner Rosenheinrich # SOME INDEFINITE INTEGRALS OF BESSEL FUNCTIONS assert integrate(x * besselj(0, x), x, meijerg=True) == x * besselj(1, x) assert integrate(x * besseli(0, x), x, meijerg=True) == x * besseli(1, x) # TODO can do higher powers, but come out as high order ... should they be # reduced to order 0, 1? assert integrate(besselj(1, x), x, meijerg=True) == -besselj(0, x) assert integrate(besselj(1, x)**2/x, x, meijerg=True) == \ -(besselj(0, x)**2 + besselj(1, x)**2)/2 # TODO more besseli when tables are extended or recursive mellin works assert integrate(besselj(0, x)**2/x**2, x, meijerg=True) == \ -2*x*besselj(0, x)**2 - 2*x*besselj(1, x)**2 \ + 2*besselj(0, x)*besselj(1, x) - besselj(0, x)**2/x assert integrate(besselj(0, x)*besselj(1, x), x, meijerg=True) == \ -besselj(0, x)**2/2 assert integrate(x**2*besselj(0, x)*besselj(1, x), x, meijerg=True) == \ x**2*besselj(1, x)**2/2 assert integrate(besselj(0, x)*besselj(1, x)/x, x, meijerg=True) == \ (x*besselj(0, x)**2 + x*besselj(1, x)**2 - besselj(0, x)*besselj(1, x)) # TODO how does besselj(0, a*x)*besselj(0, b*x) work? # TODO how does besselj(0, x)**2*besselj(1, x)**2 work? # TODO sin(x)*besselj(0, x) etc come out a mess # TODO can x*log(x)*besselj(0, x) be done? # TODO how does besselj(1, x)*besselj(0, x+a) work? # TODO more indefinite integrals when struve functions etc are implemented # test a substitution assert integrate(besselj(1, x**2)*x, x, meijerg=True) == \ -besselj(0, x**2)/2
def test_expand(): assert expand_func(besselj(S(1)/2, z)) == sqrt(2)*sin(z)/(sqrt(pi)*sqrt(z)) assert expand_func( bessely(S(1)/2, z)) == -sqrt(2)*cos(z)/(sqrt(pi)*sqrt(z))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Special math functions ====================== Common special functions to be used across Sympy implemented models """ from sympy import besselj, bessely, legendre, I, sqrt, pi from sympy import symbols p, m, v, n, z = symbols('p m v n z') # spherical Bessel function sph_bessel1 = besselj(n + 1 / 2, z) * sqrt(pi / (2 * z)) # sphericl Neumann function sph_bessel2 = bessely(n + 1 / 2, z) * sqrt(pi / (2 * z)) # spherical Hankel function of the second kind sph_hankel2 = sph_bessel1 - I * sph_bessel2 h2_nz = sph_bessel1 - I * sph_bessel2 # more compact notation. # This function can actuall be replaced by the inbuilt 'assoc_legendre' with varying mileage # upon lambdification # pmvz version of legendre function (Appendix II of Beranek & Mello 2012, eqn. 63) # this is the equivalent of Latex: P^{m}_{v}(z) legendre_mvz = ((-1)**m) * ((1 - z**2)**(m / 2)) * legendre(v, z).diff((z, m))
def test_laplace_transform(): from sympy import fresnels, fresnelc, DiracDelta LT = laplace_transform a, b, c, = symbols('a b c', positive=True) t = symbols('t') w = Symbol("w") f = Function("f") # Test unevaluated form assert laplace_transform(f(t), t, w) == LaplaceTransform(f(t), t, w) assert inverse_laplace_transform(f(w), w, t, plane=0) == InverseLaplaceTransform( f(w), w, t, 0) # test a bug spos = symbols('s', positive=True) assert LT(exp(t), t, spos)[:2] == (1 / (spos - 1), 1) # basic tests from wikipedia assert LT((t - a)**b*exp(-c*(t - a))*Heaviside(t - a), t, s) == \ ((s + c)**(-b - 1)*exp(-a*s)*gamma(b + 1), -c, True) assert LT(t**a, t, s) == (s**(-a - 1) * gamma(a + 1), 0, True) assert LT(Heaviside(t), t, s) == (1 / s, 0, True) assert LT(Heaviside(t - a), t, s) == (exp(-a * s) / s, 0, True) assert LT(1 - exp(-a * t), t, s) == (a / (s * (a + s)), 0, True) assert LT((exp(2*t) - 1)*exp(-b - t)*Heaviside(t)/2, t, s, noconds=True) \ == exp(-b)/(s**2 - 1) assert LT(exp(t), t, s)[:2] == (1 / (s - 1), 1) assert LT(exp(2 * t), t, s)[:2] == (1 / (s - 2), 2) assert LT(exp(a * t), t, s)[:2] == (1 / (s - a), a) assert LT(log(t / a), t, s) == ((log(a * s) + EulerGamma) / s / -1, 0, True) assert LT(erf(t), t, s) == (erfc(s / 2) * exp(s**2 / 4) / s, 0, True) assert LT(sin(a * t), t, s) == (a / (a**2 + s**2), 0, True) assert LT(cos(a * t), t, s) == (s / (a**2 + s**2), 0, True) # TODO would be nice to have these come out better assert LT(exp(-a * t) * sin(b * t), t, s) == (b / (b**2 + (a + s)**2), -a, True) assert LT(exp(-a*t)*cos(b*t), t, s) == \ ((a + s)/(b**2 + (a + s)**2), -a, True) assert LT(besselj(0, t), t, s) == (1 / sqrt(1 + s**2), 0, True) assert LT(besselj(1, t), t, s) == (1 - 1 / sqrt(1 + 1 / s**2), 0, True) # TODO general order works, but is a *mess* # TODO besseli also works, but is an even greater mess # test a bug in conditions processing # TODO the auxiliary condition should be recognised/simplified assert LT(exp(t) * cos(t), t, s)[:-1] in [ ((s - 1) / (s**2 - 2 * s + 2), -oo), ((s - 1) / ((s - 1)**2 + 1), -oo), ] # DiracDelta function: standard cases assert LT(DiracDelta(t), t, s) == (1, -oo, True) assert LT(DiracDelta(a * t), t, s) == (1 / a, -oo, True) assert LT(DiracDelta(t / 42), t, s) == (42, -oo, True) assert LT(DiracDelta(t + 42), t, s) == (0, -oo, True) assert LT(DiracDelta(t)+DiracDelta(t-42), t, s) == \ (1 + exp(-42*s), -oo, True) assert LT(DiracDelta(t) - a * exp(-a * t), t, s) == (-a / (a + s) + 1, 0, True) assert LT(exp(-t)*(DiracDelta(t)+DiracDelta(t-42)), t, s) == \ (exp(-42*s - 42) + 1, -oo, True) # Collection of cases that cannot be fully evaluated and/or would catch # some common implementation errors assert LT(DiracDelta(t**2), t, s) == LaplaceTransform(DiracDelta(t**2), t, s) assert LT(DiracDelta(t**2 - 1), t, s) == (exp(-s) / 2, -oo, True) assert LT(DiracDelta(t*(1 - t)), t, s) == \ LaplaceTransform(DiracDelta(-t**2 + t), t, s) assert LT((DiracDelta(t) + 1)*(DiracDelta(t - 1) + 1), t, s) == \ (LaplaceTransform(DiracDelta(t)*DiracDelta(t - 1), t, s) + \ 1 + exp(-s) + 1/s, 0, True) assert LT(DiracDelta(2*t - 2*exp(a)), t, s) == \ (exp(-s*exp(a))/2, -oo, True) # Fresnel functions assert laplace_transform(fresnels(t), t, s) == \ ((-sin(s**2/(2*pi))*fresnels(s/pi) + sin(s**2/(2*pi))/2 - cos(s**2/(2*pi))*fresnelc(s/pi) + cos(s**2/(2*pi))/2)/s, 0, True) assert laplace_transform( fresnelc(t), t, s) == (((2 * sin(s**2 / (2 * pi)) * fresnelc(s / pi) - 2 * cos(s**2 / (2 * pi)) * fresnels(s / pi) + sqrt(2) * cos(s**2 / (2 * pi) + pi / 4)) / (2 * s), 0, True)) # What is this testing: Ne(1 / s, 1) & (0 < cos(Abs(periodic_argument(s, oo))) * Abs(s) - 1) Mt = Matrix([[exp(t), t * exp(-t)], [t * exp(-t), exp(t)]]) Ms = Matrix([[1 / (s - 1), (s + 1)**(-2)], [(s + 1)**(-2), 1 / (s - 1)]]) # The default behaviour for Laplace tranform of a Matrix returns a Matrix # of Tuples and is deprecated: with warns_deprecated_sympy(): Ms_conds = Matrix([[(1 / (s - 1), 1, s > 1), ((s + 1)**(-2), 0, True)], [((s + 1)**(-2), 0, True), (1 / (s - 1), 1, s > 1)]]) with warns_deprecated_sympy(): assert LT(Mt, t, s) == Ms_conds # The new behavior is to return a tuple of a Matrix and the convergence # conditions for the matrix as a whole: assert LT(Mt, t, s, legacy_matrix=False) == (Ms, 1, s > 1) # With noconds=True the transformed matrix is returned without conditions # either way: assert LT(Mt, t, s, noconds=True) == Ms assert LT(Mt, t, s, legacy_matrix=False, noconds=True) == Ms
def test_inverse_laplace_transform(): from sympy import simplify, factor_terms, DiracDelta 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_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
def test_besselj_leading_term(): assert besselj(0, x).as_leading_term(x) == 1 assert besselj(1, sin(x)).as_leading_term(x) == x / 2 assert besselj(1, 2 * sqrt(x)).as_leading_term(x) == sqrt(x)
f = HDF5File(mpi_comm_world(), 'precomputed/precomputed_' + meshName + note + '.hdf5', 'w') # precomputation of Bessel functions============================================================================= temp = toc() coefs_r_prec = [] # these will be functions in PS coefs_i_prec = [] # these will be functions in PS c0ex = Expression("(maxc-c*(x[0]*x[0]+x[1]*x[1]))", maxc=Constant(coef_par_max), c=Constant(coef_par)) f.write(interpolate(c0ex, PS), "parab") # plot(interpolate(c0ex, PS), interactive=True, title='Parab') for i in range(8): r = symbols('r') besRe = re(coefs_mult[i] * (coefs_bes_mult[i] * besselj(0, r * coefs_bes[i]) + 1)) besIm = im(coefs_mult[i] * (coefs_bes_mult[i] * besselj(0, r * coefs_bes[i]) + 1)) besRe_lambda = lambdify(r, besRe, ['numpy', {'besselj': jv}]) besIm_lambda = lambdify(r, besIm, ['numpy', {'besselj': jv}]) class PartialReSolution(Expression): def eval(self, value, x): rad = float(sqrt(x[0] * x[0] + x[1] * x[1])) # conversion to float needed, u_lambda (and near) cannot use sympy Float as input value[0] = 0 if near(rad, R) else besRe_lambda( rad) # do not evaluate on boundaries, it's 0 # print(value) gives reasonable values expr = PartialReSolution() print('i = ' + str(i) + ' interpolating real part')
def dispeq_gyrotropic_cylindersArrayFull(N_max, w_01, a_0, ee, cylXY, k, p, EE, GG, HH, c, ): m = list(range(-N_max, N_max + 1, 1)) if N_max == 0: m = 0 nu = m w = w_01 k0 = w / c q = cmath.sqrt(1 - p **2) # q = q * (2 * ((q.imag) <= 0) - 1) Q = k0 * a_0 * q # вычисляем элементы матрицы рассеяния для внутренней области цилиндра mainq = EE ** 2 - GG ** 2 + EE * HH - (HH + EE) * p ** 2 F=cmath.sqrt(EE*GG**2*HH*(GG**2-(HH-EE)**2)) Pb=cmath.sqrt(EE-(HH+EE)*GG**2/(HH-EE)**2-2*F/(HH-EE)**2) Pc=cmath.sqrt(EE-(HH+EE)*GG**2/(HH-EE)**2+2*F/(HH-EE)**2) #radq = math.sqrt((HH - EE) ** 2 * p **4 + 2 * ((GG ** 2)*(HH + EE) - EE * (HH - EE) **2) * p **2 +(EE ** 2 - GG ** 2 - EE * HH) ** 2) radq = -(HH-EE)*cmath.sqrt((p**2-Pb**2)*(p**2-Pc**2)) q1 = cmath.sqrt(0.5 * (mainq - radq) / EE) q2 = cmath.sqrt(0.5 * (mainq + radq) / EE) n1 = -(EE / (p * GG) ) * (p ** 2 + q1 ** 2 + (GG ** 2)/EE - EE) n2 = -(EE / (p * GG) ) * (p ** 2 + q2 ** 2 + (GG ** 2)/EE - EE) alp1 = -1 + (p ** 2 + q1 ** 2 - EE) / GG alp2 = -1 + (p ** 2 + q2 ** 2 - EE) / GG bet1 = 1 + p / n1 bet2 = 1 + p / n2 #что делать с Q она мнимая, брать действительную или мнимую часть? Q1 = (q1 * a_0) * k0 Q2 = (q2 * a_0) * k0 mMax = 2 * N_max + 1 JM = np.zeros(((3, 31)), dtype=np.complex) n=0 for per in range(-N_max,N_max+1,1): JM[n][0] = besselj(per + 1, Q1) #JM1 JM[n][1] = besselj(per + 1, Q2) #JM2 JM[n][2]= besselj(per, Q1) #Jm1 JM[n][3]= besselj(per, Q2) #Jm2 JM[n][4]= JM[n][per + 2] / Q1 #Jm1_Q1 JM[n][5]= JM[n][per + 3]/ Q2 #Jm2_Q2 JM[n][6]= (((1j / HH) * n1) * q1) * JM[n][per + 2] #Ez1 JM[n][7] = (((1j / HH) * n2) * q2) * JM[n][per + 3] #Ez2 JM[n][8] = 1j * (JM[n][per] + (alp1 * per) * JM[n][per + 4]) #Ephi1 JM[n][9]= 1j * (JM[n][per+1] + (alp2 * per) *JM[n][per + 5] ) #Ephi2 JM[n][10]= - q1 * JM[n][per + 2] #Hz1 JM[n][11]= - q2 * JM[n][per + 3] #Hz2 JM[n][12] = - n1 * (JM[n][per] - (bet1 * per) * JM[n][per + 4]) #Hphi1 JM[n][13]= - n2 * (JM[n][per+1] - (bet2 * per) * JM[n][per + 5]) #Hphi2 # вычисляем элементы матрицы рассеяния для внешней области цилиндра JM[n][14] = hankel2(per, Q) #H2m JM[n][15]= -(JM[n][per + 14]/ Q)* per + hankel2(per + 1, Q) #dH2m #Jm_out = besselj(m, Q) #dJm_out = Jm_out * m / Q - besselj(m + 1, Q) JM[n][16] =-1j * q * JM[n][per + 14] #Ez_sct JM[n][17]= q * JM[n][per + 14] #Hz_sct A = 1 / (k0 * (1 - p ^ 2)) JM[n][18] = -1j*q * (A * ((p * (per / a_0)) * JM[n][per + 14])) #Ephi_sctE JM[n][19]= 1j*q * (A * (k0 * q * JM[n][per + 15])) #Ephi_sctH JM[n][20]= q * (A * ((p * (per / a_0)) * JM[n][per + 14])) #Hphi_sctH JM[n][21] = -q * (A * (k0 * q * JM[n][per + 15])) #Hphi_sctE n = n + 1 matrixGS = np.zeros(((mMax*4*2, mMax*4*2)), dtype=np.complex) for jj in range(0, 2): for jm in range(1, 4, 1): for ll in range(0, 2): for jnu in range(1, 4, 1): jmmm = (jm - 1) * 4+1 jnunu = (jnu - 1) * 4+1 # спросить про проход ведь будет совпадение номеров if jj == ll: if jm == jnu: matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax] = JM[jm + 1][6] # Ez1 matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 1] = JM[jm + 1][7] # Ez2(jm) matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 2] = JM[jm + 1][16] # Ez_sct(jm) matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 3] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax] = JM[jm + 1][10] # Hz1 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 1] = JM[jm + 1][11] # Hz2 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 2] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 3] = - JM[jm + 1][17] # -Hz_sct matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax] = JM[jm + 1][8] # Ephi1 matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 1] = JM[jm + 1][9] # Ephi2 matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 2] = -JM[jm + 1][18] # -Ephi_sctE matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 3] = -JM[jm + 1][19] # -Ephi_sctH matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax] = JM[jm + 1][12] # Hphi1 matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 1] = JM[jm + 1][13] # Hphi2 matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 2] = -JM[jm + 1][21] # -Hphi_sctE matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 3] = -JM[jm + 1][20] # -Hphi_sctH else: Ljl = float(math.sqrt((cylXY[jj , 0] - cylXY[ll , 0]) ** 2 + (cylXY[jj , 1] - cylXY[ll , 1]) ** 2)) thetaIJ =float(math.atan(abs(cylXY[jj , 1] - cylXY[ll , 1]) / abs(cylXY[jj , 0] - cylXY[ll , 0]))) if ((cylXY[jj , 0] - cylXY[ll , 0]) <= 0 and (cylXY[jj , 1] - cylXY[ll , 1]) > 0): thetaIJ = cmath.pi - thetaIJ elif ((cylXY[jj , 0] - cylXY[ll , 0]) <= 0 and (cylXY[jj, 1] - cylXY[ll , 1]) <= 0): thetaIJ = cmath.pi + thetaIJ elif ((cylXY[jj , 1] - cylXY[ll , 1]) <= 0 and (cylXY[jj, 0] - cylXY[ll , 0]) > 0): thetaIJ = 2 * cmath.pi - thetaIJ JM[jnu - 1][22] = -cmath.exp(-1j * (nu[jnu - 1] - m[jm - 1]) * thetaIJ) ** hankel2((m[jm - 1] - nu[jnu - 1]), k0 * q * Ljl) JM[jnu - 1][23] = besselj(m[jnu - 1], Q) # Jm_out JM[jnu - 1][24] = -JM[jnu - 1][23] * m[jnu - 1] / Q + besselj(m[jnu - 1] + 1, Q) # dJm_out # спросить почему они одинаковые? JM[jnu - 1][25] = JM[jnu - 1][22] * JM[jnu - 1][23] # Ez_inc JM[jnu - 1][26] = JM[jnu - 1][22] * JM[jnu - 1][23] # Hz_inc A = 1 / (k0 * (1 - p ** 2)) JM[jnu - 1][27] = - JM[jnu - 1][22] * ( A * ((p * (m[jnu - 1] / a_0)) * JM[jnu - 1][23])) # Ephi_incE JM[jnu - 1][28] = - JM[jnu - 1][22] * (A * (- 1j * k0 * q * JM[jnu - 1][24])) # Ephi_incH JM[jnu - 1][29] = - JM[jnu - 1][22] * (A * (1j * k0 * q * JM[jnu - 1][24])) # Hphi_incE JM[jnu - 1][30] = -JM[jnu - 1][22] * (A * ((p * (m[jnu - 1] / a_0)) * JM[jnu - 1][23])) matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax] = 0 matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 1] = 0 matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 2] = JM[jm + 1][25] # Ez_inc matrixGS[jmmm + jj * 4 * mMax, jnunu + ll * 4 * mMax + 3] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 1] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 2] = 0 matrixGS[jmmm + jj * 4 * mMax + 1, jnunu + ll * 4 * mMax + 3] = JM[jm + 1][25] # Hz_inc matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax] = 0 matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 1] = 0 matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 2] = JM[jm + 1][ 27] # Ephi_incE matrixGS[jmmm + jj * 4 * mMax + 2, jnunu + ll * 4 * mMax + 3] = JM[jm + 1][28] # Ephi_incH matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax] = 0 matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 1] = 0 matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 2] = JM[jm + 1][29] # Hphi_incE matrixGS[jmmm + jj * 4 * mMax + 3, jnunu + ll * 4 * mMax + 3] = JM[jm + 1][30] # Hphi_incH return abs(np.linalg.det(matrixGS))
if __name__ == "__main__": alpha = float(input("\nВведите отношение масс шарика и струны: ")) # plot_function(besselj(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * bessely(0, 2 * sqrt(alpha) * x) - bessely(1, 2 * sqrt(alpha) * x)) - bessely(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * besselj(0, 2 * sqrt(alpha) * x) - besselj(1, 2 * sqrt(alpha) * x)), 0, 8) while True: left = float(input("\nВведите левую границу отрезка: ")) right = float(input("Введите правую границу отрезка: ")) init_x = float(input("Введите начальное приближение: ")) print("Для метода половинного деления задан отрезок [" + str(left) + "; " + str(right) + "]") print("Для гибридного метода задано начальное приближение: " + str(init_x)) if init_x == left or init_x == right: print("Начальное приближение совпадает с одним из концов отрезка") else: print( "Начальное приближение не совпадает ни с одним из концов отрезка" ) find_solve_with_secant_method( besselj(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * bessely(0, 2 * sqrt(alpha) * x) - bessely(1, 2 * sqrt(alpha) * x)) - bessely(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * besselj(0, 2 * sqrt(alpha) * x) - besselj(1, 2 * sqrt(alpha) * x)), left, right, 10**(-3)) # find_solve_with_hybrid_method(besselj(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * bessely(0, 2 * sqrt(alpha) * x) - bessely(1, 2 * sqrt(alpha) * x)) - bessely(0, 2 * sqrt(alpha + 1) * x) * (sqrt(alpha) * x * besselj(0, 2 * sqrt(alpha) * x) - besselj(1, 2 * sqrt(alpha) * x)), left, right, init_x, 10 ** (-3)) progress = int( input("Для продолжения нажмите введите 1, для завершения 0: ")) if progress == 0: break
def test_expand(): assert expand_func(besselj(S(1) / 2, z)) == sqrt(2) * sin(z) / (sqrt(pi) * sqrt(z)) assert expand_func(bessely(S(1) / 2, z)) == -sqrt(2) * cos(z) / (sqrt(pi) * sqrt(z))
def test_rewrite(): assert besselj(n, z).rewrite(jn) == sqrt(2*z/pi)*jn(n - S(1)/2, z) assert bessely(n, z).rewrite(yn) == sqrt(2*z/pi)*yn(n - S(1)/2, z)
def test_expand(): from sympy import besselsimp, Symbol, exp, exp_polar, I 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))) 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.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
def test_bessel_eval(): from sympy import I assert besselj(-4, z) == besselj(4, z) assert besselj(-3, z) == -besselj(3, z) assert bessely(-2, z) == bessely(2, z) assert bessely(-1, z) == -bessely(1, z) assert besselj(0, -z) == besselj(0, z) assert besselj(1, -z) == -besselj(1, z) assert besseli(0, I*z) == besselj(0, z) assert besseli(1, I*z) == I*besselj(1, z) assert besselj(3, I*z) == -I*besseli(3, z)
def test_inverse_mellin_transform(): from sympy import (sin, simplify, expand_func, powsimp, Max, Min, expand, powdenest, powsimp, exp_polar) IMT = inverse_mellin_transform assert IMT(gamma(s), s, x, (0, oo)) == exp(-x) assert IMT(gamma(-s), s, x, (-oo, 0)) == exp(-1 / x) assert IMT(s/(2*s**2 - 2), s, x, (2, oo)) \ == (x**2/2 + S(1)/2)*Heaviside(1 - x)/(2*x) # test passing "None" assert IMT(1/(s**2 - 1), s, x, (-1, None)) \ == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x) assert IMT(1/(s**2 - 1), s, x, (None, 1)) \ == -x*Heaviside(-x + 1)/2 - Heaviside(x - 1)/(2*x) # test expansion of sums assert IMT(gamma(s) + gamma(s - 1), s, x, (1, oo)) == (x + 1) * exp(-x) / x # test factorisation of polys assert simplify(expand_func(IMT(1/(s**2 + 1), s, exp(-x), (None, oo))).rewrite(sin)) \ == sin(x)*Heaviside(1 - exp(-x)) # test multiplicative substitution a, b = symbols('a b', positive=True) c, d = symbols('c d') assert IMT(b**(-s / a) * factorial(s / a) / s, s, x, (0, oo)) == exp(-b * x**a) assert IMT(factorial(a / b + s / b) / (a + s), s, x, (-a, oo)) == x**a * exp(-x**b) from sympy import expand_mul def simp_pows(expr): return expand_mul(simplify(powsimp(expr, force=True)), deep=True).replace(exp_polar, exp) # XXX ? # Now test the inverses of all direct transforms tested above # Section 8.4.2 assert IMT(-1 / (nu + s), s, x, (-oo, None)) == x**nu * Heaviside(x - 1) assert IMT(1 / (nu + s), s, x, (None, oo)) == x**nu * Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(s)/gamma(s + beta), s, x, (0, oo))) \ == (1 - x)**(beta - 1)*Heaviside(1 - x) assert simp_pows(IMT(gamma(beta)*gamma(1-beta-s)/gamma(1-s), s, x, (-oo, None))) \ == (x - 1)**(beta - 1)*Heaviside(x - 1) assert simp_pows(IMT(gamma(s)*gamma(rho-s)/gamma(rho), s, x, (0, None))) \ == (1/(x + 1))**rho # TODO should this simplify further? assert simp_pows(IMT(d**c*d**(s-1)*sin(pi*c) \ *gamma(s)*gamma(s+c)*gamma(1-s)*gamma(1-s-c)/pi, s, x, (Max(-re(c), 0), Min(1 - re(c), 1)))) \ == d**c*(x/d)**c/(-d + x) - d**c/(-d + x) # TODO is calling simplify twice a bug? assert simplify(simplify(IMT(1/sqrt(pi)*(-c/2)*gamma(s)*gamma((1-c)/2 - s) \ *gamma(-c/2-s)/gamma(1-c-s), s, x, (0, -re(c)/2)))) == \ (1 + sqrt(x + 1))**c assert simplify(IMT(2**(a + 2*s)*b**(a + 2*s - 1)*gamma(s)*gamma(1 - a - 2*s) \ /gamma(1 - a - s), s, x, (0, (-re(a) + 1)/2))) == \ (b + sqrt(b**2 + x))**(a - 1)*(b**2 + b*sqrt(b**2 + x) + x)/(b**2 + x) assert simplify(IMT(-2**(c + 2*s)*c*b**(c + 2*s)*gamma(s)*gamma(-c - 2*s) \ / gamma(-c - s + 1), s, x, (0, -re(c)/2))) == \ (b + sqrt(b**2 + x))**c # Section 8.4.5 assert IMT(24 / s**5, s, x, (0, oo)) == log(x)**4 * Heaviside(1 - x) assert expand(IMT(6/s**4, s, x, (-oo, 0)), force=True) == \ log(x)**3*Heaviside(x - 1) assert IMT(pi / (s * sin(pi * s)), s, x, (-1, 0)) == log(x + 1) assert IMT(pi / (s * sin(pi * s / 2)), s, x, (-2, 0)) == log(x**2 + 1) assert IMT(pi / (s * sin(2 * pi * s)), s, x, (-S(1) / 2, 0)) == log(sqrt(x) + 1) assert IMT(pi / (s * sin(pi * s)), s, x, (0, 1)) == log(1 + 1 / x) # TODO def mysimp(expr): from sympy import expand, logcombine, powsimp return expand(powsimp(logcombine(expr, force=True), force=True, deep=True), force=True).replace(exp_polar, exp) assert mysimp(mysimp(IMT(pi/(s*tan(pi*s)), s, x, (-1, 0)))) == \ log(1-x)*Heaviside(1-x) + log(x-1)*Heaviside(x-1) assert mysimp(IMT(pi/(s*tan(pi*s)), s, x, (0, 1))) == \ log(1/x - 1)*Heaviside(1-x) + log(1 - 1/x)*Heaviside(x-1) # 8.4.14 assert IMT(-gamma(s + S(1)/2)/(sqrt(pi)*s), s, x, (-S(1)/2, 0)) == \ erf(sqrt(x)) # 8.4.19 # TODO these come out ugly def mysimp(expr): return expand( unpolarify(simplify(expand(expand_func(expr.rewrite(besselj)))))) assert mysimp(IMT(gamma(a/2 + s)/gamma(a/2 - s + 1), s, x, (-re(a)/2, S(3)/4))) \ == besselj(a, 2*sqrt(x)*polar_lift(-1))*exp(-I*pi*a) assert mysimp(IMT(2**a*gamma(S(1)/2 - 2*s)*gamma(s + (a + 1)/2) \ / (gamma(1 - s - a/2)*gamma(1 - 2*s + a)), s, x, (-(re(a) + 1)/2, S(1)/4))) == \ exp(-I*pi*a)*sin(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1)) assert mysimp(IMT(2**a*gamma(a/2 + s)*gamma(S(1)/2 - 2*s) \ / (gamma(S(1)/2 - s - a/2)*gamma(1 - 2*s + a)), s, x, (-re(a)/2, S(1)/4))) == \ exp(-I*pi*a)*cos(sqrt(x))*besselj(a, sqrt(x)*polar_lift(-1)) # TODO this comes out as an amazing mess, but surprisingly enough mysimp is # effective ... assert powsimp(powdenest(mysimp(IMT(gamma(a + s)*gamma(S(1)/2 - s) \ / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), s, x, (-re(a), S(1)/2))), polar=True)) == \ exp(-2*I*pi*a)*besselj(a, sqrt(x)*polar_lift(-1))**2 # NOTE the next is indeed an even function of sqrt(x), so the result is # correct assert mysimp(IMT(gamma(s)*gamma(S(1)/2 - s) \ / (sqrt(pi)*gamma(1 - s - a)*gamma(1 + a - s)), s, x, (0, S(1)/2))) == \ besselj(-a, polar_lift(-1)*sqrt(x))*besselj(a, polar_lift(-1)*sqrt(x)) assert mysimp(IMT(4**s*gamma(-2*s + 1)*gamma(a/2 + b/2 + s) \ / (gamma(-a/2 + b/2 - s + 1)*gamma(a/2 - b/2 - s + 1) \ *gamma(a/2 + b/2 - s + 1)), s, x, (-(re(a) + re(b))/2, S(1)/2))) == \ exp(-I*pi*a)*exp(-I*pi*b)*besselj(a, sqrt(x)*polar_lift(-1)) \ *besselj(b, sqrt(x)*polar_lift(-1))
# eqn. 13.226 n_B_q_kb = n_B_mka.subs({'m': n, 'n': q, 'ka': kb}) term_13_226 = (conjugate(m_B_p_kb) * n_B_q_kb) / (p + q + 1) M_mn = Sum(term_13_226, (q, 0, Q), (p, 0, P)) one_Mmn_term = lambdify([a, b, k, m, n, NN], M_mn, 'scipy') #%% # eqn. 13.227 b_term = -Sum((conjugate(m_B_p_kb) / (p + 1)) * (a / b)**(2 * p), (p, 0, P)) bterm_func = lambdify([a, b, k, m, NN], b_term, 'scipy') #%% # piston in an infinite baffle portion eq13_252_pt1 = besselj(1, ka * sin(theta)) / (ka * sin(theta)) # piston in a finite open baffle portion A_n = IndexedBase('A_n') jj = symbols('jj', integer=True) pt2_term = A_n[jj] * gamma(jj + 5 / 2) * ( (2 / kb * sin(theta))**(jj + 3 / 2)) * besselj(jj + 3 / 2, kb * sin(theta)) eq13_252_pt2 = (kb / 2) * cos(theta) * Sum(pt2_term, (jj, 0, NN - 1)) # eqn. 13.252 d_theta = eq13_252_pt1 + eq13_252_pt2 d_thetaf = lambdify([k, a, b, theta, A_n, NN], d_theta, 'sympy') # eqn 13.253 def d_0f(kv, bv, Anmat): return (1 / 2) * (1 + kv * bv * sum(Anmat))
def test_mellin_transform(): from sympy import Max, Min MT = mellin_transform bpos = symbols('b', positive=True) # 8.4.2 assert MT(x**nu*Heaviside(x - 1), x, s) \ == (1/(-nu - s), (-oo, -re(nu)), True) assert MT(x**nu*Heaviside(1 - x), x, s) \ == (1/(nu + s), (-re(nu), oo), True) assert MT((1-x)**(beta - 1)*Heaviside(1-x), x, s) \ == (gamma(beta)*gamma(s)/gamma(beta + s), (0, oo), re(-beta) < 0) assert MT((x-1)**(beta - 1)*Heaviside(x-1), x, s) \ == (gamma(beta)*gamma(1 - beta - s)/gamma(1 - s), (-oo, -re(beta) + 1), re(-beta) < 0) assert MT( (1 + x)**(-rho), x, s) == (gamma(s) * gamma(rho - s) / gamma(rho), (0, re(rho)), True) # TODO also the conditions should be simplified assert MT(abs(1-x)**(-rho), x, s) == \ (cos(pi*rho/2 - pi*s)*gamma(s)*gamma(rho-s)/(cos(pi*rho/2)*gamma(rho)),\ (0, re(rho)), And(re(rho) - 1 < 0, re(rho) < 1)) mt = MT((1 - x)**(beta - 1) * Heaviside(1 - x) + a * (x - 1)**(beta - 1) * Heaviside(x - 1), x, s) assert mt[1], mt[2] == ((0, -re(beta) + 1), True) assert MT((x**a-b**a)/(x-b), x, s)[0] == \ pi*b**(a+s-1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s))) assert MT((x**a-bpos**a)/(x-bpos), x, s) == \ (pi*bpos**(a+s-1)*sin(pi*a)/(sin(pi*s)*sin(pi*(a + s))), (Max(-re(a), 0), Min(1 - re(a), 1)), True) expr = (sqrt(x + b**2) + b)**a / sqrt(x + b**2) assert MT(expr.subs(b, bpos), x, s) == \ (2**(a + 2*s)*bpos**(a + 2*s - 1)*gamma(s) \ *gamma(1 - a - 2*s)/gamma(1 - a - s), (0, -re(a)/2 + S(1)/2), True) # TODO does not work with bneg, argument wrong. Needs changes to matching. #assert MT(expr.subs(b, -bpos), x, s) == \ # ((-1)**(a+1)*2**(a + 2*s)*bpos**(a + 2*s - 1)*gamma(a + s) \ # *gamma(1 - a - 2*s)/gamma(1 - s), # (-re(a), -re(a)/2 + S(1)/2), True) expr = (sqrt(x + b**2) + b)**a assert MT(expr.subs(b, bpos), x, s) == \ (-2**(a + 2*s)*a*bpos**(a + 2*s)*gamma(s)*gamma(-a - 2*s)/gamma(-a - s + 1), (0, -re(a)/2), True) #assert MT(expr.subs(b, -bpos), x, s) == \ # (2**(a + 2*s)*a*bpos**(a + 2*s)*gamma(-a - 2*s)*gamma(a + s)/gamma(-s + 1), # (-re(a), -re(a)/2), True) # Test exponent 1: #assert MT(expr.subs({b: -bpos, a:1}), x, s) == \ # (-bpos**(2*s + 1)*gamma(s)*gamma(-s - S(1)/2)/(2*sqrt(pi)), # (-1, -S(1)/2), True) # 8.4.2 assert MT(exp(-x), x, s) == (gamma(s), (0, oo), True) assert MT(exp(-1 / x), x, s) == (gamma(-s), (-oo, 0), True) # 8.4.5 assert MT(log(x)**4 * Heaviside(1 - x), x, s) == (24 / s**5, (0, oo), True) assert MT(log(x)**3 * Heaviside(x - 1), x, s) == (6 / s**4, (-oo, 0), True) assert MT(log(x + 1), x, s) == (pi / (s * sin(pi * s)), (-1, 0), True) assert MT(log(1 / x + 1), x, s) == (pi / (s * sin(pi * s)), (0, 1), True) assert MT(log(abs(1 - x)), x, s) == (pi / (s * tan(pi * s)), (-1, 0), True) assert MT(log(abs(1 - 1 / x)), x, s) == (pi / (s * tan(pi * s)), (0, 1), True) # TODO we cannot currently do these (needs summation of 3F2(-1)) # this also implies that they cannot be written as a single g-function # (although this is possible) mt = MT(log(x) / (x + 1), x, s) assert mt[1:] == ((0, 1), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) mt = MT(log(x)**2 / (x + 1), x, s) assert mt[1:] == ((0, 1), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) mt = MT(log(x) / (x + 1)**2, x, s) assert mt[1:] == ((0, 2), True) assert not hyperexpand(mt[0], allow_hyper=True).has(meijerg) # 8.4.14 assert MT(erf(sqrt(x)), x, s) == \ (-gamma(s + S(1)/2)/(sqrt(pi)*s), (-S(1)/2, 0), True) # 8.4.19 assert MT(besselj(a, 2*sqrt(x)), x, s) == \ (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, S(3)/4), True) assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(S(1)/2 - 2*s)*gamma((a+1)/2 + s) \ / (gamma(1 - s- a/2)*gamma(1 + a - 2*s)), (-(re(a) + 1)/2, S(1)/4), True) assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(a/2 + s)*gamma(S(1)/2 - 2*s) / (gamma(S(1)/2 - s - a/2)*gamma(a - 2*s + 1)), (-re(a)/2, S(1)/4), True) assert MT(besselj(a, sqrt(x))**2, x, s) == \ (gamma(a + s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), (-re(a), S(1)/2), True) assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \ (gamma(s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)), (0, S(1)/2), True) # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as # I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large) assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (gamma(1-s)*gamma(a + s - S(1)/2) / (sqrt(pi)*gamma(S(3)/2 - s)*gamma(a - s + S(1)/2)), (S(1)/2 - re(a), S(1)/2), True) assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \ (4**s*gamma(1 - 2*s)*gamma((a+b)/2 + s) / (gamma(1 - s + (b-a)/2)*gamma(1 - s + (a-b)/2) *gamma( 1 - s + (a+b)/2)), (-(re(a) + re(b))/2, S(1)/2), True) assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \ ((Max(re(a), -re(a)), S(1)/2), True) # Section 8.4.20 assert MT(bessely(a, 2*sqrt(x)), x, s) == \ (-cos(pi*a/2 - pi*s)*gamma(s - a/2)*gamma(s + a/2)/pi, (Max(-re(a)/2, re(a)/2), S(3)/4), True) assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*sin(pi*a/2 - pi*s)*gamma(S(1)/2 - 2*s) * gamma((1-a)/2 + s)*gamma((1+a)/2 + s) / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)), (Max(-(re(a) + 1)/2, (re(a) - 1)/2), S(1)/4), True) assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*cos(pi*a/2 - pi*s)*gamma(s - a/2)*gamma(s + a/2)*gamma(S(1)/2 - 2*s) / (sqrt(pi)*gamma(S(1)/2 - s - a/2)*gamma(S(1)/2 - s + a/2)), (Max(-re(a)/2, re(a)/2), S(1)/4), True) assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(S(1)/2 - s) / (pi**S('3/2')*gamma(1 + a - s)), (Max(-re(a), 0), S(1)/2), True) assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \ (-4**s*cos(pi*a/2 - pi*b/2 + pi*s)*gamma(1 - 2*s) * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)), (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), S(1)/2), True) assert MT(bessely(a, sqrt(x))**2, x, s) == \ ((2*cos(pi*a - pi*s)*gamma(s)*gamma(-a + s)*gamma(-s + 1)*gamma(a - s + 1) \ + pi*gamma(-s + S(1)/2)*gamma(s + S(1)/2)) \ *gamma(a + s)/(pi**(S(3)/2)*gamma(-s + 1)*gamma(s + S(1)/2)*gamma(a - s + 1)), (Max(-re(a), 0, re(a)), S(1)/2), True) # TODO bessely(a, sqrt(x))*bessely(b, sqrt(x)) is a mess # (no matter what way you look at it ...) # Section 8.4.22 # TODO we can't do any of these (delicate cancellation) # Section 8.4.23 assert MT(besselk(a, 2*sqrt(x)), x, s) == \ (gamma(s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True) # TODO this result needs expansion of F(a, b; c; 1) using gauss-summation assert MT(exp(-x/2)*besselk(a, x/2), x, s)[1:] == \ ((Max(-re(a), re(a)), oo), True)
def test_rewrite(): assert besselj(n, z).rewrite(jn) == sqrt(2 * z / pi) * jn(n - S(1) / 2, z) assert bessely(n, z).rewrite(yn) == sqrt(2 * z / pi) * yn(n - S(1) / 2, z)
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, Rational(-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 + Rational(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+ Rational(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 + S.Half) + (x**2 - x/2 - Rational(3, 2))*Dx + (-x**2 + x/2 + 1)*Dx**2 +\ (x**2 + x/2)*Dx**3, x, 0, [2, 1, S.Half]) 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.Half, x0=1) q = HolonomicFunction(x * Dx - S.Half, 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 a = symbols("a") p = expr_to_holonomic(sqrt(a * x), x=x) assert p.to_expr() == sqrt(a) * sqrt(x)
def test_besselsimp(): from sympy import besselj, besseli, exp_polar, cosh, cosine_transform, bessely assert besselsimp(exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))) == \ besselj(y, z) assert besselsimp(exp(-I*pi*a/2)*besseli(a, 2*sqrt(x)*exp_polar(I*pi/2))) == \ besselj(a, 2*sqrt(x)) assert besselsimp(sqrt(2)*sqrt(pi)*x**Rational(1, 4)*exp(I*pi/4)*exp(-I*pi*a/2) * besseli(Rational(-1, 2), sqrt(x)*exp_polar(I*pi/2)) * besseli(a, sqrt(x)*exp_polar(I*pi/2))/2) == \ besselj(a, sqrt(x)) * cos(sqrt(x)) assert besselsimp(besseli(Rational(-1, 2), z)) == \ sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z)) assert besselsimp(besseli(a, z*exp_polar(-I*pi/2))) == \ exp(-I*pi*a/2)*besselj(a, z) assert cosine_transform(1/t*sin(a/t), t, y) == \ sqrt(2)*sqrt(pi)*besselj(0, 2*sqrt(a)*sqrt(y))/2 assert besselsimp( x**2 * (a * (-2 * besselj(5 * I, x) + besselj(-2 + 5 * I, x) + besselj(2 + 5 * I, x)) + b * (-2 * bessely(5 * I, x) + bessely(-2 + 5 * I, x) + bessely(2 + 5 * I, x))) / 4 + x * (a * (besselj(-1 + 5 * I, x) / 2 - besselj(1 + 5 * I, x) / 2) + b * (bessely(-1 + 5 * I, x) / 2 - bessely(1 + 5 * I, x) / 2)) + (x**2 + 25) * (a * besselj(5 * I, x) + b * bessely(5 * I, x))) == 0 assert besselsimp(81 * x**2 * ( a * (besselj(Rational(-5, 3), 9 * x) - 2 * besselj(Rational(1, 3), 9 * x) + besselj(Rational(7, 3), 9 * x)) + b * (bessely(Rational(-5, 3), 9 * x) - 2 * bessely(Rational(1, 3), 9 * x) + bessely(Rational(7, 3), 9 * x))) / 4 + x * (a * (9 * besselj(Rational(-2, 3), 9 * x) / 2 - 9 * besselj(Rational(4, 3), 9 * x) / 2) + b * (9 * bessely(Rational(-2, 3), 9 * x) / 2 - 9 * bessely(Rational(4, 3), 9 * x) / 2)) + (81 * x**2 - Rational(1, 9)) * (a * besselj(Rational(1, 3), 9 * x) + b * bessely(Rational(1, 3), 9 * x))) == 0 assert besselsimp( besselj(a - 1, x) + besselj(a + 1, x) - 2 * a * besselj(a, x) / x) == 0 assert besselsimp(besselj(a - 1, x) + besselj(a + 1, x) + besselj(a, x)) == (2 * a + x) * besselj(a, x) / x assert besselsimp(x**2* besselj(a,x) + x**3*besselj(a+1, x) + besselj(a+2, x)) == \ 2*a*x*besselj(a + 1, x) + x**3*besselj(a + 1, x) - x**2*besselj(a + 2, x) + 2*x*besselj(a + 1, x) + besselj(a + 2, x)
def test_mellin_transform_bessel(): from sympy import Max MT = mellin_transform # 8.4.19 assert MT(besselj(a, 2*sqrt(x)), x, s) == \ (gamma(a/2 + s)/gamma(a/2 - s + 1), (-re(a)/2, S(3)/4), True) assert MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(-2*s + S(1)/2)*gamma(a/2 + s + S(1)/2)/( gamma(-a/2 - s + 1)*gamma(a - 2*s + 1)), ( -re(a)/2 - S(1)/2, S(1)/4), True) assert MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (2**a*gamma(a/2 + s)*gamma(-2*s + S(1)/2)/( gamma(-a/2 - s + S(1)/2)*gamma(a - 2*s + 1)), ( -re(a)/2, S(1)/4), True) assert MT(besselj(a, sqrt(x))**2, x, s) == \ (gamma(a + s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - s)*gamma(1 + a - s)), (-re(a), S(1)/2), True) assert MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s) == \ (gamma(s)*gamma(S(1)/2 - s) / (sqrt(pi)*gamma(1 - a - s)*gamma(1 + a - s)), (0, S(1)/2), True) # NOTE: prudnikov gives the strip below as (1/2 - re(a), 1). As far as # I can see this is wrong (since besselj(z) ~ 1/sqrt(z) for z large) assert MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s) == \ (gamma(1 - s)*gamma(a + s - S(1)/2) / (sqrt(pi)*gamma(S(3)/2 - s)*gamma(a - s + S(1)/2)), (S(1)/2 - re(a), S(1)/2), True) assert MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s) == \ (4**s*gamma(1 - 2*s)*gamma((a + b)/2 + s) / (gamma(1 - s + (b - a)/2)*gamma(1 - s + (a - b)/2) *gamma( 1 - s + (a + b)/2)), (-(re(a) + re(b))/2, S(1)/2), True) assert MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)[1:] == \ ((Max(re(a), -re(a)), S(1)/2), True) # Section 8.4.20 assert MT(bessely(a, 2*sqrt(x)), x, s) == \ (-cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)/pi, (Max(-re(a)/2, re(a)/2), S(3)/4), True) assert MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*sin(pi*(a/2 - s))*gamma(S(1)/2 - 2*s) * gamma((1 - a)/2 + s)*gamma((1 + a)/2 + s) / (sqrt(pi)*gamma(1 - s - a/2)*gamma(1 - s + a/2)), (Max(-(re(a) + 1)/2, (re(a) - 1)/2), S(1)/4), True) assert MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-4**s*cos(pi*(a/2 - s))*gamma(s - a/2)*gamma(s + a/2)*gamma(S(1)/2 - 2*s) / (sqrt(pi)*gamma(S(1)/2 - s - a/2)*gamma(S(1)/2 - s + a/2)), (Max(-re(a)/2, re(a)/2), S(1)/4), True) assert MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s) == \ (-cos(pi*s)*gamma(s)*gamma(a + s)*gamma(S(1)/2 - s) / (pi**S('3/2')*gamma(1 + a - s)), (Max(-re(a), 0), S(1)/2), True) assert MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s) == \ (-4**s*cos(pi*(a/2 - b/2 + s))*gamma(1 - 2*s) * gamma(a/2 - b/2 + s)*gamma(a/2 + b/2 + s) / (pi*gamma(a/2 - b/2 - s + 1)*gamma(a/2 + b/2 - s + 1)), (Max((-re(a) + re(b))/2, (-re(a) - re(b))/2), S(1)/2), True) # NOTE bessely(a, sqrt(x))**2 and bessely(a, sqrt(x))*bessely(b, sqrt(x)) # are a mess (no matter what way you look at it ...) assert MT(bessely(a, sqrt(x))**2, x, s)[1:] == \ ((Max(-re(a), 0, re(a)), S(1)/2), True) # Section 8.4.22 # TODO we can't do any of these (delicate cancellation) # Section 8.4.23 assert MT(besselk(a, 2*sqrt(x)), x, s) == \ (gamma( s - a/2)*gamma(s + a/2)/2, (Max(-re(a)/2, re(a)/2), oo), True) assert MT( besselj(a, 2 * sqrt(2 * sqrt(x))) * besselk(a, 2 * sqrt(2 * sqrt(x))), x, s) == (4**(-s) * gamma(2 * s) * gamma(a / 2 + s) / (2 * gamma(a / 2 - s + 1)), (Max(0, -re(a) / 2), oo), True) # TODO bessely(a, x)*besselk(a, x) is a mess assert MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s) == \ (gamma(s)*gamma( a + s)*gamma(-s + S(1)/2)/(2*sqrt(pi)*gamma(a - s + 1)), (Max(-re(a), 0), S(1)/2), True) assert MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s) == \ (2**(2*s - 1)*gamma(-2*s + 1)*gamma(-a/2 + b/2 + s)* \ gamma(a/2 + b/2 + s)/(gamma(-a/2 + b/2 - s + 1)* \ gamma(a/2 + b/2 - s + 1)), (Max(-re(a)/2 - re(b)/2, \ re(a)/2 - re(b)/2), S(1)/2), True) # TODO products of besselk are a mess mt = MT(exp(-x / 2) * besselk(a, x / 2), x, s) mt0 = gammasimp((trigsimp(gammasimp(mt[0].expand(func=True))))) assert mt0 == 2 * pi**(S(3) / 2) * cos(pi * s) * gamma(-s + S(1) / 2) / ( (cos(2 * pi * a) - cos(2 * pi * s)) * gamma(-a - s + 1) * gamma(a - s + 1)) assert mt[1:] == ((Max(-re(a), re(a)), oo), True)
def test_linear_subs(): from sympy import besselj assert integrate(sin(x - 1), x, meijerg=True) == -cos(1 - x) assert integrate(besselj(1, x - 1), x, meijerg=True) == -besselj(0, 1 - x)
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)