2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 18:12:43 +00:00

Added tutorial for dtype

This commit is contained in:
Ankit Daftery
2011-08-05 09:06:19 +00:00
parent 79182d7189
commit 7add755ae0
2 changed files with 35 additions and 1 deletions

View File

@@ -6,7 +6,7 @@ project user-config : requirements <docutils-cmd>rst2html ;
import docutils ;
import path ;
sources = tutorial.rst ndarray.rst ;
sources = tutorial.rst dtype.rst ndarray.rst ;
bases = $(sources:S=) ;
# This is a path relative to the html/ subdirectory where the

34
libs/numpy/doc/dtype.rst Normal file
View File

@@ -0,0 +1,34 @@
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 <boost/numpy.hpp>
#include <iostream>
namespace p = boost::python;
namespace np = boost::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<double>();
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 <char const \* > template ::
std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
We can also print the dtypes of the data members of the ndarray by using the get_dtype method for the ndarray ::
std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
}