Python,处理复数的复杂类型(绝对值、偏角、极地变换等)。 | 您所在的位置:网站首页 › complex函数用法python › Python,处理复数的复杂类型(绝对值、偏角、极地变换等)。 |
Python 有一个处理复数的标准类型,即 COMPLEX 类型。如果你只想做简单的计算,你不需要导入任何模块,但如果你导入标准库cmath,你也可以使用与复数相对应的数学函数(指数、对数、三角等)。 cmath — Mathematical functions for complex numbers — Python 3.10.4 Documentation这里用示例代码解释以下内容。 生成复杂的变量获得实部和虚部:real,imag归属获取共轭复数:conjugate()方法获取绝对值(幅值):abs()功能(例如:数学、编程、程序设计)。获得倾角(相位):math,cmath模块极坐标转换(极坐标形式表示):math,cmath模块复数的计算(正交、幂、平方根)。生成复杂的变量获取复数的实部和虚部: real, imag归属获取共轭复数: conjugate()获得一个复数的绝对值(幅度)。: abs()获得一个复数的倾角(相位)。: math, cmath模块复数的极坐标转换(极地形式表示法): math, cmath模块复数的计算(正交、幂、平方根)。生成复杂的变量用j表示虚数单位,写出以下内容,注意它不是i。 c = 3 + 4j print(c) print(type(c)) # (3+4j) #如果虚部是1,省略它将导致NameError。如果一个名为j的变量首先被定义,那么它就被认为是该变量。 1j应该这样明文规定。 # c = 3 + j # NameError: name 'j' is not defined c = 3 + 1j print(c) # (3+1j)如果实数部分为0,可以省略。 c = 3j print(c) # 3j如果你想把一个虚部为0的值定义为复数复数类型,请明确写上0。如下所述,可以在复数类型和整数类型或浮点类型之间进行操作。 c = 3 + 0j print(c) # (3+0j)实部和虚部可以指定为浮点浮子类型。指数符号也是可以接受的。 c = 1.2e3 + 3j print(c) # (1200+3j)它也可以由一个 “复数 “类型的构造函数生成,如 “复数(实部,虚部)”。 c = complex(3, 4) print(c) print(type(c)) # (3+4j) # 获取复数的实部和虚部: real, imag归属复数复数类型的实部和虚部可以分别用real和imag属性获得。两者都是浮点浮子类型。 c = 3 + 4j print(c.real) print(type(c.real)) # 3.0 # print(c.imag) print(type(c.imag)) # 4.0 #它是只读的,不能被改变。 # c.real = 5.5 # AttributeError: readonly attribute 获取共轭复数: conjugate()要获得共轭复数,请使用 conjugate() 方法。 c = 3 + 4j print(c.conjugate()) # (3-4j) 获得一个复数的绝对值(幅度)。: abs()要获得一个复数的绝对值(幅度),可以使用内置函数abs()。 c = 3 + 4j print(abs(c)) # 5.0 c = 1 + 1j print(abs(c)) # 1.4142135623730951 获得一个复数的倾角(相位)。: math, cmath模块要获得一个复数的倾角(相位),请使用数学或cmath模块。 cmath模块是一个复数的数学函数模块。 cmath — Mathematical functions for complex numbers — Python 3.10.4 Documentation它可以用定义的反正切函数math.atan2()计算,或者使用cmath.phase(),它返回倾角(相位)。 import cmath import math c = 1 + 1j print(math.atan2(c.imag, c.real)) # 0.7853981633974483 print(cmath.phase(c)) # 0.7853981633974483 print(cmath.phase(c) == math.atan2(c.imag, c.real)) # True在这两种情况下,可以得到的角度单位是弧度。要转换为度,请使用math.degree()。 print(math.degrees(cmath.phase(c))) # 45.0 复数的极坐标转换(极地形式表示法): math, cmath模块如上所述,复数的绝对值(幅度)和倾角(相位)可以得到,但使用cmath.polar(),它们可以作为一个(绝对值,倾角)元组一起得到。 c = 1 + 1j print(cmath.polar(c)) print(type(cmath.polar(c))) # (1.4142135623730951, 0.7853981633974483) # print(cmath.polar(c)[0] == abs(c)) # True print(cmath.polar(c)[1] == cmath.phase(c)) # True从极坐标到笛卡尔坐标的转换是用cmath.rect()完成的。cmath.rect(绝对值,偏差)和类似的参数可以用来获得等效的复数复合型的值。 print(cmath.rect(1, 1)) # (0.5403023058681398+0.8414709848078965j) print(cmath.rect(1, 0)) # (1+0j) print(cmath.rect(cmath.polar(c)[0], cmath.polar(c)[1])) # (1.0000000000000002+1j)实部和虚部相当于余弦math.cos()和正弦math.sin()从绝对值和偏角计算出来的结果。 r = 2 ph = math.pi print(cmath.rect(r, ph).real == r * math.cos(ph)) # True print(cmath.rect(r, ph).imag == r * math.sin(ph)) # True 复数的计算(正交、幂、平方根)。可以使用通常的算术运算符进行四次算术运算和功率计算。 c1 = 3 + 4j c2 = 2 - 1j print(c1 + c2) # (5+3j) print(c1 - c2) # (1+5j) print(c1 * c2) # (10+5j) print(c1 / c2) # (0.4+2.2j) print(c1 ** 3) # (-117+44j)平方根可以用**0.5来计算,但会带来误差。可以用cmath.sqrt()来计算准确值。 print((-3 + 4j) ** 0.5) # (1.0000000000000002+2j) print((-1) ** 0.5) # (6.123233995736766e-17+1j) print(cmath.sqrt(-3 + 4j)) # (1+2j) print(cmath.sqrt(-1)) # 1j它还可以对复杂类型、int类型和float类型进行算术运算。 print(c1 + 3) # (6+4j) print(c1 * 0.5) # (1.5+2j) |
CopyRight 2018-2019 实验室设备网 版权所有 |