Python不仅可以处理实数,还可以使用文件“cmath”处理复数及其相关函数。复数在许多与数学有关的应用程序中都有其用途,python提供了处理和操作复数的有用工具。
实数到复数的转换
复数表示为“ x+yi “.Python使用函数将实数x和y转换为复数 复合物(x,y) .使用该功能可以访问真实部分 real() 虚部可以用 imag() .
# Python code to demonstrate the working of # complex(), real() and imag() # importing "cmath" for complex number operations import cmath # Initializing real numbers x = 5 y = 3 # converting x and y into complex number z = complex (x,y); # printing real and imaginary part of complex number print ( "The real part of complex number is : " ,end = "") print (z.real) print ( "The imaginary part of complex number is : " ,end = "") print (z.imag) |
输出:
The real part of complex number is : 5.0 The imaginary part of complex number is : 3.0
复数相位
从几何学上讲,复数的相位是 正实轴与表示复数的向量之间的角度 .这也被称为 论点 复数的。使用 阶段( ,它以复数为参数。相位范围从 -pi到+pi。 i、 来自 -3.14至+3.14 .
# Python code to demonstrate the working of # phase() # importing "cmath" for complex number operations import cmath # Initializing real numbers x = - 1.0 y = 0.0 # converting x and y into complex number z = complex (x,y); # printing phase of a complex number using phase() print ( "The phase of complex number is : " ,end = "") print (cmath.phase(z)) |
输出:
The phase of complex number is : 3.141592653589793
从极坐标形式转换为矩形形式,反之亦然
转换为极坐标是使用 极性() ,返回一个 配对(r,ph) 指 模数r 和阶段 角ph .模数可以使用 abs() 和相位使用 阶段( . 复数通过使用 rect(r,ph) 哪里 r是模量 和 ph是相角 .它返回一个数值等于 r*(math.cos(ph)+数学。sin(ph)*1j)
# Python code to demonstrate the working of # polar() and rect() # importing "cmath" for complex number operations import cmath import math # Initializing real numbers x = 1.0 y = 1.0 # converting x and y into complex number z = complex (x,y); # converting complex number into polar using polar() w = cmath.polar(z) # printing modulus and argument of polar complex number print ( "The modulus and argument of polar complex number is : " ,end = "") print (w) # converting complex number into rectangular using rect() w = cmath.rect( 1.4142135623730951 , 0.7853981633974483 ) # printing rectangular form of complex number print ( "The rectangular form of complex number is : " ,end = "") print (w) |
输出:
The modulus and argument of polar complex number is : (1.4142135623730951, 0.7853981633974483) The rectangular form of complex number is : (1.0000000000000002+1j)
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。