无法获得c++ PyArray_SimpleNewFromData完成



我已经找到了这样做的说明。我使用了import_Array()。我使用Python 3.10,我有Windows Visual Studio。

这是我的代码:

PyObject* switch_rut(PyObject* self, PyObject* args) {
PyArrayObject* inp1;
PyArrayObject* inp2;
PyObject* outp;
double swv;
double* res;
if (!PyArg_ParseTuple(args, "O!O!d:switcher", &PyArray_Type, &inp1, &PyArray_Type, &inp2, &swv)) {
return NULL;
}
if (inp1->nd != 1 || inp1->descr->type_num != PyArray_DOUBLE || inp2->nd != 1 || inp2->descr->type_num != PyArray_DOUBLE || ) {
PyErr_SetString(PyExc_ValueError,
"arrays must be one-dimensional and of type float");
return NULL;
}
if (inp1->dimensions[0] != inp2->dimensions[0]) {
PyErr_SetString(PyExc_ValueError,
"arrays must be of equal length");
return NULL;
}
int* cnt = new int[1];
int nd = 1;
cnt[0] = inp1->dimensions[0];
int acnt = inp1->dimensions[0];
res = new double[acnt];
double ipar1, ipar2, iparr;
for (int i = 0; i < acnt; i++) {
ipar1 = *(double*)(inp1->data + i * inp1->strides[0]);
ipar2 = *(double*)(inp2->data + i * inp2->strides[0]);
iparr = ipar2 * swv + ipar1 * (1 - swv);
res[i] = iparr;
}
outp = PyArray_SimpleNewFromData(nd, cnt, NPY_DOUBLE,reinterpret_cast<void*> res);
return outp;

}

我说的是如何用Numpy数组在C或c++中嵌入Python以及如何返回一个Numpy数组的值。这是我使用的。

PyArray_New(&PyArray_Type, nd, bcnt, NPY_DOUBLE, NULL, (void*) res, 0, NPY_ARRAY_CARRAY, NULL);

相关内容

  • 没有找到相关文章

最新更新