|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r73688 - sandbox/numpy/libs/numpy/example
From: ankitdaf_at_[hidden]
Date: 2011-08-12 01:16:21
Author: ankitdaf
Date: 2011-08-12 01:16:20 EDT (Fri, 12 Aug 2011)
New Revision: 73688
URL: http://svn.boost.org/trac/boost/changeset/73688
Log:
Added example for data access using pointers
Added:
sandbox/numpy/libs/numpy/example/fromdata.cpp (contents, props changed)
Text files modified:
sandbox/numpy/libs/numpy/example/Jamfile | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
Modified: sandbox/numpy/libs/numpy/example/Jamfile
==============================================================================
--- sandbox/numpy/libs/numpy/example/Jamfile (original)
+++ sandbox/numpy/libs/numpy/example/Jamfile 2011-08-12 01:16:20 EDT (Fri, 12 Aug 2011)
@@ -22,3 +22,5 @@
exe simple : simple.cpp ../src//boost_numpy boost_python /python//python ;
exe dtype : dtype.cpp ../src//boost_numpy boost_python /python//python ;
exe ndarray : ndarray.cpp ../src//boost_numpy boost_python /python//python ;
+exe hybrid : hybrid.cpp ../src//boost_numpy boost_python /python//python ;
+exe fromdata : fromdata.cpp ../src//boost_numpy boost_python /python//python ;
Added: sandbox/numpy/libs/numpy/example/fromdata.cpp
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/numpy/example/fromdata.cpp 2011-08-12 01:16:20 EDT (Fri, 12 Aug 2011)
@@ -0,0 +1,46 @@
+/**
+ * @brief An example to show how to create ndarrays from C++ containers of (almost) arbitrary type.
+ *
+ */
+
+#include <boost/numpy.hpp>
+#include <vector>
+#include <iostream>
+
+namespace p = boost::python;
+namespace np = boost::numpy;
+
+
+int main(int argc, char **argv)
+{
+ // Initialize the Python runtime.
+ Py_Initialize();
+ // Initialize NumPy
+ np::initialize();
+ // Create an array in C++
+ int arr[] = {1,2,3,4} ;
+ // Create the ndarray in Python
+ np::ndarray py_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
+ // Print the ndarray that we created, just to be sure
+ std::cout << "C++ array :" << std::endl ;
+ for (int j=0;j<4;j++)
+ {
+ std::cout << arr[j] << ' ' ;
+ }
+ std::cout << std::endl << "Python ndarray :" << p::extract<char const *>(p::str(py_array)) << std::endl;
+ // Change an element in the python ndarray
+ py_array[1] = 5 ;
+ // And see if the C++ container is changed or not
+ std::cout << "Is the change reflected in the C++ array used to create the ndarray ? " << std::endl ;
+ for (int j = 0;j<4 ; j++)
+ {
+ std::cout << arr[j] << ' ' ;
+ }
+ // Conversely, change it in C++
+ arr[2] = 8 ;
+ // And see if the changes are reflected in the Python ndarray
+ std::cout << std::endl << "Is the change reflected in the Python ndarray ?" << std::endl << p::extract<char const *>(p::str(py_array)) << std::endl;
+
+ // Now print it.
+// std::cout << "Pixel array :" << p::extract<char const *>(p::str(py_array)) << std::endl;
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk