Tag: cython

c malloc数组指针在cython中返回

如何有效地将cython中的malloc数组指针(或numpy数组指针)返回到python3。 只要我不返回数组指针,cython代码就可以正常工作 我想要: def double complex* randn_zig(int n): … r = malloc(n*n*sizeof(double complex)) … return r c11(gcc 11)等价物是: double complex* randn_zig(int n){ r = malloc(n*n*sizeof(double complex)) return r } 我试过 randn_zig(int n): 和randn_zig( r, int n): 到目前为止,其他排列没有成功。 c和cython代码版本的速度是Numby / pylab randn版本的5倍,如果我能找到一种方法来返回指向大型10 ^ 6到10 ^ 10双复数组的指针。

包装C库

我有一个private.h , public.h和file.c ,我需要将它包装到Cython中 。 如何包装函数Person_ptr Person_create(const char* name); ? private.h: #ifndef __PERSON_PRIVATE_H__ #define __PERSON_PRIVATE_H__ #include “Person.h” typedef struct Person_TAG { char* name; int age; } Person; void person_init(Person_ptr self, const char* name); # endif /* __PERSON_PRIVATE_H__ */ public.h #ifndef __PERSON_H__ #define __PERSON_H__ #include typedef struct Person_TAG* Person_ptr; #define PERSON(x) \ ((Person_ptr) (x)) #define PERSON_CHECK_INSTANCE(x) […]

Cython等效于c定义#define myfunc(node x,…)SetNode(x.getattributeNode(),__ VA_ARGS__)

Cython等效于c define #define myfunc(Node x,…) SetNode(x.getattributeNode(),__VA_ARGS__) 我有一个ac api SetNode,它将第一个参数作为struct类型节点的节点和N个变量(N是0-N的变量号) 这是解决这种问题的例子 exampleAPI.c #include float sumN(int len,…){ va_list argp; int i; float s=0; va_start(argp,len); for(i=0;i<len;i++){ s+=va_arg(argp,int); } va_end(argp); } exampleAPI.h #include float sumN(int len,…) examplecode.c #include #include”exampleAPI.h” int len(float first,…){ va_list argp; int i=1; va_start(argp,first); while(1){ if(va_arg(argp,float)==NULL){ return i } else{ i++; } } va_end(argp); } #define […]

Cython和fortran – 如何在没有f2py的情况下一起编译

最终更新 这个问题是关于如何编写一个setup.py ,它将编译一个直接访问FORTRAN代码的cython模块,就像C一样。 这是一个相当漫长而艰巨的解决方案之旅,但下面包含了完整的混乱。 原始问题 我有一个扩展,它是一个Cython文件,它设置一些堆内存并将其传递给fortran代码,以及一个fortran文件,这是一个古老的模块,我想避免重新实现,如果可以的话。 .pyx文件可以很好地编译为C,但是cython编译器会对.f90文件进行.f90 ,并出现以下错误: $ python setup.py build_ext –inplace running build_ext cythoning delaunay/__init__.pyx to delaunay/__init__.c building ‘delaunay’ extension error: unknown file type ‘.f90’ (from ‘delaunay/stripack.f90’) 这是我的安装文件(上半部分): from distutils.core import setup, Extension from Cython.Distutils import build_ext ext_modules = [ Extension(“delaunay”, sources=[“delaunay/__init__.pyx”, “delaunay/stripack.f90”]) ] setup( cmdclass = {‘build_ext’: build_ext}, ext_modules = ext_modules, … […]

将Cython中的C结构包装/转换为Python类

我刚刚开始熟悉Cython,试图将一些C语言结构包装到Python方法和类中。 我真正不了解的是如何从(初始化的)C结构转换到相应的Python类应该是有效的。 我在这里缺少什么: 来自C头文件的片段: struct test_struct { int _something; complex_struct* _another; }; typedef struct test_struct test; test *test_new(void); int some_method(test **n, int irrelevant); 来自我.pxd的相应片段: cdef struct test_struct: pass ctypedef test_struct test test* test_new() int some_method(test **n, int irrelevant) 我的.pyx: def do_something(int irrelevant): cdef test* t = test_new() ret = some_method(&t, irrelevant) # Here comes the […]

如何在Cython中动态声明2D c数组

我需要使用各种大小的2D numpy数组执行大量工作,我想将这些计算卸载到cython上。 我的想法是我的2D numpy数组将从python传递到cython,在那里它将被转换为c-array或内存视图,并用于其他c级函数的级联中进行计算。 经过一些分析后,由于一些严重的python开销,我排除了在cython中使用numpy数组。 使用内存视图更快,更容易使用,但我怀疑我可以使用c-arrays进一步加速。 这是我的问题 – 如何在没有使用设定值预定义其尺寸的情况下在cython中声明2D c数组? 例如,我可以通过这种方式从numpy创建一个c-array: narr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]], dtype=np.dtype(“i”)) cdef int c_arr[3][4]: for i in range(3): for j in range(4): c_arr[i][j] = narr[i][j] 然后将其传递给函数: cdef void somefunction(int c_Arr[3][4]): … 但这意味着我有一个固定的数组sizde,在我的情况下将是无用的。 所以我尝试过这样的事情: narr = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]], dtype=np.dtype(“i”)) cdef int a = np.shape(narr)[0] cdef int b = np.shape(narr)[1] cdef int c_arr[a][b]: # […]

检查数组中是否存在值

我想知道如何检查数组中是否存在值或对象,如python: a = [1,2,3,4,5] b = 4 if b in a: print(“True!”) else: print(“False”) 但我想知道cython中是否已存在某些内容。 我有一个指针的struct对象数组,想知道该数组中是否存在该对象。 喜欢 cdef Node *array array = malloc( 5 * cython.sizeof(Node)) for i in range(5): array[i].index = i cdef Node test = array[3] if test in array: print(“True!”) cdef struct Node: int index 下面的代码不正确,但它是为了说明我的意思。