|
复数数组的相关操作
- import numpy as np
- import cmath
- # 你唯一的解决办法就是使用复数空间
- a = complex(2, 4)
- b = 2 + 4j
- print(a, b)
- print(a == b)
- print("复数的实部={},虚部={},共轭复数={}".format(a.real, a.imag, a.conjugate()))
- c = 10 - 2j
- print("复数相加=", a + c)
- print("复数相乘=", a * c)
- print("复数与实数相乘=", 5 * a)
- print("复数相除=", a / c)
- print("复数的绝对值=", abs(a))
- print("复数的sin=", cmath.sin(a), np.sin(a))
- print("复数的cos=", cmath.cos(a))
- print("复数的exp=", cmath.exp(a))
- # numpy可以很容易的构造一个复数数组并在这个数组上执行各种操作
- aa = np.array([2+3j, 4+5j, 6-7j, 8+9j])
- print(np.sin(aa))
- print("复数数组=", type(aa), aa.dtype)
- # 如果你想生成一个复数返回结果,你必须显示的使用 cmath 模块
- print(cmath.sqrt(-1))
- real = np.linspace(1, 10, 10)
- imag = np.linspace(11, 20, 10)
- print(real, real.dtype)
- print(imag, imag.dtype)
- bb_temp = []
- for x, y in zip(real, imag):
- # print(x, y, complex(x, y))
- bb_temp.append(complex(x, y))
- # print(lambda zip(real, imag))
- bb = np.array(bb_temp)
- # bb = complex(real, imag)
- print(bb)
- bbb = real + 1j*imag
- print("bbb", bbb, type(bbb), bbb.dtype)
复制代码
|
|