Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. # Assignment 3
  2. # Name: Jacob Khoury
  3. # Class: CSCI 4332
  4. # Date: October 17, 2018
  5.  
  6. class Complex():
  7.  
  8. #constructor method to accept real and imaginary attributes
  9. def __init__(self, a, b):
  10. self.a = a
  11. self.b = b
  12.  
  13. #function to return properly formatted string with complex number
  14. def __str__(self):
  15. #case in which b is positive and both numbers are not 0
  16. if self.a != 0 and self.b != 0 and self.b > 0:
  17. return '%s + %si' % (self.a, self.b)
  18. #case in which b is negative and both numbers are not 0
  19. elif self.a != 0 and self.b != 0 and self.b < 0:
  20. return '%s - %si' % (self.a, self.b*(-1))
  21. #case in which b is 0
  22. elif self.a != 0 and self.b == 0:
  23. return '%s' % self.a
  24. #case in which a is 0
  25. elif self.a == 0 and self.b != 0:
  26. return '%si' % self.b
  27. #case in which both a and b are 0
  28. else:
  29. return ''
  30.  
  31. #function to show a representation of the complex object and its attributes
  32. def __repr__(self):
  33. return 'Complex(%s, %s)' % (self.a, self.b)
  34.  
  35. #function to compute the conjugate of the complex number
  36. def conjugate(self):
  37. if self.b > 0:
  38. return '%s - %si' % (self.a, self.b)
  39. elif self.b < 0:
  40. return '%s + %si' % (self.a, self.b*(-1))
  41.  
  42. #addition function that checks if 'other' is a Complex number and adds appropriately
  43. def __add__(self, other):
  44. if isinstance(other, Complex):
  45. return Complex(self.a + other.a, self.b + other.b)
  46. else:
  47. return Complex(self.a + other, self.b)
  48.  
  49. #reverse operand function for addition
  50. #material reference for reverse operand methods: https://docs.python.org/2.0/ref/numeric-types.html
  51. def __radd__(self, other):
  52. return Complex(self.a + other, self.b)
  53.  
  54. #subtraction function that checks if 'other' is a Complex number and subtracts appropriately
  55. def __sub__(self, other):
  56. if isinstance(other, Complex):
  57. return Complex(self.a - other.a, self.b - other.b)
  58. else:
  59. return Complex(self.a - other, self.b)
  60. #reverse operand function for subtraction
  61. def __rsub__(self, other):
  62. return Complex(self.a - other, self.b*(-1))
  63.  
  64. #multiplication function that checks if 'other' is a Complex number and multiplies appropriately
  65. def __mul__(self, other):
  66. if isinstance(other, Complex):
  67. return Complex(self.a*other.a - self.b*other.b, self.b*other.a + self.a*other.b)
  68. else:
  69. return Complex(self.a * other, self.b)
  70.  
  71. #reverse operand function for multiplication
  72. def __rmul__(self, other):
  73. return Complex(self.a * other, self.b)
  74.  
  75. #division function that checks if 'other' is a Complex number and divides appropriately
  76. def __truediv__(self, other):
  77. if isinstance(other, Complex):
  78. #Complex number division carried out by multiplying
  79. #the num and denom by the conjugate of the denom and simplifying
  80. conjugate = Complex(other.a, other.b*(-1))
  81. newTop = self * conjugate
  82. newBottom = other * conjugate
  83. denominator = newBottom.a
  84. return Complex(newTop.a/denominator, newTop.b/denominator)
  85. else:
  86. return Complex(self.a / other, self.b)
  87.  
  88. #reverse operand function for division
  89. def __rtruediv__(self, other):
  90. return Complex(self.a / other, self.b)
  91.  
  92. if __name__ == "__main__":
  93. #print(Complex(2, -2))
  94. #print(Complex(2, 2))
  95. #print(Complex(2, 0))
  96. #print(Complex(0, 2))
  97. #print(Complex(0, 0))
  98.  
  99. #a = Complex(200, 40)
  100. #b = Complex(50, 40)
  101.  
  102. #c = Complex(2, 4)
  103. #d = Complex(4, 2)
  104.  
  105. #print(repr(a))
  106.  
  107. #print(a.conjugate())
  108. #print(a + b)
  109. #print(a - b)
  110. #print(c * d)
  111. #print(c / d)
  112.  
  113. #print(Complex(2, 4) - 2)
  114. #print(2 - Complex(2, 4))
  115. #print(2 * Complex(2, 4))
  116. #print(2 / Complex(2, 4))
  117. #print(Complex(2, 4) / 2)
  118.  
  119. #print(4.3 - Complex(2.2, -5.1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement