Search code examples
pythonpython-3.xclassmethodscomplex-numbers

implementation of complex numbers


I have this part of a code that I need to implement or modify the necessary methods in the class so that the program produces the following output.

c1 = 1+2i 
c2 = 3-4i 
c3 = -5 
c4 = 6i
c5 = -7i
c6 = 0
c1 + c2 = 4-2i
c1 - c2 = -2+6i
c1 * c2 = 11+2i 
conjugate of c1 = 1-2i

The implementation of the Complex class, representing complex number, is missing. The attributes of a complex number are, obviously, its real part and its imaginary part (floats) and do not need to be protected attributes. but I have no idea how to implement this in order to get the output above.

so far what I have:

code:

class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag

    def __str__(self):
        return str(self.real) + "+" + str(self.imag) + "i"


def main():
    c1 = Complex(1, 2)
    print("c1 =", c1)
    c2 = Complex(3, -4)
    print("c2 =", c2)
    c3 = Complex(-5, 0)
    print("c3 =", c3)
    c4 = Complex(0, 6)
    print("c4 =", c4)
    c5 = Complex(0, -7)
    print("c5 =", c5)
    c6 = Complex(0, 0)
    print("c6 =", c6)
    print("c1 + c2 =", c1 + c2)
    print("c1 - c2 =", c1 - c2)
    print("c1 * c2 =", c1 * c2)
    c7 = c1.conjugate()
    print("conjugate of c1 =", c7)

if __name__ == "__main__":
    main()

def main() cannot be changed

output:

c1 = 1+2i
c2 = 3+-4i
c3 = -5+0i
c4 = 0+6i
c5 = 0+-7i
c6 = 0+0i

Solution

  • You can use the builtin complex numbers for exactly that. Python like some other languages use j as the imaginary unit, in contrast to math where usually a lowercase i is used.

    def main():
        c1 = 1+2j
        print("c1 =", c1)
        c2 = 3-4j
        print("c2 =", c2)
        c3 = -5+0j
        print("c3 =", c3)
        c4 = 0+6j
        print("c4 =", c4)
        c5 = 0-7j
        print("c5 =", c5)
        c6 = 0+0j
        print("c6 =", c6)
        print("c1 + c2 =", c1 + c2)
        print("c1 - c2 =", c1 - c2)
        print("c1 * c2 =", c1 * c2)
        c7 = c1.conjugate()
        print("conjugate of c1 =", c7)
    
    if __name__ == "__main__":
        main()
    

    which gives the output:

    c1 = (1+2j)
    c2 = (3-4j)
    c3 = (-5+0j)
    c4 = 6j
    c5 = -7j
    c6 = 0j
    c1 + c2 = (4-2j)
    c1 - c2 = (-2+6j)
    c1 * c2 = (11+2j)
    conjugate of c1 = (1-2j)
    

    If you are fine with the additional parenthesis, you are done. Otherwise, you can do output formatting for example like this:

    print(f"conjugate of c7 = {c7.real}{c7.imag:+}j")
    

    EDIT: In case your main() method requires the use of the Complex class, you can just wrap the built-ins:

    class Complex(complex):
        pass
    

    This works because your Complex class has the same prototype as the builtin complex data type. You can even overload the __str__(self) as you proposed and it works whenever the str() is requested. So the Complex class could look like this:

    class Complex(complex):
        def __str__(self):
            return str(self.real) + "+" + str(self.imag) + "i"