diff --git a/libs/numpy/example/dtype.cpp b/libs/numpy/example/dtype.cpp new file mode 100644 index 00000000..33e040d4 --- /dev/null +++ b/libs/numpy/example/dtype.cpp @@ -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 +#include + +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(); + // 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(p::str(a)) << std::endl; + // Print the datatype of the elements + std::cout << "Datatype is:\n" << p::extract(p::str(a.get_dtype())) << std::endl ; + +} diff --git a/libs/numpy/example/ndarray.cpp b/libs/numpy/example/ndarray.cpp new file mode 100644 index 00000000..9bfd5dc5 --- /dev/null +++ b/libs/numpy/example/ndarray.cpp @@ -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 +#include + +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(); + np::ndarray example_list1 = np::array (l,dt); +} + +