numpy 的 complex128 转换 [英] numpy's complex128 conversion

查看:85
本文介绍了numpy 的 complex128 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 np.complex128 数字列表,但对于所有数字,复数部分都为零.如何提取数字的实部(这几乎是数字的唯一部分)?

I have a list of np.complex128 numbers but for all the numbers the complex part is equal to zero. How can I extract the real part of the number (which is pretty much the only part of the number)?

另一方面,我想做 scipy 集成,但我不确定他们的集成方法是否可以处理 y 数据类型为 np.complex128 的样本.

As a side not, I want to do scipy integration but I am not sure whether their integration methods can handle y samples with dtype of np.complex128.

推荐答案

如果你的列表是一个 NumPy 数组,你可以简单地引用它的 real 属性:

If your list is a NumPy array, you can simply refer to its real attribute:

In [59]: a = np.array([1+0j, 2+0j, -1+0j])
In [60]: a
Out[60]: array([ 1.+0.j,  2.+0.j, -1.+0.j])

In [61]: a.real
Out[61]: array([ 1.,  2., -1.])

如果您的列表是 Python list,那么下面的列表推导式可能是获得您想要的真实部分的最简单方法:

If your list is a Python list, perhaps the following list comprehension would be the simplest way to get the real parts you want:

In [64]: l
Out[64]: [(1+0j), (2+0j), (-1+0j)]
In [67]: [c.real for c in l]
Out[67]: [1.0, 2.0, -1.0]

如果要将返回 np.complex128 的函数与 quad 集成,则需要进行此转换:scipy.integrate.quad 期望函数返回某种浮点数.

You would need to do this conversion if you want to integrate a function returning a np.complex128 with quad: scipy.integrate.quad expects the function to return some kind of float.

这篇关于numpy 的 complex128 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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