| 1 | import math |
| 2 | |
| 3 | |
| 1 | 4 | class ComplexNumber: |
| 2 | 5 | def __init__(self, real, imaginary): |
| 3 | | pass |
| 6 | self.real = real |
| 7 | self.imaginary = imaginary |
| 4 | 8 | |
| 5 | 9 | def __eq__(self, other): |
| 6 | | pass |
| 10 | # Edge Case: Handle comparison with scalar numbers (int/float) |
| 11 | if isinstance(other, (int, float)): |
| 12 | return self.real == other and self.imaginary == 0 |
| 13 | # Edge Case: Standard comparison between two ComplexNumber instances |
| 14 | return self.real == other.real and self.imaginary == other.imaginary |
| 7 | 15 | |
| 8 | 16 | def __add__(self, other): |
| 9 | | pass |
| 17 | # Edge Case: Handle addition with scalar numbers (int/float) |
| 18 | if isinstance(other, (int, float)): |
| 19 | return ComplexNumber(self.real + other, self.imaginary) |
| 20 | # Edge Case: Standard addition between two ComplexNumber instances |
| 21 | return ComplexNumber(self.real + other.real, self.imaginary + other.imaginary) |
| 22 | |
| 23 | def __radd__(self, other): |
| 24 | # Edge Case: Handle right-side addition with scalar numbers (int/float) |
| 25 | return self.__add__(other) |
| 10 | 26 | |
| 11 | 27 | def __mul__(self, other): |
| 12 | | pass |
| 28 | # Edge Case: Handle multiplication with scalar numbers (int/float) |
| 29 | if isinstance(other, (int, float)): |
| 30 | return ComplexNumber(self.real * other, self.imaginary * other) |
| 31 | # Edge Case: Standard multiplication between two ComplexNumber instances |
| 32 | real_part = self.real * other.real - self.imaginary * other.imaginary |
| 33 | imaginary_part = self.imaginary * other.real + self.real * other.imaginary |
| 34 | return ComplexNumber(real_part, imaginary_part) |
| 35 | |
| 36 | def __rmul__(self, other): |
| 37 | # Edge Case: Handle right-side multiplication with scalar numbers (int/float) |
| 38 | return self.__mul__(other) |
| 13 | 39 | |
| 14 | 40 | def __sub__(self, other): |
| 15 | | pass |
| 41 | # Edge Case: Handle subtraction with scalar numbers (int/float) |
| 42 | if isinstance(other, (int, float)): |
| 43 | return ComplexNumber(self.real - other, self.imaginary) |
| 44 | # Edge Case: Standard subtraction between two ComplexNumber instances |
| 45 | return ComplexNumber(self.real - other.real, self.imaginary - other.imaginary) |
| 46 | |
| 47 | def __rsub__(self, other): |
| 48 | # Edge Case: Handle right-side subtraction with scalar numbers (int/float) |
| 49 | if isinstance(other, (int, float)): |
| 50 | return ComplexNumber(other - self.real, -self.imaginary) |
| 51 | # Edge Case: Standard right-side subtraction between two ComplexNumber instances |
| 52 | return ComplexNumber(other.real - self.real, other.imaginary - self.imaginary) |
| 16 | 53 | |
| 17 | 54 | def __truediv__(self, other): |
| 18 | | pass |
| 55 | # Edge Case: Handle division with scalar numbers (int/float) |
| 56 | if isinstance(other, (int, float)): |
| 57 | # Edge Case: Division by zero scalar |
| 58 | if other == 0: |
| 59 | raise ZeroDivisionError("division by zero") |
| 60 | return ComplexNumber(self.real / other, self.imaginary / other) |
| 61 | |
| 62 | # Edge Case: Division by zero complex number |
| 63 | denominator = other.real**2 + other.imaginary**2 |
| 64 | if denominator == 0: |
| 65 | raise ZeroDivisionError("division by zero complex number") |
| 66 | |
| 67 | # Edge Case: Standard division between two ComplexNumber instances |
| 68 | real_part = (self.real * other.real + self.imaginary * other.imaginary) / denominator |
| 69 | imaginary_part = (self.imaginary * other.real - self.real * other.imaginary) / denominator |
| 70 | return ComplexNumber(real_part, imaginary_part) |
| 71 | |
| 72 | def __rtruediv__(self, other): |
| 73 | # Edge Case: Handle right-side division with scalar numbers (int/float) |
| 74 | if isinstance(other, (int, float)): |
| 75 | # Edge Case: Division by zero complex number |
| 76 | denominator = self.real**2 + self.imaginary**2 |
| 77 | if denominator == 0: |
| 78 | raise ZeroDivisionError("division by zero complex number") |
| 79 | |
| 80 | # Edge Case: Standard right-side division |
| 81 | real_part = (other * self.real) / denominator |
| 82 | imaginary_part = (-other * self.imaginary) / denominator |
| 83 | return ComplexNumber(real_part, imaginary_part) |
| 19 | 84 | |
| 20 | 85 | def __abs__(self): |
| 21 | | pass |
| 86 | # Edge Case: Compute absolute value using math.hypot for better precision |
| 87 | return math.hypot(self.real, self.imaginary) |
| 22 | 88 | |
| 23 | 89 | def conjugate(self): |
| 24 | | pass |
| 90 | # Edge Case: Return conjugate with negated imaginary part |
| 91 | return ComplexNumber(self.real, -self.imaginary) |
| 25 | 92 | |
| 26 | 93 | def exp(self): |
| 27 | | pass |
| 94 | # Edge Case: Exponentiation using Euler's formula |
| 95 | exp_real = math.exp(self.real) |
| 96 | real_part = exp_real * math.cos(self.imaginary) |
| 97 | imaginary_part = exp_real * math.sin(self.imaginary) |
| 98 | return ComplexNumber(real_part, imaginary_part) |
| 99 | |
| 100 | # Handled Edge Cases: |
| 101 | # - Comparison with scalar numbers |
| 102 | # - Addition/subtraction/multiplication/division with scalar numbers |
| 103 | # - Right-side operations with scalar numbers |
| 104 | # - Division by zero (scalar and complex) |
| 105 | # - Better precision for absolute value using math.hypot |
| 106 | # - Proper conjugate calculation |
| 107 | # - Exponentiation using Euler's formula |