给numpy数组赋值?

时间:2022-12-06 21:29:09

This gives the expected result

这给出了预期的结果。

x = random.rand(1) + random.rand(1)*1j
print x.dtype
print x, x.real, x.imag

and this works

这工作

C = zeros((2,2),dtype=complex)
C[0,0] = 1+1j
print C

but if we change it to

但是如果我们把它改成

C[0,0] = 1+1j + x

I get "TypeError: can't convert complex to float".

我得到"TypeError: can't convert complex to float"

If we now omit the explicit dtype = complex, I get "ValueError: setting an array element with a sequence".

如果我们现在省略了显式的dtype = complex,我就会得到“ValueError:用序列设置数组元素”。

Can someone explain what's going on, and how to do this without errors? I'm lost.

有人能解释一下发生了什么,以及如何在没有错误的情况下做到这一点吗?我迷路了。

2 个解决方案

#1


5  

To insert complex x or x + something into C, you apparently need to treat it as if it were an array, so either index into x or assign it to a slice of C:

要将复杂的x或x +某项插入C中,显然需要把它当作一个数组来处理,因此可以将它作为x的索引,也可以将它赋给C的切片:

>>> C
array([[ 0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])
>>> C[0, 0:1] = x
>>> C
array([[ 0.47229555+0.7957525j,  0.00000000+0.j       ],
       [ 0.00000000+0.j       ,  0.00000000+0.j       ]])
>>> C[1, 1] = x[0] + 1+1j
>>> C
array([[ 0.47229555+0.7957525j,  0.00000000+0.j       ],
       [ 0.00000000+0.j       ,  1.47229555+1.7957525j]])

It looks like NumPy isn't handling this case correctly. Consider submitting a bug report.

看起来NumPy没有正确地处理这个问题。考虑提交错误报告。

#2


14  

Actually, none of the proposed solutions worked in my case (Python 2.7.6, NumPy 1.8.2). But I've found out, that change of dtype from complex (standard Python library) to numpy.complex_ may help:

实际上,在我的例子中,所提出的解决方案都不起作用(Python 2.7.6, NumPy 1.8.2)。但是我已经发现,dtype从复杂的(标准的Python库)变为numpy.complex_可能会有帮助:

>>> import numpy as np
>>> x = 1 + 2 * 1j
>>> C = np.zeros((2,2),dtype=np.complex_)
>>> C
array([[ 0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])
>>> C[0,0] = 1+1j + x
>>> C
array([[ 2.+3.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])

#1


5  

To insert complex x or x + something into C, you apparently need to treat it as if it were an array, so either index into x or assign it to a slice of C:

要将复杂的x或x +某项插入C中,显然需要把它当作一个数组来处理,因此可以将它作为x的索引,也可以将它赋给C的切片:

>>> C
array([[ 0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])
>>> C[0, 0:1] = x
>>> C
array([[ 0.47229555+0.7957525j,  0.00000000+0.j       ],
       [ 0.00000000+0.j       ,  0.00000000+0.j       ]])
>>> C[1, 1] = x[0] + 1+1j
>>> C
array([[ 0.47229555+0.7957525j,  0.00000000+0.j       ],
       [ 0.00000000+0.j       ,  1.47229555+1.7957525j]])

It looks like NumPy isn't handling this case correctly. Consider submitting a bug report.

看起来NumPy没有正确地处理这个问题。考虑提交错误报告。

#2


14  

Actually, none of the proposed solutions worked in my case (Python 2.7.6, NumPy 1.8.2). But I've found out, that change of dtype from complex (standard Python library) to numpy.complex_ may help:

实际上,在我的例子中,所提出的解决方案都不起作用(Python 2.7.6, NumPy 1.8.2)。但是我已经发现,dtype从复杂的(标准的Python库)变为numpy.complex_可能会有帮助:

>>> import numpy as np
>>> x = 1 + 2 * 1j
>>> C = np.zeros((2,2),dtype=np.complex_)
>>> C
array([[ 0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])
>>> C[0,0] = 1+1j + x
>>> C
array([[ 2.+3.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j]])