From 7add755ae07a4812737c6084ff07673b597e2d1e Mon Sep 17 00:00:00 2001 From: Ankit Daftery Date: Fri, 5 Aug 2011 09:06:19 +0000 Subject: [PATCH] Added tutorial for dtype --- libs/numpy/doc/Jamfile | 2 +- libs/numpy/doc/dtype.rst | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 libs/numpy/doc/dtype.rst diff --git a/libs/numpy/doc/Jamfile b/libs/numpy/doc/Jamfile index f76da2c4..bf38ffb7 100644 --- a/libs/numpy/doc/Jamfile +++ b/libs/numpy/doc/Jamfile @@ -6,7 +6,7 @@ project user-config : requirements 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 diff --git a/libs/numpy/doc/dtype.rst b/libs/numpy/doc/dtype.rst new file mode 100644 index 00000000..8aa0994a --- /dev/null +++ b/libs/numpy/doc/dtype.rst @@ -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 + #include + + 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(); + 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 << "Original array:\n" << p::extract(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(p::str(a.get_dtype())) << std::endl ; + }