![]() ![]() Using the same variables from the previous example, i and k, let's see the result of the subtraction of these two complex numbers. Once again, we can only combine like terms, therefore, it's important that we keep these separate when performing the subtraction operation. Subtraction works very similarly to addition with complex numbers. Return Complex(self.real + other.real, self.imag + other.imag) Taking a look at the addition class below, we can see that we have defined the first number as self and the second as other.Īfter initializing our two complex numbers, we can then add them together as seen below the addition class. In other words, we must add together the real and imaginary components of both numbers as separate pieces. When you add complex numbers together, you are only able to combine like terms. We have a class that defines complex numbers by their real and imaginary parts, now we're ready to begin creating operations to perform on complex numbers. Therefore, if a user does not pass any value into imag, then this ensures that it will still be imaginary. This is because zero is a purely imaginary number. You may have noticed that imag (short for imaginary) has been set equal to zero. The _init_ operator defines the values that form the complex number by initializing the value of self. To start off, we need to initialize our complex number: def _init_ (self, real, imag=0.0):Īs we've stated previously, complex numbers are created from a real and an imaginary number. How would we go about implementing complex operations? We can explore that concept by utilizing built-in mathematical operations to create our own complex class in Python. But, imagine if Python didn't come equipped with these conveniences. In a previous article, we talked about complex numbers in Python using built-in classes and attributes to perform operations on them.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |