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

How to use dtypes
=================

Here is a brief tutorial to show how to create ndarrays with built-in python data types, and extract the types and values of member variables

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();

Next, we create the shape and dtype. We use the get_builtin method to get the numpy dtype corresponding to the builtin C++ dtype 
Here, we will create a 3x3 array passing a tuple with (3,3) for the size, and double as the data type ::

    p::tuple shape = p::make_tuple(3, 3);
    np::dtype dtype = np::dtype::get_builtin();
    np::ndarray a = np::zeros(shape, dtype);

Finally, we can print the array using the extract method in the python namespace. 
Here, we first convert the variable into a string, and then extract it as a C++ character array from the python string using the  template ::

    std::cout 

Web Proxy Viewer  |  New URL  |  Original Page