Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

inconsistent numpy dtype in python and C++

未關閉
#304 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
cpp, numpy, python
領域
api, backend

研究方向

使用提供的 dummy.cpp 擴充功能和 run_dummpy.py 指令碼重現該報告,並使用所述版本的 Python、NumPy 和 Boost 執行。檢查兩個 ndarray 引數如何到達 Boost.NumPy binding,並驗證每個引數都保留其 dtype;當 C++ 輸出始終分別與 float32 和 float64 相符時,即表示完成。

由索引模型根據 Issue 內容生成。

描述

Hi,
I'm trying to use the boost numpy bindings. If I pass two ndarray with different dtype to C++, they don't preserve the correct dtype. Here is a simple example:
dummy.cpp:

#include <iostream>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>

namespace py = boost::python;
namespace np = boost::python::numpy;

void dummy(np::ndarray const& a, np::ndarray const& b) {

    char const* at = py::extract<char const* >(py::str(a.get_dtype()));
    char const* bt = py::extract<char const* >(py::str(b.get_dtype()));

    std::cout << at << std::endl;
    std::cout << bt << std::endl;
}


BOOST_PYTHON_MODULE(dummy) {
   Py_Initialize(); // need initialize in BOOST_PYTHON_MODULE, or will get segmentation fault and core dump 
   np::initialize();
   py::def("dummy", dummy);
}

compile

g++ -lboost_python38 -lboost_numpy38 -L$CONDA_PREFIX/lib -I$CONDA_PREFIX/include -I$CONDA_PREFIX/include/python3.8 -fPIC --shared -o dummy.so dummy.cpp

run_dummpy.py

import numpy as np
import dummy

a = np.array([[1, 2, 3], [4, 5, 6]], dtype = np.float32)
print("python", a.dtype)
b = np.array([[1, 2, 3], [4, 5, 6]], dtype = np.double)
print("python", b.dtype)

dummy.dummy(a, b)

The output is inconsistent. The first time I run it by python run_dummy.py it gives

python float32
python float64
__str__
float64

If I re-run it python run_dummy.py , it gives

python float32
python float64
float64
float64

Still not the right output.
I'm using python 3.8.2, numpy 1.18.1, and boost 1.72.0. boost is installed from anaconda conda install -c conda-forge boost
Could you help? Thanks

主要語言
C++
星號
537
分支
223
平均合併
11 小時 22 分鐘
30 天內合併 PR
2

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

boostorg/python 的其他 Issue

查看 boostorg/python 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。