ufunc.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Ufuncs
  2. ======
  3. Ufuncs or universal functions operate on ndarrays element by element, and support array broadcasting, type casting, and other features.
  4. Lets try and see how we can use the binary and unary ufunc methods
  5. After the neccessary includes ::
  6. #include <boost/python/numpy.hpp>
  7. #include <iostream>
  8. namespace p = boost::python;
  9. namespace np = boost::python::numpy;
  10. Now we create the structs necessary to implement the ufuncs. The typedefs *must* be made as the ufunc generators take these typedefs as inputs and return an error otherwise ::
  11. struct UnarySquare
  12. {
  13. typedef double argument_type;
  14. typedef double result_type;
  15. double operator()(double r) const { return r * r;}
  16. };
  17. struct BinarySquare
  18. {
  19. typedef double first_argument_type;
  20. typedef double second_argument_type;
  21. typedef double result_type;
  22. double operator()(double a,double b) const { return (a*a + b*b) ; }
  23. };
  24. Initialise the Python runtime and the numpy module ::
  25. int main(int argc, char **argv)
  26. {
  27. Py_Initialize();
  28. np::initialize();
  29. Now expose the struct UnarySquare to Python as a class, and let ud be the class object. ::
  30. p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare");
  31. ud.def("__call__", np::unary_ufunc<UnarySquare>::make());
  32. Let inst be an instance of the class ud ::
  33. p::object inst = ud();
  34. Use the "__call__" method to call the overloaded () operator and print the value ::
  35. std::cout << "Square of unary scalar 1.0 is " << p::extract<char const *>(p::str(inst.attr("__call__")(1.0))) << std::endl;
  36. Create an array in C++ ::
  37. int arr[] = {1,2,3,4} ;
  38. ..and use it to create the ndarray in Python ::
  39. np::ndarray demo_array = np::from_data(arr, np::dtype::get_builtin<int>(),
  40. p::make_tuple(4),
  41. p::make_tuple(4),
  42. p::object());
  43. Print out the demo array ::
  44. std::cout << "Demo array is " << p::extract<char const *>(p::str(demo_array)) << std::endl;
  45. Call the "__call__" method to perform the operation and assign the value to result_array ::
  46. p::object result_array = inst.attr("__call__")(demo_array);
  47. Print the resultant array ::
  48. std::cout << "Square of demo array is " << p::extract<char const *>(p::str(result_array)) << std::endl;
  49. Lets try the same with a list ::
  50. p::list li;
  51. li.append(3);
  52. li.append(7);
  53. Print out the demo list ::
  54. std::cout << "Demo list is " << p::extract<char const *>(p::str(li)) << std::endl;
  55. Call the ufunc for the list ::
  56. result_array = inst.attr("__call__")(li);
  57. And print the list out ::
  58. std::cout << "Square of demo list is " << p::extract<char const *>(p::str(result_array)) << std::endl;
  59. Now lets try Binary ufuncs. Again, expose the struct BinarySquare to Python as a class, and let ud be the class object ::
  60. ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare");
  61. ud.def("__call__", np::binary_ufunc<BinarySquare>::make());
  62. And initialise ud ::
  63. inst = ud();
  64. Print the two input lists ::
  65. std::cout << "The two input list for binary ufunc are " << std::endl
  66. << p::extract<char const *>(p::str(demo_array)) << std::endl
  67. << p::extract<char const *>(p::str(demo_array)) << std::endl;
  68. Call the binary ufunc taking demo_array as both inputs ::
  69. result_array = inst.attr("__call__")(demo_array,demo_array);
  70. And print the output ::
  71. std::cout << "Square of list with binary ufunc is " << p::extract<char const *>(p::str(result_array)) << std::endl;
  72. }