won*_*jun 5 c++ python numpy cython
只有numpy才能在cython中调用此函数的最佳方法是什么?我不打算使用ctypes,memcpy,malloc等.
功能1)
#include <stdio.h>
extern "C" void cfun(const void * indatav, int rowcount, int colcount,
void * outdatav);
void cfun(const void * indatav, int rowcount, int colcount, void *
outdatav) {
//void cfun(const double * indata, int rowcount, int colcount,
double * outdata) {
const double * indata = (double *) indatav;
double * outdata = (double *) outdatav;
int i;
puts("Here we go!");
for (i = 0; i < rowcount * colcount; ++i) {
outdata[i] = indata[i] * 4;
}
puts("Done!");
}
Run Code Online (Sandbox Code Playgroud)
功能2)
#include <stdio.h>
extern "C" __declspec(dllexport) void cfun(const double ** indata, int
rowcount, int colcount, double ** outdata) {
for (int i = 0; i < rowcount; ++i) {
for (int j = 0; j < colcount; ++j) {
outdata[i][j] = indata[i][j] * 4;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Wonjun,Choi
您无法从 cython调用此函数 - 您必须将其编写为 cython 函数 -这里有一些很好的例子。作为参考,函数 1):
cimport numpy as np
def cfun(np.ndarray indata, int rowcount, int colcount, np.ndarray outdata):
cdef int i
print("Here we go!")
for i in range(rowcount * colcount):
outdata[i] = indata[i] * 4
print("Done!")
Run Code Online (Sandbox Code Playgroud)
如果你真的想调用它,你将不得不使用 ctypes 或编写你自己的包装器。使用swig也可以。