简体   繁体   English

如何在Python中实现类实例与float和类实例之间的乘法

[英]How to implement multiplication between class instances and float and class instance in Python

I am trying to write a class that simulates a complex number in Python.我正在尝试编写一个在 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.是的,我知道已经有一个叫做complex()的内置函数可以用来构造一个复数,我们也可以使用j来创建它们。 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:问题是我找不到定义以下内容的方法:

  • Complex * Complex (left multiplication for class instances) Complex * Complex(类实例的左乘法)
  • Complex * Complex (right multiplication for class instances, but useless as the above will suffice)复杂 * 复杂(类实例的正确乘法,但没用,因为上面就足够了)
  • float * Complex (right scalar multiplication, but can be with int as well) float * Complex(右标量乘法,但也可以与 int 一起使用)
  • Complex * float (left scalar multiplication, but can be with int as well) Complex * float(左标量乘法,但也可以使用 int)

This is because when I multiply two class instances, it still calls __mul__ while it does not call __matmul__ .这是因为当我将两个类实例相乘时,它仍然调用__mul__而它不调用__matmul__ I think __matmul__ should only work with @ operator.我认为__matmul__应该只与@运算符一起使用。 How can I make all of this work without having to use another operator ( @ ) ?如何在不使用其他运算符 ( @ ) 的情况下完成所有这些工作? I would like to use the standard * operator.我想使用标准的*运算符。

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.同样,您可以实现__iadd__ ( += )、 __isub__ ( -= )、 __imul__ (*=),注意改变参数而不是返回一个新对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在两个 python class 实例之间执行求和并获取每个实例变量的长度列表 - How to perform sum between two python class instances and get the list of length of every instance variable 在同一个类的不同实例之间实现逻辑 - Implement logic between different instances of the same class 区分python中类的实例 - Differentiating between instances of a class in python 如何一次更改class的所有实例的实例变量,Python - How to change an instance variable of all instances of a class at once, Python 如何对返回相同类型的类实例的两个类实例(以float作为子类)执行算术运算? 例如Speed(2)+ Speed(2) - How to perform arithmetic on two class instances (with float as a subclass) which return a class instance of the same type? e.g. Speed(2) + Speed(2) 如何在类实例外使字典不可变但在python中的类实例内是可变的 - How to make dictionaries immutable outside class instances but mutable inside the class instance in python Python:如何使用 __add__ 特殊方法添加返回该 class 的另一个实例的相同 class 的实例 - Python: How to use __add__ special method to add instances of the same class that return another instance of that class 如何在 Python 中导入 Class 的实例? - How to import instances of a Class in Python? Python动态生成类实例之间的依赖关系 - Python dynamically generate dependencies between class instances 如何压缩Python类实例? - How to condense Python class instances?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM