传递List和numpy.从c++应用程序转换成python函数
Passing a List and numpy.matrix to a python function from a C++ application
我有一堆用python编写的函数(用于快速原型)。我的主要项目是c++,我想从我的c++程序中调用这些函数。这些函数使用一些专门的python模块,如numpy, pyside等。
首先,我有一个接受4个参数的函数。第一个是笨蛋。矩阵对象和其他三个是简单的python列表。函数返回一个numpy。矩阵对象。
我知道我应该使用Python/C API和Numpy/C API的组合,但是对于我的生命,我找不到适当的文档或示例来做任何类似的事情。
这可能吗?
这是一个非常大的问题,我建议从Python手册中的扩展和嵌入Python解释器部分开始,然后使用NumPy C/API。
使用Python C/API而不崩溃或占用内存需要很好地理解Python引用计数和C/API。
我准备了一个小示例,它嵌入了Python解释器,创建了一个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;
}
相关文章:
- C++实用程序::转换在静态链接库中不起作用
- 如何将我的小程序转换为用于项目的函数?
- 将通用Windows应用程序转换为VisualStudio 2019上的常规应用程序
- 如何将我的程序转换为.dll文件并使用rundll32.exe在cmd中运行它?
- C++程序的 Python 文本解析器?
- 将 C 程序转换为具有大量字符* 和无符号字符转换错误的C++
- 将视觉C 媒体基础捕获应用程序转换为C 构建器
- 使用一个 C 程序运行 python 脚本(不同的 python 版本)
- Qt 创建器中的实用程序::转换::to_string_t中的链接错误
- 将Parralel CUDA程序转换为顺序运行
- Pthreads - 将顺序程序转换为并行程序
- numpy.core.multiarray在通过Xcode中开发的C 应用程序调用Python模块时未能导入
- 尝试将工作python程序转换为C++但不起作用
- 将带有 std::cout C++程序转换为 NCurses / CDK
- 如何从C++程序运行 python 脚本
- 如何在C (转换Python代码)中引用一个函数
- 面向对象设计-将C应用程序转换为C++
- 将带有迭代器的C++程序转换为 Boost MPI 并行程序
- 可视化如何将项目 win32 控制台应用程序转换为C++ Windows 窗体
- 传递List和numpy.从c++应用程序转换成python函数