分配复杂值numpy的阵列? [英] Assigning complex values to numpy arrays?

查看:99
本文介绍了分配复杂值numpy的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这给出了预期的结果。

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

和这个作品

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".

如果我们现在省略了明确的 DTYPE =复杂,我得到ValueError错误:在设置数组元素与序列。

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

有人能解释这是怎么回事,以及如何做到这一点不出错?我迷路了。

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

推荐答案

要插入复杂 X X +的东西 C ,你显然需要来对待它,仿佛它是一个数组,所以要么索引 X 或分配它的片 C

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:

>>> 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]])

看起来numpy的不正确处理这种情况。考虑提交的bug报告。

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

这篇关于分配复杂值numpy的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆