当前位置: 首页>>代码示例>>Python>>正文


Python Poly.iter_all_coeffs方法代码示例

本文整理汇总了Python中sympy.Poly.iter_all_coeffs方法的典型用法代码示例。如果您正苦于以下问题:Python Poly.iter_all_coeffs方法的具体用法?Python Poly.iter_all_coeffs怎么用?Python Poly.iter_all_coeffs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sympy.Poly的用法示例。


在下文中一共展示了Poly.iter_all_coeffs方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_iterators

# 需要导入模块: from sympy import Poly [as 别名]
# 或者: from sympy.Poly import iter_all_coeffs [as 别名]
def test_iterators():
    f = Poly(x**5 + x, x)

    assert list(f.iter_all_coeffs()) == \
        [1, 0, 0, 0, 1, 0]

    assert list(f.iter_all_monoms()) == \
        [(5,), (4,), (3,), (2,), (1,), (0,)]

    assert list(f.iter_all_terms()) == \
        [(1, (5,)), (0, (4,)), (0, (3,)), (0, (2,)), (1, (1,)), (0, (0,))]
开发者ID:jcockayne,项目名称:sympy-rkern,代码行数:13,代码来源:test_polynomial.py

示例2: SymTF

# 需要导入模块: from sympy import Poly [as 别名]
# 或者: from sympy.Poly import iter_all_coeffs [as 别名]
class SymTF(object):
    def __init__(self,num,den,var='s'):
        self.num = Poly(num,var)
        self.den = Poly(den,var)

    def __repr__(self,labelstr='systemid.SymTF'):
        nstr=str(self.num)
        dstr=str(self.den)
        n=len(dstr)
        m=len(nstr)
        shift=(n-m)/2*' '
        nstr=nstr.replace('\n','\n'+shift)
        tempstr=labelstr+'\n'+shift+nstr+'\n'+'-'*n+'\n '+dstr
        return tempstr

    def __call__(self,s):
        return self.num(s)/self.den(s)

    def __add__(self,other):
        if hasattr(other,'num') and hasattr(other,'den'):
            if len(list(self.den.iter_all_coeffs()))==len(list(other.den.iter_all_coeffs())) and \
                   list(self.den.iter_all_coeffs())==list(other.den.iter_all_coeffs()):
                return SymTF(self.num+other.num,self.den)
            else:
                return SymTF(self.num*other.den+other.num*self.den,self.den*other.den)
        elif isinstance(other, int) or isinstance(other, float):
            return SymTF(other*self.den+self.num,self.den)
        else:
            raise ValueError, 'Do not know how to add SymTF and '+str(other) +' which is of type '+str(type(other))

    def __sub__(self,other):
        if hasattr(other,'num') and hasattr(other,'den'):
            if len(list(self.den.iter_all_coeffs()))==len(list(other.den.iter_all_coeffs())) and \
                   (list(self.den.iter_all_coeffs())==list(other.den.iter_all_coeffs())).all():
                return SymTF(self.num-other.num,self.den)
            else:
                return SymTF(self.num*other.den-other.num*self.den,self.den*other.den)
        elif isinstance(other, int) or isinstance(other, float):
            return SymTF(other*self.den-self.num,self.den)
        else:
            raise ValueError, 'Do not know how to subtract SymTF and '+str(other) +' which is of type '+str(type(other))

    def __mul__(self,other):
        if hasattr(other,'num') and hasattr(other,'den'):
            if self.num == other.den and self.den == other.num:
                return 1
            elif self.num == other.den:
                return self.__class__(other.num,self.den)
            elif self.den == other.num:
                return self.__class__(self.num,other.den)
            else:
               return self.__class__(self.num*other.num, \
                                             self.den*other.den)
        elif isinstance(other, int) or isinstance(other, float):
            return self.__class__(other*self.num,self.den)


    def __pow__(self, expon):
        """Basically, go self*self*self as many times as necessary.  I
        haven't thought about negative exponents.  I don't think this
        would be hard, you would just need to keep dividing by self
        until you got the right answer."""
        assert expon >= 0, 'SymTF.__pow__ does not yet support negative exponents.'
        out = 1.0
        for n in range(expon):
            out *= self
        return out

    def __div__(self,other):
        if hasattr(other,'num') and hasattr(other,'den'):
            if self.den == other.den and self.num == other.num:
                return 1
            elif self.den == other.den:
                return SymTF(self.num,other.num)
            else:
                return SymTF(self.num*other.den,self.den*other.num)
        elif isinstance(other, int) or isinstance(other, float):
            return SymTF(self.num,other*self.den)

    def to_num_TF(self,var_dict,dt=0.01,maxt=5.0,myvar='s'):
        namespace = var_dict.copy()
        num = list(self.num.iter_all_coeffs())
        den = list(self.den.iter_all_coeffs())
        tf_num = [eval(str(i),namespace) for i in num]
        tf_den = [eval(str(i),namespace) for i in den]
        print tf_num,tf_den
        return TF(tf_num,tf_den,dt=dt,maxt=maxt,myvar=myvar)

    def __radd__(self,other):
        return self.__add__(other)

    def __rmul__(self,other):
        return self.__mul__(other)

    def __rdiv__(self, other):
        return self.__div__(other)

    def __truediv__(self,other):
        return self.__div__(other)
开发者ID:ryanGT,项目名称:symcontrols,代码行数:101,代码来源:__init__.py


注:本文中的sympy.Poly.iter_all_coeffs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。