ufunc.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 
00014 #include <boost/numpy.hpp>
00015 #include <iostream>
00016 
00017 namespace p = boost::python;
00018 namespace np = boost::numpy;
00019 
00020 
00021 // Create the structs necessary to implement the ufuncs 
00022 // The typedefs *must* be made
00023 
00024 struct UnarySquare 
00025 {
00026   typedef double argument_type;
00027   typedef double result_type;
00028 
00029   double operator()(double r) const { return r * r;}
00030 };
00031 
00032 struct BinarySquare
00033 {
00034   typedef double first_argument_type;
00035   typedef double second_argument_type;
00036   typedef double result_type;
00037 
00038   double operator()(double a,double b) const { return (a*a + b*b) ; }
00039 };
00040 
00041 int main(int argc, char **argv)
00042 {
00043   // Initialize the Python runtime.
00044   Py_Initialize();
00045   // Initialize NumPy
00046   np::initialize();
00047   // Expose the struct UnarySquare to Python as a class, and let ud be the class object
00048   p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare")
00049     .def("__call__", np::unary_ufunc<UnarySquare>::make());
00050   // Let inst be an instance of the class ud
00051   p::object inst = ud();
00052   // Use the "__call__" method to call the overloaded () operator and print the value
00053   std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ; 
00054   // Create an array in C++
00055   int arr[] = {1,2,3,4} ; 
00056   // ..and use it to create the ndarray in Python
00057   np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin<int>() , p::make_tuple(4), p::make_tuple(4), p::object());
00058   // Print out the demo array
00059   std::cout << "Demo array is " << p::extract <char const * > (p::str(demo_array)) << std::endl ; 
00060   // Call the "__call__" method to perform the operation and assign the value to result_array
00061   p::object result_array = inst.attr("__call__")(demo_array) ;
00062   // Print the resultant array
00063   std::cout << "Square of demo array is " << p::extract <char const * > (p::str(result_array)) << std::endl ; 
00064   // Lets try the same with a list
00065   p::list li ;
00066   li.append(3);
00067   li.append(7);
00068   // Print out the demo list
00069   std::cout << "Demo list is " << p::extract <char const * > (p::str(li)) << std::endl ; 
00070   // Call the ufunc for the list
00071   result_array = inst.attr("__call__")(li) ;
00072   // And print the list out
00073   std::cout << "Square of demo list is " << p::extract <char const * > (p::str(result_array)) << std::endl ; 
00074   // Now lets try Binary ufuncs
00075   // Expose the struct BinarySquare to Python as a class, and let ud be the class object
00076   ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare")
00077     .def("__call__", np::binary_ufunc<BinarySquare>::make());
00078   // Again initialise inst as an instance of the class ud
00079   inst = ud();
00080   // Print the two input listsPrint the two input lists
00081   std::cout << "The two input list for binary ufunc are " << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl << p::extract <char const * > (p::str(demo_array)) << std::endl ; 
00082   // Call the binary ufunc taking demo_array as both inputs
00083   result_array = inst.attr("__call__")(demo_array,demo_array) ;
00084   std::cout << "Square of list with binary ufunc is " << p::extract <char const * > (p::str(result_array)) << std::endl ; 
00085 }
00086   


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