从包装 eigen 的 pybind11 返回数组的列表或元组

diz*_*q22 5 python numpy eigen pybind11

我有一个使用 eigen 的 C++ 函数,它使用 pybind11 包装,以便我可以从 python 调用它。预期函数的简单版本返回一个Eigen::MatrixXd类型,pybind 成功地将其转换为 2D numpy 数组。

我希望这个函数能够返回此类矩阵的列表或元组,或者 3D numpy 数组。

我在某种程度上是 C++ 的新手,并且 pybind 的文档没有(据我所知)提供任何方向。一个模拟示例如下:

测试.cpp

#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/Dense>

Eigen::MatrixXd test(Eigen::Ref<const Eigen::MatrixXd> x, double a)
{
  Eigen::MatrixXd y;
  y = x * a;
  return y;
}

Eigen::MatrixXd *test2(Eigen::Ref<const Eigen::MatrixXd> x, Eigen::Ref<const Eigen::VectorXd> as)
{
  Eigen::MatrixXd *ys = new Eigen::MatrixXd[as.size()];
  for(unsigned int k = 0; k < as.size(); k++){
    Eigen::MatrixXd& y = ys[k];
    y = x * as[k];
  }
  return ys;
}

namespace py = pybind11;

PYBIND11_MODULE(test, m)
{
  m.doc() = "minimal working example";
  m.def("test", &test);
  m.def("test2", &test2);
}
Run Code Online (Sandbox Code Playgroud)

我想test2返回数组列表或元组。

在Python中:

import test
import numpy as np
x = np.random.random((50, 50))
x = np.asfortranarray(x)
a = 0.1
a2 = np.array([1.0, 2.0, 3.0])
y = test.test(x, a)
ys = test.test2(x, a2)
Run Code Online (Sandbox Code Playgroud)

该数组y与预期一致,但ys仅包含与 的第一个系数对应的数组a2

我应该如何修改test2才能正确返回多个数组?3D 阵列也是可以接受的。

Ola*_*laf 4

我以前使用过 Eigen,但我不是专家,所以其他人也许能够改进这个解决方案。

#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <Eigen/Dense>

std::vector<Eigen::MatrixXd> 
test2(Eigen::Ref<const Eigen::MatrixXd> x, Eigen::Ref<const Eigen::VectorXd> as){
    std::vector<Eigen::MatrixXd> matrices;
    for(unsigned int k = 0; k < as.size(); k++){
        Eigen::MatrixXd ys = x * as[k];
        matrices.push_back(ys);
    }
    return matrices;
}

namespace py = pybind11;

PYBIND11_MODULE(test, m){
    m.doc() = "minimal working example";
    m.def("test2", &test2);
}
Run Code Online (Sandbox Code Playgroud)

该向量由 pybind11 转换为 numpy 数组列表。结果:

In [1]: import numpy as np; x = np.ones((2,2)); a = np.array((2., 3.)); import test

In [2]: test.test2(x, a)
Out[2]: 
[array([[2., 2.],
        [2., 2.]]), array([[3., 3.],
        [3., 3.]])]
Run Code Online (Sandbox Code Playgroud)