Works great ! Thanks ! Code attached.

Franck

>> make; python dummyTest1.py; python dummyTest2.py; python dummyTest3.py

g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp
-lboost_python -lboost_numpy

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x555a42aede80
ptrInt[1] = 2, 0x555a42aede84
ptrInt[2] = 3, 0x555a42aede88
ptrFloat[0] = 1.2, 0x555a429c4f60
ptrFloat[1] = 2.3, 0x555a429c4f64
ptrFloat[2] = 3.4, 0x555a429c4f68
ptrDouble[0] = 4.5, 0x555a429c4f80
ptrDouble[1] = 5.6, 0x555a429c4f88
ptrDouble[2] = 6.7, 0x555a429c4f90

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x55dbaa536470
ptrInt[1] = 2, 0x55dbaa536474
ptrInt[2] = 3, 0x55dbaa536478
ptrFloat[0] = 1.2, 0x55dbaa4b9bb0
ptrFloat[1] = 2.3, 0x55dbaa4b9bb4
ptrFloat[2] = 3.4, 0x55dbaa4b9bb8
ptrDouble[0] = 4.5, 0x55dbaa4b9bd0
ptrDouble[1] = 5.6, 0x55dbaa4b9bd8
ptrDouble[2] = 6.7, 0x55dbaa4b9be0

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x563725f958f0
ptrInt[1] = 2, 0x563725f958f4
ptrInt[2] = 3, 0x563725f958f8
ptrFloat[0] = 1.2, 0x563725e87a10
ptrFloat[1] = 2.3, 0x563725e87a14
ptrFloat[2] = 3.4, 0x563725e87a18
ptrDouble[0] = 4.5, 0x563725fc0de0
ptrDouble[1] = 5.6, 0x563725fc0de8
ptrDouble[2] = 6.7, 0x563725fc0df0

Le mar. 11 févr. 2020 à 20:12, stefan <ste...@seefeld.name> a écrit :

>
> On 2020-02-11 2:05 p.m., HOUSSEN Franck wrote:
>
> OK, I understand this is a type related problem. I found a workaround
> (dummyTest3.py - init numpy arrays with list seems to work).
>
> But, I unfortunately do not get how to change the code to get
> dummyTest2.py to work ?!... Should I read reinterpret_cast'ed data
> sizeof(double) by sizeof(double) : seems cryptic (?!), what's the natural
> way to do that ?
>
> Can't you change your Python code so instead of
>
>   np.append(lsFloat, 1.2)
>
> you would call
>
>   np.append(lsFloat, np.float32(1.2))
>
> to make sure the casting happens before the broadcasting.
>
> (Note: I haven't actually tried that; it just seems the natural fix to the
> issue ;-) )
> [image: Stefan]
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
#!/usr/bin/env python

import numpy as np
ls = [1, 2, 3]
lsInt = np.array(ls, dtype='int32')
ls = [1.2, 2.3, 3.4]
lsFloat = np.array(ls, dtype='float32')
ls = [4.5, 5.6, 6.7]
lsDouble = np.array(ls, dtype='float64')

t = (lsInt, lsFloat, lsDouble)
import dummy

print(lsInt)
print(lsFloat)
print(lsDouble)
dummy.doStuffs(t)
#!/usr/bin/env python

import numpy as np
lsInt = np.array([1, 2, 3], dtype='int32')
lsFloat = np.array([1.2, 2.3, 3.4], dtype='float32')
lsDouble = np.array([4.5, 5.6, 6.7], dtype='float64')

t = (lsInt, lsFloat, lsDouble)
import dummy

print(lsInt)
print(lsFloat)
print(lsDouble)
dummy.doStuffs(t)

Attachment: Makefile
Description: Binary data

#!/usr/bin/env python

import numpy as np
lsInt = np.array([], dtype='int32')
lsInt = np.append(lsInt, np.int32(1))
lsInt = np.append(lsInt, np.int32(2))
lsInt = np.append(lsInt, np.int32(3))
lsFloat = np.array([], dtype='float32')
lsFloat = np.append(lsFloat, np.float32(1.2))
lsFloat = np.append(lsFloat, np.float32(2.3))
lsFloat = np.append(lsFloat, np.float32(3.4))
lsDouble = np.array([], dtype='float64')
lsDouble = np.append(lsDouble, np.float64(4.5))
lsDouble = np.append(lsDouble, np.float64(5.6))
lsDouble = np.append(lsDouble, np.float64(6.7))

t = (lsInt, lsFloat, lsDouble)
import dummy

print(lsInt)
print(lsFloat)
print(lsDouble)
dummy.doStuffs(t)
#include <iostream>

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

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

void doStuffs(boost::python::tuple & t) {
  boost::python::extract<np::ndarray> lsIntExt(t[0]);
  if (lsIntExt.check()) {
    np::ndarray lsInt = lsIntExt(); // Conversion to np::ndarray.
    int nbInt = lsInt.shape(0);
    int * ptrInt = reinterpret_cast<int*>(lsInt.get_data());
    for(auto k = 0; ptrInt && k < nbInt; k++) {std::cout << "ptrInt[" << k << "] = " << ptrInt[k] << ", " << ptrInt+k << std::endl;};
  }

  boost::python::extract<np::ndarray> lsFloatExt(t[1]);
  if (lsFloatExt.check()) {
    np::ndarray lsFloat = lsFloatExt(); // Conversion to np::ndarray.
    int nbFloat = lsFloat.shape(0);
    float * ptrFloat = reinterpret_cast<float*>(lsFloat.get_data());
    for(auto k = 0; k < nbFloat; k++) {std::cout << "ptrFloat[" << k << "] = " << ptrFloat[k] << ", " << ptrFloat+k << std::endl;};
  }

  boost::python::extract<np::ndarray> lsDoubleExt(t[2]);
  if (lsDoubleExt.check()) {
    np::ndarray lsDouble = lsDoubleExt(); // Conversion to np::ndarray.
    int nbDouble = lsDouble.shape(0);
    double * ptrDouble = reinterpret_cast<double*>(lsDouble.get_data());
    for(auto k = 0; k < nbDouble; k++) {std::cout << "ptrDouble[" << k << "] = " << ptrDouble[k] << ", " << ptrDouble+k << std::endl;};
  }
}

BOOST_PYTHON_MODULE(dummy) {
  np::initialize();
  def("doStuffs", doStuffs);
}
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to