本文介绍了从C ++应用程序将List和numpy.matrix传递给python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bunch of functions written in python (for rapid prototyping). My main project is in C++ and I wanna call these functions from my C++ program.These functions use some specialized python modules like numpy, pyside etc.

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

To start with, I have one function that takes in 4 arguments. The first one is a numpy.matrix object and the other three are simple python lists. The function is returning a numpy.matrix object.

我知道我应该结合使用Python/C API和Numpy/C API,但在我的生命中,我找不到适合任何从事类似工作的人的适当文档或示例.

I know I'm supposed to use a combination of Python/C API and Numpy/C API, but for the life of me, I cannot find proper documentation or examples to anyone doing anything similar.

这有可能吗?

推荐答案

这是一个很大的问题,我建议以扩展和嵌入Python手册部分,然后使用NumPy C/API .

This is a very big question, I suggest starting with Extending and Embedding the Python Interpreter section in the Python Manual and then Using NumPy C/API.

使用Python C/API而不导致崩溃或内存,需要对Python参考计数和C/API有充分的了解.

Using the Python C/API without crashing or memory requires good understanding of Python reference-counting and the C/API.

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

I've prepared a small example that embeds the Python Interpreter, creates a NumPy array, creates a Python list, and then and calls two Python functions with the array and list as arguments. This could serve as a starting point you could extend.

首先是Python函数:

First the Python functions:

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;
}

这篇关于从C ++应用程序将List和numpy.matrix传递给python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:41