Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73694 - sandbox/numpy/libs/numpy/doc
From: ankitdaf_at_[hidden]
Date: 2011-08-12 05:26:56


Author: ankitdaf
Date: 2011-08-12 05:26:55 EDT (Fri, 12 Aug 2011)
New Revision: 73694
URL: http://svn.boost.org/trac/boost/changeset/73694

Log:
Added tutorial for fromdata, i.e. copy free data access
Added:
   sandbox/numpy/libs/numpy/doc/fromdata.rst (contents, props changed)
Text files modified:
   sandbox/numpy/libs/numpy/doc/Jamfile | 2 +-
   1 files changed, 1 insertions(+), 1 deletions(-)

Modified: sandbox/numpy/libs/numpy/doc/Jamfile
==============================================================================
--- sandbox/numpy/libs/numpy/doc/Jamfile (original)
+++ sandbox/numpy/libs/numpy/doc/Jamfile 2011-08-12 05:26:55 EDT (Fri, 12 Aug 2011)
@@ -6,7 +6,7 @@
 import docutils ;
 
 import path ;
-sources = tutorial.rst dtype.rst ndarray.rst ;
+sources = tutorial.rst dtype.rst ndarray.rst fromdata.rst ;
 bases = $(sources:S=) ;
   
 # This is a path relative to the html/ subdirectory where the

Added: sandbox/numpy/libs/numpy/doc/fromdata.rst
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/numpy/doc/fromdata.rst 2011-08-12 05:26:55 EDT (Fri, 12 Aug 2011)
@@ -0,0 +1,51 @@
+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 <boost/numpy.hpp>
+ #include <iostream>
+
+ namespace p = boost::python;
+ namespace np = boost::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} ;
+ np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
+
+Print the source C++ array, as well as the ndarray, to check if they are the same::
+
+ std::cout << "C++ array :" << std::endl ;
+ for (int j=0;j<4;j++)
+ {
+ std::cout << arr[j] << ' ' ;
+ }
+ std::cout << std::endl << "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
+
+Now, change an element in the Python ndarray, and check if the value changed correspondingly in the source C++ array::
+
+ py_array[1] = 5 ;
+ std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ;
+ for (int j = 0;j<4 ; j++)
+ {
+ std::cout << arr[j] << ' ' ;
+ }
+
+Next, change an element of the source C++ array and see if it is reflected in the Python ndarray::
+
+ arr[2] = 8 ;
+ std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract<char const *>(p::str(py_array)) << std::endl;
+
+}
+
+As we can see, the changes are reflected across the ends. This happens because the from_data method passes the C++ array by reference to create the ndarray, and thus uses the same locations for storing data.
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk