dtype.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 
00015 #include <boost/numpy.hpp>
00016 #include <iostream>
00017 
00018 namespace p = boost::python;
00019 namespace np = boost::numpy;
00020 
00021 int main(int argc, char **argv)
00022 {
00023   // Initialize the Python runtime.
00024   Py_Initialize();
00025   // Initialize NumPy
00026   np::initialize();
00027   // Create a 3x3 shape...
00028   p::tuple shape = p::make_tuple(3, 3);
00029   // ...as well as a type for C++ double
00030   np::dtype dtype = np::dtype::get_builtin<double>();
00031   // Construct an array with the above shape and type
00032   np::ndarray a = np::zeros(shape, dtype);
00033   // Print the array
00034   std::cout << "Original array:\n" << p::extract<char const *>(p::str(a)) << std::endl;
00035   // Print the datatype of the elements
00036   std::cout << "Datatype is:\n" << p::extract<char const *>(p::str(a.get_dtype())) << std::endl ;
00037   // Using user defined dtypes to create dtype and an array of the custom dtype
00038   // First create a tuple with a variable name and its dtype, double, to create a custom dtype
00039   p::tuple for_custom_dtype = p::make_tuple("ha",dtype) ;
00040   // The list needs to be created, because the constructor to create the custom dtype
00041   // takes a list of (variable,variable_type) as an argument
00042   p::list list_for_dtype ;
00043   list_for_dtype.append(for_custom_dtype) ;
00044   // Create the custom dtype
00045   np::dtype custom_dtype = np::dtype(list_for_dtype) ;
00046   // Create an ndarray with the custom dtype
00047   np::ndarray new_array = np::zeros(shape,custom_dtype);
00048 
00049 }


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