python如何调用c++numpy.ndarray代码_从C++到Numpy ndarray的转换非常简单

我使用Boost Python来计算程序的计算密集型部分,它工作得很好,除了将数组从C++传递到Python,反之亦然也很慢,这是程序整体效率的限制因素。在

这里有一个例子来说明我的观点。在C++侧,我返回一个具有较大大小的类型^ {< CD1> }的矩阵。在Python端,我调用该函数并尝试使用两种不同的方法:使用“{{CD2}”方法和我自己(可能是非常天真的)C++实现一个基本转换器来转换结果数组。C++部分:#include

#include

#include

using namespace std;

typedef vector vec;

typedef vector mat;

mat test()

{

int n = 1e4;

mat result(n, vec(n, 0.));

return result;

}

namespace p = boost::python;

namespace np = boost::python::numpy;

np::ndarray convert_to_numpy(mat const & input)

{

u_int n_rows = input.size();

u_int n_cols = input[0].size();

p::tuple shape = p::make_tuple(n_rows, n_cols);

np::dtype dtype = np::dtype::get_builtin();

np::ndarray converted = np::zeros(shape, dtype);

for (u_int i = 0; i < n_rows; i++)

{

for (u_int j = 0; j < n_cols; j++)

{

converted[i][j] = input[i][j];

}

}

return converted;

}

BOOST_PYTHON_MODULE(hermite_cpp)

{

using namespace boost::python;

// Initialize numpy

Py_Initialize();

boost::python::numpy::initialize();

class_("double_vec")

.def(vector_indexing_suite())

;

class_("double_mat")

.def(vector_indexing_suite())

;

def("convert_to_numpy", convert_to_numpy);

def("test", test);

}

python部分:

^{pr2}$

该项目的结果如下:0.56

36.68

26.56

转换能更快吗?或者,甚至更好的是,数组可以在NUMPY和C++之间共享。我在谷歌上搜索了很长时间,但没有多少成功。在

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些错误信息表明在导入numpy库时出现了问题,具体是numpy的某些模块无法正确导入。 这种错误通常是由于numpy库没有正确安装或版本不兼容导致的。 要解决这个问题,你可以尝试以下几个步骤: 1. 首先,请确保你已经正确安装了numpy库。你可以使用pip命令来进行安装,例如在命令行中运行:`pip install numpy`。 2. 如果你已经安装了numpy库,可以尝试升级到最新版本。可以使用pip来更新numpy库,例如在命令行中运行:`pip install --upgrade numpy`。 3. 如果你的操作系统是Windows,还可以尝试重新安装Microsoft Visual C++ Redistributable包。你可以从Microsoft官方网站下载并安装适合你操作系统的包。 4. 如果上述步骤都无法解决问题,你可以考虑卸载并重新安装Python解释器以及numpy库。确保使用最新的Python版本,并且按照官方文档的指示正确安装numpy库。 希望上述解决方法能够帮助你解决问题。如果问题仍然存在,请提供更多详细的错误信息和操作环境,以便我们能够更好地帮助你解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ImportError: numpy.core._multiarray_umath failed to import的解决方案](https://blog.csdn.net/a1456123a/article/details/115375952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python 常见问题 之 from .cv2 import * ImportError: numpy.core.multiarray failed to import](https://blog.csdn.net/u014361280/article/details/109921586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值