从 C++ 应用程序将列表和 numpy.matrix 传递给 python 函数

Passing a List and numpy.matrix to a python function from a C++ application

我有一堆用 python 编写的函数(用于快速原型制作)。我的主要项目是在 C++ 中,我想从我的 C++ 中调用这些函数 program.These 函数使用一些专门的 python 模块,如 numpy、pyside 等

首先,我有一个接受 4 个参数的函数。第一个是 numpy.matrix 对象,其他三个是简单的 python 列表。该函数返回一个 numpy.matrix 对象。

我知道我应该使用 Python/C API 和 Numpy/C API 的组合,但是对于我来说,我找不到合适的文档或任何做类似事情的人的例子。

这可能吗?

这是一个很大的问题,我建议从Extending and Embedding the Python Interpreter section in the Python Manual and then Using NumPy C/API开始。

使用 Python C/API 而不会崩溃或内存需要很好地理解 Python 引用计数和 C/API.

我准备了一个嵌入 Python 解释器的小 example,创建了一个 NumPy 数组,创建了一个 Python 列表,然后调用了两个 Python以数组和列表作为参数的函数。这可以作为您可以扩展的起点。

首先是 Python 函数:

import numpy

def print_matrix(M):
    print (M)

def transform_matrix(M, L):
    for x in L:
        M = M*x
    return M

然后是C++代码:

// Python headers
#include <Python.h>
#include <abstract.h>

// NumPy C/API headers
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION // remove warnings
#include <numpy/ndarrayobject.h>

#include <vector>

int main()
{
    // initialize python
    Py_InitializeEx(1);

    // import our test module
    PyObject* numpy_test_module = PyImport_ImportModule("numpy_test");

    // retrieve 'print_matrix(); from our module
    PyObject* print_matrix = PyObject_GetAttrString(numpy_test_module, "print_matrix");

    // retrieve 'some_function' from our module
    PyObject* transform_matrix = PyObject_GetAttrString(numpy_test_module, "transform_matrix");

    // no longer need to reference the module directly
    Py_XDECREF(numpy_test_module);

    // initialize numpy array library
    import_array1(-1); // returns -1 on failure

    // create a new numpy array

    // array dimensions
    npy_intp dim[] = {5, 5};

    // array data
    std::vector<double> buffer(25, 1.0);

    // create a new array using 'buffer'
    PyObject* array_2d = PyArray_SimpleNewFromData(2, dim, NPY_DOUBLE, &buffer[0]);

    // print the array by calling 'print_matrix'
    PyObject* return_value1 = PyObject_CallFunction(print_matrix, "O", array_2d);
    // we don't need the return value, release the reference
    Py_XDECREF(return_value1);

    // create list
    PyObject* list = PyList_New(3);
    PyList_SetItem(list, 0, PyLong_FromLong(2));
    PyList_SetItem(list, 1, PyLong_FromLong(3));
    PyList_SetItem(list, 2, PyLong_FromLong(4));

    // call the function with the array as its parameter
    PyObject* transformed_matrix = PyObject_CallFunction(transform_matrix, "OO", array_2d, list);
    // no longer need the list, free the reference
    Py_XDECREF(list);

    // print the returned array by calling 'print_matrix'
    PyObject* return_value2 = PyObject_CallFunction(print_matrix, "O", transformed_matrix);
    // no longer need the 'return_value2', release the reference
    Py_XDECREF(return_value2);

    // no longer need 'transformed_matrix'
    Py_XDECREF(transformed_matrix);

    // no longer need the array
    Py_XDECREF(array_2d);

    // no longer need the reference to transform_matrix
    Py_XDECREF(transform_matrix);

    // no longer need the reference to 'print_matrix'
    Py_XDECREF(print_matrix);

    // clean up python
    Py_Finalize();

    return 0;
}