simple.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132
  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. #include <boost/python/numpy.hpp>
  6. #include <iostream>
  7. namespace p = boost::python;
  8. namespace np = boost::python::numpy;
  9. int main(int argc, char **argv)
  10. {
  11. // Initialize the Python runtime.
  12. Py_Initialize();
  13. // Initialize NumPy
  14. np::initialize();
  15. // Create a 3x3 shape...
  16. p::tuple shape = p::make_tuple(3, 3);
  17. // ...as well as a type for C++ float
  18. np::dtype dtype = np::dtype::get_builtin<float>();
  19. // Construct an array with the above shape and type
  20. np::ndarray a = np::zeros(shape, dtype);
  21. // Construct an empty array with the above shape and dtype as well
  22. np::ndarray b = np::empty(shape,dtype);
  23. // Print the array
  24. std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
  25. // Reshape the array into a 1D array
  26. a = a.reshape(p::make_tuple(9));
  27. // Print it again.
  28. std::cout << "Reshaped array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
  29. }