Question

In: Computer Science

Write the class Complex that supports the basic complex number operations. Such operations are addition (+),...

Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-) and multiplication (*) of complex numbers, and multiplication (*) of a complex by a scalar (float or int). All methods must return (not print) the result. Class also supports the rich comparison for equality (= =)

  • - Check the doctest for object behavior examples.

  • - You must use the special methods for those 4 operators in order to override their behavior

  • - You will need other special methods to achieve a legible object representation.

  • - Multiplication by scalar and by another complex number use the same operator, so you must check the type of the object in order to decide which operation you have to perform

  • - Rich comparison returns a Boolean value

  • - The rest of the methods must return a new Complex object, this means the original

    objects should not change after the operation is performed.

  • - Test your code, this is how you ensure you get the most credit out of your work!!

  • - When returning error messages, make sure your string contains the word ‘error’

  • - You are not allowed to modify the constructor or any given code.

  • - Hint: Section 3.3.1. Basic customization (__eq__) and Full Section 3.3.8. Emulating numeric types inhttps://docs.python.org/3/reference/datamodel.html#emulating-numeric-types

    Write the property method conjugate into the Complex class. The complex conjugate of the complex number z = x + yi is given by x – yi

Remember, you are not allowed to modify the given constructor. The method conjugate must be a property method, otherwise, no credit will be given. Property methods are discussed in the video lectures!

Starting Code:

class Complex:
    '''        
        >>> a=Complex(5,-6)        
        >>> b=Complex(2,14)        
        >>> a+b        
        (7, 8i)        
        >>> a-b        
        (3, -20i)        
        >>> a*b        
        (94, 58i)        
        >>> b*5        
        (10, 70i)        
        >>> 5*b        
        (10, 70i)        
        >>> print(a)        
        5-6i        
        >>> print(b)        
        2+14i       
        >>> b        
        (2, 14i)        
        >>> isinstance(a+b, Complex)
        True
        >>> a.conjugate
        (5, 6i)
        >>> b==Complex(2,14)
        True
        >>> a==b
        False
    '''

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

Solutions

Expert Solution

class Complex:
   def __init__(self,real,imag):
       self.real = real
       self.imag = imag
  
   def __add__(self,two):
       return Complex(self.real + two.real, self.imag + two.imag)
  
   def __sub__(self,two):
       return Complex(self.real - two.real, self.imag - two.imag)
  
   def __rmul__(self,two):
       if(type(two) == int or type(two) == float):
           return Complex(self.real * two,self.imag*two)
   def __mul__(self,two):
       if(type(two) == Complex):
           res = Complex(0,0)
           res.real = (self.real * two.real) - (self.imag * two.imag)
           res.imag = (self.real * two.imag) + (self.imag * two.real)
           return res
       else:
           return Complex(self.real * two,self.imag*two)
   def conjugate(self):
       return Complex(self.real,-(self.imag))
  
   def __eq__(self,two):
       if(type(two) == Complex):
           return self.real == two.real and self.imag == two.imag
       else:
           return false
   def __str__(self):
       return "({} , {}i)".format(self.real,self.imag)
a = Complex(5,-6)
b = Complex(2,14)

print(a+b)
print(a-b)
print(a*b)
print(b*5)
print(5*b)

print(a)
print(b)

print(isinstance(a+b,Complex))
print(a.conjugate())

print(b == Complex(2,14))

print(a == b)


Related Solutions

Long Integer Addition For this assignment you are to write a class that supports the addition...
Long Integer Addition For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Write minimal code to declare a class Complex that enables operations on complex numbers, and also...
Write minimal code to declare a class Complex that enables operations on complex numbers, and also to overload the multiplication * operator.
ASAP (Math: The Complex class) A complex number is a number in the form a +...
ASAP (Math: The Complex class) 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 +...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
public class Question4 { /* For this exercise you will be doing some basic operations to...
public class Question4 { /* For this exercise you will be doing some basic operations to write to a file. You will be creating your very own Secrets of the Universe (TM)    You can use the PrintWriter class to write to files (Chapter 11.4)    Your code should perform the following actions:    1) Open/Create a file for writing at the following location: "files/question4/MySecretsOfTheUniverse.txt" * If the file already exists you will want to overwrite its contents. 2) Add...
complex number
Find the algebraic form of the following complex number \( (1+i\sqrt{3}) ^{2000} \)
complex number
write polar, carnation,argument, angele , ,and rectangular form of thi Complex number (1+3i)(3+4i)(-5+3i)
Complex number
What are the values of all the cube roots (Z = -4^3 - 4i)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT