Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Assignment 3
- # Name: Jacob Khoury
- # Class: CSCI 4332
- # Date: October 17, 2018
- class Complex():
- #constructor method to accept real and imaginary attributes
- def __init__(self, a, b):
- self.a = a
- self.b = b
- #function to return properly formatted string with complex number
- def __str__(self):
- #case in which b is positive and both numbers are not 0
- if self.a != 0 and self.b != 0 and self.b > 0:
- return '%s + %si' % (self.a, self.b)
- #case in which b is negative and both numbers are not 0
- elif self.a != 0 and self.b != 0 and self.b < 0:
- return '%s - %si' % (self.a, self.b*(-1))
- #case in which b is 0
- elif self.a != 0 and self.b == 0:
- return '%s' % self.a
- #case in which a is 0
- elif self.a == 0 and self.b != 0:
- return '%si' % self.b
- #case in which both a and b are 0
- else:
- return ''
- #function to show a representation of the complex object and its attributes
- def __repr__(self):
- return 'Complex(%s, %s)' % (self.a, self.b)
- #function to compute the conjugate of the complex number
- def conjugate(self):
- if self.b > 0:
- return '%s - %si' % (self.a, self.b)
- elif self.b < 0:
- return '%s + %si' % (self.a, self.b*(-1))
- #addition function that checks if 'other' is a Complex number and adds appropriately
- def __add__(self, other):
- if isinstance(other, Complex):
- return Complex(self.a + other.a, self.b + other.b)
- else:
- return Complex(self.a + other, self.b)
- #reverse operand function for addition
- #material reference for reverse operand methods: https://docs.python.org/2.0/ref/numeric-types.html
- def __radd__(self, other):
- return Complex(self.a + other, self.b)
- #subtraction function that checks if 'other' is a Complex number and subtracts appropriately
- def __sub__(self, other):
- if isinstance(other, Complex):
- return Complex(self.a - other.a, self.b - other.b)
- else:
- return Complex(self.a - other, self.b)
- #reverse operand function for subtraction
- def __rsub__(self, other):
- return Complex(self.a - other, self.b*(-1))
- #multiplication function that checks if 'other' is a Complex number and multiplies appropriately
- def __mul__(self, other):
- if isinstance(other, Complex):
- return Complex(self.a*other.a - self.b*other.b, self.b*other.a + self.a*other.b)
- else:
- return Complex(self.a * other, self.b)
- #reverse operand function for multiplication
- def __rmul__(self, other):
- return Complex(self.a * other, self.b)
- #division function that checks if 'other' is a Complex number and divides appropriately
- def __truediv__(self, other):
- if isinstance(other, Complex):
- #Complex number division carried out by multiplying
- #the num and denom by the conjugate of the denom and simplifying
- conjugate = Complex(other.a, other.b*(-1))
- newTop = self * conjugate
- newBottom = other * conjugate
- denominator = newBottom.a
- return Complex(newTop.a/denominator, newTop.b/denominator)
- else:
- return Complex(self.a / other, self.b)
- #reverse operand function for division
- def __rtruediv__(self, other):
- return Complex(self.a / other, self.b)
- if __name__ == "__main__":
- #print(Complex(2, -2))
- #print(Complex(2, 2))
- #print(Complex(2, 0))
- #print(Complex(0, 2))
- #print(Complex(0, 0))
- #a = Complex(200, 40)
- #b = Complex(50, 40)
- #c = Complex(2, 4)
- #d = Complex(4, 2)
- #print(repr(a))
- #print(a.conjugate())
- #print(a + b)
- #print(a - b)
- #print(c * d)
- #print(c / d)
- #print(Complex(2, 4) - 2)
- #print(2 - Complex(2, 4))
- #print(2 * Complex(2, 4))
- #print(2 / Complex(2, 4))
- #print(Complex(2, 4) / 2)
- #print(4.3 - Complex(2.2, -5.1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement