【发布时间】:2015-01-24 19:36:53
【问题描述】:
首先,我知道 scipy 中有复数的数据结构。我只是在关注一本计算物理教科书,这是其中一个问题。 到目前为止,这就是我所拥有的:
class complex:
def __init__(self,x,y):
self.re=x
self.im=y
def __add__(self, other):
return complex(self.re+other.re, self.im+other.im)
def __sub__(self, other):
return complex(self.re-other.re, self.im-other.im)
def __mul__(self, other):
return complex(self.re*other.re - self.im*other.im,
self.re*other.im + self.im*other.re)
def __repr__(self):
return '(%f , %f)' %(self.re, self.im)
但是我应该如何在我创建的类中实现除法、复共轭、模数和相位?
谢谢
【问题讨论】:
-
您知道数学上如何用复数执行这些运算吗?如果是这样,您在用 Python 实现它时遇到了什么问题?如果您的问题是如何执行这些操作,那是 Stack Overflow 的题外话。
-
我在处理复数方面没有问题。我只是不知道如何在 python 中实现它们。
-
你已经找到了基本的算术特殊方法;你的意思是你也想知道如何加入除法和模数运算符吗?从你的问题中不清楚你到底在这里挣扎什么。
-
它没有回答您的问题,但请注意,复数不需要 Scipy,它们是 Python 中的内置类型。 docs.python.org/2.7/reference/…
标签: python class methods data-structures complex-numbers