def test_limit_seq_fail(): # improve Summation algorithm or add ad-hoc criteria e = (harmonic(n)**3 * Sum(1 / harmonic(k), (k, 1, n)) / (n * Sum(harmonic(k) / k, (k, 1, n)))) assert limit_seq(e, n) == 2 # No unique dominant term e = (Sum(2**k * binomial(2 * k, k) / k**2, (k, 1, n)) / (Sum(2**k / k * 2, (k, 1, n)) * Sum(binomial(2 * k, k), (k, 1, n)))) assert limit_seq(e, n) == S(3) / 7 # Simplifications of summations needs to be improved. e = n**3 * Sum(2**k / k**2, (k, 1, n))**2 / (2**n * Sum(2**k / k, (k, 1, n))) assert limit_seq(e, n) == 2 e = (harmonic(n) * Sum(2**k / k, (k, 1, n)) / (n * Sum(2**k * harmonic(k) / k**2, (k, 1, n)))) assert limit_seq(e, n) == 1 e = (Sum(2**k * factorial(k) / k**2, (k, 1, 2 * n)) / (Sum(4**k / k**2, (k, 1, n)) * Sum(factorial(k), (k, 1, 2 * n)))) assert limit_seq(e, n) == S(3) / 16
def test_harmonic_rewrite_sum_fail(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1/_k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def test_harmonic_rewrite_sum(): n = Symbol("n") m = Symbol("m") _k = Dummy("k") assert replace_dummy(harmonic(n).rewrite(Sum), _k) == Sum(1/_k, (_k, 1, n)) assert replace_dummy(harmonic(n, m).rewrite(Sum), _k) == Sum(_k**(-m), (_k, 1, n))
def test_ideal_soliton(): raises(ValueError, lambda : IdealSoliton('sol', -12)) raises(ValueError, lambda : IdealSoliton('sol', 13.2)) raises(ValueError, lambda : IdealSoliton('sol', 0)) f = Function('f') raises(ValueError, lambda : density(IdealSoliton('sol', 10)).pmf(f)) k = Symbol('k', integer=True, positive=True) x = Symbol('x', integer=True, positive=True) t = Symbol('t') sol = IdealSoliton('sol', k) assert density(sol).low == S.One assert density(sol).high == k assert density(sol).dict == Density(density(sol)) assert density(sol).pmf(x) == Piecewise((1/k, Eq(x, 1)), (1/(x*(x - 1)), k >= x), (0, True)) k_vals = [5, 20, 50, 100, 1000] for i in k_vals: assert E(sol.subs(k, i)) == harmonic(i) == moment(sol.subs(k, i), 1) assert variance(sol.subs(k, i)) == (i - 1) + harmonic(i) - harmonic(i)**2 == cmoment(sol.subs(k, i),2) assert skewness(sol.subs(k, i)) == smoment(sol.subs(k, i), 3) assert kurtosis(sol.subs(k, i)) == smoment(sol.subs(k, i), 4) assert exp(I*t)/10 + Sum(exp(I*t*x)/(x*x - x), (x, 2, k)).subs(k, 10).doit() == characteristic_function(sol.subs(k, 10))(t) assert exp(t)/10 + Sum(exp(t*x)/(x*x - x), (x, 2, k)).subs(k, 10).doit() == moment_generating_function(sol.subs(k, 10))(t)
def test_harmonic(): 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(3, 1) == harmonic(3) assert harmonic(3, 5) == 1 + Rational(1, 2**5) + Rational(1, 3**5) assert harmonic(10, 0) == 10 assert harmonic(oo, 1) == zoo assert harmonic(oo, 2) == (pi**2)/6
def test_harmonic(): 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(3, 1) == harmonic(3) assert harmonic(3, 5) == 1 + Rational(1, 2**5) + Rational(1, 3**5) assert harmonic(10, 0) == 10 assert harmonic(oo, 1) == zoo assert harmonic(oo, 2) == (pi**2) / 6
def test_digamma(): from sympy import I assert digamma(nan) == nan assert digamma(oo) == oo assert digamma(-oo) == oo assert digamma(I * oo) == oo assert digamma(-I * oo) == oo assert digamma(-9) == zoo assert digamma(-9) == zoo assert digamma(-1) == zoo assert digamma(0) == zoo assert digamma(1) == -EulerGamma assert digamma(7) == Rational(49, 20) - EulerGamma def t(m, n): x = S(m) / n r = digamma(x) if r.has(digamma): return False return abs(digamma(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 t(123, 5) assert digamma(x).rewrite(zeta) == polygamma(0, x) assert digamma(x).rewrite(harmonic) == harmonic(x - 1) - EulerGamma assert digamma(I).is_real is None assert digamma(x, evaluate=False).fdiff() == polygamma(1, x) assert digamma(x, evaluate=False).is_real is None assert digamma(x, evaluate=False).is_positive is None assert digamma(x, evaluate=False).is_negative is None assert digamma(x, evaluate=False).rewrite(polygamma) == polygamma(0, x)
def test_difference_delta__Sum(): e = Sum(1 / k, (k, 1, n)) assert dd(e, n) == 1 / (n + 1) assert dd(e, n, 5) == Add(*[1 / (i + n + 1) for i in range(5)]) e = Sum(1 / k, (k, 1, 3 * n)) assert dd(e, n) == Add(*[1 / (i + 3 * n + 1) for i in range(3)]) e = n * Sum(1 / k, (k, 1, n)) assert dd(e, n) == 1 + Sum(1 / k, (k, 1, n)) e = Sum(1 / k, (k, 1, n), (m, 1, n)) assert dd(e, n) == harmonic(n)
def test_difference_delta__Sum(): e = Sum(1/k, (k, 1, n)) assert dd(e, n) == 1/(n + 1) assert dd(e, n, 5) == Sum(1/k, (k, n + 1, n + 5)) e = Sum(1/k, (k, 1, 3*n)) assert dd(e, n) == Sum(1/k, (k, 3*n + 1, 3*n + 3)) e = n * Sum(1/k, (k, 1, n)) assert dd(e, n) == 1 + Sum(1/k, (k, 1, n)) e = Sum(1/k, (k, 1, n), (m, 1, n)) assert dd(e, n) == harmonic(n)
def test_limit_seq(): e = binomial(2 * n, n) / Sum(binomial(2 * k, k), (k, 1, n)) assert limit_seq(e) == S(3) / 4 assert limit_seq(e, m) == e e = (5 * n**3 + 3 * n**2 + 4) / (3 * n**3 + 4 * n - 5) assert limit_seq(e, n) == S(5) / 3 e = (harmonic(n) * Sum(harmonic(k), (k, 1, n))) / (n * harmonic(2 * n)**2) assert limit_seq(e, n) == 1 e = Sum(k**2 * Sum(2**m / m, (m, 1, k)), (k, 1, n)) / (2**n * n) assert limit_seq(e, n) == 4 e = (Sum(binomial(3 * k, k) * binomial(5 * k, k), (k, 1, n)) / (binomial(3 * n, n) * binomial(5 * n, n))) assert limit_seq(e, n) == S(84375) / 83351 e = Sum(harmonic(k)**2 / k, (k, 1, 2 * n)) / harmonic(n)**3 assert limit_seq(e, n) == S(1) / 3 raises(ValueError, lambda: limit_seq(e * m))
def test_difference_delta__Sum(): e = Sum(1/k, (k, 1, n)) assert dd(e, n) == 1/(n + 1) assert dd(e, n, 5) == Add(*[1/(i + n + 1) for i in range(5)]) e = Sum(1/k, (k, 1, 3*n)) assert dd(e, n) == Add(*[1/(i + 3*n + 1) for i in range(3)]) e = n * Sum(1/k, (k, 1, n)) assert dd(e, n) == 1 + Sum(1/k, (k, 1, n)) e = Sum(1/k, (k, 1, n), (m, 1, n)) assert dd(e, n) == harmonic(n)
def test_limit_seq(): e = binomial(2*n, n) / Sum(binomial(2*k, k), (k, 1, n)) assert limit_seq(e) == S(3) / 4 assert limit_seq(e, m) == e e = (5*n**3 + 3*n**2 + 4) / (3*n**3 + 4*n - 5) assert limit_seq(e, n) == S(5) / 3 e = (harmonic(n) * Sum(harmonic(k), (k, 1, n))) / (n * harmonic(2*n)**2) assert limit_seq(e, n) == 1 e = Sum(k**2 * Sum(2**m/m, (m, 1, k)), (k, 1, n)) / (2**n*n) assert limit_seq(e, n) == 4 e = (Sum(binomial(3*k, k) * binomial(5*k, k), (k, 1, n)) / (binomial(3*n, n) * binomial(5*n, n))) assert limit_seq(e, n) == S(84375) / 83351 e = Sum(harmonic(k)**2/k, (k, 1, 2*n)) / harmonic(n)**3 assert limit_seq(e, n) == S(1) / 3 raises(ValueError, lambda: limit_seq(e * m))
def test_limit_seq_fail(): # improve Summation algorithm or add ad-hoc criteria e = (harmonic(n)**3 * Sum(1/harmonic(k), (k, 1, n)) / (n * Sum(harmonic(k)/k, (k, 1, n)))) assert limit_seq(e, n) == 2 # No unique dominant term e = (Sum(2**k * binomial(2*k, k) / k**2, (k, 1, n)) / (Sum(2**k/k*2, (k, 1, n)) * Sum(binomial(2*k, k), (k, 1, n)))) assert limit_seq(e, n) == S(3) / 7 # Simplifications of summations needs to be improved. e = n**3*Sum(2**k/k**2, (k, 1, n))**2 / (2**n * Sum(2**k/k, (k, 1, n))) assert limit_seq(e, n) == 2 e = (harmonic(n) * Sum(2**k/k, (k, 1, n)) / (n * Sum(2**k*harmonic(k)/k**2, (k, 1, n)))) assert limit_seq(e, n) == 1 e = (Sum(2**k*factorial(k) / k**2, (k, 1, 2*n)) / (Sum(4**k/k**2, (k, 1, n)) * Sum(factorial(k), (k, 1, 2*n)))) assert limit_seq(e, n) == S(3) / 16
def test_polygamma(): from sympy 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 def t(m, n): x = S(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 sympy 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)
def test_polygamma(): from sympy 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 def t(m, n): x = S(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 sympy 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)
def test_harmonic(): n = Symbol("n") assert harmonic(n, 0) == n assert harmonic(n, 1) == harmonic(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) == S.NaN assert harmonic(oo, 0) == oo assert harmonic(oo, S.Half) == oo assert harmonic(oo, 1) == oo assert harmonic(oo, 2) == (pi**2)/6 assert harmonic(oo, 3) == zeta(3)
def test_harmonic_rational(): ne = S(6) no = S(5) pe = S(8) po = S(9) qe = S(10) qo = S(13) Heee = harmonic(ne + pe/qe) Aeee = (-log(10) + 2*(-1/S(4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 - 1/S(4))*log(sqrt(sqrt(5)/8 + 5/S(8))) + pi*(1/S(4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + 5/S(8))) + 13944145/S(4720968)) Heeo = harmonic(ne + pe/qo) Aeeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + 2422020029/S(702257080)) Heoe = harmonic(ne + po/qe) Aeoe = (-log(20) + 2*(1/S(4) + sqrt(5)/4)*log(-1/S(4) + sqrt(5)/4) + 2*(-1/S(4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 - 1/S(4))*log(sqrt(sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 + 1/S(4))*log(1/S(4) + sqrt(5)/4) + 11818877030/S(4286604231) - pi*sqrt(sqrt(5)/8 + 5/S(8))/(-sqrt(5)/2 + 1/S(2)) ) Heoo = harmonic(ne + po/qo) Aeoo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + 11669332571/S(3628714320)) Hoee = harmonic(no + pe/qe) Aoee = (-log(10) + 2*(-1/S(4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 - 1/S(4))*log(sqrt(sqrt(5)/8 + 5/S(8))) + pi*(1/S(4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + 5/S(8))) + 779405/S(277704)) Hoeo = harmonic(no + pe/qo) Aoeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13) + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13) - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13) + 53857323/S(16331560)) Hooe = harmonic(no + po/qe) Aooe = (-log(20) + 2*(1/S(4) + sqrt(5)/4)*log(-1/S(4) + sqrt(5)/4) + 2*(-1/S(4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 - 1/S(4))*log(sqrt(sqrt(5)/8 + 5/S(8))) + 2*(-sqrt(5)/4 + 1/S(4))*log(1/S(4) + sqrt(5)/4) + 486853480/S(186374097) - pi*sqrt(sqrt(5)/8 + 5/S(8))/(2*(-sqrt(5)/4 + 1/S(4)))) Hooo = harmonic(no + po/qo) Aooo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13) + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13) - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2 - 2*log(sin(2*pi/13))*cos(3*pi/13) + 383693479/S(125128080)) H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo] A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo] for h, a in zip(H, A): e = expand_func(h).doit() assert cancel(e/a) == 1 assert h.n() == a.n()
def test_polygamma(): from sympy import I assert polygamma(n, nan) is nan assert polygamma(0, oo) is oo assert polygamma(0, -oo) is oo assert polygamma(0, I * oo) is oo assert polygamma(0, -I * oo) is oo assert polygamma(1, oo) == 0 assert polygamma(5, oo) == 0 assert polygamma(0, -9) is zoo assert polygamma(0, -9) is zoo assert polygamma(0, -1) is zoo assert polygamma(0, 0) is 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 polygamma(1, S.Half) == pi**2 / 2 assert polygamma(2, S.Half) == -14 * zeta(3) assert polygamma(11, S.Half) == 176896 * pi**12 def t(m, n): x = S(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 t(123, 5) 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(I, 2).rewrite(zeta) == polygamma(I, 2) n1 = Symbol('n1') n2 = Symbol('n2', real=True) n3 = Symbol('n3', integer=True) n4 = Symbol('n4', positive=True) n5 = Symbol('n5', positive=True, integer=True) assert polygamma(n1, x).rewrite(zeta) == polygamma(n1, x) assert polygamma(n2, x).rewrite(zeta) == polygamma(n2, x) assert polygamma(n3, x).rewrite(zeta) == polygamma(n3, x) assert polygamma(n4, x).rewrite(zeta) == polygamma(n4, x) assert polygamma( n5, x).rewrite(zeta) == (-1)**(n5 + 1) * factorial(n5) * zeta(n5 + 1, 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 sympy 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(2, 2.5).is_positive == False assert polygamma(2, -2.5).is_positive == False assert polygamma(3, 2.5).is_positive == True assert polygamma(3, -2.5).is_positive is True assert polygamma(-2, -2.5).is_positive is None assert polygamma(-3, -2.5).is_positive is None assert polygamma(2, 2.5).is_negative == True assert polygamma(3, 2.5).is_negative == False assert polygamma(3, -2.5).is_negative == False assert polygamma(2, -2.5).is_negative is True assert polygamma(-2, -2.5).is_negative is None assert polygamma(-3, -2.5).is_negative is None assert polygamma(I, 2).is_positive is None assert polygamma(I, 3).is_negative is None # issue 17350 assert polygamma(pi, 3).evalf() == polygamma(pi, 3) assert (I*polygamma(I, pi)).as_real_imag() == \ (-im(polygamma(I, pi)), re(polygamma(I, pi))) assert (tanh(polygamma(I, 1))).rewrite(exp) == \ (exp(polygamma(I, 1)) - exp(-polygamma(I, 1)))/(exp(polygamma(I, 1)) + exp(-polygamma(I, 1))) assert (I / polygamma(I, 4)).rewrite(exp) == \ I*sqrt(re(polygamma(I, 4))**2 + im(polygamma(I, 4))**2)\ /((re(polygamma(I, 4)) + I*im(polygamma(I, 4)))*Abs(polygamma(I, 4))) assert unchanged(polygamma, 2.3, 1.0) # issue 12569 assert unchanged(im, polygamma(0, I)) assert polygamma(Symbol('a', positive=True), Symbol( 'b', positive=True)).is_real is True assert polygamma(0, I).is_real is None
def test_harmonic_sums(): assert summation(1 / k, (k, 0, n)) == Sum(1 / k, (k, 0, n)) assert summation(1 / k, (k, 1, n)) == harmonic(n) assert summation(n / k, (k, 1, n)) == n * harmonic(n) assert summation(1 / k, (k, 5, n)) == harmonic(n) - harmonic(4)
def test_harmonic_rewrite_polygamma(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n,m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma)
def test_harmonic_limit_fail(): n = Symbol("n") m = Symbol("m") # For m > 1: assert limit(harmonic(n, m), n, oo) == zeta(m)
def test_harmonic_rewrite_sum(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic( n, 3).rewrite(polygamma) == polygamma(2, n + 1) / 2 - polygamma(2, 1) / 2 assert harmonic(n, m).rewrite(polygamma) == (-1)**m * ( polygamma(m - 1, 1) - polygamma(m - 1, n + 1)) / factorial(m - 1) assert expand_func( harmonic(n + 4) ) == harmonic(n) + 1 / (n + 4) + 1 / (n + 3) + 1 / (n + 2) + 1 / (n + 1) assert expand_func(harmonic( n - 4)) == harmonic(n) - 1 / (n - 1) - 1 / (n - 2) - 1 / (n - 3) - 1 / n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma) _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1 / _k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))
def test_harmonic_evalf(): assert str(harmonic(1.5).evalf(n=10)) == '1.280372306' assert str(harmonic(1.5, 2).evalf(n=10)) == '1.154576311' # issue 7443
def test_harmonic_sums(): assert summation(1/k, (k, 0, n)) == Sum(1/k, (k, 0, n)) assert summation(1/k, (k, 1, n)) == harmonic(n) assert summation(n/k, (k, 1, n)) == n*harmonic(n) assert summation(1/k, (k, 5, n)) == harmonic(n) - harmonic(4)
def test_harmonic_rewrite_sum_fail(): n = Symbol("n") m = Symbol("m") assert harmonic(n).rewrite(digamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(trigamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n).rewrite(polygamma) == polygamma(0, n + 1) + EulerGamma assert harmonic(n,3).rewrite(polygamma) == polygamma(2, n + 1)/2 - polygamma(2, 1)/2 assert harmonic(n,m).rewrite(polygamma) == (-1)**m*(polygamma(m - 1, 1) - polygamma(m - 1, n + 1))/factorial(m - 1) assert expand_func(harmonic(n+4)) == harmonic(n) + 1/(n + 4) + 1/(n + 3) + 1/(n + 2) + 1/(n + 1) assert expand_func(harmonic(n-4)) == harmonic(n) - 1/(n - 1) - 1/(n - 2) - 1/(n - 3) - 1/n assert harmonic(n, m).rewrite("tractable") == harmonic(n, m).rewrite(polygamma) _k = Dummy("k") assert harmonic(n).rewrite(Sum) == Sum(1/_k, (_k, 1, n)) assert harmonic(n, m).rewrite(Sum) == Sum(_k**(-m), (_k, 1, n))