python中的复数 [英] Complex numbers in python

查看:65
本文介绍了python中的复数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复数是python中支持的数据类型吗?如果是这样,您如何使用它们?

解决方案

在 python 中,你可以在数字后面加上 ‘j’ 或 ‘J’ 使其成为虚数,这样你就可以轻松地编写复杂的文字:

<预><代码>>>>1j1j>>>1J1j>>>1j * 1j(-1+0j)

j"后缀来自电气工程,其中变量i"通常用于表示电流.(此处找到推理.)

复数的类型是 complex,如果您愿意,可以将该类型用作构造函数:

<预><代码>>>>复杂(2,3)(2+3j)

一个复数有一些内置的访问器:

<预><代码>>>>z = 2+3j>>>z.real2.0>>>图像3.0>>>z.共轭()(2-3j)

一些内置函数支持复数:

<预><代码>>>>绝对(3 + 4j)5.0>>>战俘(3 + 4j,2)(-7+24j)

标准模块 cmath 有更多的函数来处理复数:

<预><代码>>>>导入 cmath>>>cmath.sin(2 + 3j)(9.15449914691143-4.168906959966565j)

Are complex numbers a supported data-type in python? If so, how do you use them?

解决方案

In python, you can put ‘j’ or ‘J’ after a number to make it imaginary, so you can write complex literals easily:

>>> 1j
1j
>>> 1J
1j
>>> 1j * 1j
(-1+0j)

The ‘j’ suffix comes from electrical engineering, where the variable ‘i’ is usually used for current. (Reasoning found here.)

The type of a complex number is complex, and you can use the type as a constructor if you prefer:

>>> complex(2,3)
(2+3j)

A complex number has some built-in accessors:

>>> z = 2+3j
>>> z.real
2.0
>>> z.imag
3.0
>>> z.conjugate()
(2-3j)

Several built-in functions support complex numbers:

>>> abs(3 + 4j)
5.0
>>> pow(3 + 4j, 2)
(-7+24j)

The standard module cmath has more functions that handle complex numbers:

>>> import cmath
>>> cmath.sin(2 + 3j)
(9.15449914691143-4.168906959966565j)

这篇关于python中的复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆