本文整理汇总了Python中basic.C.re方法的典型用法代码示例。如果您正苦于以下问题:Python C.re方法的具体用法?Python C.re怎么用?Python C.re使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类basic.C
的用法示例。
在下文中一共展示了C.re方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _eval_expand_complex
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
def _eval_expand_complex(self, deep=True, **hints):
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:
if deep:
hints['complex'] = False
return C.re(self.expand(deep, **hints)) + \
S.ImaginaryUnit*C.im(self. expand(deep, **hints))
else:
return C.re(self) + S.ImaginaryUnit*C.im(self)
return C.re(self) + S.ImaginaryUnit*C.im(self)
示例2: as_real_imag
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
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))
示例3: get_integer_part
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
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
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
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))
示例5: _eval_expand_complex
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
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)
示例6: _eval_expand_complex
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
def _eval_expand_complex(self, *args):
return C.re(self) + C.im(self)*S.ImaginaryUnit
示例7: as_real_imag
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
def as_real_imag(self, deep=True):
return (C.re(self), C.im(self))
示例8: _eval_expand_complex
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
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)
示例9: _eval_expand_complex
# 需要导入模块: from basic import C [as 别名]
# 或者: from basic.C import re [as 别名]
def _eval_expand_complex(self, deep=True, **hints):
return C.re(self) + C.im(self)*S.ImaginaryUnit