def _calc_bernoulli(n): s = 0 a = int(C.binomial(n+3, n-6)) for j in xrange(1, n//6+1): s += a * bernoulli(n - 6*j) # Avoid computing each binomial coefficient from scratch a *= _product(n-6 - 6*j + 1, n-6*j) a //= _product(6*j+4, 6*j+9) if n % 6 == 4: s = -Rational(n+3, 6) - s else: s = Rational(n+3, 3) - s return s / C.binomial(n+3, n)
def _calc_bernoulli(n): s = 0 a = int(C.binomial(n + 3, n - 6)) for j in xrange(1, n // 6 + 1): s += a * bernoulli(n - 6 * j) # Avoid computing each binomial coefficient from scratch a *= _product(n - 6 - 6 * j + 1, n - 6 * j) a //= _product(6 * j + 4, 6 * j + 9) if n % 6 == 4: s = -Rational(n + 3, 6) - s else: s = Rational(n + 3, 3) - s return s / C.binomial(n + 3, n)
def _eval_rewrite_as_Sum(self, arg): if arg.is_even: k = C.Dummy("k", integer=True) j = C.Dummy("j", integer=True) n = self.args[0] / 2 Em = (S.ImaginaryUnit * C.Sum( C.Sum( C.binomial(k,j) * ((-1)**j * (k-2*j)**(2*n+1)) / (2**k*S.ImaginaryUnit**k * k), (j,0,k)), (k, 1, 2*n+1))) return Em
def _eval_rewrite_as_Sum(self, arg): if arg.is_even: k = C.Dummy("k", integer=True) j = C.Dummy("j", integer=True) n = self.args[0] / 2 Em = (S.ImaginaryUnit * C.Sum( C.Sum( C.binomial(k, j) * ((-1)**j * (k - 2*j)**(2*n + 1)) / (2**k*S.ImaginaryUnit**k * k), (j, 0, k)), (k, 1, 2*n + 1))) return Em
def eval(cls, n, sym=None): if n.is_Number: if n.is_Integer and n.is_nonnegative: if n is S.Zero: return S.One elif n is S.One: if sym is None: return -S.Half else: return sym - S.Half # Bernoulli numbers elif sym is None: if n.is_odd: return S.Zero n = int(n) # Use mpmath for enormous Bernoulli numbers if n > 500: p, q = bernfrac(n) return Rational(int(p), int(q)) case = n % 6 highest_cached = cls._highest[case] if n <= highest_cached: return cls._cache[n] # To avoid excessive recursion when, say, bernoulli(1000) is # requested, calculate and cache the entire sequence ... B_988, # B_994, B_1000 in increasing order for i in xrange(highest_cached + 6, n + 6, 6): b = cls._calc_bernoulli(i) cls._cache[i] = b cls._highest[case] = i return b # Bernoulli polynomials else: n, result = int(n), [] for k in xrange(n + 1): result.append(C.binomial(n, k)*cls(k)*sym**(n - k)) return Add(*result) else: raise ValueError("Bernoulli numbers are defined only" " for nonnegative integer indices.")
def _eval_rewrite_as_binomial(self,n): return C.binomial(2*n,n)/(n + 1)
def _eval_rewrite_as_binomial(self, n): return C.binomial(2 * n, n) / (n + 1)