python - 从 C 代码调用 numpy 函数

标签 python numpy python-c-api python-c-extension

我正在尝试将一些带有 Mex 扩展的 MatLab 代码移动到带有 numpy 和 scipy 库的 Python 中。使用这个精彩的教程http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays ,我很快就采用了 C 函数来从 Python 调用。但是一些 C 函数调用 MatLab 函数,所以我必须通过从 C 代码调用 numpy 和 scipy 函数来替换这段代码。

我试过做这样的事情 Extending and Embedding the Python Interpreter .但是,我遇到了问题:如何将数组传递给函数参数。此外,这种在模块中查找函数然后为参数构建元组的漫长方法似乎不是很优雅。

例如,如何从 C 代码调用 numpy 模块中的 sum 函数?

如果有任何想法或链接,我将不胜感激。 鲁本

附言这里有一个例子:

    PyObject *feedback(PyObject *self, PyObject *args){
    PyArrayObject *Vecin;
    double mp=0,ret;
    if( !PyArg_ParseTuple(args,"O!d",&PyArray_Type,&Vecin,&mp)
      ||  Vecin == NULL ) return NULL;
    /* make python string with module name */
    PyObject *pName = PyString_FromString("numpy");
    if( pName == NULL){
        fprintf(stderr,"Couldn\'t setup string %s\n","numpy");
        return NULL;
    }
    /* import module */
    PyObject *pModule = PyImport_Import(pName);
    Py_DECREF(pName);
    if( pModule == NULL){
        fprintf(stderr,"Couldn\'t find module %s\n","numpy");
        return NULL;
    }
    /* get module dict */
    PyObject *dic = PyModule_GetDict(pModule);
    if( dic == NULL){
        fprintf(stderr,"Couldn\'t find dic in module %s\n","numpy");
        return NULL;
    }
    /* find function */
    PyObject *pFunction = PyDict_GetItemString(dic, "sum");
    Py_DECREF(dic);
    if( pFunction == NULL){
        fprintf(stderr,"Couldn\'t find function  %s in dic\n","sum");
        return NULL;
    }
    Py_DECREF(pModule);
    /* create typle for new function argument */
    PyObject *inarg = PyTuple_New(1);
    if( inarg == NULL){
        fprintf(stderr,"Cannot convert to Build in Value\n");
        return NULL;
    }
    /* set one input paramter */
    PyTuple_SetItem(inarg, 0, (PyObject*)Vecin);
    /* call sunction from module and get result*/
    PyObject *value = PyObject_CallObject(pFunction, inarg);
    if( value == NULL){
        fprintf(stderr,"Function return NULL pointer\n");
        return NULL;
    }
    Py_DECREF(pFunction);
    if( !PyArg_ParseTuple(value,"d",&ret) ) return NULL;
    return Py_BuildValue("d",ret*mp);
}

结果

>>print mymod.FeedBack(np.array([1.,2.,3.,4.,5.]),2)


Traceback (most recent call last):
  File "mymodtest.py", line 10, in <module>
    print mymod.FeedBack(np.array([1.,2.,3.,4.,5.]),2)
SystemError: new style getargs format but argument is not a tuple
Segmentation fault

最佳答案

NumPy C API 有 PyArray_Sum :

PyObject *sum = PyArray_Sum(arr, NPY_MAXDIMS, PyArray_DESCR(arr)->type_num, NULL);

关于python - 从 C 代码调用 numpy 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073995/

相关文章:

python - 从 CAPI 评估 Python 代码并获得输出

python - 如何将此 "Python+twill+mechanize"组合部署到 "Google App Engine"?

python - 如何将 python 3.5.1 与 MySQL 数据库一起使用

python - Python处理文件时出现内存错误

python - 在 virtualenv 中安装 pip 的 Matplotlib 要求

python - 如何使用 ctypes 将 NumPy 复杂数组与 C 函数连接起来?

Python O365 使用 HTML 文件发送电子邮件

pandas - pandas 对象如何传递到 numpy 函数中?

python - 如何从指向字符串的 PyObject 获取 char*

python - 如何允许 None 或特定类型作为 Python C 扩展函数的参数?