binary_ufunc.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. binary_ufunc
  2. ============
  3. .. contents :: Table of Contents
  4. A ``binary_ufunc`` is a struct used as an intermediate step to broadcast two arguments so that a C++ function can be converted to a ufunc like function
  5. ``<boost/python/numpy/ufunc.hpp>`` contains the ``binary_ufunc`` structure definitions
  6. synopsis
  7. --------
  8. ::
  9. namespace boost
  10. {
  11. namespace python
  12. {
  13. namespace numpy
  14. {
  15. template <typename TBinaryFunctor,
  16. typename TArgument1=typename TBinaryFunctor::first_argument_type,
  17. typename TArgument2=typename TBinaryFunctor::second_argument_type,
  18. typename TResult=typename TBinaryFunctor::result_type>
  19. struct binary_ufunc
  20. {
  21. static object call(TBinaryFunctor & self,
  22. object const & input1,
  23. object const & input2,
  24. object const & output);
  25. static object make();
  26. };
  27. }
  28. }
  29. }
  30. constructors
  31. ------------
  32. ::
  33. struct example_binary_ufunc
  34. {
  35. typedef any_valid first_argument_type;
  36. typedef any_valid second_argument_type;
  37. typedef any_valid result_type;
  38. };
  39. :Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly
  40. :Note: The struct must be exposed as a Python class, and an instance of the class must be created to use the ``call`` method corresponding to the ``__call__`` attribute of the Python object
  41. accessors
  42. ---------
  43. ::
  44. template <typename TBinaryFunctor,
  45. typename TArgument1=typename TBinaryFunctor::first_argument_type,
  46. typename TArgument2=typename TBinaryFunctor::second_argument_type,
  47. typename TResult=typename TBinaryFunctor::result_type>
  48. static object call(TBinaryFunctor & self,
  49. object const & input,
  50. object const & output);
  51. :Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type
  52. :Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments
  53. ::
  54. template <typename TBinaryFunctor,
  55. typename TArgument1=typename TBinaryFunctor::first_argument_type,
  56. typename TArgument2=typename TBinaryFunctor::second_argument_type,
  57. typename TResult=typename TBinaryFunctor::result_type>
  58. static object make();
  59. :Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type
  60. :Returns: A Python function object to call the overloaded () operator in the struct (in typical usage)
  61. Example(s)
  62. ----------
  63. ::
  64. namespace p = boost::python;
  65. namespace np = boost::python::numpy;
  66. struct BinarySquare
  67. {
  68. typedef double first_argument_type;
  69. typedef double second_argument_type;
  70. typedef double result_type;
  71. double operator()(double a,double b) const { return (a*a + b*b) ; }
  72. };
  73. p::object ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare").def("__call__", np::binary_ufunc<BinarySquare>::make());
  74. p::object inst = ud();
  75. result_array = inst.attr("__call__")(demo_array,demo_array) ;
  76. std::cout << "Square of list with binary ufunc is " << p::extract <char const * > (p::str(result_array)) << std::endl ;