Files
2025-09-10 16:12:45 +08:00

34 lines
968 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

if __name__ == '__main__':
# 在python中我们使用complex函数或者在浮点数后面加j的方式来定义一个复数
a = complex(2,4)
b = 2 + 4j
print(a, b)
# 我们可以提取复数的实部、虚部和共轭复数
print(a.real)
print(a.imag)
print(a.conjugate())
# 同样的,复数可以进行所有常见的算术操作
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(abs(a))
# 但如果需要对复数求正弦余弦或平方根这种操作,请:
import cmath
cmath.sin(a)
cmath.cos(a)
cmath.sqrt(a)
# 使用numpy可以生成复数数组并对它进行操作
import numpy as np
complex_array = np.array([1+2j, 2+3j, 3+4j])
print(complex_array + 2)
print(np.sin(complex_array))
# 在标准的math库中所有的操作都不会产生复数结果如果想要产出复数结果请使用支持复数感知的库比如cmath