當前位置: 首頁>>代碼示例>>Python>>正文


Python basic.C類代碼示例

本文整理匯總了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)
開發者ID:rkern,項目名稱:sympy-rkern,代碼行數:25,代碼來源:power.py

示例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
開發者ID:cran,項目名稱:rSymPy,代碼行數:28,代碼來源:evalf.py

示例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
開發者ID:cran,項目名稱:rSymPy,代碼行數:51,代碼來源:evalf.py

示例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))
開發者ID:bibile,項目名稱:sympy,代碼行數:57,代碼來源:power.py

示例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)
開發者ID:gnulinooks,項目名稱:sympy,代碼行數:11,代碼來源:power.py

示例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))
開發者ID:bibile,項目名稱:sympy,代碼行數:24,代碼來源:expr.py

示例7: _eval_expand_complex

 def _eval_expand_complex(self, deep=True, **hints):
         return C.re(self) + C.im(self)*S.ImaginaryUnit
開發者ID:Sumith1896,項目名稱:sympy-polys,代碼行數:2,代碼來源:symbol.py

示例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)
開發者ID:rkern,項目名稱:sympy-rkern,代碼行數:4,代碼來源:power.py

示例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)
開發者ID:rkern,項目名稱:sympy-rkern,代碼行數:4,代碼來源:power.py

示例10: _eval_power

 def _eval_power(self, exp):
     return C.exp(exp)
開發者ID:gnulinooks,項目名稱:sympy,代碼行數:2,代碼來源:numbers.py

示例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)
開發者ID:gnulinooks,項目名稱:sympy,代碼行數:3,代碼來源:function.py

示例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)
開發者ID:hazelnusse,項目名稱:sympy-old,代碼行數:6,代碼來源:function.py

示例13: __abs__

 def __abs__(self):
     return C.abs(self)
開發者ID:goriccardo,項目名稱:sympy,代碼行數:2,代碼來源:expr.py

示例14: as_real_imag

 def as_real_imag(self, deep=True):
     return (C.re(self), C.im(self))
開發者ID:bibile,項目名稱:sympy,代碼行數:2,代碼來源:symbol.py

示例15: _eval_expand_complex

 def _eval_expand_complex(self, *args):
     return C.re(self) + C.im(self)*S.ImaginaryUnit
開發者ID:gnulinooks,項目名稱:sympy,代碼行數:2,代碼來源:symbol.py


注:本文中的basic.C類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。