# [SciPy-user] poly1d equality test

David M. Cooke cookedm at physics.mcmaster.ca
Mon Feb 27 20:36:40 CST 2006

```Christian Kristukat <ckkart at hoc.net> writes:

> 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

I've added __eq__ and __ne__ methods to the poly1d class in svn.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

```