正确的方法将numpy.matrix转换为C双指针 [英] Proper way to cast numpy.matrix to C double pointer

查看:130
本文介绍了正确的方法将numpy.matrix转换为C双指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将numpy矩阵作为带双指针的C函数的参数的规范方法是什么?

What is the canonical way of getting a numpy matrix as an argument to a C function which takes a double pointer?

Context :我正在使用numpy验证一些C代码,我有一个使用const double ** const的C函数,并且正在使用ctypes进行调用Python中的.so.

Context: I'm using numpy to validate some C code-to wit, I have a C function which takes a const double ** const, and I'm using ctypes to call the .so from Python.

我尝试过:

func.argtypes = ctypeslib.ndpointer(dtype=double, ndim=2, flags="C_CONTIGUOUS")

并直接传递numpy矩阵(无效),以及

and passed the numpy matrix directly (didn't work), as well as

func.argtypes = ctypes.POINTER(ctypes.POINTER(ctypes.c_double))

,然后通过各种强制转换传递numpy矩阵.强制转换导致Python错误

and then passed the numpy matrix via various casts. Casting led to the Python error

TypeError: _type_ must have storage info

注意:这个问题是几年前提出的这里,但没有完全成功的解决方案.

Note: This question came up a few years ago here, but there was no completely successful resolution.

推荐答案

我认为您正在numpy的ndarray s(或matrix)中寻找ctypes接口.您可能有一个在此处查找有关numpy手册的更多信息.

I think you're looking for the ctypes interface in numpy's ndarrays (or matrix for that matter). You may have a look here for more information on numpy's manual.

请注意,numpy C-API使用单个指针存储ndarray(或与此相关的矩阵)(请参见

Please note that numpy C-API stores ndarrays (or matrices for that matter) with a single pointer (see http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#c.PyArrayObject). You cannot convert this single pointer into a double pointer in C, just because these are different types. Furthermore, numpy not only stores the data on these objects, it also stores information about the shape of the matrix and the strides (how the data is organised on the provided data pointer). Without knowing all these information, your code will not work on all conditions. For example, if you transpose a matrix before interfacing it to your code, you'll get unexpected results!

有几种解决方案,具体取决于您的设置:

There are a couple of solutions depending on your settings:

  1. 如果您可以修改库的API,请对其进行更改,以便您不仅可以传递有关数据指针,还可以与矩阵的形状及其步幅有关的numpy信息.然后,使用上面的链接找出如何将numpy/ctypes支持与新API进行接口.

  1. If you can modify your library's API, change it so that you can pass numpy information concerning not only the data pointer, but also the shape of the matrix and its strides. Then, use the above link to figure out how to interface numpy/ctypes support with your new API.

如果无法修改API,建议您基于ctypes创建一个Python函数,该函数将numpy数组的内容转换为使用ctypes本身创建的双指针矩阵(

If you cannot modify your API, I suggest you create a Python function based on ctypes that converts the contents of the numpy array into a double-pointer matrix created with ctypes itself (as suggested on this discussion). Also add support for the numpy object shape and stride so that you can run the conversion properly. After converting, pass the newly created double pointer structure to your original function.

这篇关于正确的方法将numpy.matrix转换为C双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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