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_K(): assert K(0) == pi / 2 assert K(S(1) / 2) == 8 * pi ** (S(3) / 2) / gamma(-S(1) / 4) ** 2 assert K(1) == zoo assert K(-1) == gamma(S(1) / 4) ** 2 / (4 * sqrt(2 * pi)) assert K(oo) == 0 assert K(-oo) == 0 assert K(I * oo) == 0 assert K(-I * oo) == 0 assert K(zoo) == 0 assert K(z).diff(z) == (E(z) - (1 - z) * K(z)) / (2 * z * (1 - z)) assert td(K(z), z) zi = Symbol("z", real=False) assert K(zi).conjugate() == K(zi.conjugate()) zr = Symbol("z", real=True, negative=True) assert K(zr).conjugate() == K(zr) assert K(z).rewrite(hyper) == (pi / 2) * hyper((S.Half, S.Half), (S.One,), z) assert tn(K(z), (pi / 2) * hyper((S.Half, S.Half), (S.One,), z)) assert K(z).rewrite(meijerg) == meijerg(((S.Half, S.Half), []), ((S.Zero,), (S.Zero,)), -z) / 2 assert tn(K(z), meijerg(((S.Half, S.Half), []), ((S.Zero,), (S.Zero,)), -z) / 2) assert K(z).series( z ) == pi / 2 + pi * z / 8 + 9 * pi * z ** 2 / 128 + 25 * pi * z ** 3 / 512 + 1225 * pi * z ** 4 / 32768 + 3969 * pi * z ** 5 / 131072 + O( z ** 6 )
def test_to_expr(): x = symbols('x') R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx') p = HolonomicFunction(Dx - 1, x, 0, [1]).to_expr() q = exp(x) assert p == q p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr() q = cos(x) assert p == q p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr() q = cosh(x) assert p == q p = HolonomicFunction(2 + (4*x - 1)*Dx + \ (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr().expand() q = 1/(x**2 - 2*x + 1) assert p == q p = expr_to_holonomic(sin(x)**2/x).integrate((x, 0, x)).to_expr() q = (sin(x)**2/x).integrate((x, 0, x)) assert p == q C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3') p = expr_to_holonomic(log(1+x**2)).to_expr() q = C_2*log(x**2 + 1) assert p == q p = expr_to_holonomic(log(1+x**2)).diff().to_expr() q = C_0*x/(x**2 + 1) assert p == q p = expr_to_holonomic(erf(x) + x).to_expr() q = 3*C_3*x - 3*sqrt(pi)*C_3*erf(x)/2 + x + 2*x/sqrt(pi) assert p == q p = expr_to_holonomic(sqrt(x), x0=1).to_expr() assert p == sqrt(x) assert expr_to_holonomic(sqrt(x)).to_expr() == sqrt(x) p = expr_to_holonomic(sqrt(1 + x**2)).to_expr() assert p == sqrt(1+x**2) p = expr_to_holonomic((2*x**2 + 1)**(S(2)/3)).to_expr() assert p == (2*x**2 + 1)**(S(2)/3) p = expr_to_holonomic(sqrt(-x**2+2*x)).to_expr() assert p == sqrt(x)*sqrt(-x + 2) p = expr_to_holonomic((-2*x**3+7*x)**(S(2)/3)).to_expr() q = x**(S(2)/3)*(-2*x**2 + 7)**(S(2)/3) assert p == q p = from_hyper(hyper((-2, -3), (S(1)/2, ), x)) s = hyperexpand(hyper((-2, -3), (S(1)/2, ), x)) D_0 = Symbol('D_0') C_0 = Symbol('C_0') assert (p.to_expr().subs({C_0:1, D_0:0}) - s).simplify() == 0 p.y0 = {0: [1], S(1)/2: [0]} assert p.to_expr() == s assert expr_to_holonomic(x**5).to_expr() == x**5 assert expr_to_holonomic(2*x**3-3*x**2).to_expr().expand() == \ 2*x**3-3*x**2 a = symbols("a") p = (expr_to_holonomic(1.4*x)*expr_to_holonomic(a*x, x)).to_expr() q = 1.4*a*x**2 assert p == q p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(a*x, x)).to_expr() q = x*(a + 1.4) assert p == q p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(x)).to_expr() assert p == 2.4*x
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_K(): assert K(0) == pi/2 assert K(S(1)/2) == 8*pi**(S(3)/2)/gamma(-S(1)/4)**2 assert K(1) == zoo assert K(-1) == gamma(S(1)/4)**2/(4*sqrt(2*pi)) assert K(oo) == 0 assert K(-oo) == 0 assert K(I*oo) == 0 assert K(-I*oo) == 0 assert K(zoo) == 0 assert K(z).diff(z) == (E(z) - (1 - z)*K(z))/(2*z*(1 - z)) assert td(K(z), z) zi = Symbol('z', real=False) assert K(zi).conjugate() == K(zi.conjugate()) zr = Symbol('z', real=True, negative=True) assert K(zr).conjugate() == K(zr) assert K(z).rewrite(hyper) == \ (pi/2)*hyper((S.Half, S.Half), (S.One,), z) assert tn(K(z), (pi/2)*hyper((S.Half, S.Half), (S.One,), z)) assert K(z).rewrite(meijerg) == \ meijerg(((S.Half, S.Half), []), ((S.Zero,), (S.Zero,)), -z)/2 assert tn(K(z), meijerg(((S.Half, S.Half), []), ((S.Zero,), (S.Zero,)), -z)/2)
def test_E(): assert E(z, 0) == z assert E(0, m) == 0 assert E(i*pi/2, m) == i*E(m) assert E(z, oo) == zoo assert E(z, -oo) == zoo assert E(0) == pi/2 assert E(1) == 1 assert E(oo) == I*oo assert E(-oo) == oo assert E(zoo) == zoo assert E(-z, m) == -E(z, m) assert E(z, m).diff(z) == sqrt(1 - m*sin(z)**2) assert E(z, m).diff(m) == (E(z, m) - F(z, m))/(2*m) assert E(z).diff(z) == (E(z) - K(z))/(2*z) r = randcplx() assert td(E(r, m), m) assert td(E(z, r), z) assert td(E(z), z) mi = Symbol('m', real=False) assert E(z, mi).conjugate() == E(z.conjugate(), mi.conjugate()) mr = Symbol('m', real=True, negative=True) assert E(z, mr).conjugate() == E(z.conjugate(), mr) assert E(z).rewrite(hyper) == (pi/2)*hyper((-S.Half, S.Half), (S.One,), z) assert tn(E(z), (pi/2)*hyper((-S.Half, S.Half), (S.One,), z)) assert E(z).rewrite(meijerg) == \ -meijerg(((S.Half, S(3)/2), []), ((S.Zero,), (S.Zero,)), -z)/4 assert tn(E(z), -meijerg(((S.Half, S(3)/2), []), ((S.Zero,), (S.Zero,)), -z)/4)
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_plan(): assert devise_plan(Hyper_Function([0], ()), Hyper_Function([0], ()), z) == [] with raises(ValueError): devise_plan(Hyper_Function([1], ()), Hyper_Function((), ()), z) with raises(ValueError): devise_plan(Hyper_Function([2], [1]), Hyper_Function([2], [2]), z) with raises(ValueError): devise_plan(Hyper_Function([2], []), Hyper_Function([S("1/2")], []), z) # We cannot use pi/(10000 + n) because polys is insanely slow. a1, a2, b1 = map(lambda n: randcplx(n), range(3)) b1 += 2*I h = hyper([a1, a2], [b1], z) h2 = hyper((a1 + 1, a2), [b1], z) assert tn(apply_operators(h, devise_plan(Hyper_Function((a1 + 1, a2), [b1]), Hyper_Function((a1, a2), [b1]), z), op), h2, z) h2 = hyper((a1 + 1, a2 - 1), [b1], z) assert tn(apply_operators(h, devise_plan(Hyper_Function((a1 + 1, a2 - 1), [b1]), Hyper_Function((a1, a2), [b1]), z), op), h2, z)
def test_polynomial(): from sympy import oo assert hyperexpand(hyper([], [-1], z)) == oo assert hyperexpand(hyper([-2], [-1], z)) == oo assert hyperexpand(hyper([0, 0], [-1], z)) == 1 assert can_do([-5, -2, randcplx(), randcplx()], [-10, randcplx()]) assert hyperexpand(hyper((-1, 1), (-2,), z)) == 1 + z/2
def test_hyper_unpolarify(): from sympy import exp_polar a = exp_polar(2*pi*I)*x b = x assert hyper([], [], a).argument == b assert hyper([0], [], a).argument == a assert hyper([0], [0], a).argument == b assert hyper([0, 1], [0], a).argument == a
def test_hyperexpand_parametric(): assert ( hyperexpand(hyper([a, S(1) / 2 + a], [S(1) / 2], z)) == (1 + sqrt(z)) ** (-2 * a) / 2 + (1 - sqrt(z)) ** (-2 * a) / 2 ) assert hyperexpand(hyper([a, -S(1) / 2 + a], [2 * a], z)) == 2 ** (2 * a - 1) * ((-z + 1) ** (S(1) / 2) + 1) ** ( -2 * a + 1 )
def test_limits(): k, x = symbols('k, x') assert hyper((1,), (S(4)/3, S(5)/3), k**2).series(k) == \ hyper((1,), (S(4)/3, S(5)/3), 0) + \ 9*k**2*hyper((2,), (S(7)/3, S(8)/3), 0)/20 + \ 81*k**4*hyper((3,), (S(10)/3, S(11)/3), 0)/1120 + \ O(k**6) # issue 6350 assert limit(meijerg((), (), (1,), (0,), -x), x, 0) == \ meijerg(((), ()), ((1,), (0,)), 0) # issue 6052
def test_hyper_rewrite_sum(): from sympy import RisingFactorial, factorial, Dummy, Sum _k = Dummy("k") assert replace_dummy(hyper((1, 2), (1, 3), x).rewrite(Sum), _k) == \ Sum(x**_k / factorial(_k) * RisingFactorial(2, _k) / RisingFactorial(3, _k), (_k, 0, oo)) assert hyper((1, 2, 3), (-1, 3), z).rewrite(Sum) == \ hyper((1, 2, 3), (-1, 3), z)
def test_hyperexpand(): # Luke, Y. L. (1969), The Special Functions and Their Approximations, # Volume 1, section 6.2 assert hyperexpand(hyper([], [], z)) == exp(z) assert hyperexpand(hyper([1, 1], [2], -z) * z) == log(1 + z) assert hyperexpand(hyper([], [S.Half], -z ** 2 / 4)) == cos(z) assert hyperexpand(z * hyper([], [S("3/2")], -z ** 2 / 4)) == sin(z) assert hyperexpand(hyper([S("1/2"), S("1/2")], [S("3/2")], z ** 2) * z) == asin(z)
def test_hyperexpand_special(): assert hyperexpand(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(c - a - b)/gamma(c - a)/gamma(c - b) assert hyperexpand(hyper([a, b], [1 + a - b], -1)) == \ gamma(1 + a/2)*gamma(1 + a - b)/gamma(1 + a)/gamma(1 + a/2 - b) assert hyperexpand(hyper([a, b], [1 + b - a], -1)) == \ gamma(1 + b/2)*gamma(1 + b - a)/gamma(1 + b)/gamma(1 + b/2 - a) assert hyperexpand(meijerg([1 - z - a/2], [1 - z + a/2], [b/2], [-b/2], 1)) == \ gamma(1 - 2*z)*gamma(z + a/2 + b/2)/gamma(1 - z + a/2 - b/2) \ /gamma(1 - z - a/2 + b/2)/gamma(1 - z + a/2 + b/2)
def test_hyperexpand(): # Luke, Y. L. (1969), The Special Functions and Their Approximations, # Volume 1, section 6.2 assert hyperexpand(hyper([], [], z)) == exp(z) assert hyperexpand(hyper([1, 1], [2], -z)*z) == log(1 + z) assert hyperexpand(hyper([], [S.Half], -z**2/4)) == cos(z) assert hyperexpand(z*hyper([], [S('3/2')], -z**2/4)) == sin(z) assert hyperexpand(hyper([S('1/2'), S('1/2')], [S('3/2')], z**2)*z) \ == asin(z) assert isinstance(Sum(binomial(2, z)*z**2, (z, 0, a)).doit(), Expr)
def can_do(ap, bq, numerical=True): r = hyperexpand(hyper(ap, bq, z)) if r.has(hyper): return False if not numerical: return True repl = {} for n, a in enumerate(r.free_symbols - set([z])): repl[a] = randcplx(n) return tn(hyper(ap, bq, z).subs(repl), r.subs(repl), z)
def test_plan_derivatives(): a1, a2, a3 = 1, 2, S("1/2") b1, b2 = 3, S("5/2") h = hyper((a1, a2, a3), (b1, b2), z) h2 = hyper((a1 + 1, a2 + 1, a3 + 2), (b1 + 1, b2 + 1), z) ops = devise_plan(IndexPair((a1 + 1, a2 + 1, a3 + 2), (b1 + 1, b2 + 1)), IndexPair((a1, a2, a3), (b1, b2)), z) f = Formula((a1, a2, a3), (b1, b2), z, h, []) deriv = make_derivative_operator(f.M, z) assert tn((apply_operators(f.C, ops, deriv) * f.B)[0], h2, z) h2 = hyper((a1, a2 - 1, a3 - 2), (b1 - 1, b2 - 1), z) ops = devise_plan(IndexPair((a1, a2 - 1, a3 - 2), (b1 - 1, b2 - 1)), IndexPair((a1, a2, a3), (b1, b2)), z) assert tn((apply_operators(f.C, ops, deriv) * f.B)[0], h2, z)
def test_from_hyper(): x = symbols('x') R, Dx = DifferentialOperators(QQ.old_poly_ring(x), 'Dx') p = hyper([1, 1], [S(3)/2], x**2/4) q = HolonomicFunction((4*x) + (5*x**2 - 8)*Dx + (x**3 - 4*x)*Dx**2, x, 1, [2*sqrt(3)*pi/9, -4*sqrt(3)*pi/27 + 4/3]) r = from_hyper(p) assert r == q p = from_hyper(hyper([1], [S(3)/2], x**2/4)) q = HolonomicFunction(-x + (-x**2/2 + 2)*Dx + x*Dx**2, x) x0 = 1 y0 = '[sqrt(pi)*exp(1/4)*erf(1/2), -sqrt(pi)*exp(1/4)*erf(1/2)/2 + 1]' assert sstr(p.y0) == y0 assert q.annihilator == p.annihilator
def test_hyperexpand_bases(): assert ( hyperexpand(hyper([2], [a], z)) == a + z ** (-a + 1) * (-a ** 2 + 3 * a + z * (a - 1) - 2) * exp(z) * lowergamma(a - 1, z) - 1 ) # TODO [a+1, a-S.Half], [2*a] assert hyperexpand(hyper([1, 2], [3], z)) == -2 / z - 2 * log(exp_polar(-I * pi) * z + 1) / z ** 2 assert hyperexpand(hyper([S.Half, 2], [S(3) / 2], z)) == -1 / (2 * z - 2) + log((sqrt(z) + 1) / (-sqrt(z) + 1)) / ( 4 * sqrt(z) ) assert hyperexpand(hyper([S(1) / 2, S(1) / 2], [S(5) / 2], z)) == (-3 * z + 3) / 4 / (z * sqrt(-z + 1)) + ( 6 * z - 3 ) * asin(sqrt(z)) / (4 * z ** (S(3) / 2)) assert hyperexpand(hyper([1, 2], [S(3) / 2], z)) == -1 / (2 * z - 2) - asin(sqrt(z)) / ( sqrt(z) * (2 * z - 2) * sqrt(-z + 1) ) assert hyperexpand(hyper([-S.Half - 1, 1, 2], [S.Half, 3], z)) == sqrt(z) * (6 * z / 7 - S(6) / 5) * atanh( sqrt(z) ) + (-30 * z ** 2 + 32 * z - 6) / 35 / z - 6 * log(-z + 1) / (35 * z ** 2) assert hyperexpand(hyper([1 + S.Half, 1, 1], [2, 2], z)) == -4 * log(sqrt(-z + 1) / 2 + S(1) / 2) / z # TODO hyperexpand(hyper([a], [2*a + 1], z)) # TODO [S.Half, a], [S(3)/2, a+1] assert hyperexpand(hyper([2], [b, 1], z)) == z ** (-b / 2 + S(1) / 2) * besseli(b - 1, 2 * sqrt(z)) * gamma( b ) + z ** (-b / 2 + 1) * besseli(b, 2 * sqrt(z)) * gamma(b)
def test_hyper(): raises(TypeError, 'hyper(1, 2, z)') assert hyper((1, 2),(1,), z) == hyper(Tuple(1, 2), Tuple(1), z) h = hyper((1, 2), (3, 4, 5), z) assert h.ap == Tuple(1, 2) assert h.bq == Tuple(3, 4, 5) assert h.argument == z assert h.is_commutative is True # just a few checks to make sure that all arguments go where they should assert tn(hyper(Tuple(), Tuple(), z), exp(z), z) assert tn(z*hyper((1, 1), Tuple(2), -z), log(1 + z), z) # differentiation h = hyper((randcplx(), randcplx(), randcplx()), (randcplx(), randcplx()), z) assert td(h, z) a1, a2, b1, b2, b3 = symbols('a1:3, b1:4') assert hyper((a1, a2), (b1, b2, b3), z).diff(z) == \ a1*a2/(b1*b2*b3) * hyper((a1+1, a2+1), (b1+1, b2+1, b3+1), z) # differentiation wrt parameters is not supported raises(NotImplementedError, 'hyper((z,), (), z).diff(z)')
def test_hyper_printing(): from sympy import pi, Tuple from sympy.abc import x, z assert latex(meijerg(Tuple(pi, pi, x), Tuple(1), \ (0,1), Tuple(1, 2, 3/pi),z)) == \ r'{G_{4, 5}^{2, 3}\left.\left(\begin{matrix} \pi, \pi, x & 1 \\0, 1 & 1, 2, \frac{3}{\pi} \end{matrix} \right| {z} \right)}' assert latex(meijerg(Tuple(), Tuple(1), (0,), Tuple(),z)) == \ r'{G_{1, 1}^{1, 0}\left.\left(\begin{matrix} & 1 \\0 & \end{matrix} \right| {z} \right)}' assert latex(hyper((x, 2), (3,), z)) == \ r'{{}_{2}F_{1}\left.\left(\begin{matrix} x, 2 ' \ r'\\ 3 \end{matrix}\right| {z} \right)}' assert latex(hyper(Tuple(), Tuple(1), z)) == \ r'{{}_{0}F_{1}\left.\left(\begin{matrix} ' \ r'\\ 1 \end{matrix}\right| {z} \right)}'
def can_do(ap, bq, numerical=True, div=1, lowerplane=False): from sympy import exp_polar, exp r = hyperexpand(hyper(ap, bq, z)) if r.has(hyper): return False if not numerical: return True repl = {} for n, a in enumerate(r.free_symbols - set([z])): repl[a] = randcplx(n) / div [a, b, c, d] = [2, -1, 3, 1] if lowerplane: [a, b, c, d] = [2, -2, 3, -1] return tn(hyper(ap, bq, z).subs(repl), r.replace(exp_polar, exp).subs(repl), z, a=a, b=b, c=c, d=d)
def test_shifted_sum(): from sympy import simplify assert ( simplify(hyperexpand(z ** 4 * hyper([2], [3, S("3/2")], -z ** 2))) == z * sin(2 * z) + (-z ** 2 + S.Half) * cos(2 * z) - S.Half )
def test_branch_bug(): assert hyperexpand(hyper((-S(1)/3, S(1)/2), (S(2)/3, S(3)/2), -z)) == \ -z**S('1/3')*lowergamma(exp_polar(I*pi)/3, z)/5 \ + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z)) assert hyperexpand(meijerg([S(7)/6, 1], [], [S(2)/3], [S(1)/6, 0], z)) == \ 2*z**S('2/3')*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) - 2*lowergamma( S(2)/3, z)/z**S('2/3'))*gamma(S(2)/3)/gamma(S(5)/3)
def test_studentt(): nu = Symbol("nu", positive=True) X = StudentT('x', nu) assert density(X)(x) == (1 + x**2/nu)**(-nu/2 - S(1)/2)/(sqrt(nu)*beta(S(1)/2, nu/2)) assert cdf(X)(x) == S(1)/2 + x*gamma(nu/2 + S(1)/2)*hyper((S(1)/2, nu/2 + S(1)/2), (S(3)/2,), -x**2/nu)/(sqrt(pi)*sqrt(nu)*gamma(nu/2))
def test_formulae(): from sympy.simplify.hyperexpand import FormulaCollection formulae = FormulaCollection().formulae for formula in formulae: h = hyper(formula.indices.ap, formula.indices.bq, formula.z) rep = {} for n, sym in enumerate(formula.symbols): rep[sym] = randcplx(n) # NOTE hyperexpand returns truly branched functions. We know we are # on the main sheet, but numerical evaluation can still go wrong # (e.g. if exp_polar cannot be evalf'd). # Just replace all exp_polar by exp, this usually works. # first test if the closed-form is actually correct h = h.subs(rep) closed_form = formula.closed_form.subs(rep).rewrite('nonrepsmall') z = formula.z assert tn(h, closed_form.replace(exp_polar, exp), z) # now test the computed matrix cl = (formula.C * formula.B)[0].subs(rep).rewrite('nonrepsmall') assert tn(closed_form.replace(exp_polar, exp), cl.replace(exp_polar, exp), z) deriv1 = z*formula.B.applyfunc(lambda t: t.rewrite('nonrepsmall')).diff(z) deriv2 = formula.M * formula.B for d1, d2 in zip(deriv1, deriv2): assert tn(d1.subs(rep).replace(exp_polar, exp), d2.subs(rep).rewrite('nonrepsmall').replace(exp_polar, exp), z)
def test_issue_10681(): from sympy import RR from sympy.abc import R, r f = integrate(r**2*(R**2-r**2)**0.5, r, meijerg=True) g = (1.0/3)*R**1.0*r**3*hyper((-0.5, S(3)/2), (S(5)/2,), r**2*exp_polar(2*I*pi)/R**2) assert RR.almosteq((f/g).n(), 1.0, 1e-12)
def test_jacobi(): n = Symbol("n") a = Symbol("a") b = Symbol("b") assert jacobi(0, a, b, x) == 1 assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1) assert jacobi(n, a, a, x) == RisingFactorial(a + 1, n)*gegenbauer(n, a + S(1)/2, x)/RisingFactorial(2*a + 1, n) assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x)* factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1))) assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x)* gamma(-b + n + 1)/gamma(n + 1)) assert jacobi(n, 0, 0, x) == legendre(n, x) assert jacobi(n, S.Half, S.Half, x) == RisingFactorial(S(3)/2, n)*chebyshevu(n, x)/factorial(n + 1) assert jacobi(n, -S.Half, -S.Half, x) == RisingFactorial(S(1)/2, n)*chebyshevt(n, x)/factorial(n) X = jacobi(n, a, b, x) assert isinstance(X, jacobi) assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x) assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper((-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1)) assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n) m = Symbol("m", positive=True) assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m) assert conjugate(jacobi(m, a, b, x)) == jacobi(m, conjugate(a), conjugate(b), conjugate(x)) assert diff(jacobi(n,a,b,x), n) == Derivative(jacobi(n, a, b, x), n) assert diff(jacobi(n,a,b,x), x) == (a/2 + b/2 + n/2 + S(1)/2)*jacobi(n - 1, a + 1, b + 1, x)
def test_meijerg_with_Floats(): # see issue #10681 from sympy import RR f = meijerg(((3.0, 1), ()), ((S(3)/2,), (0,)), z) a = -2.3632718012073 g = a*z**(S(3)/2)*hyper((-0.5, S(3)/2), (S(5)/2,), z*exp_polar(I*pi)) assert RR.almosteq((hyperexpand(f)/g).n(), 1.0, 1e-12)
def test_hyperrep(): from sympy.functions.special.hyper import (HyperRep, HyperRep_atanh, HyperRep_power1, HyperRep_power2, HyperRep_log1, HyperRep_asin1, HyperRep_asin2, HyperRep_sqrts1, HyperRep_sqrts2, HyperRep_log2, HyperRep_cosasin, HyperRep_sinasin) # First test the base class works. from sympy import Piecewise, exp_polar a, b, c, d, z = symbols('a b c d z') class myrep(HyperRep): @classmethod def _expr_small(cls, x): return a @classmethod def _expr_small_minus(cls, x): return b @classmethod def _expr_big(cls, x, n): return c*n @classmethod def _expr_big_minus(cls, x, n): return d*n assert myrep(z).rewrite('nonrep') == Piecewise((0, abs(z) > 1), (a, True)) assert myrep(exp_polar(I*pi)*z).rewrite('nonrep') == \ Piecewise((0, abs(z) > 1), (b, True)) assert myrep(exp_polar(2*I*pi)*z).rewrite('nonrep') == \ Piecewise((c, abs(z) > 1), (a, True)) assert myrep(exp_polar(3*I*pi)*z).rewrite('nonrep') == \ Piecewise((d, abs(z) > 1), (b, True)) assert myrep(exp_polar(4*I*pi)*z).rewrite('nonrep') == \ Piecewise((2*c, abs(z) > 1), (a, True)) assert myrep(exp_polar(5*I*pi)*z).rewrite('nonrep') == \ Piecewise((2*d, abs(z) > 1), (b, True)) assert myrep(z).rewrite('nonrepsmall') == a assert myrep(exp_polar(I*pi)*z).rewrite('nonrepsmall') == b def t(func, hyp, z): """ Test that func is a valid representation of hyp. """ # First test that func agrees with hyp for small z if not tn(func.rewrite('nonrepsmall'), hyp, z, a=Rational(-1, 2), b=Rational(-1, 2), c=S.Half, d=S.Half): return False # Next check that the two small representations agree. if not tn( func.rewrite('nonrepsmall').subs( z, exp_polar(I*pi)*z).replace(exp_polar, exp), func.subs(z, exp_polar(I*pi)*z).rewrite('nonrepsmall'), z, a=Rational(-1, 2), b=Rational(-1, 2), c=S.Half, d=S.Half): return False # Next check continuity along exp_polar(I*pi)*t expr = func.subs(z, exp_polar(I*pi)*z).rewrite('nonrep') if abs(expr.subs(z, 1 + 1e-15).n() - expr.subs(z, 1 - 1e-15).n()) > 1e-10: return False # Finally check continuity of the big reps. def dosubs(func, a, b): rv = func.subs(z, exp_polar(a)*z).rewrite('nonrep') return rv.subs(z, exp_polar(b)*z).replace(exp_polar, exp) for n in [0, 1, 2, 3, 4, -1, -2, -3, -4]: expr1 = dosubs(func, 2*I*pi*n, I*pi/2) expr2 = dosubs(func, 2*I*pi*n + I*pi, -I*pi/2) if not tn(expr1, expr2, z): return False expr1 = dosubs(func, 2*I*pi*(n + 1), -I*pi/2) expr2 = dosubs(func, 2*I*pi*n + I*pi, I*pi/2) if not tn(expr1, expr2, z): return False return True # Now test the various representatives. a = Rational(1, 3) assert t(HyperRep_atanh(z), hyper([S.Half, 1], [Rational(3, 2)], z), z) assert t(HyperRep_power1(a, z), hyper([-a], [], z), z) assert t(HyperRep_power2(a, z), hyper([a, a - S.Half], [2*a], z), z) assert t(HyperRep_log1(z), -z*hyper([1, 1], [2], z), z) assert t(HyperRep_asin1(z), hyper([S.Half, S.Half], [Rational(3, 2)], z), z) assert t(HyperRep_asin2(z), hyper([1, 1], [Rational(3, 2)], z), z) assert t(HyperRep_sqrts1(a, z), hyper([-a, S.Half - a], [S.Half], z), z) assert t(HyperRep_sqrts2(a, z), -2*z/(2*a + 1)*hyper([-a - S.Half, -a], [S.Half], z).diff(z), z) assert t(HyperRep_log2(z), -z/4*hyper([Rational(3, 2), 1, 1], [2, 2], z), z) assert t(HyperRep_cosasin(a, z), hyper([-a, a], [S.Half], z), z) assert t(HyperRep_sinasin(a, z), 2*a*z*hyper([1 - a, 1 + a], [Rational(3, 2)], z), z)
def test_radius_of_convergence(): assert hyper((1, 2), [3], z).radius_of_convergence == 1 assert hyper((1, 2), [3, 4], z).radius_of_convergence is oo assert hyper((1, 2, 3), [4], z).radius_of_convergence == 0 assert hyper((0, 1, 2), [4], z).radius_of_convergence is oo assert hyper((-1, 1, 2), [-4], z).radius_of_convergence == 0 assert hyper((-1, -2, 2), [-1], z).radius_of_convergence is oo assert hyper((-1, 2), [-1, -2], z).radius_of_convergence == 0 assert hyper([-1, 1, 3], [-2, 2], z).radius_of_convergence == 1 assert hyper([-1, 1], [-2, 2], z).radius_of_convergence is oo assert hyper([-1, 1, 3], [-2], z).radius_of_convergence == 0 assert hyper((-1, 2, 3, 4), [], z).radius_of_convergence is oo assert hyper([1, 1], [3], 1).convergence_statement == True assert hyper([1, 1], [2], 1).convergence_statement == False assert hyper([1, 1], [2], -1).convergence_statement == True assert hyper([1, 1], [1], -1).convergence_statement == False
def test_fresnel(): assert fresnels(0) == 0 assert fresnels(oo) == S.Half assert fresnels(-oo) == -S.Half assert fresnels(z) == fresnels(z) assert fresnels(-z) == -fresnels(z) assert fresnels(I * z) == -I * fresnels(z) assert fresnels(-I * z) == I * fresnels(z) assert conjugate(fresnels(z)) == fresnels(conjugate(z)) assert fresnels(z).diff(z) == sin(pi * z**2 / 2) assert fresnels(z).rewrite(erf) == (S.One + I) / 4 * (erf( (S.One + I) / 2 * sqrt(pi) * z) - I * erf( (S.One - I) / 2 * sqrt(pi) * z)) assert fresnels(z).rewrite(hyper) == \ pi*z**3/6 * hyper([S(3)/4], [S(3)/2, S(7)/4], -pi**2*z**4/16) assert fresnels(z).series(z, n=15) == \ pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15) assert fresnels(w).is_real is True assert fresnels(z).as_real_imag() == \ ((fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 + fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2, I*(fresnels(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) - fresnels(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) * re(z)*Abs(im(z))/(2*im(z)*Abs(re(z))))) assert fresnels(2 + 3 * I).as_real_imag() == ( fresnels(2 + 3 * I) / 2 + fresnels(2 - 3 * I) / 2, I * (fresnels(2 - 3 * I) - fresnels(2 + 3 * I)) / 2) assert expand_func(integrate(fresnels(z), z)) == \ z*fresnels(z) + cos(pi*z**2/2)/pi assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**(S(9)/4) * \ meijerg(((), (1,)), ((S(3)/4,), (S(1)/4, 0)), -pi**2*z**4/16)/(2*(-z)**(S(3)/4)*(z**2)**(S(3)/4)) assert fresnelc(0) == 0 assert fresnelc(oo) == S.Half assert fresnelc(-oo) == -S.Half assert fresnelc(z) == fresnelc(z) assert fresnelc(-z) == -fresnelc(z) assert fresnelc(I * z) == I * fresnelc(z) assert fresnelc(-I * z) == -I * fresnelc(z) assert conjugate(fresnelc(z)) == fresnelc(conjugate(z)) assert fresnelc(z).diff(z) == cos(pi * z**2 / 2) assert fresnelc(z).rewrite(erf) == (S.One - I) / 4 * (erf( (S.One + I) / 2 * sqrt(pi) * z) + I * erf( (S.One - I) / 2 * sqrt(pi) * z)) assert fresnelc(z).rewrite(hyper) == \ z * hyper([S.One/4], [S.One/2, S(5)/4], -pi**2*z**4/16) assert fresnelc(z).series(z, n=15) == \ z - pi**2*z**5/40 + pi**4*z**9/3456 - pi**6*z**13/599040 + O(z**15) # issue 6510 assert fresnels(z).series(z, S.Infinity) == \ (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + \ (3/(pi**3*z**5) - 1/(pi*z) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + S.Half assert fresnelc(z).series(z, S.Infinity) == \ (-1/(pi**2*z**3) + O(z**(-6), (z, oo)))*cos(pi*z**2/2) + \ (-3/(pi**3*z**5) + 1/(pi*z) + O(z**(-6), (z, oo)))*sin(pi*z**2/2) + S.Half assert fresnels(1/z).series(z) == \ (-z**3/pi**2 + O(z**6))*sin(pi/(2*z**2)) + (-z/pi + 3*z**5/pi**3 + \ O(z**6))*cos(pi/(2*z**2)) + S.Half assert fresnelc(1/z).series(z) == \ (-z**3/pi**2 + O(z**6))*cos(pi/(2*z**2)) + (z/pi - 3*z**5/pi**3 + \ O(z**6))*sin(pi/(2*z**2)) + S.Half assert fresnelc(w).is_real is True assert fresnelc(z).as_real_imag() == \ ((fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z)))/2 + fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))/2, I*(fresnelc(re(z) - I*re(z)*Abs(im(z))/Abs(re(z))) - fresnelc(re(z) + I*re(z)*Abs(im(z))/Abs(re(z)))) * re(z)*Abs(im(z))/(2*im(z)*Abs(re(z))))) assert fresnelc(2 + 3 * I).as_real_imag() == ( fresnelc(2 - 3 * I) / 2 + fresnelc(2 + 3 * I) / 2, I * (fresnelc(2 - 3 * I) - fresnelc(2 + 3 * I)) / 2) assert expand_func(integrate(fresnelc(z), z)) == \ z*fresnelc(z) - sin(pi*z**2/2)/pi assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**(S(3)/4) * \ meijerg(((), (1,)), ((S(1)/4,), (S(3)/4, 0)), -pi**2*z**4/16)/(2*(-z)**(S(1)/4)*(z**2)**(S(1)/4)) from sympy.utilities.randtest import test_numerically test_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z) test_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z) test_numerically(fresnels(z), fresnels(z).rewrite(hyper), z) test_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z) test_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z) test_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z) test_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z) test_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z)
def test_erf(): assert erf(nan) is nan assert erf(oo) == 1 assert erf(-oo) == -1 assert erf(0) == 0 assert erf(I * oo) == oo * I assert erf(-I * oo) == -oo * I assert erf(-2) == -erf(2) assert erf(-x * y) == -erf(x * y) assert erf(-x - y) == -erf(x + y) assert erf(erfinv(x)) == x assert erf(erfcinv(x)) == 1 - x assert erf(erf2inv(0, x)) == x assert erf(erf2inv(0, x, evaluate=False)) == x # To cover code in erf assert erf(erf2inv(0, erf(erfcinv(1 - erf(erfinv(x)))))) == x assert erf(I).is_real is False assert erf(0).is_real is True assert conjugate(erf(z)) == erf(conjugate(z)) assert erf(x).as_leading_term(x) == 2 * x / sqrt(pi) assert erf(x * y).as_leading_term(y) == 2 * x * y / sqrt(pi) assert (erf(x * y) / erf(y)).as_leading_term(y) == x assert erf(1 / x).as_leading_term(x) == S.One assert erf(z).rewrite('uppergamma') == sqrt(z** 2) * (1 - erfc(sqrt(z**2))) / z assert erf(z).rewrite('erfc') == S.One - erfc(z) assert erf(z).rewrite('erfi') == -I * erfi(I * z) assert erf(z).rewrite('fresnels') == (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erf(z).rewrite('fresnelc') == (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erf(z).rewrite('hyper') == 2 * z * hyper([S.Half], [3 * S.Half], -z**2) / sqrt(pi) assert erf(z).rewrite('meijerg') == z * meijerg( [S.Half], [], [0], [Rational(-1, 2)], z**2) / sqrt(pi) assert erf(z).rewrite( 'expint') == sqrt(z**2) / z - z * expint(S.Half, z**2) / sqrt(S.Pi) assert limit(exp(x)*exp(x**2)*(erf(x + 1/exp(x)) - erf(x)), x, oo) == \ 2/sqrt(pi) assert limit((1 - erf(z)) * exp(z**2) * z, z, oo) == 1 / sqrt(pi) assert limit((1 - erf(x)) * exp(x**2) * sqrt(pi) * x, x, oo) == 1 assert limit(((1 - erf(x)) * exp(x**2) * sqrt(pi) * x - 1) * 2 * x**2, x, oo) == -1 assert limit(erf(x) / x, x, 0) == 2 / sqrt(pi) assert limit(x**(-4) - sqrt(pi) * erf(x**2) / (2 * x**6), x, 0) == S(1) / 3 assert erf(x).as_real_imag() == \ (erf(re(x) - I*im(x))/2 + erf(re(x) + I*im(x))/2, -I*(-erf(re(x) - I*im(x)) + erf(re(x) + I*im(x)))/2) assert erf(x).as_real_imag(deep=False) == \ (erf(re(x) - I*im(x))/2 + erf(re(x) + I*im(x))/2, -I*(-erf(re(x) - I*im(x)) + erf(re(x) + I*im(x)))/2) assert erf(w).as_real_imag() == (erf(w), 0) assert erf(w).as_real_imag(deep=False) == (erf(w), 0) # issue 13575 assert erf(I).as_real_imag() == (0, -I * erf(I)) raises(ArgumentIndexError, lambda: erf(x).fdiff(2)) assert erf(x).inverse() == erfinv
def test_fresnel(): assert fresnels(0) == 0 assert fresnels(oo) == S.Half assert fresnels(-oo) == Rational(-1, 2) assert fresnels(I * oo) == -I * S.Half assert unchanged(fresnels, z) assert fresnels(-z) == -fresnels(z) assert fresnels(I * z) == -I * fresnels(z) assert fresnels(-I * z) == I * fresnels(z) assert conjugate(fresnels(z)) == fresnels(conjugate(z)) assert fresnels(z).diff(z) == sin(pi * z**2 / 2) assert fresnels(z).rewrite(erf) == (S.One + I) / 4 * (erf( (S.One + I) / 2 * sqrt(pi) * z) - I * erf( (S.One - I) / 2 * sqrt(pi) * z)) assert fresnels(z).rewrite(hyper) == \ pi*z**3/6 * hyper([Rational(3, 4)], [Rational(3, 2), Rational(7, 4)], -pi**2*z**4/16) assert fresnels(z).series(z, n=15) == \ pi*z**3/6 - pi**3*z**7/336 + pi**5*z**11/42240 + O(z**15) assert fresnels(w).is_extended_real is True assert fresnels(w).is_finite is True assert fresnels(z).is_extended_real is None assert fresnels(z).is_finite is None assert fresnels(z).as_real_imag() == ( fresnels(re(z) - I * im(z)) / 2 + fresnels(re(z) + I * im(z)) / 2, -I * (-fresnels(re(z) - I * im(z)) + fresnels(re(z) + I * im(z))) / 2) assert fresnels(z).as_real_imag(deep=False) == ( fresnels(re(z) - I * im(z)) / 2 + fresnels(re(z) + I * im(z)) / 2, -I * (-fresnels(re(z) - I * im(z)) + fresnels(re(z) + I * im(z))) / 2) assert fresnels(w).as_real_imag() == (fresnels(w), 0) assert fresnels(w).as_real_imag(deep=True) == (fresnels(w), 0) assert fresnels(2 + 3 * I).as_real_imag() == ( fresnels(2 + 3 * I) / 2 + fresnels(2 - 3 * I) / 2, -I * (fresnels(2 + 3 * I) - fresnels(2 - 3 * I)) / 2) assert expand_func(integrate(fresnels(z), z)) == \ z*fresnels(z) + cos(pi*z**2/2)/pi assert fresnels(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(9, 4) * \ meijerg(((), (1,)), ((Rational(3, 4),), (Rational(1, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(3, 4)*(z**2)**Rational(3, 4)) assert fresnelc(0) == 0 assert fresnelc(oo) == S.Half assert fresnelc(-oo) == Rational(-1, 2) assert fresnelc(I * oo) == I * S.Half assert unchanged(fresnelc, z) assert fresnelc(-z) == -fresnelc(z) assert fresnelc(I * z) == I * fresnelc(z) assert fresnelc(-I * z) == -I * fresnelc(z) assert conjugate(fresnelc(z)) == fresnelc(conjugate(z)) assert fresnelc(z).diff(z) == cos(pi * z**2 / 2) assert fresnelc(z).rewrite(erf) == (S.One - I) / 4 * (erf( (S.One + I) / 2 * sqrt(pi) * z) + I * erf( (S.One - I) / 2 * sqrt(pi) * z)) assert fresnelc(z).rewrite(hyper) == \ z * hyper([Rational(1, 4)], [S.Half, Rational(5, 4)], -pi**2*z**4/16) assert fresnelc(w).is_extended_real is True assert fresnelc(z).as_real_imag() == \ (fresnelc(re(z) - I*im(z))/2 + fresnelc(re(z) + I*im(z))/2, -I*(-fresnelc(re(z) - I*im(z)) + fresnelc(re(z) + I*im(z)))/2) assert fresnelc(z).as_real_imag(deep=False) == \ (fresnelc(re(z) - I*im(z))/2 + fresnelc(re(z) + I*im(z))/2, -I*(-fresnelc(re(z) - I*im(z)) + fresnelc(re(z) + I*im(z)))/2) assert fresnelc(2 + 3 * I).as_real_imag() == ( fresnelc(2 - 3 * I) / 2 + fresnelc(2 + 3 * I) / 2, -I * (fresnelc(2 + 3 * I) - fresnelc(2 - 3 * I)) / 2) assert expand_func(integrate(fresnelc(z), z)) == \ z*fresnelc(z) - sin(pi*z**2/2)/pi assert fresnelc(z).rewrite(meijerg) == sqrt(2)*pi*z**Rational(3, 4) * \ meijerg(((), (1,)), ((Rational(1, 4),), (Rational(3, 4), 0)), -pi**2*z**4/16)/(2*(-z)**Rational(1, 4)*(z**2)**Rational(1, 4)) from sympy.testing.randtest import verify_numerically verify_numerically(re(fresnels(z)), fresnels(z).as_real_imag()[0], z) verify_numerically(im(fresnels(z)), fresnels(z).as_real_imag()[1], z) verify_numerically(fresnels(z), fresnels(z).rewrite(hyper), z) verify_numerically(fresnels(z), fresnels(z).rewrite(meijerg), z) verify_numerically(re(fresnelc(z)), fresnelc(z).as_real_imag()[0], z) verify_numerically(im(fresnelc(z)), fresnelc(z).as_real_imag()[1], z) verify_numerically(fresnelc(z), fresnelc(z).rewrite(hyper), z) verify_numerically(fresnelc(z), fresnelc(z).rewrite(meijerg), z) raises(ArgumentIndexError, lambda: fresnels(z).fdiff(2)) raises(ArgumentIndexError, lambda: fresnelc(z).fdiff(2)) assert fresnels(x).taylor_term(-1, x) is S.Zero assert fresnelc(x).taylor_term(-1, x) is S.Zero assert fresnelc(x).taylor_term(1, x) == -pi**2 * x**5 / 40
def test_TupleParametersBase(): # test that our implementation of the chain rule works p = hyper((), (), z**2) assert p.diff(z) == p*2*z
def test_simplify_expr(): x, y, z, k, n, m, w, s, A = symbols('x,y,z,k,n,m,w,s,A') f = Function('f') assert all(simplify(tmp) == tmp for tmp in [I, E, oo, x, -x, -oo, -E, -I]) e = 1 / x + 1 / y assert e != (x + y) / (x * y) assert simplify(e) == (x + y) / (x * y) e = A**2 * s**4 / (4 * pi * k * m**3) assert simplify(e) == e e = (4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x) assert simplify(e) == 0 e = (-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2 assert simplify(e) == -2 * y e = -x - y - (x + y)**(-1) * y**2 + (x + y)**(-1) * x**2 assert simplify(e) == -2 * y e = (x + x * y) / x assert simplify(e) == 1 + y e = (f(x) + y * f(x)) / f(x) assert simplify(e) == 1 + y e = (2 * (1 / n - cos(n * pi) / n)) / pi assert simplify(e) == (-cos(pi * n) + 1) / (pi * n) * 2 e = integrate(1 / (x**3 + 1), x).diff(x) assert simplify(e) == 1 / (x**3 + 1) e = integrate(x / (x**2 + 3 * x + 1), x).diff(x) assert simplify(e) == x / (x**2 + 3 * x + 1) f = Symbol('f') A = Matrix([[2 * k - m * w**2, -k], [-k, k - m * w**2]]).inv() assert simplify((A * Matrix([0, f]))[1] - (-f * (2 * k - m * w**2) / (k**2 - (k - m * w**2) * (2 * k - m * w**2)))) == 0 f = -x + y / (z + t) + z * x / (z + t) + z * a / (z + t) + t * x / (z + t) assert simplify(f) == (y + a * z) / (z + t) # issue 10347 expr = -x * (y**2 - 1) * ( 2 * y**2 * (x**2 - 1) / (a * (x**2 - y**2)**2) + (x**2 - 1) / (a * (x**2 - y**2))) / (a * (x**2 - y**2)) + x * ( -2 * x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2)**2) - x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - 1) * (x**2 - y**2)) + (x**2 * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (x**2 - 1) + sqrt( (-x**2 + 1) * (y**2 - 1)) * (x * (-x * y**2 + x) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1) + sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) * sin(z)) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / ( a * (x**2 - y**2)) + x * ( -2 * x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2)**2) - x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - 1) * (x**2 - y**2)) + (x**2 * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (x**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x * y**2 + x) * cos(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1) + sqrt( (-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z)) / (a * sqrt((-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / ( a * (x**2 - y**2)) - y * sqrt((-x**2 + 1) * (y**2 - 1)) * ( -x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2) * (y**2 - 1)) + 2 * x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2)**2) + (x * y * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (y**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x**2 * y + y) * sin(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sin(z) / (a * (x**2 - y**2)) + y * (x**2 - 1) * ( -2 * x * y * (x**2 - 1) / (a * (x**2 - y**2)**2) + 2 * x * y / (a * (x**2 - y**2)) ) / (a * (x**2 - y**2)) + y * (x**2 - 1) * (y**2 - 1) * ( -x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2) * (y**2 - 1)) + 2 * x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2)**2) + (x * y * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (y**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x**2 * y + y) * cos(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) / (a * sqrt((-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2))) * cos(z) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) - x * sqrt( (-x**2 + 1) * (y**2 - 1) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin( z)**2 / (a**2 * (x**2 - 1) * (x**2 - y**2) * (y**2 - 1)) - x * sqrt( (-x**2 + 1) * (y**2 - 1)) * sqrt( -x**2 * y**2 + x**2 + y**2 - 1) * cos(z)**2 / ( a**2 * (x**2 - 1) * (x**2 - y**2) * (y**2 - 1)) assert simplify(expr) == 2 * x / (a**2 * (x**2 - y**2)) #issue 17631 assert simplify('((-1/2)*Boole(True)*Boole(False)-1)*Boole(True)') == \ Mul(sympify('(2 + Boole(True)*Boole(False))'), sympify('-Boole(True)/2')) A, B = symbols('A,B', commutative=False) assert simplify(A * B - B * A) == A * B - B * A assert simplify(A / (1 + y / x)) == x * A / (x + y) assert simplify(A * (1 / x + 1 / y)) == A / x + A / y #(x + y)*A/(x*y) assert simplify(log(2) + log(3)) == log(6) assert simplify(log(2 * x) - log(2)) == log(x) assert simplify(hyper([], [], x)) == exp(x)
def test_separatevars(): x, y, z, n = symbols('x,y,z,n') assert separatevars(2 * n * x * z + 2 * x * y * z) == 2 * x * z * (n + y) assert separatevars(x * z + x * y * z) == x * z * (1 + y) assert separatevars(pi * x * z + pi * x * y * z) == pi * x * z * (1 + y) assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == \ x*(sin(y) + y**2)*sin(x) assert separatevars(x * exp(x + y) + x * exp(x)) == x * (1 + exp(y)) * exp(x) assert separatevars((x * (y + 1))**z).is_Pow # != x**z*(1 + y)**z assert separatevars(1 + x + y + x * y) == (x + 1) * (y + 1) assert separatevars(y/pi*exp(-(z - x)/cos(n))) == \ y*exp(x/cos(n))*exp(-z/cos(n))/pi assert separatevars((x + y) * (x - y) + y**2 + 2 * x + 1) == (x + 1)**2 # issue 4858 p = Symbol('p', positive=True) assert separatevars(sqrt(p**2 + x * p**2)) == p * sqrt(1 + x) assert separatevars(sqrt(y * (p**2 + x * p**2))) == p * sqrt(y * (1 + x)) assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == \ p*sqrt(y)*sqrt(1 + x) # issue 4865 assert separatevars(sqrt(x * y)).is_Pow assert separatevars(sqrt(x * y), force=True) == sqrt(x) * sqrt(y) # issue 4957 # any type sequence for symbols is fine assert separatevars(((2*x + 2)*y), dict=True, symbols=()) == \ {'coeff': 1, x: 2*x + 2, y: y} # separable assert separatevars(((2*x + 2)*y), dict=True, symbols=[x]) == \ {'coeff': y, x: 2*x + 2} assert separatevars(((2*x + 2)*y), dict=True, symbols=[]) == \ {'coeff': 1, x: 2*x + 2, y: y} assert separatevars(((2*x + 2)*y), dict=True) == \ {'coeff': 1, x: 2*x + 2, y: y} assert separatevars(((2*x + 2)*y), dict=True, symbols=None) == \ {'coeff': y*(2*x + 2)} # not separable assert separatevars(3, dict=True) is None assert separatevars(2 * x + y, dict=True, symbols=()) is None assert separatevars(2 * x + y, dict=True) is None assert separatevars(2 * x + y, dict=True, symbols=None) == { 'coeff': 2 * x + y } # issue 4808 n, m = symbols('n,m', commutative=False) assert separatevars(m + n * m) == (1 + n) * m assert separatevars(x + x * n) == x * (1 + n) # issue 4910 f = Function('f') assert separatevars(f(x) + x * f(x)) == f(x) + x * f(x) # a noncommutable object present eq = x * (1 + hyper((), (), y * z)) assert separatevars(eq) == eq s = separatevars(abs(x * y)) assert s == abs(x) * abs(y) and s.is_Mul z = cos(1)**2 + sin(1)**2 - 1 a = abs(x * z) s = separatevars(a) assert not a.is_Mul and s.is_Mul and s == abs(x) * abs(z) s = separatevars(abs(x * y * z)) assert s == abs(x) * abs(y) * abs(z) # abs(x+y)/abs(z) would be better but we test this here to # see that it doesn't raise assert separatevars(abs((x + y) / z)) == abs((x + y) / z)
def test_bug(): h = hyper([-1, 1], [z], -1) assert hyperexpand(h) == (z + 1) / z
def test_omgissue_203(): h = hyper((-5, -3, -4), (-6, -6), 1) assert hyperexpand(h) == Rational(1, 30) h = hyper((-6, -7, -5), (-6, -6), 1) assert hyperexpand(h) == -Rational(1, 6)
def test_hyperexpand_parametric(): assert hyperexpand(hyper([a, S(1)/2 + a], [S(1)/2], z)) \ == (1 + sqrt(z))**(-2*a)/2 + (1 - sqrt(z))**(-2*a)/2 assert hyperexpand(hyper([a, -S(1)/2 + a], [2*a], z)) \ == 2**(2*a - 1)*((-z + 1)**(S(1)/2) + 1)**(-2*a + 1)
def test_hyper(): raises(TypeError, lambda: hyper(1, 2, z)) assert hyper((1, 2), (1,), z) == hyper(Tuple(1, 2), Tuple(1), z) h = hyper((1, 2), (3, 4, 5), z) assert h.ap == Tuple(1, 2) assert h.bq == Tuple(3, 4, 5) assert h.argument == z assert h.is_commutative is True # just a few checks to make sure that all arguments go where they should assert tn(hyper(Tuple(), Tuple(), z), exp(z), z) assert tn(z*hyper((1, 1), Tuple(2), -z), log(1 + z), z) # differentiation h = hyper( (randcplx(), randcplx(), randcplx()), (randcplx(), randcplx()), z) assert td(h, z) a1, a2, b1, b2, b3 = symbols('a1:3, b1:4') assert hyper((a1, a2), (b1, b2, b3), z).diff(z) == \ a1*a2/(b1*b2*b3) * hyper((a1 + 1, a2 + 1), (b1 + 1, b2 + 1, b3 + 1), z) # differentiation wrt parameters is not supported assert hyper([z], [], z).diff(z) == Derivative(hyper([z], [], z), z) # hyper is unbranched wrt parameters from sympy import polar_lift assert hyper([polar_lift(z)], [polar_lift(k)], polar_lift(x)) == \ hyper([z], [k], polar_lift(x)) # hyper does not automatically evaluate anyway, but the test is to make # sure that the evaluate keyword is accepted assert hyper((1, 2), (1,), z, evaluate=False).func is hyper
def test_meijerg_expand(): from sympy import gammasimp, simplify # from mpmath docs assert hyperexpand(meijerg([[], []], [[0], []], -z)) == exp(z) assert hyperexpand(meijerg([[1, 1], []], [[1], [0]], z)) == \ log(z + 1) assert hyperexpand(meijerg([[1, 1], []], [[1], [1]], z)) == \ z/(z + 1) assert hyperexpand(meijerg([[], []], [[S(1)/2], [0]], (z/2)**2)) \ == sin(z)/sqrt(pi) assert hyperexpand(meijerg([[], []], [[0], [S(1)/2]], (z/2)**2)) \ == cos(z)/sqrt(pi) assert can_do_meijer([], [a], [a - 1, a - S.Half], []) assert can_do_meijer([], [], [a / 2], [-a / 2], False) # branches... assert can_do_meijer([a], [b], [a], [b, a - 1]) # wikipedia assert hyperexpand(meijerg([1], [], [], [0], z)) == \ Piecewise((0, abs(z) < 1), (1, abs(1/z) < 1), (meijerg([1], [], [], [0], z), True)) assert hyperexpand(meijerg([], [1], [0], [], z)) == \ Piecewise((1, abs(z) < 1), (0, abs(1/z) < 1), (meijerg([], [1], [0], [], z), True)) # The Special Functions and their Approximations assert can_do_meijer([], [], [a + b / 2], [a, a - b / 2, a + S.Half]) assert can_do_meijer([], [], [a], [b], False) # branches only agree for small z assert can_do_meijer([], [S.Half], [a], [-a]) assert can_do_meijer([], [], [a, b], []) assert can_do_meijer([], [], [a, b], []) assert can_do_meijer([], [], [a, a + S.Half], [b, b + S.Half]) assert can_do_meijer([], [], [a, -a], [0, S.Half], False) # dito assert can_do_meijer([], [], [a, a + S.Half, b, b + S.Half], []) assert can_do_meijer([S.Half], [], [0], [a, -a]) assert can_do_meijer([S.Half], [], [a], [0, -a], False) # dito assert can_do_meijer([], [a - S.Half], [a, b], [a - S.Half], False) assert can_do_meijer([], [a + S.Half], [a + b, a - b, a], [], False) assert can_do_meijer([a + S.Half], [], [b, 2 * a - b, a], [], False) # This for example is actually zero. assert can_do_meijer([], [], [], [a, b]) # Testing a bug: assert hyperexpand(meijerg([0, 2], [], [], [-1, 1], z)) == \ Piecewise((0, abs(z) < 1), (z/2 - 1/(2*z), abs(1/z) < 1), (meijerg([0, 2], [], [], [-1, 1], z), True)) # Test that the simplest possible answer is returned: assert gammasimp(simplify(hyperexpand( meijerg([1], [1 - a], [-a/2, -a/2 + S(1)/2], [], 1/z)))) == \ -2*sqrt(pi)*(sqrt(z + 1) + 1)**a/a # Test that hyper is returned assert hyperexpand(meijerg([1], [], [a], [0, 0], z)) == hyper( (a, ), (a + 1, a + 1), z * exp_polar(I * pi)) * z**a * gamma(a) / gamma(a + 1)**2 # Test place option f = meijerg(((0, 1), ()), ((S(1) / 2, ), (0, )), z**2) assert hyperexpand(f) == sqrt(pi) / sqrt(1 + z**(-2)) assert hyperexpand(f, place=0) == sqrt(pi) * z / sqrt(z**2 + 1)
#import pythonista """ This module cooks up a docstring when imported. Its only purpose is to be displayed in the sphinx documentation. """ from __future__ import print_function, division from sympy.simplify.hyperexpand import FormulaCollection from sympy import latex, Eq, hyper c = FormulaCollection() doc = "" for f in c.formulae: obj = Eq(hyper(f.func.ap, f.func.bq, f.z), f.closed_form.rewrite('nonrepsmall')) doc += ".. math::\n %s\n" % latex(obj) __doc__ = doc
def test_shifted_sum(): from sympy import simplify assert simplify(hyperexpand(z**4*hyper([2], [3, S('3/2')], -z**2))) \ == z*sin(2*z) + (-z**2 + S.Half)*cos(2*z) - S.Half
def _characteristic_function(self, t): rho = self.rho return rho * hyper((1, 1), (rho + 2, ), exp(I * t)) * exp(I * t) / (rho + 1)
def _moment_generating_function(self, t): rho = self.rho return rho * hyper((1, 1), (rho + 2, ), exp(t)) * exp(t) / (rho + 1)