实现与 numpy 一起使用的奇异复数

发布于 2024-12-12 08:58:47 字数 364 浏览 0 评论 0原文

我正在使用 python + numpy + scipy 对复数数组进行一些卷积过滤。

field = np.zeros((field_size, field_size), dtype=complex)
...
field = scipy.signal.convolve(field, kernel, 'same')

因此,当我想在 numpy 中使用复杂数组时,我需要做的就是传递 dtype=complex 参数。 对于我的研究,我需要实现其他两种类型的复数:双精度 (i*i=0) 和双精度 (i*i=1)。这不是什么大问题 - 我只是获取复数的 python 源代码并更改乘法函数。 问题:我如何制作这些奇异数字类型的 numpy 数组?

i'm using python + numpy + scipy to do some convolution filtering over a complex-number array.

field = np.zeros((field_size, field_size), dtype=complex)
...
field = scipy.signal.convolve(field, kernel, 'same')

So, when i want to use a complex array in numpy all i need to do is pass the dtype=complex parameter.
For my research i need to implement two other types of complex numbers: dual (i*i=0) and double (i*i=1). It's not a big deal - i just take the python source code for complex numbers and change the multiplication function.
The problem: how do i make a numpy array of those exotic numeric types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无语# 2024-12-19 08:58:48

把事情从里到外颠倒过来有用吗?我的意思是,不要将数组作为外部容器,其中包含保存几个浮点值作为复数的小容器,而是将其翻转,使您的复数成为外部容器。您将有两个数组,一个是普通浮点数作为实部,另一个数组作为虚部。基本的超快速卷积器可以完成其工作,尽管您必须编写代码才能使用它四次,以适应两个因素的实部/虚部的所有组合。

在彩色图像处理中,我经常将代码从使用 RGB 值数组重构为三个标量值数组,并且由于更简单的卷积和其他操作在字节或浮点数组上运行得更快,因此发现了良好的加速效果。

YMMV,因为复合物(或颜色)的成分的局部性可能很重要。

Would it work to turn things inside-out? I mean instead of an array as the outer container holding small containers holding a couple floating point values as a complex number, turn that around so that your complex number is the outer container. You'd have two arrays, one of plain floats as the real part, and another array as the imaginary part. The basic super-fast convolver can do its job although you'd have to write code to use it four times, for all combinations of real/imaginary of the two factors.

In color image processing, I have often refactored my code from using arrays of RGB values to three arrays of scalar values, and found a good speed-up due to simpler convolutions and other operations working much faster on arrays of bytes or floats.

YMMV, since locality of the components of the complex (or color) can be important.

青巷忧颜 2024-12-19 08:58:47

看起来您正在尝试为例如双数创建一个新的数据类型。可以使用以下代码来完成此操作:

dual_type = np.dtype([("a", np.float), ("b", np.float)])
dual_array = np.zeros((10,), dtype=dual_type)

但是这只是存储数据类型的一种方式,并且不会告诉 numpy 有关其所遵循的特殊代数的任何信息。

您可以通过子类化 numpy.ndarray 并重写相关成员函数(例如用于乘法的 __mul__ 等)来部分实现所需的效果。这对于任何 python 代码都应该可以正常工作,但我相当确定任何基于 C 或 fortran 的例程(即大多数 numpy 和 scipy)都会直接将数字相乘,而不是调用 __mul__ 。我怀疑 convolve 会落入这个篮子,因此它不会遵守您定义的规则,除非您编写自己的纯 python 版本。

It looks like you are trying to create a new dtype for e.g. dual numbers. It is possible to do this with the following code:

dual_type = np.dtype([("a", np.float), ("b", np.float)])
dual_array = np.zeros((10,), dtype=dual_type)

However this is just a way of storing the data type, and doesn't tell numpy anything about the special algebra which it obeys.

You can partially achieve the desired effect by subclassing numpy.ndarray and overriding the relevant member functions, such as __mul__ for multiply and so on. This should work fine for any python code, but I am fairly sure that any C or fortran-based routines (i.e. most of numpy and scipy) would multiply the numbers directly, rather than calling the __mul__. I suspect that convolve would fall into this basket, therefore it would not respect the rules which you define unless you wrote your own pure python version.

拍不死你 2024-12-19 08:58:47

这是我的解决方案:

from iComplex import SplitComplex as c_split
...
ctype = c_split
constructor = np.vectorize(ctype, otypes=[np.object])
field = constructor(np.zeros((field_size, field_size)))

这是创建 numpy 对象数组的简单方法。
scipy.signal.convolve 怎么样——它似乎不适用于我的复数,我必须自己制作卷积,而且它的工作速度非常慢。所以现在我正在寻找加快速度的方法。

Here's my solution:

from iComplex import SplitComplex as c_split
...
ctype = c_split
constructor = np.vectorize(ctype, otypes=[np.object])
field = constructor(np.zeros((field_size, field_size)))

That is the easy way to create numpy object array.
What about scipy.signal.convolve - it doesn't seem to work with my complex numbers and i had to make my own convolution and it works deadly slow. So now i am looking for ways to speed it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文