Python:让一个类使用复数 [英] Python: Making a class to use complex numbers
问题描述
我已经开始了每种方法,我想我只需填补空白。在该方法中,我使用self.x作为占位符。我不确定那里有什么。
首先,程序需要创建它自己的复数。我是新来的方法和功能,我不知道我是否在正确的地方使用(自我)。
我很确定我有一些语法问题,即它一直说变量i没有被定义,当我清楚地在多个地方定义它时。
完成所有这些工作后,我需要创建一个实际使用代码的测试函数。
任何帮助,将不胜感激。
到目前为止我的代码:
导入数学
i = math.sqrt(-1)
类Complex(object):
def __init __(Complex,a,b):
'''创建复杂数字'''
a = 0
b = 0
return(Complex,a,b)
$ b $ def __str __(Complex,a,b) :
'''以字符串形式返回复数'''
a = 0
b = 0
return str(a,b)
def __add__ (self):
'''添加复数'''
i = math.sqrt(-1)
self.x =(a + bi)+(c + di)=( a + c)+(b + d)i
def __sub __(self):
'''减去复数'''
self.x =(a + bi ) - (c + di)=(a - c)+(b - d)i
def __mul __(self):
'''复数'''
自我x =(a + bi)*(c + di)=(ac-bd)+(bc + ad)i
def __div __(self):
'''除以复数'''
self.x =(a + bi)/(c + di)=(ac + bd)/(c ** 2 + d ** 2)+(bc -ad)i /(c ** 2 + d ** 2)
def __abs __(self):
'''确定绝对值复数'''
self.x = math.sqrt(a ** 2 + b ** 2)
以下是您尝试执行的一个子集的简单实现:
导入数学
类复杂(对象):
def __init __(self,a,b):
'''创建复杂数字'''
self.a = a
self.b = b
def __str __(self):
'''以字符串形式返回复数 ''
return'(%s,%s)'%(self.a,self.b)
def __add __(self,rhs):
'''添加复数'''
复数(self.a + rhs.a,self.b + rhs.b)
i =复合(0,1)
分解方法更多用于说明:
def __add __(self,rhs) :
'''添加复数'''
new_a = self.a + rhs.a
new_b = self.b + rhs.b
复数(new_a,new_b )
或者,将它与您的原件直接关联:
def __add __(self,rhs):
'''添加复数'''
a = self.a
b = self。 b
c = rhs.a
d = rhs.b
new_real_part = a + c
new_imag_part = b + d
复数(new_real_part,new_imag_part)
如果您避免调用成员变量 a
和 b
,这是非常通用和毫无意义的,并且在处理智慧时可能会引起混淆h算法,其中对象的实部和虚部可以被称为 a
和 b
或 c
和 d
或其他各种各样的东西。
从这里拿出其余的钱?
请注意,您应该只需要 math
的唯一地方就在 __ abs __
。
如果您想了解一些原来错误的内容:
- 您根据math.sqrt(-1)定义了您的全局变量
i
,这引发了一个异常。唯一可能的方法是返回一个本地复数 - 因为你要做的是创建你自己的复杂类,而不是使用本地支持,这是毫无意义的。相反,您希望将i
定义为您的类的实例。 - 所有实例方法 - 包括
__ init__
- 应该将self
作为第一个参数。
- 访问实例变量的唯一方法 - 包括设置他们在
__ init __
- 是通过self参数。a = 0
只是创建一个新的局部变量,一旦函数结束,它就会消失;self.a = 0
修改对象的a
成员。
__ str __
方法不会接受任何额外的参数。__ add __ 方法不应该改变对象的位置,而是返回一个新的对象。如果您想在原地更改对象,那是 __ iadd __
。 (正如你可能猜到的那样,根据__ iadd __
定义__ add __
是很常见的)。对于接下来的三个方法。
__ add __
方法需要另一个参数来操作。否则,你在给自己添加什么?同样,接下来的几种方法也是如此。
- 您没有
x
成员,那么为什么要设置一个__添加__
? (当然这是合法的,并且会创建一个新的,但是你永远不会在任何地方使用它。)
- 你的算法似乎试图创建一个本地的复杂值。如果你想得到一个
Complex
,其实数部分是self
和<$ c $的实部的总和c> rhs 并且其虚数部分是虚部的总和,则必须从这两个和中构造Complex
。 (您没有办法从单个合并值中构建Complex
)。
- 您期望什么
self.x = ... = ...
要做什么?例如,你真的试图将(a + c)+(b + d)i
的值赋给表达式(a + bi )+(c + di)
?
- 并置并不意味着Python或大多数其他编程语言中的乘法。所以
bi
是一个完全独立的变量,而不是b
次i
,(b + d)i
是语法错误。
- 访问实例变量的唯一方法 - 包括设置他们在
I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare them to one another. I think I have gotten a good start but have some issues.
I have started each method, and I think I just need to fill in the gaps. In the method, I used self.x as a placeholder. I'm not really sure what goes there. First off, the program needs to create it's own complex numbers. I'm new to making methods and functions and I'm not sure if I used (self) in the correct places.
I'm pretty sure I have some syntax issues, i.e. It keeps saying that the variable "i" is not defined, when I clearly have it defined in multiple places.
Once all this is sorted out, I need to create a test function that actually uses the code.
Any help would be appreciated. Thanks!
My code so far:
import math
i = math.sqrt(-1)
class Complex(object):
def __init__(Complex, a, b):
'''Creates Complex Number'''
a = 0
b = 0
return(Complex, a, b)
def __str__(Complex, a, b):
'''Returns complex number as a string'''
a = 0
b = 0
return str(a, b)
def __add__(self):
'''Adds complex numbers'''
i = math.sqrt(-1)
self.x = (a + bi) + (c + di) = (a + c) + (b + d)i
def __sub__(self):
'''Subtracts complex numbers'''
self.x = (a + bi) - (c + di) = (a - c) + (b - d)i
def __mul__(self):
'''Multiplies complex numbers'''
self.x = (a + bi) * (c + di) = (ac - bd) + (bc + ad)i
def __div__(self):
'''Divides complex numbers'''
self.x = (a + bi) / (c + di) = (ac + bd)/(c**2 + d**2) + (bc - ad)i/(c**2 + d**2)
def __abs__(self):
'''Determines absolute value of complex numbers'''
self.x = math.sqrt(a**2 + b**2)
Here's a simple implementation of just a subset of what you're trying to do:
import math
class Complex(object):
def __init__(self, a, b):
'''Creates Complex Number'''
self.a = a
self.b = b
def __str__(self):
'''Returns complex number as a string'''
return '(%s, %s)' % (self.a, self.b)
def __add__(self, rhs):
'''Adds complex numbers'''
return Complex(self.a + rhs.a, self.b + rhs.b)
i = Complex(0, 1)
To break down the __add__
method a bit more for exposition:
def __add__(self, rhs):
'''Adds complex numbers'''
new_a = self.a + rhs.a
new_b = self.b + rhs.b
return Complex(new_a, new_b)
Or, relating it more directly to your original:
def __add__(self, rhs):
'''Adds complex numbers'''
a = self.a
b = self.b
c = rhs.a
d = rhs.b
new_real_part = a + c
new_imag_part = b + d
return Complex(new_real_part, new_imag_part)
This might all be simpler if you avoided calling your member variables a
and b
, which are pretty generic and meaningless, and can cause confusion when you're dealing with algorithms where the real and imaginary parts of an object might be referred to as a
and b
, or c
and d
, or various other things.
Can you figure out the rest from here?
Note that the only place you should need math
is inside __abs__
.
For a rundown of some of the things you got wrong in the original:
- You defined your global variable
i
in terms of math.sqrt(-1), which raises an exception. And the only way this could possibly work would be to return a native complex number—since the whole point of what you're trying to do is create your own complex class instead of using the native support, this would be pointless. Instead, you want to definei
as an instance of your class. - All instance methods—including
__init__
—should takeself
as the first parameter. - The only way to access instance variables—including setting them in
__init__
—is via the self parameter.a = 0
just creates a new local variable that goes away as soon as the function is over;self.a = 0
modifies thea
member of the object. - The
__str__
method doesn't take any extra parameters. - The
__add__
method shouldn't be changing the object in-place, but returning a new one. If you want to change the object in-place, that's__iadd__
. (It's pretty common to define__add__
in terms of__iadd__
, as you might guess.) And the same is true for the next three methods. - The
__add__
method has to take another parameter to operate on. Otherwise, what are you adding self to? And again, ditto for the next few methods. - You don't have an
x
member, so why are you setting one in__add__
? (Of course this is legal, and will create a new one, but you're never going to use it anywhere.) - Your algorithms seem to be trying to create a native complex value. If you want to get a
Complex
whose real part is the sum of the real parts ofself
andrhs
and whose imaginary part is the sum of the imaginary parts, you have to construct aComplex
out of those two sums. (You don't have any way to construct aComplex
out of a single combined value.) - What did you expect
self.x = … = …
to do? For example, are you really trying to assign the value of(a + c) + (b + d)i
to the expression(a + bi) + (c + di)
? - Juxtaposition doesn't mean multiplication in Python, or most other programming languages. So
bi
is an entirely separate variable, notb
timesi
, and(b + d)i
is a syntax error.
这篇关于Python:让一个类使用复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!