fromdata.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 
00011 #include <boost/numpy.hpp>
00012 #include <iostream>
00013 
00014 namespace p = boost::python;
00015 namespace np = boost::numpy;
00016 
00017 
00018 int main(int argc, char **argv)
00019 {
00020   // Initialize the Python runtime.
00021   Py_Initialize();
00022   // Initialize NumPy
00023   np::initialize();
00024   // Create an array in C++
00025   int arr[] = {1,2,3,4} ; 
00026   // Create the ndarray in Python
00027   np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
00028   // Print the ndarray that we just created, and the source C++ array
00029   std::cout << "C++ array :" << std::endl ;
00030   for (int j=0;j<4;j++)
00031   {
00032     std::cout << arr[j] << ' ' ;
00033   }
00034   std::cout << std::endl << "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
00035   // Change an element in the python ndarray
00036   py_array[1] = 5 ; 
00037   // And see if the C++ container is changed or not
00038   std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ; 
00039   for (int j = 0;j<4 ; j++)
00040   {
00041     std::cout << arr[j] << ' ' ;
00042   }
00043   // Conversely, change it in C++
00044   arr[2] = 8 ;
00045   // And see if the changes are reflected in the Python ndarray
00046   std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract<char const *>(p::str(py_array)) << std::endl;
00047 
00048 }


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