ndarray.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright Ankit Daftery 2011-2012.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /**
  6. * @brief An example to show how to create ndarrays using arbitrary Python sequences.
  7. *
  8. * The Python sequence could be any object whose __array__ method returns an array, or any
  9. * (nested) sequence. This example also shows how to create arrays using both unit and
  10. * non-unit strides.
  11. */
  12. #include <boost/python/numpy.hpp>
  13. #include <iostream>
  14. namespace p = boost::python;
  15. namespace np = boost::python::numpy;
  16. #if _MSC_VER
  17. using boost::uint8_t;
  18. #endif
  19. int main(int argc, char **argv)
  20. {
  21. // Initialize the Python runtime.
  22. Py_Initialize();
  23. // Initialize NumPy
  24. np::initialize();
  25. // Create an ndarray from a simple tuple
  26. p::object tu = p::make_tuple('a','b','c') ;
  27. np::ndarray example_tuple = np::array (tu) ;
  28. // and from a list
  29. p::list l ;
  30. np::ndarray example_list = np::array (l) ;
  31. // Optionally, you can also specify a dtype
  32. np::dtype dt = np::dtype::get_builtin<int>();
  33. np::ndarray example_list1 = np::array (l,dt);
  34. // You can also create an array by supplying data.First,create an integer array
  35. int data[] = {1,2,3,4} ;
  36. // Create a shape, and strides, needed by the function
  37. p::tuple shape = p::make_tuple(4) ;
  38. p::tuple stride = p::make_tuple(4) ;
  39. // The function also needs an owner, to keep track of the data array passed. Passing none is dangerous
  40. p::object own ;
  41. // The from_data function takes the data array, datatype,shape,stride and owner as arguments
  42. // and returns an ndarray
  43. np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
  44. // Print the ndarray we created
  45. std::cout << "Single dimensional array ::" << std::endl << p::extract < char const * > (p::str(data_ex)) << std::endl ;
  46. // Now lets make an 3x2 ndarray from a multi-dimensional array using non-unit strides
  47. // First lets create a 3x4 array of 8-bit integers
  48. uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
  49. // Now let's create an array of 3x2 elements, picking the first and third elements from each row
  50. // For that, the shape will be 3x2
  51. shape = p::make_tuple(3,2) ;
  52. // 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
  53. stride = p::make_tuple(4,2) ;
  54. // Get the numpy dtype for the built-in 8-bit integer data type
  55. np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
  56. // First lets create and print out the ndarray as is
  57. np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
  58. std::cout << "Original multi dimensional array :: " << std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
  59. // Now create the new ndarray using the shape and strides
  60. mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());
  61. // Print out the array we created using non-unit strides
  62. std::cout << "Selective multidimensional array :: "<<std::endl << p::extract < char const * > (p::str(mul_data_ex)) << std::endl ;
  63. }