本文整理汇总了Python中basic.C类的典型用法代码示例。如果您正苦于以下问题:Python C类的具体用法?Python C怎么用?Python C使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了C类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _eval_expand_complex
def _eval_expand_complex(self, *args):
if self.exp.is_Integer:
exp = self.exp
re, im = self.base.as_real_imag()
if exp >= 0:
base = re + S.ImaginaryUnit * im
else:
mag = re ** 2 + im ** 2
base = re / mag - S.ImaginaryUnit * (im / mag)
exp = -exp
return (base ** exp).expand()
elif self.exp.is_Rational:
# NOTE: This is not totally correct since for x**(p/q) with
# x being imaginary there are actually q roots, but
# only a single one is returned from here.
re, im = self.base.as_real_imag()
r = (re ** 2 + im ** 2) ** S.Half
t = C.atan2(im, re)
rp, tp = r ** self.exp, t * self.exp
return rp * C.cos(tp) + rp * C.sin(tp) * S.ImaginaryUnit
else:
return C.re(self) + S.ImaginaryUnit * C.im(self)
示例2: evalf_log
def evalf_log(expr, prec, options):
arg = expr.args[0]
workprec = prec+10
xre, xim, xacc, _ = evalf(arg, workprec, options)
if xim:
# XXX: use get_abs etc instead
re = evalf_log(C.log(C.abs(arg, evaluate=False), evaluate=False), prec, options)
im = mpf_atan2(xim, xre or fzero, prec)
return re[0], im, re[2], prec
imaginary_term = (mpf_cmp(xre, fzero) < 0)
re = mpf_log(mpf_abs(xre), prec, round_nearest)
size = fastlog(re)
if prec - size > workprec:
# We actually need to compute 1+x accurately, not x
arg = C.Add(S.NegativeOne,arg,evaluate=False)
xre, xim, xre_acc, xim_acc = evalf_add(arg, prec, options)
prec2 = workprec - fastlog(xre)
re = mpf_log(mpf_add(xre, fone, prec2), prec, round_nearest)
re_acc = prec
if imaginary_term:
return re, mpf_pi(prec), re_acc, prec
else:
return re, None, re_acc, None
示例3: get_integer_part
def get_integer_part(expr, no, options, return_ints=False):
"""
With no = 1, computes ceiling(expr)
With no = -1, computes floor(expr)
Note: this function either gives the exact result or signals failure.
"""
# The expression is likely less than 2^30 or so
assumed_size = 30
ire, iim, ire_acc, iim_acc = evalf(expr, assumed_size, options)
# We now know the size, so we can calculate how much extra precision
# (if any) is needed to get within the nearest integer
if ire and iim:
gap = max(fastlog(ire)-ire_acc, fastlog(iim)-iim_acc)
elif ire:
gap = fastlog(ire)-ire_acc
elif iim:
gap = fastlog(iim)-iim_acc
else:
# ... or maybe the expression was exactly zero
return None, None, None, None
margin = 10
if gap >= -margin:
ire, iim, ire_acc, iim_acc = evalf(expr, margin+assumed_size+gap, options)
# We can now easily find the nearest integer, but to find floor/ceil, we
# must also calculate whether the difference to the nearest integer is
# positive or negative (which may fail if very close)
def calc_part(expr, nexpr):
nint = int(to_int(nexpr, round_nearest))
expr = C.Add(expr, -nint, evaluate=False)
x, _, x_acc, _ = evalf(expr, 10, options)
check_target(expr, (x, None, x_acc, None), 3)
nint += int(no*(mpf_cmp(x or fzero, fzero) == no))
nint = from_int(nint)
return nint, fastlog(nint) + 10
re, im, re_acc, im_acc = None, None, None, None
if ire:
re, re_acc = calc_part(C.re(expr, evaluate=False), ire)
if iim:
im, im_acc = calc_part(C.im(expr, evaluate=False), iim)
if return_ints:
return int(to_int(re or fzero)), int(to_int(im or fzero))
return re, im, re_acc, im_acc
示例4: as_real_imag
def as_real_imag(self, deep=True, **hints):
from sympy.core.symbol import symbols
from sympy.polys.polytools import poly
from sympy.core.function import expand_multinomial
if self.exp.is_Integer:
exp = self.exp
re, im = self.base.as_real_imag(deep=deep)
a, b = symbols('a, b', dummy=True)
if exp >= 0:
if re.is_Number and im.is_Number:
# We can be more efficient in this case
expr = expand_multinomial(self.base**exp)
return expr.as_real_imag()
expr = poly((a + b)**exp) # a = re, b = im; expr = (a + b*I)**exp
else:
mag = re**2 + im**2
re, im = re/mag, -im/mag
if re.is_Number and im.is_Number:
# We can be more efficient in this case
expr = expand_multinomial((re + im*S.ImaginaryUnit)**-exp)
return expr.as_real_imag()
expr = poly((a + b)**-exp)
# Terms with even b powers will be real
r = [i for i in expr.terms() if not i[0][1] % 2]
re_part = Add(*[cc*a**aa*b**bb for (aa, bb), cc in r])
# Terms odd b powers will be imaginary
r = [i for i in expr.terms() if i[0][1] % 4 == 1]
im_part1 = Add(*[cc*a**aa*b**bb for (aa, bb), cc in r])
r = [i for i in expr.terms() if i[0][1] % 4 == 3]
im_part3 = Add(*[cc*a**a*b**bb for (aa, bb), cc in r])
return (re_part.subs({a: re, b: S.ImaginaryUnit*im}),
im_part1.subs({a: re, b: im}) + im_part3.subs({a: re, b: -im}))
elif self.exp.is_Rational:
# NOTE: This is not totally correct since for x**(p/q) with
# x being imaginary there are actually q roots, but
# only a single one is returned from here.
re, im = self.base.as_real_imag(deep=deep)
r = (re**2 + im**2)**S.Half
t = C.atan2(im, re)
rp, tp = r**self.exp, t*self.exp
return (rp*C.cos(tp), rp*C.sin(tp))
else:
if deep:
hints['complex'] = False
return (C.re(self.expand(deep, complex=False)),
C.im(self. expand(deep, **hints)))
else:
return (C.re(self), C.im(self))
示例5: _eval_subs
def _eval_subs(self, old, new):
if self==old: return new
if isinstance(old, self.__class__) and self.base==old.base:
coeff1,terms1 = self.exp.as_coeff_terms()
coeff2,terms2 = old.exp.as_coeff_terms()
if terms1==terms2: return new ** (coeff1/coeff2) # (x**(2*y)).subs(x**(3*y),z) -> z**(2/3*y)
if old.func is C.exp:
coeff1,terms1 = old.args[0].as_coeff_terms()
coeff2,terms2 = (self.exp * C.log(self.base)).as_coeff_terms()
if terms1==terms2: return new ** (coeff1/coeff2) # (x**(2*y)).subs(exp(3*y*log(x)),z) -> z**(2/3*y)
return self.base._eval_subs(old, new) ** self.exp._eval_subs(old, new)
示例6: as_real_imag
def as_real_imag(self, deep=True):
"""Performs complex expansion on 'self' and returns a tuple
containing collected both real and imaginary parts. This
method can't be confused with re() and im() functions,
which does not perform complex expansion at evaluation.
However it is possible to expand both re() and im()
functions and get exactly the same results as with
a single call to this function.
>>> from sympy import symbols, I
>>> x, y = symbols('xy', real=True)
>>> (x + y*I).as_real_imag()
(x, y)
>>> from sympy.abc import z, w
>>> (z + w*I).as_real_imag()
(-im(w) + re(z), im(z) + re(w))
"""
return (C.re(self), C.im(self))
示例7: _eval_expand_complex
def _eval_expand_complex(self, deep=True, **hints):
return C.re(self) + C.im(self)*S.ImaginaryUnit
示例8: _eval_derivative
def _eval_derivative(self, s):
dbase = self.base.diff(s)
dexp = self.exp.diff(s)
return self * (dexp * C.log(self.base) + dbase * self.exp / self.base)
示例9: _eval_as_leading_term
def _eval_as_leading_term(self, x):
if not self.exp.has(x):
return self.base.as_leading_term(x) ** self.exp
return C.exp(self.exp * C.log(self.base)).as_leading_term(x)
示例10: _eval_power
def _eval_power(self, exp):
return C.exp(exp)
示例11: _eval_expand_complex
def _eval_expand_complex(self, *args):
func = self.func(*[ a._eval_expand_complex(*args) for a in self.args ])
return C.re(func) + S.ImaginaryUnit * C.im(func)
示例12: _eval_expand_complex
def _eval_expand_complex(self, deep=True, **hints):
if deep:
func = self.func(*[ a.expand(deep, **hints) for a in self.args ])
else:
func = self.func(*self.args)
return C.re(func) + S.ImaginaryUnit * C.im(func)
示例14: as_real_imag
def as_real_imag(self, deep=True):
return (C.re(self), C.im(self))
示例15: _eval_expand_complex
def _eval_expand_complex(self, *args):
return C.re(self) + C.im(self)*S.ImaginaryUnit