Processing math: 100%

Problem 1: Complex Number (100pts)

In mathematics, a complex number is a number that can be expressed in the form a+bi, where a and b are real numbers, and i represents the imaginary unit that satisfies the equation i2=1. For example, 2+3i is a complex number. For the complex number a+bi, a is called the real part, and b is called the imaginary part. To emphasize, the imaginary part does not include a factor i; that is, the imaginary part is b, not bi.

Complex numbers can be added and multiplied. For any complex number c1 and c2 where c1=a+bi and c2=c+di, the addition operator '+' is defined as c1+c2=(a+c)+(b+d)i and the multiplication operator '⋅' is defined as c1c2=(acbd)+(ad+bc)i.

Implement required methods of class Complex such that representing a Complex object Complex(a, b) displays Complex(real=a, imaginary=b), printing it displays a + bi and we can directly add or multiply two complex objects.

class Complex: """Complex Number. >>> a = Complex(1, 2) >>> a Complex(real=1, imaginary=2) >>> print(a) 1 + 2i >>> b = Complex(-1, -2) >>> b Complex(real=-1, imaginary=-2) >>> print(b) -1 + -2i >>> print(a + b) 0 + 0i >>> print(a * b) 3 + -4i >>> print(a) 1 + 2i >>> print(b) # a and b should not be changed -1 + -2i """ def __init__(self, real, imaginary): self.real = real self.imaginary = imaginary "*** YOUR CODE HERE ***"

You may find Python string formatting syntax or f-strings useful. A quick example:

>>> ten, twenty, thirty = 10, 'twenty', [30] >>> '{0} plus {1} is {2}'.format(ten, twenty, thirty) '10 plus twenty is [30]' >>> feeling = 'love' >>> course = 61 >>> f'I {feeling} {course}A!' 'I love 61A!'