# Special methods in Python #1: Implementing Quaternions

Jul 22, 2023 ### Introduction

Python has an extremely useful yet often overlooked tool — dunder methods. These are special methods that all classes share and allow us to customize the behavior of user-defined classes. If you have programmed in Python before, you probably are already familiar with the most common dunder methods: __init__ and __str__.

However, there are dozens of these dunder methods. They allow us to customize how the classes we create behave in different contexts. In this post, we’ll explore how to change class behavior when used in arithmetic operations like +, -, * and /.

I have a confession to make — I don’t like numbers. So we are going to use Python’s dunder methods to create our own number system, called quaternions.

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

### What are quaternions?

Quaternions are an extension of complex numbers that have the following structure: They have applications in many areas of mathematics, physics and engineering, robotics and video game graphics, especially because they allow us to easily represent rotations and orientations in three-dimensional spaces. To learn more, you can check out this video with a detailed explanation created by the excellent YouTube channel 3blue1brown.

### Quaternions in Python

To implement quaternions in Python we are going to create a new class, and we are going to redefine how these numbers add, subtract, multiply and divide. We start by creating the __init__ method, where we will define the coefficients of the real and imaginary parts of our quaternion.

` `

Then, we overwrite the __str__ method to create an adequate textual representation for this type of number:

` `

Now, we can already create and see our first quaternion:

` `

The next step is to adapt this class to be able to perform basic arithmetic operations with it. Let’s start by defining how to add two quaternions. To do this, we can overwrite the __add__ function, which takes another quaternion as a parameter. The sum of two quaternions is calculated as:

So, in Python, we can implement this operation as:

` `

On the other hand, the subtraction of quaternions is defined as:

Again, in Python we can implement it as follows:

` `

If we now try to carry out the two previous operations, we can check that they work correctly:

` `

The next step is to implement the product between two quaternions, also known as the Hamilton product. This operation is a bit more complicated, but can be written as:

Each of the rows determines the value of one of the coefficients: a, b, c and d respectively. In Python, we can implement it as follows, overwriting the __mul__ function.

` `

Finally, we are going to define division between quaternions. The operation is easy to carry out, although a bit complex to explain, since it involves the concepts of norm and conjugate, which are beyond the scope of this tutorial. However, you can delve deeper into these concepts in the Wikipedia article on quaternions. The division is defined as: In code, we can carry it out as follows:

` `

If we now test these two operations, we can see that they work correctly:

` `

The methods seen so far allow us to do basic arithmetic operations. However, there is a version of each of these operators that, in addition to performing the operation, assigns the result to a target variable. Of course, I’m referring to the operators: +=, -=, *= and /=. In Python, there are also special methods to overwrite these operators. They have the same name as the previous ones, preceded by the letter ‘i’: __iadd__, __isub__, __imul__ and __itruediv__. Once we have the previous methods implemented, it is easy to implement these:

` `

### Summing up

In short, Python’s dunder methods allow us to alter how our objects respond to the different basic operations.

In the next post of this series, we are going to continue with the quaternions example to learn about other dunder methods that will allow us to compare the magnitude our numbers. See you there!