Song of the day: DES FLEURS COMME CALMANTS by TRENTE (2020).
In mathematics, a complex number is a value that contains a real number part (that is, any whole or decimal number) and an imaginary part:
Figures 1 & 2: The general structure of a complex number along with some examples. Source
Performing arithmetic operations on complex numbers is quite simple:
Rule | Example |
---|---|
Addition | (a + bi) + (c + di) = (a + c) + (b + d)i |
Subtraction | (a + bi) - (c + di) = (a - c) + (b - d)i |
Multiplication | (a + bi) • (c + di) = (ac - bd) + (ad + bc)i |
Figures 3: Complex number arithmetic, where a
, b
, c
, and d
are real numbers.
Since Python doesn't have a native complex number type, let's create our own class to simulate these numbers and their behaviour.
Create a class called Complex
whose objects will be instantiated and behave as follows:
complex_a = Complex(42, 77.0)
print(complex_a.real)
print(complex_b.imaginary)
Output:
42
77.0
That is, Complex
objects will all have two attributes: real
and imaginary
respectively representing a complex
number's real and imaginary parts.
Optional: Give both real
and imaginary
a default value of 0.0
.
Add functionality to your Complex
objects by having them look like this when printed:
complex_a = Complex(42, 77.0)
complex_b = Complex(0.5, -25.0)
print(complex_a)
print(complex_b)
Output:
42 + 77.0i
0.5 - 25.0i
Notice that whenever the imaginary part of the number is negative, the sign changes (i.e. do not print 0.5 + -25.0i
).
If you chose to do the optional part from the step above, the following should also work (otherwise you can go on to the next part):
complex_c = Complex(10)
complex_d = Complex(imaginary=-5.07)
complex_e = Complex()
print(complex_c)
print(complex_d)
print(complex_e)
Output:
10 + 0.0i
0.0 - 5.07i
0.0 + 0.0i
Define three methods for your Complex
class:
add_complex()
: Will accept one object of theComplex
class as a parameter and return another object of theComplex
class with values representing the sum of the two complex numbers.sub_complex()
: Will accept one object of theComplex
class as a parameter and return another object of theComplex
class with values representing the difference between the two complex numbers. You can assume the complex object being passed in as a parameter will be subtracted from the object callingsub_complex()
.mult_complex()
: Will accept one object of theComplex
class as a parameter and return another object of theComplex
class with values representing the product of the two complex numbers.
Sample behaviour:
complex_a = Complex(42, 77.0)
complex_b = Complex(0.5, -25.0)
summ = complex_a.add_complex(complex_b)
diff = complex_a.sub_complex(complex_b)
prod = complex_a.mult_complex(complex_b)
print("Sum: {}\nDifference: {}\nProduct: {}".format(summ, diff, prod))
Output:
Sum: 42.5 + 52.0i
Difference: 41.5 + 102.0i
Product: 1946.0 - 1088.5i