sách gpt4 ai đã đi

python - PyArray_SimpleNewFromData 示例

In lại Tác giả: Space Dog 更新时间:2023-10-29 21:03:08 25 4
mua khóa gpt4 Nike

我知道这个问题已经回答了很多次,我也阅读了文档,但我仍然无法清楚地理解它是如何工作的。就像,我无法理解这些值是如何填充到它的参数中的。这些例子不是很清楚地解释它(或者可能是我不能)。谁能帮我理解这个函数的参数是如何填充的?他们的值(value)观应该是什么?我必须在不重新分配内存的情况下将 vector 从 C++ 传递到 Python。任何帮助深表感谢。很多天以来,我一直坚持这一点。

我正在执行的代码:

int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,*pXVec,*c, *xarr1;
int i;
float fArray[5] = {0,1,2,3,4};
//float *p = &fArray[0] ;
npy_intp m = 5;
//void* PyArray_GetPtr(PyArrayObject* aobj, npy_intp* ind)¶


// Initialize the Python Interpreter
Khởi tạo Py();
PySys_SetArgv(argc, argv);
// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);
printf("check0\n");
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
printf("check1\n");
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);
printf("check2\n");
// if (PyCallable_Check(pFunc))
// {
// Prepare the argument list for the call
//xarr1 = PyFloat_FromDouble(xarr[1]);
printf("check3\n");
c = PyArray_SimpleNewFromData(1,&m,NPY_FLOAT,(void *)fArray);
printf("check3\n");
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs,0, c);

pValue = PyObject_CallObject(pFunc, pArgs);

if (pArgs != NULL)
{
Py_DECREF(pArgs);
}

//}
// else
// {
// PyErr_Print();
// }

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Finish the Python Interpreter
Py_Hoàn tất();

trả về 0;
}

1 Câu trả lời

chức năng :

 PyObject *
PyArray_SimpleNewFromData(
int nd,
npy_intp* dims,
int typenum,
void* data)
  • 最后一个参数 (dữ liệu) 是数据的缓冲区。让我们放弃它。

  • 第二个参数(dims)是一个缓冲区,它的每个条目都是一个维度;所以对于一维数组,它可以是一个长度为 1 的缓冲区(或者甚至是一个整数,因为每个整数都是一个长度为 1 的缓冲区)

  • 由于第二个参数是一个缓冲区,第一个参数 (nd) 告诉它的长度

  • 第三个参数(typenum)表示类型。


例如,假设您在 x 处有 4 个 64 位整数:

要创建一个数组,使用

int dims[1];
dims[0] = 4;
PyArray_SimpleNewFromData(1, dims, NPY_INT64, x)

要创建 2X2 矩阵,请使用

int dims[2];
dims[0] = dims[1] = 2;
PyArray_SimpleNewFromData(2, dims, NPY_INT64, x)

关于python - PyArray_SimpleNewFromData 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30357115/

25 4 0
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com