You might look at the new rewritten polys module on issue 1598. We should hopefully have that merged in whenever Mateusz is fully finished with it. Until then, you can pull his branch and check it out.
Aaron Meurer On Sep 8, 2009, at 7:24 AM, William Purcell wrote: > > I am trying to subclass Poly to change Poly.coeffs. Here is the code. > > --------------------------------------------------------------------------- > def poly_coeffs_filled(p): > cs = p.coeffs > coeff_dict = dict(map(lambda x: > (str(x[0][0]),str(x[1])),p.as_dict().items())) > degree = p.degree > for d in range(p.degree): > if not coeff_dict.has_key(str(d)): > coeff_dict[str(d)] = '0' > l = [coeff_dict[item] for item in > sorted(coeff_dict.keys(),reverse=True)] > return tuple(l) > > class Poly(SympyPoly): > def __new__(cls,expr,var='s',*args,**flags): > new = SympyPoly.__new__(cls,expr,var) > new.coeffs = poly_coeffs_filled(new) > return Basic.__new__(cls,*args,**flags) > --------------------------------------------------------------------------- > > Here is the error I get. > > --------------------------------------------------------------------------- > In [5]: p = symcontrols.Poly('s**2+2') > --------------------------------------------------------------------------- > AttributeError Traceback (most recent > call last) > > /home/william/repos/symcontrols/symcontrols/<ipython console> in > <module>() > > /home/william/repos/symcontrols/symcontrols/__init__.py in __new__ > (cls, > expr, var, *args, **flags) > 16 def __new__(cls,expr,var='s',*args,**flags): > 17 new = SympyPoly.__new__(cls,expr,var) > ---> 18 new.coeffs = poly_coeffs_filled(new) > 19 return Basic.__new__(cls,*args,**flags) > 20 > > AttributeError: can't set attribute > --------------------------------------------------------------------------- > > The problem I am having with the current Poly.coeffs is that > 's**2+1'.coeffs will be (1,1) as will 's+1'.coeffs. I want to be able > to distinguish between these two in a comparison. > > So my question is, what is the proper way to subclass Poly and > override > the coeffs attribute? I normally would just override the __init__ > method but I see that Sympy is big on __new__ which I have never seen > before Sympy. > > - Bill > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---