本文整理汇总了Python中sympy.core.C.bernoulli方法的典型用法代码示例。如果您正苦于以下问题:Python C.bernoulli方法的具体用法?Python C.bernoulli怎么用?Python C.bernoulli使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sympy.core.C
的用法示例。
在下文中一共展示了C.bernoulli方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eval
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def eval(cls, z, a=S.One):
z, a = map(sympify, (z, a))
if a.is_Number:
if a is S.NaN:
return S.NaN
elif a is S.Zero:
return cls(z)
if z.is_Number:
if z is S.NaN:
return S.NaN
elif z is S.Infinity:
return S.One
elif z is S.Zero:
if a.is_negative:
return S.Half - a - 1
else:
return S.Half - a
elif z is S.One:
return S.ComplexInfinity
elif z.is_Integer:
if a.is_Integer:
if z.is_negative:
zeta = (-1)**z * C.bernoulli(-z+1)/(-z+1)
elif z.is_even:
B, F = C.bernoulli(z), C.factorial(z)
zeta = 2**(z-1) * abs(B) * pi**z / F
else:
return
if a.is_negative:
return zeta + C.harmonic(abs(a), z)
else:
return zeta - C.harmonic(a-1, z)
示例2: taylor_term
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def taylor_term(n, x, *previous_terms):
if n == 0:
return 1 / sympify(x)
elif n < 0 or n % 2 == 0:
return S.Zero
else:
x = sympify(x)
B = C.bernoulli(n+1)
F = C.Factorial(n+1)
return 2**(n+1) * B/F * x**n
示例3: taylor_term
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def taylor_term(n, x, *previous_terms):
if n < 0 or n % 2 == 0:
return S.Zero
else:
x = sympify(x)
a = 2**(n+1)
B = C.bernoulli(n+1)
F = C.factorial(n+1)
return a*(a-1) * B/F * x**n
示例4: eval
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def eval(cls, z, a_=None):
if a_ is None:
z, a = list(map(sympify, (z, 1)))
else:
z, a = list(map(sympify, (z, a_)))
if a.is_Number:
if a is S.NaN:
return S.NaN
elif a is S.One and a_ is not None:
return cls(z)
# TODO Should a == 0 return S.NaN as well?
if z.is_Number:
if z is S.NaN:
return S.NaN
elif z is S.Infinity:
return S.One
elif z is S.Zero:
if a.is_negative:
return S.Half - a - 1
else:
return S.Half - a
elif z is S.One:
return S.ComplexInfinity
elif z.is_Integer:
if a.is_Integer:
if z.is_negative:
zeta = (-1)**z * C.bernoulli(-z + 1)/(-z + 1)
elif z.is_even:
B, F = C.bernoulli(z), C.factorial(z)
zeta = 2**(z - 1) * abs(B) * pi**z / F
else:
return
if a.is_negative:
return zeta + C.harmonic(abs(a), z)
else:
return zeta - C.harmonic(a - 1, z)
示例5: taylor_term
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def taylor_term(n, x, *previous_terms):
"""
Returns the next term in the Taylor series expansion
"""
if n == 0:
return 1/sympify(x)
elif n < 0 or n % 2 == 0:
return S.Zero
else:
x = sympify(x)
B = C.bernoulli(n + 1)
F = factorial(n + 1)
return 2 * (1 - 2**n) * B/F * x**n
示例6: euler_maclaurin
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def euler_maclaurin(self, m=0, n=0, eps=0, eval_integral=True):
"""
Return an Euler-Maclaurin approximation of self, where m is the
number of leading terms to sum directly and n is the number of
terms in the tail.
With m = n = 0, this is simply the corresponding integral
plus a first-order endpoint correction.
Returns (s, e) where s is the Euler-Maclaurin approximation
and e is the estimated error (taken to be the magnitude of
the first omitted term in the tail):
>>> from sympy.abc import k, a, b
>>> from sympy import Sum
>>> Sum(1/k, (k, 2, 5)).doit().evalf()
1.28333333333333
>>> s, e = Sum(1/k, (k, 2, 5)).euler_maclaurin()
>>> s
7/20 - log(2) + log(5)
>>> from sympy import sstr
>>> print sstr((s.evalf(), e.evalf()), full_prec=True)
(1.26629073187416, 0.0175000000000000)
The endpoints may be symbolic:
>>> s, e = Sum(1/k, (k, a, b)).euler_maclaurin()
>>> s
-log(a) + log(b) + 1/(2*a) + 1/(2*b)
>>> e
abs(-1/(12*b**2) + 1/(12*a**2))
If the function is a polynomial of degree at most 2n+1, the
Euler-Maclaurin formula becomes exact (and e = 0 is returned):
>>> Sum(k, (k, 2, b)).euler_maclaurin()
(-1 + b/2 + b**2/2, 0)
>>> Sum(k, (k, 2, b)).doit()
-1 + b/2 + b**2/2
With a nonzero eps specified, the summation is ended
as soon as the remainder term is less than the epsilon.
"""
m = int(m)
n = int(n)
f = self.function
assert len(self.limits) == 1
i, a, b = self.limits[0]
s = S.Zero
if m:
for k in range(m):
term = f.subs(i, a+k)
if (eps and term and abs(term.evalf(3)) < eps):
return s, abs(term)
s += term
a += m
x = Symbol('x', dummy=True)
I = C.Integral(f.subs(i, x), (x, a, b))
if eval_integral:
I = I.doit()
s += I
def fpoint(expr):
if b is S.Infinity:
return expr.subs(i, a), 0
return expr.subs(i, a), expr.subs(i, b)
fa, fb = fpoint(f)
iterm = (fa + fb)/2
g = f.diff(i)
for k in xrange(1, n+2):
ga, gb = fpoint(g)
term = C.bernoulli(2*k)/C.Factorial(2*k)*(gb-ga)
if (eps and term and abs(term.evalf(3)) < eps) or (k > n):
break
s += term
g = g.diff(i, 2)
return s + iterm, abs(term)
示例7: eval_sum_symbolic
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def eval_sum_symbolic(f, limits):
(i, a, b) = limits
if not f.has(i):
return f*(b-a+1)
# Linearity
if f.is_Mul:
L, R = f.as_two_terms()
if not L.has(i):
sR = eval_sum_symbolic(R, (i, a, b))
if sR: return L*sR
if not R.has(i):
sL = eval_sum_symbolic(L, (i, a, b))
if sL: return R*sL
try:
f = apart(f, i) # see if it becomes an Add
except PolynomialError:
pass
if f.is_Add:
L, R = f.as_two_terms()
lrsum = telescopic(L, R, (i, a, b))
if lrsum:
return lrsum
lsum = eval_sum_symbolic(L, (i, a, b))
rsum = eval_sum_symbolic(R, (i, a, b))
if None not in (lsum, rsum):
return lsum + rsum
# Polynomial terms with Faulhaber's formula
n = Wild('n')
result = f.match(i**n)
if result is not None:
n = result[n]
if n.is_Integer:
if n >= 0:
return ((C.bernoulli(n+1, b+1) - C.bernoulli(n+1, a))/(n+1)).expand()
elif a.is_Integer and a >= 1:
if n == -1:
return C.harmonic(b) - C.harmonic(a - 1)
else:
return C.harmonic(b, abs(n)) - C.harmonic(a - 1, abs(n))
# Geometric terms
c1 = C.Wild('c1', exclude=[i])
c2 = C.Wild('c2', exclude=[i])
c3 = C.Wild('c3', exclude=[i])
e = f.match(c1**(c2*i+c3))
if e is not None:
c1 = c1.subs(e)
c2 = c2.subs(e)
c3 = c3.subs(e)
# TODO: more general limit handling
return c1**c3 * (c1**(a*c2) - c1**(c2+b*c2)) / (1 - c1**c2)
if not (a.has(S.Infinity, S.NegativeInfinity) or \
b.has(S.Infinity, S.NegativeInfinity)):
r = gosper_sum(f, (i, a, b))
if not r in (None, S.NaN):
return r
return eval_sum_hyper(f, (i, a, b))
示例8: eval_sum_symbolic
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
def eval_sum_symbolic(f, limits):
(i, a, b) = limits
if not f.has(i):
return f * (b - a + 1)
# Linearity
if f.is_Mul:
L, R = f.as_two_terms()
if not L.has(i):
sR = eval_sum_symbolic(R, (i, a, b))
if sR:
return L * sR
if not R.has(i):
sL = eval_sum_symbolic(L, (i, a, b))
if sL:
return R * sL
try:
f = apart(f, i) # see if it becomes an Add
except PolynomialError:
pass
if f.is_Add:
L, R = f.as_two_terms()
lrsum = telescopic(L, R, (i, a, b))
if lrsum:
return lrsum
lsum = eval_sum_symbolic(L, (i, a, b))
rsum = eval_sum_symbolic(R, (i, a, b))
if None not in (lsum, rsum):
return lsum + rsum
# Polynomial terms with Faulhaber's formula
n = Wild("n")
result = f.match(i ** n)
if result is not None:
n = result[n]
if n.is_Integer:
if n >= 0:
return ((C.bernoulli(n + 1, b + 1) - C.bernoulli(n + 1, a)) / (n + 1)).expand()
elif a.is_Integer and a >= 1:
if n == -1:
return C.harmonic(b) - C.harmonic(a - 1)
else:
return C.harmonic(b, abs(n)) - C.harmonic(a - 1, abs(n))
if not (a.has(S.Infinity, S.NegativeInfinity) or b.has(S.Infinity, S.NegativeInfinity)):
# Geometric terms
c1 = C.Wild("c1", exclude=[i])
c2 = C.Wild("c2", exclude=[i])
c3 = C.Wild("c3", exclude=[i])
e = f.match(c1 ** (c2 * i + c3))
if e is not None:
p = (c1 ** c3).subs(e)
q = (c1 ** c2).subs(e)
r = p * (q ** a - q ** (b + 1)) / (1 - q)
l = p * (b - a + 1)
return Piecewise((l, Eq(q, S.One)), (r, True))
r = gosper_sum(f, (i, a, b))
if not r in (None, S.NaN):
return r
return eval_sum_hyper(f, (i, a, b))
示例9: eval_sum_symbolic
# 需要导入模块: from sympy.core import C [as 别名]
# 或者: from sympy.core.C import bernoulli [as 别名]
lsum = eval_sum_symbolic(L, (i, a, b))
rsum = eval_sum_symbolic(R, (i, a, b))
if None not in (lsum, rsum):
return lsum + rsum
# Polynomial terms with Faulhaber's formula
n = Wild('n')
result = f.match(i**n)
if result is not None:
n = result[n]
if n.is_Integer:
if n >= 0:
return ((C.bernoulli(n+1, b+1) - C.bernoulli(n+1, a))/(n+1)).expand()
elif a.is_Integer and a >= 1:
if n == -1:
return C.harmonic(b) - C.harmonic(a - 1)
else:
return C.harmonic(b, abs(n)) - C.harmonic(a - 1, abs(n))
# Geometric terms
c1 = C.Wild('c1', exclude=[i])
c2 = C.Wild('c2', exclude=[i])
c3 = C.Wild('c3', exclude=[i])
e = f.match(c1**(c2*i+c3))
if e is not None:
c1 = c1.subs(e)