def test_meijerg_eval(): a = randcplx() arg = x*exp_polar(k*pi*I) expr1 = pi*meijerg([[], [(a + 1)/2]], [[a/2], [-a/2, (a + 1)/2]], arg**2/4) expr2 = besseli(a, arg) # Test that the two expressions agree for all arguments. for x_ in [0.5, 1.5]: for k_ in [0.0, 0.1, 0.3, 0.5, 0.8, 1, 5.751, 15.3]: assert abs((expr1 - expr2).evalf(subs={x: x_, k: k_}, strict=False)) < 1e-10 assert abs((expr1 - expr2).evalf(subs={x: x_, k: -k_}, strict=False)) < 1e-10 # Test continuity independently eps = 1e-13 expr2 = expr1.subs({k: l}) for x_ in [0.5, 1.5]: for k_ in [0.5, Rational(1, 3), 0.25, 0.75, Rational(2, 3), 1.0, 1.5]: assert abs((expr1 - expr2).evalf( subs={x: x_, k: k_ + eps, l: k_ - eps})) < 1e-10 assert abs((expr1 - expr2).evalf( subs={x: x_, k: -k_ + eps, l: -k_ - eps})) < 1e-10 expr = (meijerg(((0.5,), ()), ((0.5, 0, 0.5), ()), exp_polar(-I*pi)/4) + meijerg(((0.5,), ()), ((0.5, 0, 0.5), ()), exp_polar(I*pi)/4)) \ / (2*sqrt(pi)) assert (expr - pi/exp(1)).evalf(chop=True) == 0
def test_matplotlib_advanced(): """Examples from the 'advanced' notebook.""" try: name = 'test' tmp_file = TmpFileManager.tmp_file s = summation(1 / x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p = plot(summation(1 / x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) ### # Test expressions that can not be translated to np and # generate complex results. ### plot(sin(x) + I * cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) # Characteristic function of a StudentT distribution with nu=10 plot((meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I * pi) / 2) + meijerg( ((1 / 2, ), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(I * pi) / 2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) finally: TmpFileManager.cleanup()
def test_functions(): g = WildFunction('g') p = Wild('p') q = Wild('q') f = cos(5 * x) notf = x assert f.match(p * cos(q * x)) == {p: 1, q: 5} assert f.match(p * g) == {p: 1, g: cos(5 * x)} assert notf.match(g) is None F = WildFunction('F', nargs=2) assert F.nargs == FiniteSet(2) f = Function('f') assert f(x).match(F) is None F = WildFunction('F', nargs=(1, 2)) assert F.nargs == FiniteSet(1, 2) # issue sympy/sympy#2711 f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == { 0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1, (): 3, (0, ): 1, ((), ()): 1, ((0, ), ()): 1 } assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1} assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
def can_do_meijer(a1, a2, b1, b2, numeric=True): """ This helper function tries to hyperexpand() the meijer g-function corresponding to the parameters a1, a2, b1, b2. It returns False if this expansion still contains g-functions. If numeric is True, it also tests the so-obtained formula numerically (at random values) and returns False if the test fails. Else it returns True. """ from diofant import unpolarify, expand r = hyperexpand(meijerg(a1, a2, b1, b2, z)) if r.has(meijerg): return False # NOTE hyperexpand() returns a truly branched function, whereas numerical # evaluation only works on the main branch. Since we are evaluating on # the main branch, this should not be a problem, but expressions like # exp_polar(I*pi/2*x)**a are evaluated incorrectly. We thus have to get # rid of them. The expand heuristically does this... r = unpolarify(expand(r, force=True, power_base=True, power_exp=False, mul=False, log=False, multinomial=False, basic=False)) if not numeric: return True repl = {} for n, a in enumerate(meijerg(a1, a2, b1, b2, z).free_symbols - {z}): repl[a] = randcplx(n) return tn(meijerg(a1, a2, b1, b2, z).subs(repl), r.subs(repl), z)
def can_do_meijer(a1, a2, b1, b2, numeric=True): """ This helper function tries to hyperexpand() the meijer g-function corresponding to the parameters a1, a2, b1, b2. It returns False if this expansion still contains g-functions. If numeric is True, it also tests the so-obtained formula numerically (at random values) and returns False if the test fails. Else it returns True. """ r = hyperexpand(meijerg(a1, a2, b1, b2, z)) if r.has(meijerg): return False # NOTE hyperexpand() returns a truly branched function, whereas numerical # evaluation only works on the main branch. Since we are evaluating on # the main branch, this should not be a problem, but expressions like # exp_polar(I*pi/2*x)**a are evaluated incorrectly. We thus have to get # rid of them. The expand heuristically does this... r = unpolarify(expand(r, force=True, power_base=True, power_exp=False, mul=False, log=False, multinomial=False, basic=False)) if not numeric: return True repl = {} for n, a in enumerate(meijerg(a1, a2, b1, b2, z).free_symbols - {z}): repl[a] = randcplx(n) return tn(meijerg(a1, a2, b1, b2, z).subs(repl), r.subs(repl), z)
def test_K(): assert K(0) == pi/2 assert K(Rational(1, 2)) == 8*pi**Rational(3, 2)/gamma(-Rational(1, 4))**2 assert K(1) == zoo assert K(-1) == gamma(Rational(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) pytest.raises(ArgumentIndexError, lambda: K(z).fdiff(2)) zi = Symbol('z', extended_real=False) assert K(zi).conjugate() == K(zi.conjugate()) zr = Symbol('z', extended_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_cosine_transform(): f = Function("f") # Test unevaluated form assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w) assert inverse_cosine_transform( f(w), w, t) == InverseCosineTransform(f(w), w, t) assert cosine_transform(1/sqrt(t), t, w) == 1/sqrt(w) assert inverse_cosine_transform(1/sqrt(w), w, t) == 1/sqrt(t) assert cosine_transform(1/( a**2 + t**2), t, w) == sqrt(2)*sqrt(pi)*exp(-a*w)/(2*a) assert cosine_transform(t**( -a), t, w) == 2**(-a + Rational(1, 2))*w**(a - 1)*gamma((-a + 1)/2)/gamma(a/2) assert inverse_cosine_transform(2**(-a + Rational(1, 2))*w**(a - 1)*gamma(-a/2 + Rational(1, 2))/gamma(a/2), w, t) == t**(-a) assert cosine_transform( exp(-a*t), t, w) == sqrt(2)*a/(sqrt(pi)*(a**2 + w**2)) assert inverse_cosine_transform( sqrt(2)*a/(sqrt(pi)*(a**2 + w**2)), w, t) == exp(-a*t) assert cosine_transform(exp(-a*sqrt(t))*cos(a*sqrt( t)), t, w) == a*exp(-a**2/(2*w))/(2*w**Rational(3, 2)) assert cosine_transform(1/(a + t), t, w) == sqrt(2)*( (-2*Si(a*w) + pi)*sin(a*w)/2 - cos(a*w)*Ci(a*w))/sqrt(pi) assert inverse_cosine_transform(sqrt(2)*meijerg(((Rational(1, 2), 0), ()), ( (Rational(1, 2), 0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*pi), w, t) == 1/(a + t) assert cosine_transform(1/sqrt(a**2 + t**2), t, w) == sqrt(2)*meijerg( ((Rational(1, 2),), ()), ((0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*sqrt(pi)) assert inverse_cosine_transform(sqrt(2)*meijerg(((Rational(1, 2),), ()), ((0, 0), (Rational(1, 2),)), a**2*w**2/4)/(2*sqrt(pi)), w, t) == 1/(a*sqrt(1 + t**2/a**2))
def test_matplotlib_advanced(): """Examples from the 'advanced' notebook.""" try: name = 'test' tmp_file = TmpFileManager.tmp_file s = summation(1/x**y, (x, 1, oo)) p = plot(s, (y, 2, 10)) p.save(tmp_file('%s_advanced_inf_sum' % name)) p = plot(summation(1/x, (x, 1, y)), (y, 2, 10), show=False) p[0].only_integers = True p[0].steps = True p.save(tmp_file('%s_advanced_fin_sum' % name)) ### # Test expressions that can not be translated to np and # generate complex results. ### plot(sin(x) + I*cos(x)).save(tmp_file()) plot(sqrt(sqrt(-x))).save(tmp_file()) plot(LambertW(x)).save(tmp_file()) plot(sqrt(LambertW(x))).save(tmp_file()) # Characteristic function of a StudentT distribution with nu=10 plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I*pi)/2) + meijerg(((1/2,), ()), ((5, 0, 1/2), ()), 5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) finally: TmpFileManager.cleanup()
def test_expint(): assert mytn(expint(x, y), expint(x, y).rewrite(uppergamma), y**(x - 1)*uppergamma(1 - x, y), x) assert mytd( expint(x, y), -y**(x - 1)*meijerg([], [1, 1], [0, 0, 1 - x], [], y), x) assert mytd(expint(x, y), -expint(x - 1, y), y) assert mytn(expint(1, x), expint(1, x).rewrite(Ei), -Ei(x*polar_lift(-1)) + I*pi, x) assert expint(-4, x) == exp(-x)/x + 4*exp(-x)/x**2 + 12*exp(-x)/x**3 \ + 24*exp(-x)/x**4 + 24*exp(-x)/x**5 assert expint(-Rational(3, 2), x) == \ exp(-x)/x + 3*exp(-x)/(2*x**2) - 3*sqrt(pi)*erf(sqrt(x))/(4*x**Rational(5, 2)) \ + 3*sqrt(pi)/(4*x**Rational(5, 2)) assert tn_branch(expint, 1) assert tn_branch(expint, 2) assert tn_branch(expint, 3) assert tn_branch(expint, 1.7) assert tn_branch(expint, pi) assert expint(y, x*exp_polar(2*I*pi)) == \ x**(y - 1)*(exp(2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x) assert expint(y, x*exp_polar(-2*I*pi)) == \ x**(y - 1)*(exp(-2*I*pi*y) - 1)*gamma(-y + 1) + expint(y, x) assert expint(2, x*exp_polar(2*I*pi)) == 2*I*pi*x + expint(2, x) assert expint(2, x*exp_polar(-2*I*pi)) == -2*I*pi*x + expint(2, x) assert (expint(n, x*exp_polar(2*I*pi)) == expint(n, x*exp_polar(2*I*pi), evaluate=False)) assert expint(1, x).rewrite(Ei).rewrite(expint) == expint(1, x) assert (expint(2, x, evaluate=False).rewrite(Shi) == expint(2, x, evaluate=False)) assert mytn(E1(x), E1(x).rewrite(Shi), Shi(x) - Chi(x), x) assert mytn(E1(polar_lift(I)*x), E1(polar_lift(I)*x).rewrite(Si), -Ci(x) + I*Si(x) - I*pi/2, x) assert mytn(expint(2, x), expint(2, x).rewrite(Ei).rewrite(expint), -x*E1(x) + exp(-x), x) assert mytn(expint(3, x), expint(3, x).rewrite(Ei).rewrite(expint), x**2*E1(x)/2 + (1 - x)*exp(-x)/2, x) assert expint(Rational(3, 2), z).nseries(z, n=10) == \ 2 + 2*z - z**2/3 + z**3/15 - z**4/84 + z**5/540 - \ 2*sqrt(pi)*sqrt(z) + O(z**6) assert E1(z).series(z) == -EulerGamma - log(z) + z - \ z**2/4 + z**3/18 - z**4/96 + z**5/600 + O(z**6) assert expint(4, z).series(z) == Rational(1, 3) - z/2 + z**2/2 + \ z**3*(log(z)/6 - Rational(11, 36) + EulerGamma/6) - z**4/24 + \ z**5/240 + O(z**6) assert (expint(x, x).series(x, x0=1, n=2) == expint(1, 1) + (x - 1)*(-meijerg(((), (1, 1)), ((0, 0, 0), ()), 1) - 1/E) + O((x - 1)**2, (x, 1))) pytest.raises(ArgumentIndexError, lambda: expint(x, y).fdiff(3))
def test_limits(): k, x = symbols('k, x') assert hyper((1,), (Rational(4, 3), Rational(5, 3)), k**2).series(k) == \ hyper((1,), (Rational(4, 3), Rational(5, 3)), 0) + \ 9*k**2*hyper((2,), (Rational(7, 3), Rational(8, 3)), 0)/20 + \ 81*k**4*hyper((3,), (Rational(10, 3), Rational(11, 3)), 0)/1120 + \ O(k**6) # issue sympy/sympy#6350 assert limit(meijerg((), (), (1,), (0,), -x), x, 0) == \ meijerg(((), ()), ((1,), (0,)), 0) # issue sympy/sympy#6052
def test_sympyissue_2711(): f = meijerg(((), ()), ((0,), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == {0: 1, x: 1, meijerg(((), ()), ((0,), ()), x): 1, (): 3, (0,): 1, ((), ()): 1, ((0,), ()): 1} assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0,), ()), x): 1} assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0,), ()), x): 1}
def test_cse_not_possible(): # No substitution possible. e = Add(x, y) substs, reduced = cse([e]) assert substs == [] assert reduced == [x + y] # issue sympy/sympy#6329 eq = (meijerg((1, 2), (y, 4), (5, ), [], x) + meijerg((1, 3), (y, 4), (5, ), [], x)) assert cse(eq) == ([], [eq])
def test_sympyissue_2711(): f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == {(S.Zero, ), ((), ()), ((S.Zero, ), ()), x, S.Zero, (), meijerg(((), ()), ((S.Zero, ), ()), x)} assert f.find(a + b) == \ {meijerg(((), ()), ((S.Zero,), ()), x), x, S.Zero} assert f.find(a**2) == {meijerg(((), ()), ((S.Zero, ), ()), x), x}
def test_meijerg_shift_operators(): # carefully set up the parameters. XXX this still fails sometimes a1, a2, a3, a4, a5, b1, b2, b3, b4, b5 = (randcplx(n) for n in range(10)) g = meijerg([a1], [a3, a4], [b1], [b3, b4], z) assert tn(MeijerShiftA(b1).apply(g, op), meijerg([a1], [a3, a4], [b1 + 1], [b3, b4], z), z) assert tn(MeijerShiftB(a1).apply(g, op), meijerg([a1 - 1], [a3, a4], [b1], [b3, b4], z), z) assert tn(MeijerShiftC(b3).apply(g, op), meijerg([a1], [a3, a4], [b1], [b3 + 1, b4], z), z) assert tn(MeijerShiftD(a3).apply(g, op), meijerg([a1], [a3 - 1, a4], [b1], [b3, b4], z), z) s = MeijerUnShiftA([a1], [a3, a4], [b1], [b3, b4], 0, z) assert tn( s.apply(g, op), meijerg([a1], [a3, a4], [b1 - 1], [b3, b4], z), z) s = MeijerUnShiftC([a1], [a3, a4], [b1], [b3, b4], 0, z) assert tn( s.apply(g, op), meijerg([a1], [a3, a4], [b1], [b3 - 1, b4], z), z) s = MeijerUnShiftB([a1], [a3, a4], [b1], [b3, b4], 0, z) assert tn( s.apply(g, op), meijerg([a1 + 1], [a3, a4], [b1], [b3, b4], z), z) s = MeijerUnShiftD([a1], [a3, a4], [b1], [b3, b4], 0, z) assert tn( s.apply(g, op), meijerg([a1], [a3 + 1, a4], [b1], [b3, b4], z), z)
def test_gh_issue_2711(): x = Symbol('x') f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == {(S.Zero, ), ((), ()), ((S.Zero, ), ()), x, S.Zero, (), meijerg(((), ()), ((S.Zero, ), ()), x)} assert f.find(a + b) == \ {meijerg(((), ()), ((S.Zero,), ()), x), x, S.Zero} assert f.find(a**2) == {meijerg(((), ()), ((S.Zero, ), ()), x), x}
def test_cosine_transform(): from diofant import Si, Ci t = symbols("t") w = symbols("w") a = symbols("a") f = Function("f") # Test unevaluated form assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w) assert inverse_cosine_transform(f(w), w, t) == InverseCosineTransform(f(w), w, t) assert cosine_transform(1 / sqrt(t), t, w) == 1 / sqrt(w) assert inverse_cosine_transform(1 / sqrt(w), w, t) == 1 / sqrt(t) assert cosine_transform(1 / (a**2 + t**2), t, w) == sqrt(2) * sqrt(pi) * exp(-a * w) / (2 * a) assert cosine_transform( t**(-a), t, w) == 2**(-a + Rational(1, 2)) * w**(a - 1) * gamma( (-a + 1) / 2) / gamma(a / 2) assert inverse_cosine_transform( 2**(-a + Rational(1, 2)) * w**(a - 1) * gamma(-a / 2 + Rational(1, 2)) / gamma(a / 2), w, t) == t**(-a) assert cosine_transform(exp(-a * t), t, w) == sqrt(2) * a / (sqrt(pi) * (a**2 + w**2)) assert inverse_cosine_transform( sqrt(2) * a / (sqrt(pi) * (a**2 + w**2)), w, t) == exp(-a * t) assert cosine_transform(exp(-a * sqrt(t)) * cos(a * sqrt(t)), t, w) == a * exp(-a**2 / (2 * w)) / (2 * w**Rational(3, 2)) assert cosine_transform( 1 / (a + t), t, w) == sqrt(2) * ((-2 * Si(a * w) + pi) * sin(a * w) / 2 - cos(a * w) * Ci(a * w)) / sqrt(pi) assert inverse_cosine_transform( sqrt(2) * meijerg(((Rational(1, 2), 0), ()), ((Rational(1, 2), 0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * pi), w, t) == 1 / (a + t) assert cosine_transform(1 / sqrt(a**2 + t**2), t, w) == sqrt(2) * meijerg( ((Rational(1, 2), ), ()), ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi)) assert inverse_cosine_transform( sqrt(2) * meijerg( ((Rational(1, 2), ), ()), ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi)), w, t) == 1 / (a * sqrt(1 + t**2 / a**2))
def test_plan(): assert devise_plan(Hyper_Function([0], ()), Hyper_Function([0], ()), z) == [] with pytest.raises(ValueError): devise_plan(Hyper_Function([1], ()), Hyper_Function((), ()), z) with pytest.raises(ValueError): devise_plan(Hyper_Function([2], [1]), Hyper_Function([2], [2]), z) with pytest.raises(ValueError): devise_plan(Hyper_Function([2], []), Hyper_Function([Rational(1, 2)], []), z) # We cannot use pi/(10000 + n) because polys is insanely slow. a1, a2, b1 = (randcplx(n) for n in 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) m = meijerg(((0, ), ()), ((), ()), z) m2 = meijerg(((1, ), ()), ((), ()), z) assert tn( apply_operators( m, devise_plan_meijer(G_Function([0], [], [], []), G_Function([1], [], [], []), z), op), m2, z) m2 = meijerg(((-1, ), ()), ((), ()), z) assert tn( apply_operators( m, devise_plan_meijer(G_Function([0], [], [], []), G_Function([-1], [], [], []), z), op), m2, z) m = meijerg(((), (1, )), ((), ()), z) m2 = meijerg(((), (2, )), ((), ()), z) assert tn( apply_operators( m, devise_plan_meijer(G_Function([], [1], [], []), G_Function([], [2], [], []), z), op), m2, z)
def test_elliptic_e(): assert elliptic_e(z, 0) == z assert elliptic_e(0, m) == 0 assert elliptic_e(i * pi / 2, m) == i * elliptic_e(m) assert elliptic_e(z, oo) == zoo assert elliptic_e(z, -oo) == zoo assert elliptic_e(0) == pi / 2 assert elliptic_e(1) == 1 assert elliptic_e(oo) == I * oo assert elliptic_e(-oo) == oo assert elliptic_e(zoo) == zoo assert elliptic_e(-z, m) == -elliptic_e(z, m) assert elliptic_e(z, m).diff(z) == sqrt(1 - m * sin(z)**2) assert elliptic_e( z, m).diff(m) == (elliptic_e(z, m) - elliptic_f(z, m)) / (2 * m) assert elliptic_e(z).diff(z) == (elliptic_e(z) - elliptic_k(z)) / (2 * z) r = randcplx() assert td(elliptic_e(r, m), m) assert td(elliptic_e(z, r), z) assert td(elliptic_e(z), z) pytest.raises(ArgumentIndexError, lambda: elliptic_e(z, m).fdiff(3)) pytest.raises(ArgumentIndexError, lambda: elliptic_e(z).fdiff(2)) mi = Symbol('m', extended_real=False) assert elliptic_e(z, mi).conjugate() == elliptic_e(z.conjugate(), mi.conjugate()) assert elliptic_e(mi).conjugate() == elliptic_e(mi.conjugate()) mr = Symbol('m', extended_real=True, negative=True) assert elliptic_e(z, mr).conjugate() == elliptic_e(z.conjugate(), mr) assert elliptic_e(mr).conjugate() == elliptic_e(mr) assert elliptic_e(z, m).conjugate() == conjugate(elliptic_e(z, m)) assert elliptic_e(z).conjugate() == conjugate(elliptic_e(z)) assert elliptic_e(z).rewrite(hyper) == (pi / 2) * hyper( (Rational(-1, 2), Rational(1, 2)), (1, ), z) assert elliptic_e(z, m).rewrite(hyper) == elliptic_e(z, m) assert tn(elliptic_e(z), (pi / 2) * hyper( (Rational(-1, 2), Rational(1, 2)), (1, ), z)) assert elliptic_e(z).rewrite(meijerg) == \ -meijerg(((Rational(1, 2), Rational(3, 2)), []), ((0,), (0,)), -z)/4 assert elliptic_e(z, m).rewrite(meijerg) == elliptic_e(z, m) assert tn( elliptic_e(z), -meijerg(((Rational(1, 2), Rational(3, 2)), []), ((0, ), (0, )), -z) / 4) assert elliptic_e(z, m).series(z) == \ z + z**5*(-m**2/40 + m/30) - m*z**3/6 + O(z**6) assert elliptic_e(z).series(z) == pi/2 - pi*z/8 - 3*pi*z**2/128 - \ 5*pi*z**3/512 - 175*pi*z**4/32768 - 441*pi*z**5/131072 + O(z**6)
def test_expand_func(): # evaluation at 1 of Gauss' hypergeometric function: a1, b1, c1 = randcplx(), randcplx(), randcplx() + 5 assert expand_func(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(-a - b + c)/(gamma(-a + c)*gamma(-b + c)) assert abs(expand_func(hyper([a1, b1], [c1], 1)) - hyper([a1, b1], [c1], 1)).evalf(strict=False) < 1e-10 # hyperexpand wrapper for hyper: assert expand_func(hyper([], [], z)) == exp(z) assert expand_func(hyper([1, 2, 3], [], z)) == hyper([1, 2, 3], [], z) assert expand_func(meijerg([[1, 1], []], [[1], [0]], z)) == log(z + 1) assert expand_func(meijerg([[1, 1], []], [[], []], z)) == \ meijerg([[1, 1], []], [[], []], z)
def test_meijerg_lookup(): assert hyperexpand(meijerg([a], [], [b, a], [], z)) == \ z**b*exp(z)*gamma(-a + b + 1)*uppergamma(a - b, z) assert hyperexpand(meijerg([0], [], [0, 0], [], z)) == \ exp(z)*uppergamma(0, z) assert can_do_meijer([a], [], [b, a + 1], []) assert can_do_meijer([a], [], [b + 2, a], []) assert can_do_meijer([a], [], [b - 2, a], []) assert hyperexpand(meijerg([a], [], [a, a, a - Rational(1, 2)], [], z)) == \ -sqrt(pi)*z**(a - Rational(1, 2))*(2*cos(2*sqrt(z))*(Si(2*sqrt(z)) - pi/2) - 2*sin(2*sqrt(z))*Ci(2*sqrt(z))) == \ hyperexpand(meijerg([a], [], [a, a - Rational(1, 2), a], [], z)) == \ hyperexpand(meijerg([a], [], [a - Rational(1, 2), a, a], [], z)) assert can_do_meijer([a - 1], [], [a + 2, a - Rational(3, 2), a + 1], [])
def test_lerchphi(): from diofant import combsimp, exp_polar, polylog, log, lerchphi assert hyperexpand(hyper([1, a], [a + 1], z) / a) == lerchphi(z, 1, a) assert hyperexpand(hyper([1, a, a], [a + 1, a + 1], z) / a**2) == lerchphi( z, 2, a) assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \ lerchphi(z, 3, a) assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \ lerchphi(z, 10, a) assert combsimp( hyperexpand(meijerg([0, 1 - a], [], [0], [-a], exp_polar(-I * pi) * z))) == lerchphi(z, 1, a) assert combsimp( hyperexpand( meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a], exp_polar(-I * pi) * z))) == lerchphi(z, 2, a) assert combsimp( hyperexpand( meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a], exp_polar(-I * pi) * z))) == lerchphi(z, 3, a) assert hyperexpand(z * hyper([1, 1], [2], z)) == -log(1 + -z) assert hyperexpand(z * hyper([1, 1, 1], [2, 2], z)) == polylog(2, z) assert hyperexpand(z * hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z) assert hyperexpand(hyper([1, a, 1 + Rational(1, 2)], [a + 1, Rational(1, 2)], z)) == \ -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a) # Now numerical tests. These make sure reductions etc are carried out # correctly # a rational function (polylog at negative integer order) assert can_do([2, 2, 2], [1, 1]) # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1 # reduction of order for polylog assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10) # reduction of order for lerchphi # XXX lerchphi in mpmath is flaky assert can_do([1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b], numerical=False) # test a bug from diofant import Abs assert hyperexpand(hyper([Rational(1, 2), Rational(1, 2), Rational(1, 2), 1], [Rational(3, 2), Rational(3, 2), Rational(3, 2)], Rational(1, 4))) == \ Abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, Rational(1, 2)))
def test_meijerg_formulae(): from diofant.simplify.hyperexpand import MeijerFormulaCollection formulae = MeijerFormulaCollection().formulae for sig in formulae: for formula in formulae[sig]: g = meijerg(formula.func.an, formula.func.ap, formula.func.bm, formula.func.bq, formula.z) rep = {} for sym in formula.symbols: rep[sym] = randcplx() # first test if the closed-form is actually correct g = g.subs(rep) closed_form = formula.closed_form.subs(rep) z = formula.z assert tn(g, closed_form, z) # now test the computed matrix cl = (formula.C * formula.B)[0].subs(rep) assert tn(closed_form, cl, z) deriv1 = z*formula.B.diff(z) deriv2 = formula.M * formula.B for d1, d2 in zip(deriv1, deriv2): assert tn(d1.subs(rep), d2.subs(rep), z)
def t(fac, arg): g = meijerg([a], [b], [c], [d], arg)*fac subs = {a: randcplx()/10, b: randcplx()/10 + I, c: randcplx(), d: randcplx()} integral = meijerint_indefinite(g, x) assert integral is not None assert verify_numerically(g.subs(subs), integral.diff(x).subs(subs), x)
def test_li(): z = Symbol("z") zr = Symbol("z", extended_real=True) zp = Symbol("z", positive=True) zn = Symbol("z", negative=True) assert li(0) == 0 assert li(1) == -oo assert li(oo) == oo assert isinstance(li(z), li) assert diff(li(z), z) == 1/log(z) assert conjugate(li(z)) == li(conjugate(z)) assert conjugate(li(-zr)) == li(-zr) assert conjugate(li(-zp)) == conjugate(li(-zp)) assert conjugate(li(zn)) == conjugate(li(zn)) assert li(z).rewrite(Li) == Li(z) + li(2) assert li(z).rewrite(Ei) == Ei(log(z)) assert li(z).rewrite(uppergamma) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - expint(1, -log(z))) assert li(z).rewrite(Si) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Ci) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Shi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(Chi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(hyper) == (log(z)*hyper((1, 1), (2, 2), log(z)) - log(1/log(z))/2 + log(log(z))/2 + EulerGamma) assert li(z).rewrite(meijerg) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - meijerg(((), (1,)), ((0, 0), ()), -log(z)))
def test_meijerg_with_Floats(): # see sympy/sympy#10681 f = meijerg(((3.0, 1), ()), ((Rational(3, 2),), (0,)), z) a = -2.3632718012073 g = a*z**Rational(3, 2)*hyper((-0.5, Rational(3, 2)), (Rational(5, 2),), z*exp_polar(I*pi)) assert RR.almosteq((hyperexpand(f)/g).n(), 1.0, 1e-12)
def test_erf(): assert erf(nan) == 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, erf(erfcinv(1 - erf(erfinv(x)))))) == x assert erf(I).is_extended_real is False assert erf(w).is_extended_real is True assert erf(z).is_extended_real is None assert conjugate(erf(z)) == erf(conjugate(z)) assert erf(x).as_leading_term(x) == 2 * x / sqrt(pi) assert erf(1 / x).as_leading_term(x) == erf(1 / x) assert erf(z).rewrite('uppergamma') == sqrt(z**2) * erf(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], [-S.Half], 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 erf(x).as_real_imag() == \ ((erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erf(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erf(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) pytest.raises(ArgumentIndexError, lambda: erf(x).fdiff(2))
def test_branch_bug(): assert hyperexpand(hyper((-Rational(1, 3), Rational(1, 2)), (Rational(2, 3), Rational(3, 2)), -z)) == \ -z**Rational(1, 3)*lowergamma(exp_polar(I*pi)/3, z)/5 \ + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z)) assert hyperexpand(meijerg([Rational(7, 6), 1], [], [Rational(2, 3)], [Rational(1, 6), 0], z)) == \ 2*z**Rational(2, 3)*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) - 2*lowergamma(Rational(2, 3), z)/z**Rational(2, 3))*gamma(Rational(2, 3))/gamma(Rational(5, 3))
def test_branch_bug(): assert hyperexpand(hyper((-Rational(1, 3), Rational(1, 2)), (Rational(2, 3), Rational(3, 2)), -z)) == \ -cbrt(z)*lowergamma(exp_polar(I*pi)/3, z)/5 \ + sqrt(pi)*erf(sqrt(z))/(5*sqrt(z)) assert hyperexpand(meijerg([Rational(7, 6), 1], [], [Rational(2, 3)], [Rational(1, 6), 0], z)) == \ 2*z**Rational(2, 3)*(2*sqrt(pi)*erf(sqrt(z))/sqrt(z) - 2*lowergamma(Rational(2, 3), z)/z**Rational(2, 3))*gamma(Rational(2, 3))/gamma(Rational(5, 3))
def test_diofantissue_149(): a = Symbol('a', positive=True) expr = (2 - x)**a*sin(a/x) res = sqrt(pi)*a*meijerg(((), (a/2 + 1/2, a/2 + 1)), ((0, 0, Rational(1, 2)), (Rational(-1, 2),)), a**2/16)*gamma(a + 1)/4 assert integrate(expr, (x, 0, 2)) == res
def test_sympyissue_21202(): res = (Piecewise( (s / (s**2 - 4), (4 * abs(s**-2) < 1) | (abs(s**2) / 4 < 1)), (pi * meijerg(((Rational(1, 2), ), (0, 0)), ((0, Rational(1, 2)), (0, )), s**2 / 4) / 2, True)), 2, Ne(s**2 / 4, 1)) assert laplace_transform(cosh(2 * x), x, s) == res
def test_erfc(): assert erfc(nan) == nan assert erfc(oo) == 0 assert erfc(-oo) == 2 assert erfc(0) == 1 assert erfc(I * oo) == -oo * I assert erfc(-I * oo) == oo * I assert erfc(-x) == Integer(2) - erfc(x) assert erfc(erfcinv(x)) == x assert erfc(erfinv(x)) == 1 - x assert erfc(I).is_extended_real is False assert erfc(w).is_extended_real is True assert erfc(z).is_extended_real is None assert conjugate(erfc(z)) == erfc(conjugate(z)) assert erfc(x).as_leading_term(x) == 1 assert erfc(1 / x).as_leading_term(x) == erfc(1 / x) assert erfc(z).rewrite('erf') == 1 - erf(z) assert erfc(z).rewrite('erfi') == 1 + I * erfi(I * z) assert erfc(z).rewrite('fresnels') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite('fresnelc') == 1 - (1 + I) * ( fresnelc(z * (1 - I) / sqrt(pi)) - I * fresnels(z * (1 - I) / sqrt(pi))) assert erfc(z).rewrite('hyper') == 1 - 2 * z * hyper( [Rational(1, 2)], [Rational(3, 2)], -z**2) / sqrt(pi) assert erfc(z).rewrite('meijerg') == 1 - z * meijerg( [Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2) / sqrt(pi) assert erfc(z).rewrite( 'uppergamma') == 1 - sqrt(z**2) * erf(sqrt(z**2)) / z assert erfc(z).rewrite('expint') == 1 - sqrt(z**2) / z + z * expint( Rational(1, 2), z**2) / sqrt(pi) assert erfc(x).as_real_imag() == \ ((erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(erfc(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - erfc(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag() assert erfc(w).as_real_imag() == (erfc(w), 0) assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag() assert erfc(I).as_real_imag() == (1, -erfi(1)) pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2)) assert erfc(x).taylor_term(3, x, *(-2 * x / sqrt(pi), 0)) == 2 * x**3 / 3 / sqrt(pi) assert erfc(x).limit(x, oo) == 0 assert erfc(x).diff(x) == -2 * exp(-x**2) / sqrt(pi)
def test_sympyissue_2711(): f = meijerg(((), ()), ((0, ), ()), x) a = Wild('a') b = Wild('b') assert f.find(a) == { 0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1, (): 3, (0, ): 1, ((), ()): 1, ((0, ), ()): 1 } assert f.find(a + b) == {0: 1, x: 1, meijerg(((), ()), ((0, ), ()), x): 1} assert f.find(a**2) == {x: 1, meijerg(((), ()), ((0, ), ()), x): 1}
def test_sympyissue_8368(): assert integrate(exp(-s*x)*cosh(x), (x, 0, oo)) == \ Piecewise((pi*Piecewise((-s/(pi*(-s**2 + 1)), Abs(s**2) < 1), (1/(pi*s*(1 - 1/s**2)), Abs(s**(-2)) < 1), (meijerg(((Rational(1, 2),), (0, 0)), ((0, Rational(1, 2)), (0,)), polar_lift(s)**2), True)), And(Abs(periodic_argument(polar_lift(s)**2, oo)) < pi, Ne(s**2, 1), cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0)), (Integral(exp(-s*x)*cosh(x), (x, 0, oo)), True)) assert integrate(exp(-s*x)*sinh(x), (x, 0, oo)) == \ Piecewise((pi*Piecewise((2/(pi*(2*s**2 - 2)), Abs(s**2) < 1), (-2/(pi*s**2*(-2 + 2/s**2)), Abs(s**(-2)) < 1), (meijerg(((0,), (Rational(-1, 2), Rational(1, 2))), ((0, Rational(1, 2)), (Rational(-1, 2),)), polar_lift(s)**2), True)), And(Abs(periodic_argument(polar_lift(s)**2, oo)) < pi, Ne(s**2, 1), cos(Abs(periodic_argument(polar_lift(s)**2, oo))/2)*sqrt(Abs(s**2)) - 1 > 0)), (Integral(E**(-s*x)*sinh(x), (x, 0, oo)), True))
def test_expand_func(): # evaluation at 1 of Gauss' hypergeometric function: from diofant.abc import a, b, c from diofant import gamma, expand_func a1, b1, c1 = randcplx(), randcplx(), randcplx() + 5 assert expand_func(hyper([a, b], [c], 1)) == \ gamma(c)*gamma(-a - b + c)/(gamma(-a + c)*gamma(-b + c)) assert abs( expand_func(hyper([a1, b1], [c1], 1)).n() - hyper([a1, b1], [c1], 1).n()) < 1e-10 # hyperexpand wrapper for hyper: assert expand_func(hyper([], [], z)) == exp(z) assert expand_func(hyper([1, 2, 3], [], z)) == hyper([1, 2, 3], [], z) assert expand_func(meijerg([[1, 1], []], [[1], [0]], z)) == log(z + 1) assert expand_func(meijerg([[1, 1], []], [[], []], z)) == \ meijerg([[1, 1], []], [[], []], z)
def test_erfi(): assert erfi(nan) == nan assert erfi(+oo) == +oo assert erfi(-oo) == -oo assert erfi(0) == 0 assert erfi(I * oo) == I assert erfi(-I * oo) == -I assert erfi(-x) == -erfi(x) assert erfi(I * erfinv(x)) == I * x assert erfi(I * erfcinv(x)) == I * (1 - x) assert erfi(I * erf2inv(0, x)) == I * x assert erfi(I).is_extended_real is False assert erfi(w).is_extended_real is True assert erfi(z).is_extended_real is None assert conjugate(erfi(z)) == erfi(conjugate(z)) assert erfi(z).rewrite('erf') == -I * erf(I * z) assert erfi(z).rewrite('erfc') == I * erfc(I * z) - I assert erfi(z).rewrite('fresnels') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('fresnelc') == (1 - I) * ( fresnelc(z * (1 + I) / sqrt(pi)) - I * fresnels(z * (1 + I) / sqrt(pi))) assert erfi(z).rewrite('hyper') == 2 * z * hyper( [Rational(1, 2)], [Rational(3, 2)], z**2) / sqrt(pi) assert erfi(z).rewrite('meijerg') == z * meijerg( [Rational(1, 2)], [], [0], [Rational(-1, 2)], -z**2) / sqrt(pi) assert erfi(z).rewrite('uppergamma') == ( sqrt(-z**2) / z * (uppergamma(Rational(1, 2), -z**2) / sqrt(pi) - 1)) assert erfi(z).rewrite('expint') == sqrt(-z**2) / z - z * expint( Rational(1, 2), -z**2) / sqrt(pi) assert erfi(x).as_real_imag() == \ ((erfi(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 + erfi(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2, I*(erfi(re(x) - I*re(x)*abs(im(x))/abs(re(x))) - erfi(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) * re(x)*abs(im(x))/(2*im(x)*abs(re(x))))) assert erfi(x).as_real_imag(deep=False) == erfi(x).as_real_imag() assert erfi(w).as_real_imag() == (erfi(w), 0) assert erfi(w).as_real_imag(deep=False) == erfi(w).as_real_imag() assert erfi(I).as_real_imag() == (0, erf(1)) pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2)) assert erfi(x).taylor_term(3, x, *(2 * x / sqrt(pi), 0)) == 2 * x**3 / 3 / sqrt(pi) assert erfi(x).limit(x, oo) == oo
def t(a, b, arg, n): m1 = meijerg(a, b, arg) m2 = Mul(*_inflate_g(m1, n)) # NOTE: (the random number)**9 must still be on the principal sheet. # Thus make b&d small to create random numbers of small imaginary part. return verify_numerically(m1.subs(subs), m2.subs(subs), x, b=0.1, d=-0.1)
def test_lerchphi(): assert hyperexpand(hyper([1, a], [a + 1], z)/a) == lerchphi(z, 1, a) assert hyperexpand( hyper([1, a, a], [a + 1, a + 1], z)/a**2) == lerchphi(z, 2, a) assert hyperexpand(hyper([1, a, a, a], [a + 1, a + 1, a + 1], z)/a**3) == \ lerchphi(z, 3, a) assert hyperexpand(hyper([1] + [a]*10, [a + 1]*10, z)/a**10) == \ lerchphi(z, 10, a) assert combsimp(hyperexpand(meijerg([0, 1 - a], [], [0], [-a], exp_polar(-I*pi)*z))) == lerchphi(z, 1, a) assert combsimp(hyperexpand(meijerg([0, 1 - a, 1 - a], [], [0], [-a, -a], exp_polar(-I*pi)*z))) == lerchphi(z, 2, a) assert combsimp(hyperexpand(meijerg([0, 1 - a, 1 - a, 1 - a], [], [0], [-a, -a, -a], exp_polar(-I*pi)*z))) == lerchphi(z, 3, a) assert hyperexpand(z*hyper([1, 1], [2], z)) == -log(1 + -z) assert hyperexpand(z*hyper([1, 1, 1], [2, 2], z)) == polylog(2, z) assert hyperexpand(z*hyper([1, 1, 1, 1], [2, 2, 2], z)) == polylog(3, z) assert hyperexpand(hyper([1, a, 1 + Rational(1, 2)], [a + 1, Rational(1, 2)], z)) == \ -2*a/(z - 1) + (-2*a**2 + a)*lerchphi(z, 1, a) # Now numerical tests. These make sure reductions etc are carried out # correctly # a rational function (polylog at negative integer order) assert can_do([2, 2, 2], [1, 1]) # NOTE these contain log(1-x) etc ... better make sure we have |z| < 1 # reduction of order for polylog assert can_do([1, 1, 1, b + 5], [2, 2, b], div=10) # reduction of order for lerchphi # XXX lerchphi in mpmath is flaky assert can_do( [1, a, a, a, b + 5], [a + 1, a + 1, a + 1, b], numerical=False) # test a bug assert hyperexpand(hyper([Rational(1, 2), Rational(1, 2), Rational(1, 2), 1], [Rational(3, 2), Rational(3, 2), Rational(3, 2)], Rational(1, 4))) == \ abs(-polylog(3, exp_polar(I*pi)/2) + polylog(3, Rational(1, 2)))
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) assert hyperexpand(hyper([a], [b], 0)) == 1 assert hyper([a], [b], 0) != 0
def test_erfc(): assert erfc(nan) == nan assert erfc(oo) == 0 assert erfc(-oo) == 2 assert erfc(0) == 1 assert erfc(I*oo) == -oo*I assert erfc(-I*oo) == oo*I assert erfc(-x) == Integer(2) - erfc(x) assert erfc(erfcinv(x)) == x assert erfc(erfinv(x)) == 1 - x assert erfc(I).is_extended_real is False assert erfc(w).is_extended_real is True assert erfc(z).is_extended_real is None assert conjugate(erfc(z)) == erfc(conjugate(z)) assert erfc(x).as_leading_term(x) == 1 assert erfc(1/x).as_leading_term(x) == erfc(1/x) assert erfc(z).rewrite('erf') == 1 - erf(z) assert erfc(z).rewrite('erfi') == 1 + I*erfi(I*z) assert erfc(z).rewrite('fresnels') == 1 - (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) - I*fresnels(z*(1 - I)/sqrt(pi))) assert erfc(z).rewrite('fresnelc') == 1 - (1 + I)*(fresnelc(z*(1 - I)/sqrt(pi)) - I*fresnels(z*(1 - I)/sqrt(pi))) assert erfc(z).rewrite('hyper') == 1 - 2*z*hyper([Rational(1, 2)], [Rational(3, 2)], -z**2)/sqrt(pi) assert erfc(z).rewrite('meijerg') == 1 - z*meijerg([Rational(1, 2)], [], [0], [Rational(-1, 2)], z**2)/sqrt(pi) assert erfc(z).rewrite('uppergamma') == 1 - sqrt(z**2)*erf(sqrt(z**2))/z assert erfc(z).rewrite('expint') == 1 - sqrt(z**2)/z + z*expint(Rational(1, 2), z**2)/sqrt(pi) assert erfc(x).as_real_imag() == \ ((erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfc(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfc(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) assert erfc(x).as_real_imag(deep=False) == erfc(x).as_real_imag() assert erfc(w).as_real_imag() == (erfc(w), 0) assert erfc(w).as_real_imag(deep=False) == erfc(w).as_real_imag() assert erfc(I).as_real_imag() == (1, -erfi(1)) pytest.raises(ArgumentIndexError, lambda: erfc(x).fdiff(2)) assert erfc(x).taylor_term(3, x, *(-2*x/sqrt(pi), 0)) == 2*x**3/3/sqrt(pi) assert erfc(x).limit(x, oo) == 0 assert erfc(x).diff(x) == -2*exp(-x**2)/sqrt(pi)
def test_erfi(): assert erfi(nan) == nan assert erfi(+oo) == +oo assert erfi(-oo) == -oo assert erfi(0) == 0 assert erfi(I*oo) == I assert erfi(-I*oo) == -I assert erfi(-x) == -erfi(x) assert erfi(I*erfinv(x)) == I*x assert erfi(I*erfcinv(x)) == I*(1 - x) assert erfi(I*erf2inv(0, x)) == I*x assert erfi(I).is_extended_real is False assert erfi(w).is_extended_real is True assert erfi(z).is_extended_real is None assert conjugate(erfi(z)) == erfi(conjugate(z)) assert erfi(z).rewrite('erf') == -I*erf(I*z) assert erfi(z).rewrite('erfc') == I*erfc(I*z) - I assert erfi(z).rewrite('fresnels') == (1 - I)*(fresnelc(z*(1 + I)/sqrt(pi)) - I*fresnels(z*(1 + I)/sqrt(pi))) assert erfi(z).rewrite('fresnelc') == (1 - I)*(fresnelc(z*(1 + I)/sqrt(pi)) - I*fresnels(z*(1 + I)/sqrt(pi))) assert erfi(z).rewrite('hyper') == 2*z*hyper([Rational(1, 2)], [Rational(3, 2)], z**2)/sqrt(pi) assert erfi(z).rewrite('meijerg') == z*meijerg([Rational(1, 2)], [], [0], [Rational(-1, 2)], -z**2)/sqrt(pi) assert erfi(z).rewrite('uppergamma') == (sqrt(-z**2)/z*(uppergamma(Rational(1, 2), -z**2)/sqrt(pi) - 1)) assert erfi(z).rewrite('expint') == sqrt(-z**2)/z - z*expint(Rational(1, 2), -z**2)/sqrt(pi) assert erfi(x).as_real_imag() == \ ((erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x)))/2 + erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))/2, I*(erfi(re(x) - I*re(x)*Abs(im(x))/Abs(re(x))) - erfi(re(x) + I*re(x)*Abs(im(x))/Abs(re(x)))) * re(x)*Abs(im(x))/(2*im(x)*Abs(re(x))))) assert erfi(x).as_real_imag(deep=False) == erfi(x).as_real_imag() assert erfi(w).as_real_imag() == (erfi(w), 0) assert erfi(w).as_real_imag(deep=False) == erfi(w).as_real_imag() assert erfi(I).as_real_imag() == (0, erf(1)) pytest.raises(ArgumentIndexError, lambda: erfi(x).fdiff(2)) assert erfi(x).taylor_term(3, x, *(2*x/sqrt(pi), 0)) == 2*x**3/3/sqrt(pi) assert erfi(x).limit(x, oo) == oo
def test_meijerg_period(): assert meijerg([], [1], [0], [], x).get_period() == 2*pi assert meijerg([1], [], [], [0], x).get_period() == 2*pi assert meijerg([], [], [0], [], x).get_period() == 2*pi # exp(x) assert meijerg( [], [], [0], [Rational(1, 2)], x).get_period() == 2*pi # cos(sqrt(x)) assert meijerg( [], [], [Rational(1, 2)], [0], x).get_period() == 4*pi # sin(sqrt(x)) assert meijerg([1, 1], [], [1], [0], x).get_period() == oo # log(1 + x)
def test_rewrite_single(): def t(expr, c, m): e = _rewrite_single(meijerg([a], [b], [c], [d], expr), x) assert e is not None assert isinstance(e[0][0][2], meijerg) assert e[0][0][2].argument.as_coeff_mul(x) == (c, (m,)) def tn(expr): assert _rewrite_single(meijerg([a], [b], [c], [d], expr), x) is None t(x, 1, x) t(x**2, 1, x**2) t(x**2 + y*x**2, y + 1, x**2) tn(x**2 + x) tn(x**y) def u(expr, x): r = _rewrite_single(expr, x) e = Add(*[res[0]*res[2] for res in r[0]]).replace( exp_polar, exp) # XXX Hack? assert verify_numerically(e, expr, x) u(exp(-x)*sin(x), x) # The following has stopped working because hyperexpand changed slightly. # It is probably not worth fixing # u(exp(-x)*sin(x)*cos(x), x) # This one cannot be done numerically, since it comes out as a g-function # of argument 4*pi # NOTE This also tests a bug in inverse mellin transform (which used to # turn exp(4*pi*I*t) into a factor of exp(4*pi*I)**t instead of # exp_polar). # u(exp(x)*sin(x), x) assert _rewrite_single(exp(x)*sin(x), x) == \ ([(-sqrt(2)/(2*sqrt(pi)), 0, meijerg(((-Rational(1, 2), 0, Rational(1, 4), Rational(1, 2), Rational(3, 4)), (1,)), ((), (-Rational(1, 2), 0)), 64*exp_polar(-4*I*pi)/x**4))], True)
def test_meijerg_formulae(): formulae = MeijerFormulaCollection().formulae for sig in formulae: for formula in formulae[sig]: g = meijerg(formula.func.an, formula.func.ap, formula.func.bm, formula.func.bq, formula.z) rep = {} for sym in formula.symbols: rep[sym] = randcplx() # first test if the closed-form is actually correct g = g.subs(rep) closed_form = formula.closed_form.subs(rep) z = formula.z assert tn(g, closed_form, z) # now test the computed matrix cl = (formula.C * formula.B)[0].subs(rep) assert tn(closed_form, cl, z) deriv1 = z*formula.B.diff(z) deriv2 = formula.M * formula.B for d1, d2 in zip(deriv1, deriv2): assert tn(d1.subs(rep), d2.subs(rep), z)
def test_li(): z = Symbol("z") zr = Symbol("z", extended_real=True) zp = Symbol("z", positive=True) zn = Symbol("z", negative=True) assert li(0) == 0 assert li(1) == -oo assert li(oo) == oo assert isinstance(li(z), li) assert diff(li(z), z) == 1/log(z) pytest.raises(ArgumentIndexError, lambda: li(z).fdiff(2)) assert conjugate(li(z)) == li(conjugate(z)) assert conjugate(li(-zr)) == li(-zr) assert conjugate(li(-zp)) == conjugate(li(-zp)) assert conjugate(li(zn)) == conjugate(li(zn)) assert li(z).rewrite(Li) == Li(z) + li(2) assert li(z).rewrite(Ei) == Ei(log(z)) assert li(z).rewrite(uppergamma) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - expint(1, -log(z))) assert li(z).rewrite(Si) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Ci) == (-log(I*log(z)) - log(1/log(z))/2 + log(log(z))/2 + Ci(I*log(z)) + Shi(log(z))) assert li(z).rewrite(Shi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(Chi) == (-log(1/log(z))/2 + log(log(z))/2 + Chi(log(z)) - Shi(log(z))) assert li(z).rewrite(hyper) == (log(z)*hyper((1, 1), (2, 2), log(z)) - log(1/log(z))/2 + log(log(z))/2 + EulerGamma) assert li(z).rewrite(meijerg) == (-log(1/log(z))/2 - log(-log(z)) + log(log(z))/2 - meijerg(((), (1,)), ((0, 0), ()), -log(z)))
def test_meijerg_confluence(): def t(m, a, b): a, b = sympify([a, b]) m_ = m m = hyperexpand(m) if not m == Piecewise((a, abs(z) < 1), (b, abs(1/z) < 1), (m_, True)): return False if not (m.args[0].args[0] == a and m.args[1].args[0] == b): return False z0 = randcplx()/10 if abs(m.subs({z: z0}) - a.subs({z: z0})).evalf(strict=False) > 1e-10: return False if abs(m.subs({z: 1/z0}) - b.subs({z: 1/z0})).evalf(strict=False) > 1e-10: return False return True assert t(meijerg([], [1, 1], [0, 0], [], z), -log(z), 0) assert t(meijerg( [], [3, 1], [0, 0], [], z), -z**2/4 + z - log(z)/2 - Rational(3, 4), 0) assert t(meijerg([], [3, 1], [-1, 0], [], z), z**2/12 - z/2 + log(z)/2 + Rational(1, 4) + 1/(6*z), 0) assert t(meijerg([], [1, 1, 1, 1], [0, 0, 0, 0], [], z), -log(z)**3/6, 0) assert t(meijerg([1, 1], [], [], [0, 0], z), 0, -log(1/z)) assert t(meijerg([1, 1], [2, 2], [1, 1], [0, 0], z), -z*log(z) + 2*z, -log(1/z) + 2) assert t(meijerg([Rational(1, 2)], [1, 1], [0, 0], [Rational(3, 2)], z), log(z)/2 - 1, 0) def u(an, ap, bm, bq): m = meijerg(an, ap, bm, bq, z) m2 = hyperexpand(m, allow_hyper=True) if m2.has(meijerg) and not (m2.is_Piecewise and len(m2.args) == 3): return False return tn(m, m2, z) assert u([], [1], [0, 0], []) assert u([1, 1], [], [], [0]) assert u([1, 1], [2, 2, 5], [1, 1, 6], [0, 0]) assert u([1, 1], [2, 2, 5], [1, 1, 6], [0])
def test_meijerg(): # carefully set up the parameters. # NOTE: this used to fail sometimes. I believe it is fixed, but if you # hit an inexplicable test failure here, please let me know the seed. a1, a2 = (randcplx(n) - 5*I - n*I for n in range(2)) b1, b2 = (randcplx(n) + 5*I + n*I for n in range(2)) b3, b4, b5, a3, a4, a5 = (randcplx() for n in range(6)) g = meijerg([a1], [a3, a4], [b1], [b3, b4], z) assert ReduceOrder.meijer_minus(3, 4) is None assert ReduceOrder.meijer_plus(4, 3) is None g2 = meijerg([a1, a2], [a3, a4], [b1], [b3, b4, a2], z) assert tn(ReduceOrder.meijer_plus(a2, a2).apply(g, op), g2, z) g2 = meijerg([a1, a2], [a3, a4], [b1], [b3, b4, a2 + 1], z) assert tn(ReduceOrder.meijer_plus(a2, a2 + 1).apply(g, op), g2, z) g2 = meijerg([a1, a2 - 1], [a3, a4], [b1], [b3, b4, a2 + 2], z) assert tn(ReduceOrder.meijer_plus(a2 - 1, a2 + 2).apply(g, op), g2, z) g2 = meijerg([a1], [a3, a4, b2 - 1], [b1, b2 + 2], [b3, b4], z) assert tn(ReduceOrder.meijer_minus( b2 + 2, b2 - 1).apply(g, op), g2, z, tol=1e-6) # test several-step reduction an = [a1, a2] bq = [b3, b4, a2 + 1] ap = [a3, a4, b2 - 1] bm = [b1, b2 + 1] niq, ops = reduce_order_meijer(G_Function(an, ap, bm, bq)) assert niq.an == (a1,) assert set(niq.ap) == {a3, a4} assert niq.bm == (b1,) assert set(niq.bq) == {b3, b4} assert tn(apply_operators(g, ops, op), meijerg(an, ap, bm, bq, z), z)
def test_rewrite1(): assert _rewrite1(x**3*meijerg([a], [b], [c], [d], x**2 + y*x**2)*5, x) == \ (5, x**3, [(1, 0, meijerg([a], [b], [c], [d], x**2*(y + 1)))], True)
def tn(expr): assert _rewrite_single(meijerg([a], [b], [c], [d], expr), x) is None
def test_meijerg_expand(): # 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([[], []], [[Rational(1, 2)], [0]], (z/2)**2)) \ == sin(z)/sqrt(pi) assert hyperexpand(meijerg([[], []], [[0], [Rational(1, 2)]], (z/2)**2)) \ == cos(z)/sqrt(pi) assert can_do_meijer([], [a], [a - 1, a - Rational(1, 2)], []) 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 + Rational(1, 2)]) assert can_do_meijer( [], [], [a], [b], False) # branches only agree for small z assert can_do_meijer([], [Rational(1, 2)], [a], [-a]) assert can_do_meijer([], [], [a, b], []) assert can_do_meijer([], [], [a, b], []) assert can_do_meijer([], [], [a, a + Rational(1, 2)], [b, b + Rational(1, 2)]) assert can_do_meijer([], [], [a, -a], [0, Rational(1, 2)], False) # dito assert can_do_meijer([], [], [a, a + Rational(1, 2), b, b + Rational(1, 2)], []) assert can_do_meijer([Rational(1, 2)], [], [0], [a, -a]) assert can_do_meijer([Rational(1, 2)], [], [a], [0, -a], False) # dito assert can_do_meijer([], [a - Rational(1, 2)], [a, b], [a - Rational(1, 2)], False) assert can_do_meijer([], [a + Rational(1, 2)], [a + b, a - b, a], [], False) assert can_do_meijer([a + Rational(1, 2)], [], [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 combsimp(simplify(hyperexpand( meijerg([1], [1 - a], [-a/2, -a/2 + Rational(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 assert can_do_meijer([], [], [a + Rational(1, 2)], [a, a - b/2, a + b/2]) assert can_do_meijer([], [], [3*a - Rational(1, 2), a, -a - Rational(1, 2)], [a - Rational(1, 2)]) assert can_do_meijer([], [], [0, a - Rational(1, 2), -a - Rational(1, 2)], [Rational(1, 2)]) assert can_do_meijer([Rational(1, 2)], [], [-a, a], [0])
def t(expr, c, m): e = _rewrite_single(meijerg([a], [b], [c], [d], expr), x) assert e is not None assert isinstance(e[0][0][2], meijerg) assert e[0][0][2].argument.as_coeff_mul(x) == (c, (m,))
def u(an, ap, bm, bq): m = meijerg(an, ap, bm, bq, z) m2 = hyperexpand(m, allow_hyper=True) if m2.has(meijerg) and not (m2.is_Piecewise and len(m2.args) == 3): return False return tn(m, m2, z)
def test_Mod1_behavior(): n = Symbol('n', integer=True) # Note: this should not hang. assert simplify(hyperexpand(meijerg([1], [], [n + 1], [0], z))) == \ lowergamma(n + 1, z)
def test_sympyissue_6052(): G0 = meijerg((), (), (1,), (0,), 0) assert hyperexpand(G0) == 0 assert hyperexpand(hyper((), (2,), 0)) == 1