Question

A complex number is a number of the form a + bi, where a and b...

A complex number is a number of the form a + bi, where a and b are real numbers √

and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number.

The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows:

(a+bi)+(c+di) = (a+c)+(b+d)i

(a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i

(a + bi)/(c + di) = (ac + bd) + (bc − ad)i (c2 + d2) (c2 + d2)

The absolute value for a complex number is given by: |a+bi|= a2 +b2

A complex number a + bi can be interpreted as a point on a plane by identifying the (a, b) values as the coordinates of a point in two dimensions. The absolute value of the complex number corresponds to the distance of the point to the origin (0, 0).

Python has the complex class for performing complex number arithmetic. Here, you will design and implement your own class to represent complex numbers and do operations on complex numbers.

In a file called complex.py, create a class named Complex for representing complex numbers, including overriding special methods for addition ( add ), subtraction ( sub ), multiplication ( mul ), division ( truediv ), and absolute value ( abs ). Also include a str method that returns a string representation for a complex number a + bi as “(a+bi)”. If b is 0, str must return a.

Provide a constructor for the Complex class that has two optional parameters, say a and b, to create the complex number a + bi; the default values for a and b is 0. Also provide the accessor methods getRealPart() and getImaginaryPart() for returning the real and imaginary parts of the complex number, respectively.

Write a main function to test your Complex class by prompting the user to enter the real and imaginary parts of two complex numbers and displaying the results of calling each method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Complex:

        # Constructor
        def __init__(self, re=0.0, im=0.0):
                self._re = re
                self._im = im

        # Return the real part of self.
        def getRealPart(self):
                return self._re

        # Return the imaginary part of self.
        def getImaginaryPart(self):
                return self._im

        # Return the conjugate of self.
        def conjugate(self):
                return Complex(self._re, -self._im)

        def __add__(self, other):
                if type(other) == int or type(other) == float:
                        return Complex(self._re + other, self._im)

                re = self._re + other._re
                im = self._im + other._im
                return Complex(re, im)

        def __radd__(self, other):
                return Complex(self._re + other, self._im)

        def __sub__(self, other):
                if type(other) == int or type(other) == float:
                        return Complex(self._re - other, self._im)

                re = self._re - other._re
                im = self._im - other._im
                return Complex(re, im)

        def __rsub__(self, other):
                return Complex(-self._re + other, -1 * self._im)

        def __mul__(self, other):
                if type(other) == int or type(other) == float:
                        return Complex(self._re * other, self._im * other)

                re = self._re * other._re - self._im * other._im
                im = self._re * other._im + self._im * other._re
                return Complex(re, im)

        def __rmul__(self, other):
                return Complex(self._re * other, self._im * other)

        def __truediv__(self, other):
                if type(other) == int or type(other) == float:
                        return Complex(self._re / other, self._im / other)

                conjugate = other.conjugate()
                numerator = self * conjugate
                multiple = conjugate * other
                multiple = multiple._re

                numerator._re /= multiple
                numerator._im /= multiple
                
                return numerator

        def __rtruediv__(self, other):
                conjugate = self.conjugate()

                multiple = conjugate * self
                conjugate._re *= other
                conjugate._im *= other
                
                conjugate._re /= multiple._re
                conjugate._im /= multiple._re
                return conjugate


        # Return a string representation of self.
        def __str__(self):
                if self._re == 0:
                        return str(self._im) + 'i'
                if self._im == 0:
                        return str(self._im)
                if self._im < 0:
                        return  str(self._re) + ' - ' + str(abs(self._im)) + 'i'

                return str(self._re) + ' + ' + str(self._im) + 'i'

        def __repr__(self):
                return 'Complex(' + str(self._re) + ', ' + str(self._im) + ')'

        
a = int(input('Enter real part of first number: '))
b = int(input('Enter imaginary part of first number: '))
c1 = Complex(a, b)
a = int(input('Enter real part of second number: '))
b = int(input('Enter imaginary part of second number: '))
c2 = Complex(a, b)

print(c1 + c2)
print(c1 - c2)
print(c1 * c2)
print(c1 / c2)
print(c1 - 2)
print(2 - c2)


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
A complex number is a number of the form a + bi, where a and b...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING A complex number is a number in the form a + bi, where a...

    JAVA PROGRAMMING A complex number is a number in the form a + bi, where a and b are real numbers and i is V-1. The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + di a + bi - (c +...

  • 2. A complex number can be expressed as a + bi where a and b are...

    2. A complex number can be expressed as a + bi where a and b are real numbers and i is the imaginary unit. The multiplication of two complex numbers is defined as follows: (a+bi)(c+di) = (ac-bd) + (bc+ad)i Define a class which represents a complex number. The only member functions you have to define and implement are those which overload the * and *= symbols.

  • A complex number is a number in the form a + bi, where a and b...

    A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c + di)...

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • For the complex number given as: z = a + bi / c+di where i =...

    For the complex number given as: z = a + bi / c+di where i = √−1 is the imaginary unit. The parameters are defined as a = √2, b = 0, c = 0.5 and d = −0.5. (a) Find the real and the imaginary parts of z, and then draw the Argand dia- gram. (Hint: Use the conjugate of the denominator.) 2.5 (b) Based on the Argand diagram, find the distance r of the complex number z from...

  • Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form...

    Assignment 5 Class For Basic Complex Number Operations 1. A complex number is of the form a + ib. Let x = a +ib and y = c +id. The operation of adding two complex numbers is: x + y = (a + c)+i(b+d). The operation of subtracting two complex number is: x-y = (a -c)+i(b-d). The operation of multiplying two complex number is: xxy = (ac - bd) +i(bc + ad). The operation of dividing two complex number is:...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT