• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 Stefan Seefeld.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/python/numpy.hpp>
7 #include <iostream>
8 
9 namespace p = boost::python;
10 namespace np = boost::python::numpy;
11 
main(int argc,char ** argv)12 int main(int argc, char **argv)
13 {
14   // Initialize the Python runtime.
15   Py_Initialize();
16   // Initialize NumPy
17   np::initialize();
18   // Create a 3x3 shape...
19   p::tuple shape = p::make_tuple(3, 3);
20   // ...as well as a type for C++ float
21   np::dtype dtype = np::dtype::get_builtin<float>();
22   // Construct an array with the above shape and type
23   np::ndarray a = np::zeros(shape, dtype);
24   // Construct an empty array with the above shape and dtype as well
25   np::ndarray b = np::empty(shape,dtype);
26   // Print the array
27   std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
28   // Reshape the array into a 1D array
29   a = a.reshape(p::make_tuple(9));
30   // Print it again.
31   std::cout << "Reshaped array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
32 }
33