ndarray.cpp
Go to the documentation of this file.
00001 // Copyright Ankit Daftery 2011-2012.
00002 // Distributed under the Boost Software License, Version 1.0.
00003 //    (See accompanying file LICENSE_1_0.txt or copy at
00004 //          http://www.boost.org/LICENSE_1_0.txt)
00005 
00014 #include <boost/numpy.hpp>
00015 #include <iostream>
00016 
00017 namespace p = boost::python;
00018 namespace np = boost::numpy;
00019 
00020 #if _MSC_VER
00021 using boost::uint8_t;
00022 #endif
00023 
00024 int main(int argc, char **argv)
00025 {
00026   // Initialize the Python runtime.
00027   Py_Initialize();
00028   // Initialize NumPy
00029   np::initialize();
00030   // Create an ndarray from a simple tuple
00031   p::object tu = p::make_tuple('a','b','c') ;
00032   np::ndarray example_tuple = np::array (tu) ; 
00033   // and from a list
00034   p::list l ;
00035   np::ndarray example_list = np::array (l) ; 
00036   // Optionally, you can also specify a dtype
00037   np::dtype dt = np::dtype::get_builtin<int>();
00038   np::ndarray example_list1 = np::array (l,dt);
00039   // You can also create an array by supplying data.First,create an integer array
00040   int data[] = {1,2,3,4} ;
00041   // Create a shape, and strides, needed by the function
00042   p::tuple shape = p::make_tuple(4) ;
00043   p::tuple stride = p::make_tuple(4) ; 
00044   // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous
00045   p::object own ;
00046   // The from_data function takes the data array, datatype,shape,stride and owner as arguments
00047   // and returns an ndarray
00048   np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
00049   // Print the ndarray we created
00050   std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ; 
00051   // Now lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
00052   // First lets create a 3x4 array of 8-bit integers
00053   uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
00054   // Now let's create an array of 3x2 elements, picking the first and third elements from each row
00055   // For that, the shape will be 3x2
00056   shape = p::make_tuple(3,2) ;
00057   // 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
00058   stride = p::make_tuple(4,2) ; 
00059   // Get the numpy dtype for the built-in 8-bit integer data type
00060   np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
00061   // First lets create and print out the ndarray as is
00062   np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
00063   std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ; 
00064   // Now create the new ndarray using the shape and strides
00065   mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());
00066   // Print out the array we created using non-unit strides
00067   std::cout << "Selective multidimensional array :: "<<std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ; 
00068 
00069 }
00070 
00071 


boost_numpy
Author(s): Jim Bosch, Ankit Daftery
autogenerated on Fri Aug 28 2015 10:10:40