Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73723 - sandbox/numpy/libs/numpy/example
From: ankitdaf_at_[hidden]
Date: 2011-08-14 02:48:47


Author: ankitdaf
Date: 2011-08-14 02:48:46 EDT (Sun, 14 Aug 2011)
New Revision: 73723
URL: http://svn.boost.org/trac/boost/changeset/73723

Log:
Added non-unit strides example
Text files modified:
   sandbox/numpy/libs/numpy/example/ndarray.cpp | 31 +++++++++++++++++++++++++------
   1 files changed, 25 insertions(+), 6 deletions(-)

Modified: sandbox/numpy/libs/numpy/example/ndarray.cpp
==============================================================================
--- sandbox/numpy/libs/numpy/example/ndarray.cpp (original)
+++ sandbox/numpy/libs/numpy/example/ndarray.cpp 2011-08-14 02:48:46 EDT (Sun, 14 Aug 2011)
@@ -1,7 +1,7 @@
 /**
- * @brief An example to show how to create ndarrays using arbitrary Python sequences
+ * @brief An example to show how to create ndarrays using arbitrary Python sequences.
  * The Python sequence could be any object whose __array__ method returns an array, or any (nested) sequence.
- *
+ * This example also shows how to create arrays using both unit and non-unit strides
  *
  */
 
@@ -29,13 +29,32 @@
   // You can also create an array by supplying data.First,create an integer array
   int data[] = {1,2,3,4} ;
   // Create a shape, and strides, needed by the function
- p::tuple shape = p::make_tuple(2,2) ;
- p::tuple strides = p::make_tuple(strides(data)) ;
+ p::tuple shape = p::make_tuple(4) ;
+ p::tuple stride = p::make_tuple(4) ;
   // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous
- p::object owner ;
+ p::object own ;
   // The from_data function takes the data array, datatype,shape,stride and owner as arguments
   // and returns an ndarray
- np::ndarray data_ex1 = np::from_data(data,dt, shape,strides,owner);
+ np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
+ // Print the ndarray we created
+ std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ;
+ // Now lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
+ // First lets create a 3x4 array of 8-bit integers
+ uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
+ // Now let's create an array of 3x2 elements, picking the first and third elements from each row
+ // For that, the shape will be 3x2
+ shape = p::make_tuple(3,2) ;
+ // The strides will be 4x2 i.e. 4 bytes to go to the next desired row, and 2 bytes to go to the next desired column
+ stride = p::make_tuple(4,2) ;
+ // Get the numpy dtype for the built-in 8-bit integer data type
+ np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
+ // First lets create and print out the ndarray as is
+ np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
+ std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
+ // Now create the new ndarray using the shape and strides
+ mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());
+ // Print out the array we created using non-unit strides
+ std::cout << "Selective multidimensional array :: "<<std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
 
 }
 


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