def test_gruntz_eval_special_slow(): sskip() assert gruntz(gamma(x+1)/sqrt(2*pi) - exp(-x)*(x**(x+S(1)/2) + x**(x-S(1)/2)/12), x, oo) == oo assert gruntz(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x, oo) == 0 # XXX This sometimes fails!!! assert gruntz(exp(gamma(x-exp(-x))*exp(1/x)) - exp(gamma(x)), x, oo) == oo
def test_beta(): x, y = Symbol('x'), Symbol('y') assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x)*gamma(y)/gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta(x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y)*(digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y)*(digamma(y) - digamma(x + y))
def test_beta(): x, y = Symbol('x'), Symbol('y') assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x) * gamma(y) / gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta( x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y) * (digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y) * (digamma(y) - digamma(x + y))
def test_gruntz_eval_special_slow(): _sskip() assert ( gruntz( gamma(x + 1) / sqrt(2 * pi) - exp(-x) * (x ** (x + S.Half) + x ** (x - S.Half) / 12), x, oo, ) is oo ) assert gruntz(exp(exp(exp(digamma(digamma(digamma(x)))))) / x, x, oo) == 0
def test_beta(): x, y = Symbol('x'), Symbol('y') assert isinstance(beta(x, y), beta) assert expand_func(beta(x, y)) == gamma(x) * gamma(y) / gamma(x + y) assert expand_func(beta(x, y) - beta(y, x)) == 0 # Symmetric assert expand_func(beta( x, y)) == expand_func(beta(x, y + 1) + beta(x + 1, y)).simplify() assert diff(beta(x, y), x) == beta(x, y) * (digamma(x) - digamma(x + y)) assert diff(beta(x, y), y) == beta(x, y) * (digamma(y) - digamma(x + y)) assert conjugate(beta(x, y)) == beta(conjugate(x), conjugate(y)) raises(ArgumentIndexError, lambda: beta(x, y).fdiff(3))
def gamma_at_2_coefficient(k): """Reference: https://dlmf.nist.gov/5.7#E3""" if k == 0: return S(0) elif k == 1: return S(1 + digamma(1)) else: return ((-1)**k * (zeta(k) - 1) / k)
def get_reciprocal_factorial_minus_1_coefficients(count): """Reference: https://dlmf.nist.gov/5.7#E1""" #x = symbols('x') #return list(reversed(Poly((1 / gamma(x + 1) - 1).series(x, 0, count).removeO(), x).all_coeffs())) print(f"get_reciprocal_factorial_minus_1_coefficients({count})") c = [1] zetas = [(-digamma(1) if i==0 else (-1)**i*zeta(i+1)).evalf(default_evalf_inner_precision) for i in range(0,count)] for k in range(1,count): c.append(sum([c[i]*zetas[k-i-1] for i in range(0,k)])/k) return [S(0 if k==0 else c[k]) for k in range(0,count)]
def test_lowergamma(): from sympy import meijerg, exp_polar, I, expint assert lowergamma(x, 0) == 0 assert lowergamma(x, y).diff(y) == y ** (x - 1) * exp(-y) assert td(lowergamma(randcplx(), y), y) assert td(lowergamma(x, randcplx()), x) assert lowergamma(x, y).diff(x) == gamma(x) * digamma(x) - uppergamma(x, y) * log( y ) - meijerg([], [1, 1], [0, 0, x], [], y) assert lowergamma(S.Half, x) == sqrt(pi) * erf(sqrt(x)) assert not lowergamma(S.Half - 3, x).has(lowergamma) assert not lowergamma(S.Half + 3, x).has(lowergamma) assert lowergamma(S.Half, x, evaluate=False).has(lowergamma) assert tn(lowergamma(S.Half + 3, x, evaluate=False), lowergamma(S.Half + 3, x), x) assert tn(lowergamma(S.Half - 3, x, evaluate=False), lowergamma(S.Half - 3, x), x) assert tn_branch(-3, lowergamma) assert tn_branch(-4, lowergamma) assert tn_branch(Rational(1, 3), lowergamma) assert tn_branch(pi, lowergamma) assert lowergamma(3, exp_polar(4 * pi * I) * x) == lowergamma(3, x) assert lowergamma(y, exp_polar(5 * pi * I) * x) == exp(4 * I * pi * y) * lowergamma( y, x * exp_polar(pi * I) ) assert ( lowergamma(-2, exp_polar(5 * pi * I) * x) == lowergamma(-2, x * exp_polar(I * pi)) + 2 * pi * I ) assert conjugate(lowergamma(x, y)) == lowergamma(conjugate(x), conjugate(y)) assert conjugate(lowergamma(x, 0)) == 0 assert unchanged(conjugate, lowergamma(x, -oo)) assert lowergamma(x, y).rewrite(expint) == -(y ** x) * expint(-x + 1, y) + gamma(x) k = Symbol("k", integer=True) assert lowergamma(k, y).rewrite(expint) == -(y ** k) * expint(-k + 1, y) + gamma(k) k = Symbol("k", integer=True, positive=False) assert lowergamma(k, y).rewrite(expint) == lowergamma(k, y) assert lowergamma(x, y).rewrite(uppergamma) == gamma(x) - uppergamma(x, y) assert lowergamma(70, 6) == factorial( 69 ) - 69035724522603011058660187038367026272747334489677105069435923032634389419656200387949342530805432320 * exp( -6 ) assert ( lowergamma(S(77) / 2, 6) - lowergamma(S(77) / 2, 6, evaluate=False) ).evalf() < 1e-16 assert ( lowergamma(-S(77) / 2, 6) - lowergamma(-S(77) / 2, 6, evaluate=False) ).evalf() < 1e-16
def test_digamma_expand_func(): assert digamma(x).expand(func=True) == polygamma(0, x) assert digamma(2 * x).expand(func=True) == polygamma(0, x) / 2 + polygamma( 0, Rational(1, 2) + x ) / 2 + log(2) assert digamma(-1 + x).expand(func=True) == polygamma(0, x) - 1 / (x - 1) assert digamma(1 + x).expand(func=True) == 1 / x + polygamma(0, x) assert digamma(2 + x).expand(func=True) == 1 / x + 1 / (1 + x) + polygamma(0, x) assert digamma(3 + x).expand(func=True) == polygamma(0, x) + 1 / x + 1 / ( 1 + x ) + 1 / (2 + x) assert digamma(4 + x).expand(func=True) == polygamma(0, x) + 1 / x + 1 / ( 1 + x ) + 1 / (2 + x) + 1 / (3 + x) assert digamma(x + y).expand(func=True) == polygamma(0, x + y)
def print_error_bound(name, indent, variable_name, series_length, coefficients, nextc): is_alternating = sign(coefficients[-1]) != sign(coefficients[-2]) if is_alternating: nextc = abs(nextc) print( f"{indent}// Error is at most {format_real_coefficient(nextc)}*{variable_name}**{series_length} when {variable_name} >= 0" ) ulp1 = 2.220446049250313e-16 if name == "2: Digamma at 1": offset = S(1e6) elif name == "3: Digamma at 2": offset = 1 + digamma(1) elif name == "4: Digamma asymptotic": offset = S(1) elif name == "5: Trigamma at 1": offset = S(1e8) elif name == "7: Tetragamma at 1": offset = S(2e12) elif name == "6: Trigamma asymptotic": offset = S(1) elif name == "8: Tetragamma asymptotic": offset = S(12**-3) elif name == "15: log(exp(x) - 1) / x": offset = S(-log(1e-3)) else: offset = abs(coefficients[0]) if offset == 0: offset = abs(coefficients[1]) if offset == 0: offset = abs(coefficients[2]) bound = (offset * ulp1 / 2 / nextc)**(1.0 / (series_length - 2)) print( f"{indent}// which is at most Ulp({format_real_coefficient(offset)}*{variable_name}*{variable_name})/2 when 0 <= {variable_name} <= {format_real_coefficient(bound)}" ) else: bound = (offset * ulp1 / 2 / nextc)**(1.0 / (series_length - 1)) print( f"{indent}// which is at most Ulp({format_real_coefficient(offset)}*{variable_name})/2 when 0 <= {variable_name} <= {format_real_coefficient(bound)}" ) else: bound = (offset * ulp1 / 2 / nextc)**(1.0 / series_length) print( f"{indent}// which is at most Ulp({format_real_coefficient(offset)})/2 when 0 <= {variable_name} <= {format_real_coefficient(bound)}" )
def test_gruntz_eval_special(): # Gruntz, p. 126 assert gruntz(exp(x) * (sin(1 / x + exp(-x)) - sin(1 / x + exp(-x ** 2))), x, oo) == 1 assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x ** 2), x, oo) == -2 / sqrt(pi) assert gruntz(exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))), x, oo) == 1 assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x, oo) == oo assert gruntz(exp(exp(digamma(digamma(x)))) / x, x, oo) == exp(-S(1) / 2) assert gruntz(exp(exp(digamma(log(x)))) / x, x, oo) == exp(-S(1) / 2) assert gruntz(digamma(digamma(digamma(x))), x, oo) == oo assert gruntz(loggamma(loggamma(x)), x, oo) == oo assert gruntz(((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x * log(x), x, oo) == -S(1) / 2 assert gruntz(x * (gamma(x - 1 / gamma(x)) - gamma(x) + log(x)), x, oo) == S(1) / 2 assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x, oo) == 1
def test_gruntz_eval_special(): # Gruntz, p. 126 assert gruntz(exp(x)*(sin(1/x + exp(-x)) - sin(1/x + exp(-x**2))), x, oo) == 1 assert gruntz((erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x**2), x, oo) == -2/sqrt(pi) assert gruntz(exp(exp(x)) * (exp(sin(1/x + exp(-exp(x)))) - exp(sin(1/x))), x, oo) == 1 assert gruntz(exp(x)*(gamma(x + exp(-x)) - gamma(x)), x, oo) == oo assert gruntz(exp(exp(digamma(digamma(x))))/x, x, oo) == exp(-S(1)/2) assert gruntz(exp(exp(digamma(log(x))))/x, x, oo) == exp(-S(1)/2) assert gruntz(digamma(digamma(digamma(x))), x, oo) == oo assert gruntz(loggamma(loggamma(x)), x, oo) == oo assert gruntz(((gamma(x + 1/gamma(x)) - gamma(x))/log(x) - cos(1/x)) * x*log(x), x, oo) == -S(1)/2 assert gruntz(x * (gamma(x - 1/gamma(x)) - gamma(x) + log(x)), x, oo) \ == S(1)/2 assert gruntz((gamma(x + 1/gamma(x)) - gamma(x)) / log(x), x, oo) == 1
def test_gruntz_eval_special(): # Gruntz, p. 126 assert ( gruntz(exp(x) * (sin(1 / x + exp(-x)) - sin(1 / x + exp(-(x ** 2)))), x, oo) == 1 ) assert gruntz( (erf(x - exp(-exp(x))) - erf(x)) * exp(exp(x)) * exp(x ** 2), x, oo ) == -2 / sqrt(pi) assert ( gruntz(exp(exp(x)) * (exp(sin(1 / x + exp(-exp(x)))) - exp(sin(1 / x))), x, oo) == 1 ) assert gruntz(exp(x) * (gamma(x + exp(-x)) - gamma(x)), x, oo) is oo assert gruntz(exp(exp(digamma(digamma(x)))) / x, x, oo) == exp(Rational(-1, 2)) assert gruntz(exp(exp(digamma(log(x)))) / x, x, oo) == exp(Rational(-1, 2)) assert gruntz(digamma(digamma(digamma(x))), x, oo) is oo assert gruntz(loggamma(loggamma(x)), x, oo) is oo assert gruntz( ((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x) - cos(1 / x)) * x * log(x), x, oo ) == Rational(-1, 2) assert gruntz(x * (gamma(x - 1 / gamma(x)) - gamma(x) + log(x)), x, oo) == S.Half assert gruntz((gamma(x + 1 / gamma(x)) - gamma(x)) / log(x), x, oo) == 1
def test_leading_term(): from sympy import digamma assert O(1 / digamma(1 / x)) == O(1 / log(x))
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 digamma_at_1_coefficient(k): """Reference: https://dlmf.nist.gov/5.7#E4""" if k == 0: return digamma(1) return ((-1)**(k + 1) * zeta(k + 1))
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
def test_gruntz_eval_special_slow(): _sskip() assert gruntz(gamma(x + 1)/sqrt(2*pi) - exp(-x)*(x**(x + S(1)/2) + x**(x - S(1)/2)/12), x, oo) == oo assert gruntz(exp(exp(exp(digamma(digamma(digamma(x))))))/x, x, oo) == 0
def test_issue_15146(): e = (x/2) * (-2*x**3 - 2*(x**3 - 1) * x**2 * digamma(x**3 + 1) + \ 2*(x**3 - 1) * x**2 * digamma(x**3 + x + 1) + x + 3) assert limit(e, x, oo) == S(1)/3
def digamma_at_1_coefficient(k): if k == 0: return digamma(1).evalf(decimal_precision, maxn=evalf_inner_precision) return ((-1)**(k + 1) * zeta(k + 1)).evalf(decimal_precision, maxn=evalf_inner_precision)