Reputation: 3571
I am trying to write a class that simulates a complex number in Python. Yes, I know there already exists a built-in function called complex()
which can be used to construct a complex number, and we can also use j
to create them. However I wanted to write my own just for fun. Here is a small version of it:
class Complex:
def __init__(self, real: float = 0, img: float = 0):
self.real = real
self.img = img
self.num = (self.real, self.img)
def __repr__(self) -> str:
string = "{real}".format(real=self.real) if self.real != 0 else ""
# Want to have "a + bi", "a", "bi"
# Want to avoid "+ bi" or "a + "
if self.real != 0 and self.img < 0:
string += " - " # img will already have -
elif self.real != 0 and self.img > 0:
string += " + "
string += "{img}i".format(img=abs(self.img)) if self.img != 0 else ""
return string
def __add__(self, other: 'Complex') -> 'Complex':
return Complex(self.real + other.real, self.img + other.img)
def __sub__(self, other: 'Complex') -> 'Complex':
return Complex(self.real - other.real, self.img - other.img)
def __matmul__(self, other: 'Complex') -> 'Complex':
# (ac - bd)
real = self.real * other.real - self.img * other.img
# (ad + bc)
img = self.real * other.img + self.img * other.real
return Complex(real, img)
def __mul__(self, other):
return Complex(self.real * other, self.img * other)
def __rmul__(self, other):
return self.__mul__(other)
The issue is that I cannot find a way of defining the following:
This is because when I multiply two class instances, it still calls __mul__
while it does not call __matmul__
. I think __matmul__
should only work with @
operator. How can I make all of this work without having to use another operator (@
) ? I would like to use the standard *
operator.
Upvotes: 2
Views: 1488
Reputation: 36722
In this case, you have to test for type and execute the proper arithmetic based on type.
class Complex:
def __init__(self, real: float = 0, img: float = 0):
self.real = real
self.img = img
self.num = (self.real, self.img)
def __repr__(self) -> str:
string = "{real}".format(real=self.real) if self.real != 0 else ""
# Want to have "a + bi", "a", "bi"
# Want to avoid "+ bi" or "a + "
if self.real != 0 and self.img < 0:
string += " - " # img will already have -
elif self.real != 0 and self.img > 0:
string += " + "
string += "{img}i".format(img=abs(self.img)) if self.img != 0 else ""
return string
def __add__(self, other):
if isinstance(other, Complex):
return Complex(self.real + other.real, self.img + other.img)
elif isinstance(other, int) or isinstance(other, float):
return Complex(self.real + other, self.img)
def __radd__(self, other):
return self + other
def __sub__(self, other):
if isinstance(other, Complex):
return Complex(self.real - other.real, self.img - other.img)
elif isinstance(other, int) or isinstance(other, float):
return Complex(self.real - other, self.img)
def __rsub__(self, other):
return -self + other
def __neg__(self):
return Complex(-self.real, -self.img)
def __mul__(self, other):
if isinstance(other, Complex):
real = self.real * other.real - self.img * other.img # (ac - bd)
img = self.real * other.img + self.img * other.real # (ad + bc)
return Complex(real, img)
elif isinstance(other, int) or isinstance(other, float):
return Complex(self.real * other, self.img * other)
def __rmul__(self, other):
return self * other
and a few tests:
a, b = Complex(1, 2), Complex(2, 1)
a + 2, 2 + a, a - 2, 2 - a, 2 * a, a * 2, a + b, b + a, a - b, b - a, a * b, b * a
output:
(3 + 2i,
3 + 2i,
-1 + 2i,
1 - 2i,
2 + 4i,
2 + 4i,
3 + 3i,
3 + 3i,
-1 + 1i,
1 - 1i,
5i,
5i)
In the same vein, you can implement __iadd__
(+=
), __isub__
(-=
), __imul__
(*=), paying attention to mutate the argument instead of returning a new object.
Upvotes: 2