Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r73516 - sandbox/numpy/libs/numpy/example
From: ankitdaf_at_[hidden]
Date: 2011-08-03 16:07:21


Author: ankitdaf
Date: 2011-08-03 16:07:20 EDT (Wed, 03 Aug 2011)
New Revision: 73516
URL: http://svn.boost.org/trac/boost/changeset/73516

Log:
New examples
Added:
   sandbox/numpy/libs/numpy/example/dtype.cpp (contents, props changed)
   sandbox/numpy/libs/numpy/example/ndarray.cpp (contents, props changed)

Added: sandbox/numpy/libs/numpy/example/dtype.cpp
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/numpy/example/dtype.cpp 2011-08-03 16:07:20 EDT (Wed, 03 Aug 2011)
@@ -0,0 +1,33 @@
+/**
+ * @brief An example to show how to create ndarrays with built-in python data types, and extract the types and values
+ * of member variables
+ *
+ * @todo Add an example to show type conversion.
+ * Add an example to show use of user-defined types
+ * Doesn't work for char. Works for int, long int, short int, float, double
+ */
+
+#include <boost/numpy.hpp>
+#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 a 3x3 shape...
+ p::tuple shape = p::make_tuple(3, 3);
+ // ...as well as a type for C++ double
+ np::dtype dtype = np::dtype::get_builtin<double>();
+ // Construct an array with the above shape and type
+ np::ndarray a = np::zeros(shape, dtype);
+ // Print the array
+ std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
+ // Print the datatype of the elements
+ std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
+
+}

Added: sandbox/numpy/libs/numpy/example/ndarray.cpp
==============================================================================
--- (empty file)
+++ sandbox/numpy/libs/numpy/example/ndarray.cpp 2011-08-03 16:07:20 EDT (Wed, 03 Aug 2011)
@@ -0,0 +1,33 @@
+/**
+ * @brief An example to show how to create ndarrays using arbitrary Python sequences
+ * The Python sequence could be any object whose __array__ method returns an array, or any (nested) sequence.
+ *
+ * @todo Find a way to create a list explicitly
+ *
+ */
+
+#include <boost/numpy.hpp>
+#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 ndarray from a simple tuple
+ p::object tu = p::make_tuple('a','b','c') ;
+ np::ndarray example_tuple = np::array (tu) ;
+ // and from a list
+ p::list l ;
+ l.append('a') ;
+ np::ndarray example_list = np::array (l) ;
+ // Optionally, you can also specify a dtype
+ np::dtype dt = np::dtype::get_builtin<int>();
+ np::ndarray example_list1 = np::array (l,dt);
+}
+
+


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