Issue
>>> 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
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.
Solution
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]]
Answered By - talonmies
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.