将交错的NumPy整数数组转换为complex64的最快方法是什么? [英] What's the fastest way to convert an interleaved NumPy integer array to complex64?
本文介绍了将交错的NumPy整数数组转换为complex64的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入数据流,该数据交织了实数和虚数整数.将它们转换为complex64值是我程序中最慢的操作.这是我目前的方法:
I have a stream of incoming data that has interleaved real and imaginary integers. Converting these to complex64 values is the slowest operation in my program. This is my current approach:
import numpy as np
a = np.zeros(1000000, dtype=np.int16)
b = np.complex64(a[::2]) + np.complex64(1j) * np.complex64(a[1::2])
在不进行C扩展或不使用cython之类的情况下,我可以做得更好吗?如果我不能做得更好,那么使用其中一种技术最简单的方法是什么?
Can I do better without making a C extension or using something like cython? If I can't do better, what's my easiest approach using a technology like one of these?
推荐答案
[~]
|1> import numpy as np
[~]
|2> a = np.zeros(1000000, dtype=np.int16)
[~]
|3> b = a.astype(np.float32).view(np.complex64)
[~]
|4> b.shape
(500000,)
[~]
|5> b.dtype
dtype('complex64')
这篇关于将交错的NumPy整数数组转换为complex64的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文