python中如何使用复数

2025-10-20 08:23:31

1、数学中复数有a+bi表示,python中复数是由一个实数和一个虚数组合构成,表示为:x+yj

一个复数有一对有序浮点数 (x,y),其中 x 是实数部分,y 是虚数部分。

python中如何使用复数

2、我们可以通过help(a)命令来查看复数的帮助文档。

Help on complex object:

class complex(object)

 |  complex(real[, imag]) -> complex number

 |  

 |  Create a complex number from a real part and an optional imaginary part.

 |  This is equivalent to (real + imag*1j) where imag defaults to 0.

 |  

 |  Methods defined here:

python中如何使用复数

3、我们通过dir(a)命令,发现复数有这些属性。

['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']

python中如何使用复数

4、复数的第一个属性是模,也是绝对值abs(),这里abs(a)和a.__abs__()是等效的。我们对a取模,没有改变a。 

python中如何使用复数

5、同样复数的__add__()属性也不会改变a的值,a.__add__(x)会返回a和x的和。注意x为一个单位的虚值时不能写成j(这样j就是一个变量),而要写成1j。

python中如何使用复数

6、复数的内建属性:

复数对象拥有数据属性,分别为该复数的实部和虚部。

复数还拥有 conjugate 方法,调用它可以返回该复数的共轭复数对象。

复数属性:real(复数的实部)、imag(复数的虚部)、conjugate()(返回复数的共轭复数)

 复数还有很多其它内部属性,我们以后慢慢学习。

python中如何使用复数

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢