【问题标题】:Division of complex numbers in Python 3 classPython 3类中的复数除法
【发布时间】:2019-10-11 23:57:07
【问题描述】:

我在 Python 类中为复数创建 __div__ 方法时遇到了麻烦,该复数应该除以两个复数。

这是我的代码:

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

        def __str__(self):
            if self.imag > 0:
                return str(self.real) + "+" + str(self.imag) + "i"
            elif self.imag < 0:
                return str(self.real) + str(self.imag) + "i"

        def __div__(self, other):
            x = self.real * other.real + self.imag * other.imag
            y = self.imag * other.real - self.real * other.imag
            z = other.real**2 + other.imag**2
            real = x / z
            imag = y / z
            return Complex(real, imag)

no = Complex(2,-8)
no2 = Complex(3,7)
print(no/no2)

不幸的是,我的方法不起作用。有什么建议吗?

【问题讨论】:

  • 为什么不用Python内置的复杂类型呢?
  • @JohnO 因为我必须使用方法:)
  • 观察 div-by-0
  • @Eve 你在计算结果的实部和虚部时犯了错误,它应该是x = self.real * other.real - self.imag * other.imagy = self.imag * other.real + self.real * other.imag

标签: python python-3.x class oop methods


【解决方案1】:

这是__truediv__,而不是__div____div__ 是旧的 Python 2“整数 floordiv,非整数 truediv”除法的名称。

在修复问题时,您可能应该在 __str__ 中添加一个 else 案例。

【讨论】:

    【解决方案2】:

    __div__ 在 Python 3 中不再存在。它已被 __truediv__ 代替 / 和 __floordiv__ 代替 //

    看看 https://docs.python.org/3/reference/datamodel.html

    【讨论】:

      【解决方案3】:

      您需要创建一个方法 __div__(self, other) 来划分 no,还需要创建一个新方法 __opposite__(self) 在乘法时更改符号, 也调用方法如何定义即no/no1不是神方法

      使用@johnO 解决方案,覆盖__truediv__ 所以OP可以使用no/no2来除两个复数。

      见下面的代码

      类复杂(对象): def init(self, real = 0, imag = 0): self.real = 真实的 self.imag = 图像

          def __str__(self):
              if self.imag > 0:
                  return str(self.real) + "+" + str(self.imag) + "i"
              elif self.imag < 0:
                  return str(self.real) + str(self.imag) + "i"
          def __opposite__(self):
              self.real =self.real
              self.imag = self. imag if self.imag<0 else self.imag * -1
      
      
          def __truediv__(self, other):
              other.__opposite__()
      
      
      
              x = self.real * other.real - self.imag * other.imag
              y = self.imag * other.real + self.real * other.imag
              z = other.real**2 + other.imag**2
              self.new_real = x / z
              self.new_imag = y / z
              if self.new_imag>0:
                  result = "{} + {}i".format(self.new_real, self.new_imag)
              else:
                  result = "{} {}i".format(self.new_real, self.new_imag)
              return result
      
      no = Complex(4,5)
      no2 = Complex(2,6)
      print(no/no2)
      

      输出

      0.24 + 0.68i
      

      【讨论】:

      • @Eve 也许这就是你想要做的
      • 你的意思是__invert__而不是__opposite__
      • @NChauhan 我忘了怎么做复杂的无除法,所以go thorugh this 和可信的运算计算,__opposite__ 只是我给的一个函数名,它可以是任何东西
      • 在这种情况下,不建议使用双前导和尾随下划线名称__xxx__。它们如有更改,恕不另行通知。您应该将其重命名为 opposite
      • @NChauhan 这只是意味着这是私有函数,它将在内部使用,不需要用作外部函数,正如你所说,我可以将 __div__ 更改为 @ 987654334@ 但将 __opposite__ 更改为 opposite 违反了 python 标准
      猜你喜欢
      • 1970-01-01
      • 2017-07-13
      • 2018-01-06
      • 2019-06-20
      • 1970-01-01
      • 2018-09-12
      • 2018-07-31
      • 1970-01-01
      • 2018-08-17
      相关资源
      最近更新 更多