[SciPy-user] poly1d equality test

Christian Kristukat ckkart at hoc.net
Mon Feb 27 20:40:51 EST 2006


Ryan Krauss wrote:
> Is there an easy way to test for the equality of two poly1d objects?
> 
> I have the following problem:
> In [36]: p1
> Out[36]: poly1d([  6.66666667,   0.        , -49.        ])
> 
> In [37]: p2
> Out[37]: poly1d([  6.66666667,   0.        , -49.        ])
> 
> In [38]: p1==p2
> Out[38]: False
> 
> In [39]: (p1.coeffs==p2.coeffs).all()
> Out[39]: True
> 
> It seems like == is only testing if they are the same object.  I am
> trying to write a class for transfer functions which model a system as
> a numerator and denominator poly1d.  When I multiply or divide them I
> need to check if the numerator and denominators cancel exactly.

Hi Ryan,
you could subclass poly1d and define the special method __eq__ to which is
called in situations lika a==b:

In [3]: class p(poly1d):
   ...:     def __eq__(self,other):
   ...:         return (self.coeffs==other.coeffs).all()
   ...:
   ...:

In [4]: p1 = p([  6.66666667,   0.        , -49.        ])

In [5]: p2 = p([  6.66666667,   0.        , -49.        ])

In [6]: p1 == p2
Out[6]: True

Regards, Christian




More information about the SciPy-User mailing list