【问题标题】:Python - How can I divide a number by a complex number, implementing my own class for complex numbers?Python - 如何将一个数字除以一个复数,实现我自己的复数类?
【发布时间】:2020-03-06 21:25:03
【问题描述】:

我目前正在为 here 等复数实现自己的类。多亏了这篇文章,我设法做到了两个复数之间的划分。

但我仍然遇到一个问题:我知道4 / (42i + 4)(42i + 4) / 4 不一样,但就我而言,我会得到相同的结果。这是因为在这种情况下程序会将数字单独作为整数,并且它无法将整数除以我的 Complex 对象。

我知道我需要创建一个 __rdiv__ 方法来处理这个问题。 我已经为其他基本操作(__add__、__sub__、__mul__)这样做了:

class Complex(object):
    def __init__(self, real=0, imag=0):
        self.real = real
        self.imag = imag

    def __add__(self, other):
        return Complex(self.real + other.real,
                       self.imag + other.imag)

    def __radd__(self, other):
        return self.__add__(other) 

    def __sub__(self, other):
        print(self, other)
        return Complex(self.real - other.real,
                       self.imag - other.imag)

    def __rsub__(self, other):
        return self.__sub__(other)

    def __mul__(self, other):
        # print(self, other)
        return Complex(self.real*other.real - self.imag*other.imag,
                       self.imag*other.real + self.real*other.imag)

    def __rmul__(self, other):
        return self.__mul__(other)

但是我不能对除法使用相同的逻辑。也许数学应该有所不同,但我无法理解它。有人可以帮我吗?非常感谢

这是我的 __div__ :

def __div__(self, other):
    conjugation = Complex(other.real, -other.imag)
    denominatorRes = other * conjugation
    denominator = float(denominatorRes.real)
    nominator = self * conjugation
    try:
        return Complex(nominator.real/denominator, nominator.imag/denominator)
    except ZeroDivisionError as e:
        print e
        return None

【问题讨论】:

    标签: python class methods division complex-numbers


    【解决方案1】:
    def __truediv__(self, other):
        return Complex((self.real * other.real + self.imag * other.imag) / (other.real ** 2 + other.imag ** 2), (self.imag * other.real - self.real * other.imag) / (other.real ** 2 + other.imag ** 2))
    

    来源:wolfram mathworld

    【讨论】:

    • 嗨,不幸的是我遇到了同样的问题:TypeError: unsupported operand type(s) for /: 'int' and 'Complex'
    • 所以我的问题实际上出在 _div_ 上,但也出现在 _sub_ 上,当我使用 FIRST 时:一个 int/float 和 SECOND :一个 Complex .仅在 _rdiv_ 和 _sub_ 上,因为顺序很重要:x - y ≠ y - xx / y ≠ y / x 而 _add_ 和_mul_ 因为:x + y = y + xx * y = y * x
    • 你的减法实现是正确的,我的除法实现只要两个输入都是复杂的(都是复杂类型)
    • 不幸的是,这不是我需要的。我需要在实数和复数之间进行一些计算。不过还是谢谢你的帮助。
    【解决方案2】:

    完成!

    问题不在于数学,而在于我的逻辑。 这是我的代码:

    class Complex(object):
        def __init__(self, real=0, imag=0):
            self.real = real
            self.imag = imag
    
        def __add__(self, other):
            # if isinstance(other, (float,int)):
            #   other = Complex(other)
            return Complex(self.real + other.real,
                           self.imag + other.imag)
    
        def __radd__(self, other):
            return self.__add__(other) 
    
        def __mul__(self, other):
            # if isinstance(other, (float,int)):
            #   other = Complex(other)
            return Complex(self.real*other.real - self.imag*other.imag,
                           self.imag*other.real + self.real*other.imag)
    
        def __rmul__(self, other):
            return self.__mul__(other)
    
        def __sub__(self, other):
            if isinstance(other, (float,int)):
                other = Complex(other)
            return Complex(self.real - other.real,
                           self.imag - other.imag)
    
        def __rsub__(self, other):
            if isinstance(other, (float,int)):
                other = Complex(other)
            return other.__sub__(self)
    
        def __div__(self, other):
            if isinstance(other, (float,int)):
                other = Complex(other)
            s1, s2, o1, o2 = self.real, self.imag, other.real, other.imag
            r = float(o1**2 + o2**2)
            try: 
                return Complex((s1*o1+s2*o2)/r, (s2*o1-s1*o2)/r)
            except ZeroDivisionError as e:
                print e
                return None
    
        def __rdiv__(self, other):
            if isinstance(other, (float,int)):
                other = Complex(other)
            return other.__div__(other)
        ```
    

    【讨论】:

      猜你喜欢
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2017-07-10
      • 2016-02-26
      相关资源
      最近更新 更多