如何将复数放入numpy的数组中? [英] How to put complex into a numpy's array?
本文介绍了如何将复数放入numpy的数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>>> import numpy as np
>>> A = np.zeros((3,3))
>>> A[0,0] = 9
>>> A
array([[ 9., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
>>> A[0,1] = 1+2j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
>>> A[0,1] = np.complex(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
根据我的示例代码.我试图将复数放入numpy的数组中,但是没有用.可能是我想念一些基本的东西.
According to my example code. i tried to put complex number into the numpy's array but it didn't work. May be i miss some basic thing.
推荐答案
如果要创建包含复杂值的数组,则需要为numpy指定复杂类型:
If you want to create an array containing complex values, you need to specify a complex type to numpy:
>>> A = np.zeros((3,3), dtype=np.complex)
>>> print A
[[ 0.+0.j 0.+0.j 0.+0.j]
[ 0.+0.j 0.+0.j 0.+0.j]
[ 0.+0.j 0.+0.j 0.+0.j]]
>>> A[0,0] = 1. + 2.j
>>> print A
[[ 1.+2.j 0.+0.j 0.+0.j]
[ 0.+0.j 0.+0.j 0.+0.j]
[ 0.+0.j 0.+0.j 0.+0.j]]
这篇关于如何将复数放入numpy的数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文