python 复数归一化处理
归一化算法
复数格式为 x = a + b i x=a+bi x=a+bi, 归一化算法如下:
x ′ = a ÷ a 2 + b 2 + b i ÷ a 2 + b 2 x' = a\div\sqrt{a^2 + b^2} + bi\div\sqrt{a^2 + b^2} x′=a÷a2+b2+bi÷a2+b2
算法实现
from math import sqrt
def normalize(num):
# numpy里complex可改为numpy.complex128
if type(num) == complex:
real = num.real
imag = num.imag
temp = sqrt(real*real+imag*imag)
real = real/temp
imag = imag/temp * 1j
return real + imag
else:
return num
# 测试
d = 8 + 2j
print(normalize(d)) # print (0.9701425001453319+0.24253562503633297j)