Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

inconsistent numpy dtype in python and C++

Aperta
#304 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
cpp, numpy, python
Ambito
api, backend

Direzione di ricerca

Riproduci il report utilizzando l’estensione dummy.cpp fornita e lo script run_dummpy.py, ed eseguilo con le versioni indicate di Python, NumPy e Boost. Esamina come i due argomenti ndarray raggiungono il binding Boost.NumPy e verifica che ciascuno mantenga il proprio dtype; il lavoro è completato quando l’output C++ corrisponde coerentemente a float32 e float64.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
C++
Stelle
537
Fork
223
Merge medio
11h 22m
PR unite (30g)
2

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/python

Tutte le issue di boostorg/python

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.