如何连接使用ctypes的C函数一个numpy的一系列复杂? [英] How to interface a NumPy complex array with C function using ctypes?

查看:393
本文介绍了如何连接使用ctypes的C函数一个numpy的一系列复杂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C函数,它复数浮点的数组,并计算是否对他们就地:

  / * foo.c的* /
无效美孚(cmplx_float *数组,INT长度){...}

复杂浮动结构是这样的:

  typedef结构cmplx_float {
   真正的浮动;
   浮IMAG;
} cmplx_float;

我需要调用使用ctypes的Python的功能。在Python我有complex64元素的numpy的1-D ndarray。

我也取得了从ctypes.Structure派生的类:

 类c_float(结构):
   _fields_ = [('真正的',c_float)
               (IMAG,c_float)

我想,我可能需要实现结构的数组另一个Python类。总的来说,我只是随便用个连接在一起的问题。需要采取哪些措施,以最终调用我的函数在Python基本上是这样或多或少:

  some_ctype_array = SomeConversion code(cmplx_numpy_array)
lib.foo(some_ctype_array,长度)


解决方案

您可以使用的 ndpointer 从的 numpy.ctypeslib 申报的第一个参数是类型的一维数组相邻 numpy.complex64

 导入numpy的是NP
从numpy的进口ctypeslib#... code加载共享库作为`lib`没有显示...#声明参数类型lib.foo的:
lib.foo.argtypes = [ctypeslib.ndpointer(np.complex64,NDIM = 1,旗帜='C'),c_int的]

然后,你可以做,例如,

  Z = np.array([1 + 2J,-3 + 4J,5.0J],DTYPE = np.complex64)
lib.foo(Z,z.size)

您可能需要包装,在不需要第二个参数的函数:

 高清富(Z):
    #确保我们使用complex64的连续数组。如果
    #调用到foo(Z,z.size)修改ž的地方,那就是
    #预期的功能的效果,那么以下行应
    #被移除。 (输入z被那么* *需要成为一个连续的
    np.complex64排名阵列)。
    Z = np.ascontiguousarray(Z,DTYPE = np.complex64)
    #调用C函数。
    lib.foo(Z,z.size)

I have a function in C that takes an array of complex floats and does calculations on them in-place.:

/* foo.c */
void foo(cmplx_float* array, int length) {...}

The complex float struct looks like this:

typedef struct cmplx_float {
   float real;
   float imag;
} cmplx_float ;

I need to call that function in python using ctypes. In Python I have an Numpy 1-D ndarray of complex64 elements.

I have also made a class derived from ctypes.Structure:

class c_float(Structure):
   _fields_ = [('real', c_float),
               ('imag', c_float)]

I imagine that I might need another python class that implements an array of structs. Overall I'm just having problems with connecting the pieces together. What needs to be done to eventually call my function in Python basically something like this more or less:

some_ctype_array = SomeConversionCode(cmplx_numpy_array) 
lib.foo(some_ctype_array, length)

解决方案

You can use the ndpointer from numpy.ctypeslib to declare the first argument to be a one-dimensional contiguous array of type numpy.complex64:

import numpy as np
from numpy import ctypeslib

# ...code to load the shared library as `lib` not shown...

# Declare the argument types of lib.foo:
lib.foo.argtypes = [ctypeslib.ndpointer(np.complex64, ndim=1, flags='C'), c_int]

Then you can do, for example,

z = np.array([1+2j, -3+4j, 5.0j], dtype=np.complex64)
lib.foo(z, z.size)

You might want to wrap that in a function that doesn't require the second argument:

def foo(z):
    # Ensure that we use a contiguous array of complex64.  If the
    # call to foo(z, z.size) modifies z in place, and that is the
    # intended effect of the function, then the following line should
    # be removed. (The input z is then *required* to be a contiguous 
    # array of np.complex64.) 
    z = np.ascontiguousarray(z, dtype=np.complex64)
    # Call the C function.
    lib.foo(z, z.size)

这篇关于如何连接使用ctypes的C函数一个numpy的一系列复杂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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