相关疑难解决方法(0)

使用cython将numpy数组列表传递给C.

我有一个list_of_arrays3D numpy数组列表,我想用模板传递给C函数

int my_func_c(double **data, int **shape, int n_arrays)
Run Code Online (Sandbox Code Playgroud)

这样的

data[i]  : pointer to the numpy array values in list_of_arrays[i]
shape[i] : pointer to the shape of the array in list_of_arrays[i] e.g. [2,3,4]
Run Code Online (Sandbox Code Playgroud)

如何my_func_c使用cython接口函数调用?

我的第一个想法是做类似下面的事情(有效),但我觉得有一个更好的方法就是使用numpy数组而不需要mallocing和freeing.

# my_func_c.pyx

import numpy as np
cimport numpy as np
cimport cython
from libc.stdlib cimport malloc, free

cdef extern from "my_func.c":
    double my_func_c(double **data, int **shape, int n_arrays)

def my_func(list list_of_arrays):
    cdef int n_arrays  = len(list_of_arrays)
    cdef double **data = <double …
Run Code Online (Sandbox Code Playgroud)

c python numpy cython

5
推荐指数
1
解决办法
1286
查看次数

标签 统计

c ×1

cython ×1

numpy ×1

python ×1