def test_lerchphi_expansion(): assert myexpand(lerchphi(1, s, a), zeta(s, a)) assert myexpand(lerchphi(z, s, 1), polylog(s, z) / z) # direct summation assert myexpand(lerchphi(z, -1, a), a / (1 - z) + z / (1 - z)**2) assert myexpand(lerchphi(z, -3, a), None) # polylog reduction assert myexpand( lerchphi(z, s, Rational(1, 2)), 2**(s - 1) * (polylog(s, sqrt(z)) / sqrt(z) - polylog(s, polar_lift(-1) * sqrt(z)) / sqrt(z))) assert myexpand(lerchphi(z, s, 2), -1 / z + polylog(s, z) / z**2) assert myexpand(lerchphi(z, s, Rational(3, 2)), None) assert myexpand(lerchphi(z, s, Rational(7, 3)), None) assert myexpand(lerchphi(z, s, -Rational(1, 3)), None) assert myexpand(lerchphi(z, s, -Rational(5, 2)), None) # hurwitz zeta reduction assert myexpand(lerchphi(-1, s, a), 2**(-s) * zeta(s, a / 2) - 2**(-s) * zeta(s, (a + 1) / 2)) assert myexpand(lerchphi(I, s, a), None) assert myexpand(lerchphi(-I, s, a), None) assert myexpand(lerchphi(exp(2 * I * pi / 5), s, a), None)
def test_gruntz_eval_special(): # Gruntz, p. 126 assert gruntz( exp(x) * (sin(1 / x + exp(-x)) - sin(1 / x + exp(-x**2))), x) == 1 assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2), x) == -2 / sqrt(pi) assert gruntz( exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))), x) == 1 assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x) == oo assert gruntz(exp(exp(digamma(digamma(x)))) / x, x) == exp(-Rational(1, 2)) assert gruntz(exp(exp(digamma(log(x)))) / x, x) == exp(-Rational(1, 2)) assert gruntz(digamma(digamma(digamma(x))), x) == oo assert gruntz(loggamma(loggamma(x)), x) == oo assert gruntz( ((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x * log(x), x) == -Rational(1, 2) assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x) \ == Rational(1, 2) assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x) == 1 assert gruntz( gamma(x + 1) / sqrt(2 * pi) - exp(-x) * (x**(x + Rational(1, 2)) + x**(x - Rational(1, 2)) / 12), x) == oo assert gruntz(exp(exp(exp(digamma(digamma(digamma(x)))))) / x, x) == 0 assert gruntz(exp(gamma(x - exp(-x)) * exp(1 / x)) - exp(gamma(x)), x) == oo assert gruntz((Ei(x - exp(-exp(x))) - Ei(x)) * exp(-x) * exp(exp(x)) * x, x) == -1 assert gruntz(exp((log(2) + 1) * x) * (zeta(x + exp(-x)) - zeta(x)), x) == -log(2)
def test_specfun(): for f in [besselj, bessely, besseli, besselk]: assert octave_code(f(n, x)) == f.__name__ + '(n, x)' assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)' assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)' assert octave_code(airyai(x)) == 'airy(0, x)' assert octave_code(airyaiprime(x)) == 'airy(1, x)' assert octave_code(airybi(x)) == 'airy(2, x)' assert octave_code(airybiprime(x)) == 'airy(3, x)' assert octave_code(uppergamma(n, x)) == "gammainc(x, n, 'upper')" assert octave_code(lowergamma(n, x)) == "gammainc(x, n, 'lower')" assert octave_code(jn( n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2' assert octave_code(yn( n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2' assert octave_code(Chi(x)) == 'coshint(x)' assert octave_code(Ci(x)) == 'cosint(x)' assert octave_code(laguerre(n, x)) == 'laguerreL(n, x)' assert octave_code(li(x)) == 'logint(x)' assert octave_code(loggamma(x)) == 'gammaln(x)' assert octave_code(polygamma(n, x)) == 'psi(n, x)' assert octave_code(Shi(x)) == 'sinhint(x)' assert octave_code(Si(x)) == 'sinint(x)' assert octave_code(LambertW(x)) == 'lambertw(x)' assert octave_code(LambertW(x, n)) == 'lambertw(n, x)' assert octave_code(zeta(x)) == 'zeta(x)' assert octave_code(zeta( x, y)) == '% Not supported in Octave:\n% zeta\nzeta(x, y)'
def test_limit(): assert gruntz(x, x) == oo assert gruntz(-x, x) == -oo assert gruntz(-x, x) == -oo assert gruntz((-x)**2, x) == oo assert gruntz(-x**2, x) == -oo assert gruntz((1 / x) * log(1 / x), x) == 0 # Gruntz: p15, 2.11 assert gruntz(1 / x, x) == 0 assert gruntz(exp(x), x) == oo assert gruntz(-exp(x), x) == -oo assert gruntz(exp(x) / x, x) == oo assert gruntz(1 / x - exp(-x), x) == 0 assert gruntz(x + 1 / x, x) == oo assert gruntz((1 / x)**(1 / x), x) == 1 # Gruntz: p15, 2.11 assert gruntz((exp(1 / x) - 1) * x, x) == 1 assert gruntz(1 + 1 / x, x) == 1 assert gruntz(-exp(1 / x), x) == -1 assert gruntz(x + exp(-x), x) == oo assert gruntz(x + exp(-x**2), x) == oo assert gruntz(x + exp(-exp(x)), x) == oo assert gruntz(13 + 1 / x - exp(-x), x) == 13 a = Symbol('a') assert gruntz(x - log(1 + exp(x)), x) == 0 assert gruntz(x - log(a + exp(x)), x) == 0 assert gruntz(exp(x) / (1 + exp(x)), x) == 1 assert gruntz(exp(x) / (a + exp(x)), x) == 1 assert gruntz((3**x + 5**x)**(1 / x), x) == 5 # issue sympy/sympy#3463 assert gruntz(Ei(x + exp(-x)) * exp(-x) * x, x) == 1 assert gruntz(1 / li(x), x) == 0 assert gruntz(1 / Li(x), x) == 0 # issue diofant/diofant#56 assert gruntz((log(E + 1 / x) - 1)**(1 - sqrt(E + 1 / x)), x) == oo # issue sympy/sympy#9471 assert gruntz((((27**(log(x, 3)))) / x**3), x) == 1 assert gruntz((((27**(log(x, 3) + 1))) / x**3), x) == 27 # issue sympy/sympy#9449 y = Symbol('y') assert gruntz(x * (abs(1 / x + y) - abs(y - 1 / x)) / 2, x) == sign(y) # issue sympy/sympy#8481 assert gruntz(m**x * exp(-m) / factorial(x), x) == 0 # issue sympy/sympy#4187 assert gruntz(exp(1 / x) * log(1 / x) - Ei(1 / x), x) == -EulerGamma assert gruntz(exp(x) * log(x) - Ei(x), x) == oo # issue sympy/sympy#10382 assert gruntz(fibonacci(x + 1) / fibonacci(x), x) == GoldenRatio assert gruntz(zeta(x), x) == 1 assert gruntz(zeta(m) * zeta(x), x) == zeta(m)
def test_limit(): assert gruntz(x, x) == oo assert gruntz(-x, x) == -oo assert gruntz(-x, x) == -oo assert gruntz((-x)**2, x) == oo assert gruntz(-x**2, x) == -oo assert gruntz((1/x)*log(1/x), x) == 0 # Gruntz: p15, 2.11 assert gruntz(1/x, x) == 0 assert gruntz(exp(x), x) == oo assert gruntz(-exp(x), x) == -oo assert gruntz(exp(x)/x, x) == oo assert gruntz(1/x - exp(-x), x) == 0 assert gruntz(x + 1/x, x) == oo assert gruntz((1/x)**(1/x), x) == 1 # Gruntz: p15, 2.11 assert gruntz((exp(1/x) - 1)*x, x) == 1 assert gruntz(1 + 1/x, x) == 1 assert gruntz(-exp(1/x), x) == -1 assert gruntz(x + exp(-x), x) == oo assert gruntz(x + exp(-x**2), x) == oo assert gruntz(x + exp(-exp(x)), x) == oo assert gruntz(13 + 1/x - exp(-x), x) == 13 a = Symbol('a') assert gruntz(x - log(1 + exp(x)), x) == 0 assert gruntz(x - log(a + exp(x)), x) == 0 assert gruntz(exp(x)/(1 + exp(x)), x) == 1 assert gruntz(exp(x)/(a + exp(x)), x) == 1 assert gruntz((3**x + 5**x)**(1/x), x) == 5 # issue sympy/sympy#3463 assert gruntz(Ei(x + exp(-x))*exp(-x)*x, x) == 1 assert gruntz(1/li(x), x) == 0 assert gruntz(1/Li(x), x) == 0 # issue diofant/diofant#56 assert gruntz((log(E + 1/x) - 1)**(1 - sqrt(E + 1/x)), x) == oo # issue sympy/sympy#9471 assert gruntz((((27**(log(x, 3))))/x**3), x) == 1 assert gruntz((((27**(log(x, 3) + 1)))/x**3), x) == 27 # issue sympy/sympy#9449 y = Symbol('y') assert gruntz(x*(abs(1/x + y) - abs(y - 1/x))/2, x) == sign(y) # issue sympy/sympy#8481 assert gruntz(m**x * exp(-m) / factorial(x), x) == 0 # issue sympy/sympy#4187 assert gruntz(exp(1/x)*log(1/x) - Ei(1/x), x) == -EulerGamma assert gruntz(exp(x)*log(x) - Ei(x), x) == oo # issue sympy/sympy#10382 assert gruntz(fibonacci(x + 1)/fibonacci(x), x) == GoldenRatio assert gruntz(zeta(x), x) == 1 assert gruntz(zeta(m)*zeta(x), x) == zeta(m)
def test_gruntz_eval_special_slow(): assert gruntz(gamma(x + 1)/sqrt(2*pi) - exp(-x)*(x**(x + Rational(1, 2)) + x**(x - Rational(1, 2))/12), x) == oo assert gruntz(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x) == 0 assert gruntz(exp(gamma(x - exp(-x))*exp(1/x)) - exp(gamma(x)), x) == oo assert gruntz( (Ei(x - exp(-exp(x))) - Ei(x))*exp(-x)*exp(exp(x))*x, x) == -1 assert gruntz( exp((log(2) + 1)*x) * (zeta(x + exp(-x)) - zeta(x)), x) == -log(2)
def test_gruntz_eval_special_slow(): assert limit(gamma(x + 1)/sqrt(2*pi) - exp(-x)*(x**(x + Rational(1, 2)) + x**(x - Rational(1, 2))/12), x, oo) == oo assert limit(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x, oo) == 0 assert limit(exp(gamma(x - exp(-x))*exp(1/x)) - exp(gamma(x)), x, oo) == oo assert limit((Ei(x - exp(-exp(x))) - Ei(x)) * exp(-x)*exp(exp(x))*x, x, oo) == -1 assert limit(exp((log(2) + 1)*x)*(zeta(x + exp(-x)) - zeta(x)), x, oo) == -log(2) # TODO 8.36 (bessel) assert limit(Max(x, exp(x))/log(Min(exp(-x), exp(-exp(x)))), x, oo) == -1
def test_derivatives(): assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x) assert zeta(x, a).diff(a) == -x*zeta(x + 1, a) assert lerchphi( z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a*lerchphi(z, s, a))/z assert lerchphi(z, s, a).diff(a) == -s*lerchphi(z, s + 1, a) assert polylog(s, z).diff(z) == polylog(s - 1, z)/z b = randcplx() c = randcplx() assert td(zeta(b, x), x) assert td(polylog(b, z), z) assert td(lerchphi(c, b, x), x) assert td(lerchphi(x, b, c), x)
def test_rewriting(): assert dirichlet_eta(x).rewrite(zeta) == (1 - 2**(1 - x))*zeta(x) assert zeta(x).rewrite(dirichlet_eta) == dirichlet_eta(x)/(1 - 2**(1 - x)) assert zeta(z, 2).rewrite(dirichlet_eta) == zeta(z, 2) assert zeta(z, 2).rewrite('tractable') == zeta(z, 2) assert tn(dirichlet_eta(x), dirichlet_eta(x).rewrite(zeta), x) assert tn(zeta(x), zeta(x).rewrite(dirichlet_eta), x) assert zeta(x, a).rewrite(lerchphi) == lerchphi(1, x, a) assert polylog(s, z).rewrite(lerchphi) == lerchphi(z, s, 1)*z assert lerchphi(1, x, a).rewrite(zeta) == zeta(x, a) assert z*lerchphi(z, s, 1).rewrite(polylog) == polylog(s, z) assert lerchphi(z, s, a).rewrite(zeta) == lerchphi(z, s, a)
def eval(cls, n, m=None): from diofant import zeta if m is S.One: return cls(n) if m is None: m = S.One if m.is_zero: return n if n is S.Infinity and m.is_Number: # TODO: Fix for symbolic values of m if m.is_negative: return S.NaN elif LessThan(m, S.One): return S.Infinity elif StrictGreaterThan(m, S.One): return zeta(m) else: return cls if n.is_Integer and n.is_nonnegative and m.is_Integer: if n == 0: return S.Zero if m not in cls._functions: @recurrence_memo([0]) def f(n, prev): return prev[-1] + S.One / n**m cls._functions[m] = f return cls._functions[m](int(n))
def test_evalf_fast_series(): # Euler transformed series for sqrt(1+x) assert NS(Sum(fac(2*n + 1)/fac(n)**2/2**(3*n + 1), (n, 0, oo)), 100, strict=False) == NS(sqrt(2), 100) # Some series for exp(1) estr = NS(E, 100) assert NS(Sum(1/fac(n), (n, 0, oo)), 100, strict=False) == estr assert NS(1/Sum((1 - 2*n)/fac(2*n), (n, 0, oo)), 100) == estr assert NS(Sum((2*n + 1)/fac(2*n), (n, 0, oo)), 100, strict=False) == estr assert NS(Sum((4*n + 3)/2**(2*n + 1)/fac(2*n + 1), (n, 0, oo))**2, 100, strict=False) == estr pistr = NS(pi, 100) # Ramanujan series for pi assert NS(9801/sqrt(8)/Sum(fac(4*n)*(1103 + 26390*n)/fac(n)**4/396**(4*n), (n, 0, oo)), 100, strict=False) == pistr assert NS(1/Sum( binomial(2*n, n)**3 * (42*n + 5)/2**(12*n + 4), (n, 0, oo)), 100) == pistr # Machin's formula for pi assert NS(16*Sum((-1)**n/(2*n + 1)/5**(2*n + 1), (n, 0, oo)) - 4*Sum((-1)**n/(2*n + 1)/239**(2*n + 1), (n, 0, oo)), 100) == pistr # Apery's constant astr = NS(zeta(3), 100) P = 126392*n**5 + 412708*n**4 + 531578*n**3 + 336367*n**2 + 104000 * \ n + 12463 assert NS(Sum((-1)**n * P / 24 * (fac(2*n + 1)*fac(2*n)*fac(n))**3 / fac(3*n + 2) / fac(4*n + 3)**3, (n, 0, oo)), 100, strict=False) == astr assert NS(Sum((-1)**n * (205*n**2 + 250*n + 77)/64 * fac(n)**10 / fac(2*n + 1)**5, (n, 0, oo)), 100, strict=False) == astr pytest.raises(ValueError, lambda: Sum(factorial(n), (n, 0, oo)).evalf())
def test_polylog_eval(): assert polylog(s, 0) == 0 assert polylog(s, 1) == zeta(s) assert polylog(s, -1) == -dirichlet_eta(s) assert polylog(s, exp_polar(I * pi)) == polylog(s, -1) assert polylog(s, 2 * exp_polar(2 * I * pi)) == polylog( s, 2 * exp_polar(2 * I * pi), evaluate=False)
def test_harmonic(): n = Symbol('n') assert harmonic(n, 0) == n assert harmonic(n).evalf() == harmonic(n) assert harmonic(n, 1) == harmonic(n) assert harmonic(1, n).evalf() == harmonic(1, n) assert harmonic(0, 1) == 0 assert harmonic(1, 1) == 1 assert harmonic(2, 1) == Rational(3, 2) assert harmonic(3, 1) == Rational(11, 6) assert harmonic(4, 1) == Rational(25, 12) assert harmonic(0, 2) == 0 assert harmonic(1, 2) == 1 assert harmonic(2, 2) == Rational(5, 4) assert harmonic(3, 2) == Rational(49, 36) assert harmonic(4, 2) == Rational(205, 144) assert harmonic(0, 3) == 0 assert harmonic(1, 3) == 1 assert harmonic(2, 3) == Rational(9, 8) assert harmonic(3, 3) == Rational(251, 216) assert harmonic(4, 3) == Rational(2035, 1728) assert harmonic(oo, -1) == nan assert harmonic(oo, 0) == oo assert harmonic(oo, Rational(1, 2)) == oo assert harmonic(oo, 1) == oo assert harmonic(oo, 2) == (pi**2) / 6 assert harmonic(oo, 3) == zeta(3) assert harmonic(oo, x) == harmonic(oo, x, evaluate=False)
def test_harmonic_limit(): n = Symbol('n') m = Symbol('m', positive=True) assert limit(harmonic(n, m + 1), n, oo) == zeta(m + 1) assert limit(harmonic(n, 2), n, oo) == pi**2 / 6 assert limit(harmonic(n, 3), n, oo) == -polygamma(2, 1) / 2
def test_evalf_fast_series_sympyissue_4021(): # Catalan's constant assert NS(Sum((-1)**(n - 1)*2**(8*n)*(40*n**2 - 24*n + 3)*fac(2*n)**3 * fac(n)**2/n**3/(2*n - 1)/fac(4*n)**2, (n, 1, oo))/64, 100, strict=False) == NS(Catalan, 100) astr = NS(zeta(3), 100) assert NS(5*Sum( (-1)**(n - 1)*fac(n)**2 / n**3 / fac(2*n), (n, 1, oo))/2, 100) == astr assert NS(Sum((-1)**(n - 1)*(56*n**2 - 32*n + 5) / (2*n - 1)**2 * fac(n - 1) ** 3 / fac(3*n), (n, 1, oo))/4, 100, strict=False) == astr
def test_lerchphi_expansion(): assert myexpand(lerchphi(1, s, a), zeta(s, a)) assert myexpand(lerchphi(z, s, 1), polylog(s, z)/z) # direct summation assert myexpand(lerchphi(z, -1, a), a/(1 - z) + z/(1 - z)**2) assert myexpand(lerchphi(z, -3, a), None) # polylog reduction assert myexpand(lerchphi(z, s, Rational(1, 2)), 2**(s - 1)*(polylog(s, sqrt(z))/sqrt(z) - polylog(s, polar_lift(-1)*sqrt(z))/sqrt(z))) assert myexpand(lerchphi(z, s, 2), -1/z + polylog(s, z)/z**2) assert myexpand(lerchphi(z, s, Rational(3, 2)), None) assert myexpand(lerchphi(z, s, Rational(7, 3)), None) assert myexpand(lerchphi(z, s, -Rational(1, 3)), None) assert myexpand(lerchphi(z, s, -Rational(5, 2)), None) # hurwitz zeta reduction assert myexpand(lerchphi(-1, s, a), 2**(-s)*zeta(s, a/2) - 2**(-s)*zeta(s, (a + 1)/2)) assert myexpand(lerchphi(I, s, a), None) assert myexpand(lerchphi(-I, s, a), None) assert myexpand(lerchphi(exp(2*I*pi/5), s, a), None)
def test_hypersum(): assert simplify(summation(x**n/fac(n), (n, 1, oo))) == -1 + exp(x) assert summation((-1)**n * x**(2*n) / fac(2*n), (n, 0, oo)) == cos(x) assert simplify(summation((-1)**n*x**(2*n + 1) / factorial(2*n + 1), (n, 3, oo))) == -x + sin(x) + x**3/6 - x**5/120 assert summation(1/(n + 2)**3, (n, 1, oo)) == -Rational(9, 8) + zeta(3) assert summation(1/n**4, (n, 1, oo)) == pi**4/90 s = summation(x**n*n, (n, -oo, 0)) assert s.is_Piecewise assert s.args[0].args[0] == -1/(x*(1 - 1/x)**2) assert s.args[0].args[1] == (abs(1/x) < 1) m = Symbol('n', integer=True, positive=True) assert summation(binomial(m, k), (k, 0, m)) == 2**m
def test_derivatives(): assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x) assert zeta(x, a).diff(a) == -x * zeta(x + 1, a) assert zeta(z).diff(z) == Derivative(zeta(z), z) assert lerchphi( z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a * lerchphi(z, s, a)) / z pytest.raises(ArgumentIndexError, lambda: lerchphi(z, s, a).fdiff(4)) assert lerchphi(z, s, a).diff(a) == -s * lerchphi(z, s + 1, a) assert polylog(s, z).diff(z) == polylog(s - 1, z) / z pytest.raises(ArgumentIndexError, lambda: polylog(s, z).fdiff(3)) b = randcplx() c = randcplx() assert td(zeta(b, x), x) assert td(polylog(b, z), z) assert td(lerchphi(c, b, x), x) assert td(lerchphi(x, b, c), x)
def test_derivatives(): assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x) assert zeta(x, a).diff(a) == -x*zeta(x + 1, a) assert zeta(z).diff(z) == Derivative(zeta(z), z) assert lerchphi( z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a*lerchphi(z, s, a))/z pytest.raises(ArgumentIndexError, lambda: lerchphi(z, s, a).fdiff(4)) assert lerchphi(z, s, a).diff(a) == -s*lerchphi(z, s + 1, a) assert polylog(s, z).diff(z) == polylog(s - 1, z)/z pytest.raises(ArgumentIndexError, lambda: polylog(s, z).fdiff(3)) b = randcplx() c = randcplx() assert td(zeta(b, x), x) assert td(polylog(b, z), z) assert td(lerchphi(c, b, x), x) assert td(lerchphi(x, b, c), x)
def test_euler_maclaurin(): # Exact polynomial sums with E-M def check_exact(f, a, b, m, n): A = Sum(f, (k, a, b)) s, e = A.euler_maclaurin(m, n) assert (e == 0) and (s.expand() == A.doit()) check_exact(k**4, a, b, 0, 2) check_exact(k**4 + 2 * k, a, b, 1, 2) check_exact(k**4 + k**2, a, b, 1, 5) check_exact(k**5, 2, 6, 1, 2) check_exact(k**5, 2, 6, 1, 3) assert Sum(x - 1, (x, 0, 2)).euler_maclaurin(m=30, n=30, eps=2**-15) == (0, 0) # Not exact assert Sum(k**6, (k, a, b)).euler_maclaurin(0, 2)[1] != 0 # Numerical test for m, n in [(2, 4), (2, 20), (10, 20), (18, 20)]: A = Sum(1 / k**3, (k, 1, oo)) s, e = A.euler_maclaurin(m, n) assert abs((s - zeta(3)).evalf()) < e.evalf()
def test_euler_maclaurin(): pytest.raises(ValueError, lambda: Sum(x - y, (x, 0, 2), (y, 0, 2)).euler_maclaurin()) # Exact polynomial sums with E-M def check_exact(f, a, b, m, n): A = Sum(f, (k, a, b)) s, e = A.euler_maclaurin(m, n) assert (e == 0) and (s.expand() == A.doit()) check_exact(k**4, a, b, 0, 2) check_exact(k**4 + 2*k, a, b, 1, 2) check_exact(k**4 + k**2, a, b, 1, 5) check_exact(k**5, 2, 6, 1, 2) check_exact(k**5, 2, 6, 1, 3) assert Sum(x-1, (x, 0, 2)).euler_maclaurin(m=30, n=30, eps=2**-15) == (0, 0) # Not exact assert Sum(k**6, (k, a, b)).euler_maclaurin(0, 2)[1] != 0 # Numerical test for m, n in [(2, 4), (2, 20), (10, 20), (18, 20)]: A = Sum(1/k**3, (k, 1, oo)) s, e = A.euler_maclaurin(m, n) assert abs((s - zeta(3)).evalf()) < e.evalf()
def test_polygamma(): from diofant import I assert polygamma(n, nan) == nan assert polygamma(0, oo) == oo assert polygamma(0, -oo) == oo assert polygamma(0, I * oo) == oo assert polygamma(0, -I * oo) == oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(0, -9) == zoo assert polygamma(0, -9) == zoo assert polygamma(0, -1) == zoo assert polygamma(0, 0) == zoo assert polygamma(0, 1) == -EulerGamma assert polygamma(0, 7) == Rational(49, 20) - EulerGamma assert polygamma(1, 1) == pi**2 / 6 assert polygamma(1, 2) == pi**2 / 6 - 1 assert polygamma(1, 3) == pi**2 / 6 - Rational(5, 4) assert polygamma(3, 1) == pi**4 / 15 assert polygamma(3, 5) == 6 * (Rational(-22369, 20736) + pi**4 / 90) assert polygamma(5, 1) == 8 * pi**6 / 63 assert trigamma(x) == polygamma(1, x) def t(m, n): x = Integer(m) / n r = polygamma(0, x) if r.has(polygamma): return False return abs(polygamma(0, x.n()).n() - r.n()).n() < 1e-10 assert t(1, 2) assert t(3, 2) assert t(-1, 2) assert t(1, 4) assert t(-3, 4) assert t(1, 3) assert t(4, 3) assert t(3, 4) assert t(2, 3) assert polygamma(0, x).rewrite(zeta) == polygamma(0, x) assert polygamma(1, x).rewrite(zeta) == zeta(2, x) assert polygamma(2, x).rewrite(zeta) == -2 * zeta(3, x) assert polygamma(3, 7 * x).diff(x) == 7 * polygamma(4, 7 * x) assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma assert polygamma( 2, x).rewrite(harmonic) == 2 * harmonic(x - 1, 3) - 2 * zeta(3) ni = Symbol("n", integer=True) assert polygamma( ni, x).rewrite(harmonic) == (-1)**(ni + 1) * (-harmonic(x - 1, ni + 1) + zeta(ni + 1)) * factorial(ni) # Polygamma of non-negative integer order is unbranched: from diofant import exp_polar k = Symbol('n', integer=True, nonnegative=True) assert polygamma(k, exp_polar(2 * I * pi) * x) == polygamma(k, x) # but negative integers are branched! k = Symbol('n', integer=True) assert polygamma(k, exp_polar(2 * I * pi) * x).args == (k, exp_polar(2 * I * pi) * x) # Polygamma of order -1 is loggamma: assert polygamma(-1, x) == loggamma(x) # But smaller orders are iterated integrals and don't have a special name assert polygamma(-2, x).func is polygamma # Test a bug assert polygamma(0, -x).expand(func=True) == polygamma(0, -x) assert polygamma(1, x).as_leading_term(x) == polygamma(1, x)
def test_zeta(): assert str(zeta(3)) == "zeta(3)"
def test_Function(): assert mathematica_code(f(x, y, z)) == 'f[x, y, z]' assert mathematica_code(sin(x)**cos(x)) == 'Sin[x]^Cos[x]' assert mathematica_code(sign(x)) == 'Sign[x]' assert mathematica_code(atanh(x), user_functions={'atanh': 'ArcTanh'}) == 'ArcTanh[x]' assert (mathematica_code(meijerg( ((1, 1), (3, 4)), ((1, ), ()), x)) == 'MeijerG[{{1, 1}, {3, 4}}, {{1}, {}}, x]') assert (mathematica_code(hyper( (1, 2, 3), (3, 4), x)) == 'HypergeometricPFQ[{1, 2, 3}, {3, 4}, x]') assert mathematica_code(Min(x, y)) == 'Min[x, y]' assert mathematica_code(Max(x, y)) == 'Max[x, y]' assert mathematica_code(Max(x, 2)) == 'Max[2, x]' # issue sympy/sympy#15344 assert mathematica_code(binomial(x, y)) == 'Binomial[x, y]' assert mathematica_code(log(x)) == 'Log[x]' assert mathematica_code(tan(x)) == 'Tan[x]' assert mathematica_code(cot(x)) == 'Cot[x]' assert mathematica_code(asin(x)) == 'ArcSin[x]' assert mathematica_code(acos(x)) == 'ArcCos[x]' assert mathematica_code(atan(x)) == 'ArcTan[x]' assert mathematica_code(acot(x)) == 'ArcCot[x]' assert mathematica_code(sinh(x)) == 'Sinh[x]' assert mathematica_code(cosh(x)) == 'Cosh[x]' assert mathematica_code(tanh(x)) == 'Tanh[x]' assert mathematica_code(coth(x)) == 'Coth[x]' assert mathematica_code(asinh(x)) == 'ArcSinh[x]' assert mathematica_code(acosh(x)) == 'ArcCosh[x]' assert mathematica_code(atanh(x)) == 'ArcTanh[x]' assert mathematica_code(acoth(x)) == 'ArcCoth[x]' assert mathematica_code(sech(x)) == 'Sech[x]' assert mathematica_code(csch(x)) == 'Csch[x]' assert mathematica_code(erf(x)) == 'Erf[x]' assert mathematica_code(erfi(x)) == 'Erfi[x]' assert mathematica_code(erfc(x)) == 'Erfc[x]' assert mathematica_code(conjugate(x)) == 'Conjugate[x]' assert mathematica_code(re(x)) == 'Re[x]' assert mathematica_code(im(x)) == 'Im[x]' assert mathematica_code(polygamma(x, y)) == 'PolyGamma[x, y]' assert mathematica_code(factorial(x)) == 'Factorial[x]' assert mathematica_code(factorial2(x)) == 'Factorial2[x]' assert mathematica_code(rf(x, y)) == 'Pochhammer[x, y]' assert mathematica_code(gamma(x)) == 'Gamma[x]' assert mathematica_code(zeta(x)) == 'Zeta[x]' assert mathematica_code(Heaviside(x)) == 'UnitStep[x]' assert mathematica_code(fibonacci(x)) == 'Fibonacci[x]' assert mathematica_code(polylog(x, y)) == 'PolyLog[x, y]' assert mathematica_code(loggamma(x)) == 'LogGamma[x]' assert mathematica_code(uppergamma(x, y)) == 'Gamma[x, y]' class MyFunc1(Function): @classmethod def eval(cls, x): pass class MyFunc2(Function): @classmethod def eval(cls, x, y): pass pytest.raises( ValueError, lambda: mathematica_code(MyFunc1(x), user_functions={'MyFunc1': ['Myfunc1']})) assert mathematica_code(MyFunc1(x), user_functions={'MyFunc1': 'Myfunc1'}) == 'Myfunc1[x]' assert mathematica_code( MyFunc2(x, y), user_functions={'MyFunc2': [(lambda *x: False, 'Myfunc2')]}) == 'MyFunc2[x, y]'
def test_zeta_series(): assert zeta(x, a).series(a, 0, 2) == \ zeta(x, 0) - x*a*zeta(x + 1, 0) + O(a**2)
def test_zeta_eval(): assert zeta(nan) == nan assert zeta(x, nan) == nan assert zeta(oo) == 1 assert zeta(0) == Rational(-1, 2) assert zeta(0, x) == Rational(1, 2) - x assert zeta(0, b) == Rational(1, 2) - b assert zeta(1) == zoo assert zeta(1, 2) == zoo assert zeta(1, -7) == zoo assert zeta(1, x) == zoo assert zeta(2, 1) == pi**2 / 6 assert zeta(2) == pi**2 / 6 assert zeta(4) == pi**4 / 90 assert zeta(6) == pi**6 / 945 assert zeta(2, 2) == pi**2 / 6 - 1 assert zeta(4, 3) == pi**4 / 90 - Rational(17, 16) assert zeta(6, 4) == pi**6 / 945 - Rational(47449, 46656) assert zeta(2, -2) == pi**2 / 6 + Rational(5, 4) assert zeta(4, -3) == pi**4 / 90 + Rational(1393, 1296) assert zeta(6, -4) == pi**6 / 945 + Rational(3037465, 2985984) assert zeta(-1) == -Rational(1, 12) assert zeta(-2) == 0 assert zeta(-3) == Rational(1, 120) assert zeta(-4) == 0 assert zeta(-5) == -Rational(1, 252) assert zeta(-1, 3) == -Rational(37, 12) assert zeta(-1, 7) == -Rational(253, 12) assert zeta(-1, -4) == Rational(119, 12) assert zeta(-1, -9) == Rational(539, 12) assert zeta(-4, 3) == -17 assert zeta(-4, -8) == 8772 assert zeta(0, 1) == -Rational(1, 2) assert zeta(0, -1) == Rational(3, 2) assert zeta(0, 2) == -Rational(3, 2) assert zeta(0, -2) == Rational(5, 2) assert zeta(3).evalf(20).epsilon_eq(Float('1.2020569031595942854', 20), 1e-19) assert zeta(Rational(1, 2)) == zeta(Rational(1, 2), evaluate=False)
def test_zeta_eval(): assert zeta(nan) == nan assert zeta(x, nan) == nan assert zeta(oo) == 1 assert zeta(0) == Rational(-1, 2) assert zeta(0, x) == Rational(1, 2) - x assert zeta(0, b) == Rational(1, 2) - b assert zeta(1) == zoo assert zeta(1, 2) == zoo assert zeta(1, -7) == zoo assert zeta(1, x) == zoo assert zeta(2, 1) == pi**2/6 assert zeta(2) == pi**2/6 assert zeta(4) == pi**4/90 assert zeta(6) == pi**6/945 assert zeta(2, 2) == pi**2/6 - 1 assert zeta(4, 3) == pi**4/90 - Rational(17, 16) assert zeta(6, 4) == pi**6/945 - Rational(47449, 46656) assert zeta(2, -2) == pi**2/6 + Rational(5, 4) assert zeta(4, -3) == pi**4/90 + Rational(1393, 1296) assert zeta(6, -4) == pi**6/945 + Rational(3037465, 2985984) assert zeta(-1) == -Rational(1, 12) assert zeta(-2) == 0 assert zeta(-3) == Rational(1, 120) assert zeta(-4) == 0 assert zeta(-5) == -Rational(1, 252) assert zeta(-1, 3) == -Rational(37, 12) assert zeta(-1, 7) == -Rational(253, 12) assert zeta(-1, -4) == Rational(119, 12) assert zeta(-1, -9) == Rational(539, 12) assert zeta(-4, 3) == -17 assert zeta(-4, -8) == 8772 assert zeta(0, 1) == -Rational(1, 2) assert zeta(0, -1) == Rational(3, 2) assert zeta(0, 2) == -Rational(3, 2) assert zeta(0, -2) == Rational(5, 2) assert zeta( 3).evalf(20).epsilon_eq(Float("1.2020569031595942854", 20), 1e-19) assert zeta(Rational(1, 2)) == zeta(Rational(1, 2), evaluate=False)
def test_zeta(): assert str(zeta(3)) == 'zeta(3)'
def test_polylog_eval(): assert polylog(s, 0) == 0 assert polylog(s, 1) == zeta(s) assert polylog(s, -1) == -dirichlet_eta(s) assert polylog(s, exp_polar(I*pi)) == polylog(s, -1) assert polylog(s, 2*exp_polar(2*I*pi)) == polylog(s, 2*exp_polar(2*I*pi), evaluate=False)
def test_polygamma(): assert polygamma(n, nan) == nan assert polygamma(0, oo) == oo assert polygamma(0, -oo) == oo assert polygamma(0, I*oo) == oo assert polygamma(0, -I*oo) == oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(n, oo) == polygamma(n, oo, evaluate=False) assert polygamma(0, -9) == zoo assert polygamma(0, -9) == zoo assert polygamma(0, -1) == zoo assert polygamma(0, 0) == zoo assert polygamma(0, 1) == -EulerGamma assert polygamma(0, 7) == Rational(49, 20) - EulerGamma assert polygamma(0, -Rational(3, 11)) == polygamma(0, -Rational(3, 11), evaluate=False) assert polygamma(1, 1) == pi**2/6 assert polygamma(1, 2) == pi**2/6 - 1 assert polygamma(1, 3) == pi**2/6 - Rational(5, 4) assert polygamma(3, 1) == pi**4 / 15 assert polygamma(3, 5) == 6*(Rational(-22369, 20736) + pi**4/90) assert polygamma(5, 1) == 8 * pi**6 / 63 assert trigamma(x) == polygamma(1, x) def t(m, n): x = Integer(m)/n r = polygamma(0, x) if r.has(polygamma): return False return abs(polygamma(0, x.evalf()).evalf(strict=False) - r.evalf()).evalf(strict=False) < 1e-10 assert t(1, 2) assert t(3, 2) assert t(-1, 2) assert t(1, 4) assert t(-3, 4) assert t(1, 3) assert t(4, 3) assert t(3, 4) assert t(2, 3) assert polygamma(0, x).rewrite(zeta) == polygamma(0, x) assert polygamma(1, x).rewrite(zeta) == zeta(2, x) assert polygamma(2, x).rewrite(zeta) == -2*zeta(3, x) assert polygamma(3, 7*x).diff(x) == 7*polygamma(4, 7*x) pytest.raises(ArgumentIndexError, lambda: polygamma(3, 7*x).fdiff(3)) assert polygamma(0, x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma assert polygamma(2, x).rewrite(harmonic) == 2*harmonic(x - 1, 3) - 2*zeta(3) ni = Symbol('n', integer=True) assert polygamma(ni, x).rewrite(harmonic) == (-1)**(ni + 1)*(-harmonic(x - 1, ni + 1) + zeta(ni + 1))*factorial(ni) assert polygamma(x, y).rewrite(harmonic) == polygamma(x, y) # Polygamma of non-negative integer order is unbranched: k = Symbol('n', integer=True, nonnegative=True) assert polygamma(k, exp_polar(2*I*pi)*x) == polygamma(k, x) # but negative integers are branched! k = Symbol('n', integer=True) assert polygamma(k, exp_polar(2*I*pi)*x).args == (k, exp_polar(2*I*pi)*x) # Polygamma of order -1 is loggamma: assert polygamma(-1, x) == loggamma(x) # But smaller orders are iterated integrals and don't have a special name assert isinstance(polygamma(-2, x), polygamma) # Test a bug assert polygamma(0, -x).expand(func=True) == polygamma(0, -x) assert polygamma(1, x).as_leading_term(x) == polygamma(1, x)