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