Complex number class in python
Get this book -> Problems on Array: For Interviews and Competitive Programming Show In a previous article, we talked about complex numbers in Python using built-in classes and attributes to perform operations on them. But, imagine if Python didn't come equipped with these conveniences. 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. To start off, we need to initialize our complex number:
As we've stated previously, complex numbers are created from a real and an imaginary number. The You may have noticed that
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. AdditionWhen you add complex numbers together, you are only able to combine
like terms. In other words, we must add together the real and imaginary components of both numbers as separate pieces. Taking a look at the addition class below, we can see that we have defined the first number as After initializing our two complex numbers, we can then add them together as seen below the addition class.
SubtractionSubtraction works very similarly to addition with complex numbers. Once again, we can only combine like terms, therefore, it's important that we keep these separate when performing the subtraction operation. Using the same variables from the previous example,
As expected, when you subtract 3 from 2 the result is -1, and 10j - 5j resulted in 5j. MultiplicationMultiplication is a bit trickier. Unlike addition and subtraction, we will now have to combine coefficients of the imaginary numbers with the real numbers. However, the result must be returned as a complex number in the end. Before we jump into the code, let's take a look at how the math works out under the hood. As you can see below, we break the problem up into two separate pieces: one that solves for the real number and one that solves for the imaginary. This should look familiar to anyone who knows algebra.
NoteThe real part of the
complex number is solved by subtracting Understanding the math involved in solving for the multiplication of two complex numbers is an important first step. We are ready to move forward with the class. As we look below, you'll notice that the math we performed above is laid out very similarly to the code itself.
DivisionLike multiplication, dividing complex numbers can become messy pretty quickly. Especially if you're not familiar with the fundamental concepts of how to solve for the quotient of two complex numbers. Continuing with the same complex numbers used throughout this article, we begin with the problem that solves for the real number. Similar to multiplication, division of complex numbers can be broken down into two separate equations for each part of the complex number. Then we move onto the second half of the problem in which we solve for the imaginary part. This is pretty straight forward as we're just following the same basic division principles that we use with real numbers. Once we have two pieces for each - the real and imaginary parts of the complex number - then we need to combine them in order to get a final result.
The divisor in the problem above is found by raising the real and imaginary parts of the The j of 5j is eliminated once the number is raised to the second power, turning it into a real number and allowing it to be combined with Let's see this written out in Python:
Note:You may have noticed that the division operator is named Absolute ValueOn a less math-heavy note, let's try to find the absolute value of a complex number. To do so, we must extract the coefficient from the imaginary part and combine that with the real number. We've already done this with the divisor in the last example.
As we can see with i
You may be wondering why it is even necessary to raise the numbers to the second power if we're just going to solve for the square root, anyway. As noted earlier, raising the real and imaginary numbers to the second power pulls them out of the complex number by eliminating all imaginary parts (j squared is -1). This allows us to combine the two numbers in order to find their absolute value. All TogetherSo far we've discussed addition, subtraction, multiplication, division, and absolute value with complex numbers. You can contribute even more operations to your complex class on your own. But, first, let's clean up the code and see how it works all together.
As you see from our resulting output, we can add a new line ( With this article at OpenGenus, you must have the complete idea of implementing and designing Complex Numbers using classes in Python. Enjoy. What is complex class in python?Python complex()
The complex() method returns a complex number when real and imaginary parts are provided, or it converts a string to a complex number. The syntax of complex() is: complex([real[, imag]])
What is a complex number in python?A complex number is created from real numbers. Python complex number can be created either using direct assignment statement or by using complex () function. Complex numbers which are mostly used where we are using two real numbers.
How do you enter complex numbers in python?Method 1- Take a sting as input then convert it into complex. Method 2- Take two separate numbers and then convert them into complex. a = input() # user will enter 3+5j a = complex(a) # then this will be converted into complex number.
Are there complex numbers in python?Since complex is a native data type in Python, you can plug complex numbers into arithmetic expressions and call many of the built-in functions on them. More advanced functions for complex numbers are defined in the cmath module, which is part of the standard library.
|