[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/boostorg/python/python313/doc/numpy/tutorial/fromdata.rst [Back]  [Original]

How to access data using raw pointers
=====================================

One of the advantages of the ndarray wrapper is that the same data can be used in both Python and C++ and changes can be made to reflect at both ends.
The from_data method makes this possible.

Like before, first get the necessary headers, setup the namespaces and initialize the Python runtime and numpy module::

  #include 
  #include 

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

  int main(int argc, char **argv)
  {
    Py_Initialize();
    np::initialize();

Create an array in C++ , and pass the pointer to it to the from_data method to create an ndarray::

    int arr[] = {1,2,3,4,5};
    np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin(),
                                         p::make_tuple(5),
					 p::make_tuple(sizeof(int)),
					 p::object());

Print the source C++ array, as well as the ndarray, to check if they are the same::

    std::cout 

Web Proxy Viewer  |  New URL  |  Original Page