def stieltjes(n): """Computes the nth Stieltjes constant.""" n = int(n) if n == 0: return +euler if n < 0: raise ValueError("Stieltjes constants defined for n >= 0") if n in stieltjes_cache: prec, s = stieltjes_cache[n] if prec >= mp.prec: return +s from quadrature import quadgl def f(x): r = exp(pi*j*x) return (zeta(r+1) / r**n).real orig = mp.prec try: p = int(log(factorial(n), 2) + 35) mp.prec += p u = quadgl(f, [-1, 1]) v = mpf(-1)**n * factorial(n) * u / 2 finally: mp.prec = orig stieltjes_cache[n] = (mp.prec, v) return +v
def stieltjes(n): """Computes the nth Stieltjes constant.""" n = int(n) if n == 0: return +euler if n < 0: raise ValueError("Stieltjes constants defined for n >= 0") if n in stieltjes_cache: prec, s = stieltjes_cache[n] if prec >= mp.prec: return +s from quadrature import quadgl def f(x): r = exp(pi * j * x) return (zeta(r + 1) / r**n).real orig = mp.prec try: p = int(log(factorial(n), 2) + 35) mp.prec += p u = quadgl(f, [-1, 1]) v = mpf(-1)**n * factorial(n) * u / 2 finally: mp.prec = orig stieltjes_cache[n] = (mp.prec, v) return +v
def quadosc(f, interval, period=None, zeros=None, alt=1): """ Integrates f(x) over interval = [a, b] where at least one of a and b is infinite and f is a slowly decaying oscillatory function. The zeros of f must be provided, either by specifying a period (suitable when f contains a pure sine or cosine factor) or providing a function that returns the nth zero (suitable when the oscillation is not strictly periodic). """ a, b = AS_POINTS(interval) a = convert_lossless(a) b = convert_lossless(b) if period is None and zeros is None: raise ValueError( \ "either the period or zeros keyword parameter must be specified") if a == -inf and b == inf: s1 = quadosc(f, [a, 0], zeros=zeros, period=period, alt=alt) s2 = quadosc(f, [0, b], zeros=zeros, period=period, alt=alt) return s1 + s2 if a == -inf: if zeros: return quadosc(lambda x:f(-x), [-b,-a], lambda n: zeros(-n), alt=alt) else: return quadosc(lambda x:f(-x), [-b,-a], period=period, alt=alt) if b != inf: raise ValueError("quadosc requires an infinite integration interval") if not zeros: zeros = lambda n: n*period/2 for n in range(1,10): p = zeros(n) if p > a: break if n >= 9: raise ValueError("zeros do not appear to be correctly indexed") if alt == 0: s = quadgl(f, [a, zeros(n+1)]) s += sumrich(lambda k: quadgl(f, [zeros(2*k), zeros(2*k+2)]), [n, inf]) else: s = quadgl(f, [a, zeros(n)]) s += sumsh(lambda k: quadgl(f, [zeros(k), zeros(k+1)]), [n, inf]) return s