# Special methods in Python #2: Comparing Quaternions

Jul 23, 2023 ### Introduction

This is the second article in our series on special methods in Python. In this link you can find the first one.

In this article we are going to continue implementing quaternions in Python. Thanks to the code we wrote in the previous article, we can already perform basic arithmetic operations with these numbers, but we still don't have a way to compare them. In Python, we can compare two elements using the <, >, <=, >=, == and != operators. For example, we can compare if a is greater than or equal to b like this:

However, to be able to make these comparisons, we need to know the rules that lead us to consider one element greater/less than or equal/different from another. In the case of numbers, this is easy. But when we want to compare another type of data, like the quaternions we are creating, Python does not know how to perform the comparison.

To solve this, we are going to override several special methods __eq__, __ne__, __lt__, __le__, __gt__, __ge__. Each of them defines how Python should perform a type of comparison between elements of this type.

To learn Python and build the skills to land your first job, check out our

### Magnitude of quaternions

To compare quaternions we are going to use the notion of "magnitude". This value represents the "length" of the quaternion from the origin. To understand this concept, you can think of quaternions as 4D vectors, and the magnitude as the Euclidean norm of that vector: ### Comparison of quaternions in Python

In the previous post we saw how to create a quaternion, as shown below. We have excluded the arithmetic operations we implemented for simplicity:

Now, we are going to start defining the comparison operations for quaternions, and we will begin with the == and != operators. These operators check if two quaternions are equal or different, respectively. To define how quaternions behave with these operators, we have to overwrite the special methods __eq__ and __ne__:

As you can see, two quaternions are equal if all coefficients of their real and imaginary parts match. Otherwise, they are different:

Now, we are going to implement the < and <= operators, which determine if a reference quaternion is less than or less than or equal to another, respectively. To do this, we create an auxiliary function called magnitude, which will determine the magnitude of a quaternion according to the definition we saw in the previous section. Then, we use that function in the __lt__ and __le__ dunder methods, which will define the behavior of quaternions with the previous operators:

If we now check these two operations, you can see that we get the correct results:

Finally, we are going to implement the opposite operators to the above: > and >=, which define if a reference quaternion is greater than or greater than or equal to another. To do this, we are going to implement the __gt__ and __ge__ dunder methods:

And again, if we check the result of these operations, we can see they work:

### Summing up

In this article, we continued implementing a Quaternion class in Python by overriding special methods for comparisons. Using quaternion magnitude, we defined less than, greater than, and equality operators. I hope you are enjoying our series on dunder methods in Python. In the next instalment we are going to learn to learn to redefine how classes perform logical operations.