如何定义自定义float类型numpy dtypes(C-API) [英] How to define custom float-type numpy dtypes (C-API)

查看:577
本文介绍了如何定义自定义float类型numpy dtypes(C-API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的浮点数据类型,模拟128位浮点数使用两个64位浮点数(双双类 dd_real QD库)。从C ++我想导出一个ndarray到python。我已经知道如何做这个64位浮点数,但对于双双,我不知何故需要指定我自己的自定义dtype。如何做到这一点?

I have a custom float data type that emulates 128bit floats using two 64bit floats (the double-double class dd_real from the QD library). From C++ I want to export an ndarray to python. I already know how to do this for 64bit floats, but for double-doubles I somehow need to specify my own custom dtype. How to do that?

注意:numpy有自己的128位浮点数(np.float128)不幸的是这映射到 long double 在C / C ++这只是一个80bit浮点存储在128bit(在我所有的平台上)。

Note: numpy has its own 128bit float (np.float128) unfortunately this maps to long double in C/C++ which is merely an 80bit-float stored in 128bit (on all of my platforms).

事实上,一个人应该能够以与numpy export np.float128完全相同的方式来做(我只是不知道如何做),唯一的区别是它在C ++端使用 dd_real ,而不是 long double

In fact, one should be able to do this exactly in the same way that numpy exports np.float128 (I just don't know how that is done), with the only difference that it uses dd_real on the C++ side instead of long double.

如果这有帮助,我已经使用 boost :: python dd_real

If this helps, I already exported the C++ type dd_real to python using boost::python maybe this can be reused somehow.

到目前为止,我能够研究以下

So far I was able to research the following


  1. dtypes 是指 C-API 如何导出自定义dtypes,但该文档不知何故只解释现有的dtypes,而不是如何创建新的。

  1. The numpy documentation for dtypes refers to C-API for how to export custom dtypes, but that document somehow only explains the existing dtypes not how to create new ones.

浏览 stackoverflow我发现例如,但我不知道如果 dd_real 这可以更简单。我也没有看到dtype实际生成的位置。也许只有在python __ init__ 通过 np .typeDict ['quaternion'] = np.dtype(quaternion)

When browsing stackoverflow I found this example, but I wonder if for dd_real this could be simpler. I also don't see where the dtype is actually generated. Maybe only in python __ init__ via np.typeDict['quaternion'] = np.dtype(quaternion). How to use that dtype in C++ when I want to generate an ndarray?


推荐答案

p>您链接到的存储库,

The repository you linked to,

https://github.com/numpy/numpy-dtypes

可能包含有关如何向Numpy添加新dtype的最简单的示例。我不知道一个更容易的方法。注意这些文件中对 register_cast_function REGISTER_UFUNC 的调用:这些告诉Numpy如何处理乘法和转换等操作

probably contains the simplest possible examples on how to add new dtype to Numpy. I'm not aware of an easier way. Note the calls to register_cast_function and REGISTER_UFUNC in these files: these tell Numpy how operations such as multiplication and casting should be dealt with on an element-by-element level.

但是,如果你真正想做的只是导出你的数据,你可以导出为一个双精度数组,或者可以将两个双精度绑定到单个数据类型

However, if what you actually want to do is to only export your data, you could just export as an array of doubles, or maybe bundling two doubles to a single data type

np.dtype([('a', double), ('b', double)])

然后,您需要编写单独的函数这些数组( arr1 * arr2 不会做你想要的)。一个可能的方法进一步,使 arr1 * arr2 工作将子类 np.ndarray 您的数据类型,覆盖 __ mul __ 等操作。

Then, you'd need to write separate functions to do operations on these arrays (as arr1 * arr2 won't do what you want here). One possible way to go further and make also arr1 * arr2 to work would be to subclass np.ndarray your data type, overriding __mul__ etc operations.

这篇关于如何定义自定义float类型numpy dtypes(C-API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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