复数的极坐标表示为: z = r [ c o s ( φ ) + i s i n ( φ ) ] = r e i φ z=r[\mathrm{cos}(\varphi)+i\mathrm{sin}(\varphi)]=r\mathrm{e}^{i\varphi} z=r[cos(φ)+isin(φ)]=reiφ
复数开方法则(忽略多根):
z
1
n
=
(
r
e
i
φ
)
1
n
=
r
1
n
e
i
φ
n
z^{\frac{1}{n}}=(r\mathrm{e}^{i\varphi})^\frac{1}{n}=r^{\frac{1}{n}}\mathrm{e}^{i\frac{\varphi}{n}}
zn1=(reiφ)n1=rn1einφ
代码实现:
import numpy as np
def c_sqrt(x:complex)->complex: # 复数开平方
radii = np.abs(x)
angles = np.angle(x)
return radii/2 * np.exp(1j*angles/2)
def c_nsqrt(x:complex, n:int)->complex: # 复数开多次方
radii = np.abs(x)
angles = np.angle(x)
return radii/n * np.exp(1j*angles/n)
x = 1+2j # 待操作复数
print(c_sqrt(x))
print(c_nsqrt(x, 3)) # 开三次方
(0.9510565162951536+0.5877852522924731j)
(0.6951716309338446+0.2688716405654084j)