unary_ufunc.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. unary_ufunc
  2. ===========
  3. .. contents :: Table of Contents
  4. A ``unary_ufunc`` is a struct used as an intermediate step to broadcast a single argument so that a C++ function can be converted to a ufunc like function
  5. ``<boost/python/numpy/ufunc.hpp>`` contains the ``unary_ufunc`` structure definitions
  6. synopsis
  7. --------
  8. ::
  9. namespace boost
  10. {
  11. namespace python
  12. {
  13. namespace numpy
  14. {
  15. template <typename TUnaryFunctor,
  16. typename TArgument=typename TUnaryFunctor::argument_type,
  17. typename TResult=typename TUnaryFunctor::result_type>
  18. struct unary_ufunc
  19. {
  20. static object call(TUnaryFunctor & self,
  21. object const & input,
  22. object const & output) ;
  23. static object make();
  24. };
  25. }
  26. }
  27. }
  28. constructors
  29. ------------
  30. ::
  31. struct example_unary_ufunc
  32. {
  33. typedef any_valid_type argument_type;
  34. typedef any_valid_type result_type;
  35. };
  36. :Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly
  37. :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
  38. accessors
  39. ---------
  40. ::
  41. template <typename TUnaryFunctor,
  42. typename TArgument=typename TUnaryFunctor::argument_type,
  43. typename TResult=typename TUnaryFunctor::result_type>
  44. static object call(TUnaryFunctor & self,
  45. object const & input,
  46. object const & output);
  47. :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type
  48. :Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments
  49. ::
  50. template <typename TUnaryFunctor,
  51. typename TArgument=typename TUnaryFunctor::argument_type,
  52. typename TResult=typename TUnaryFunctor::result_type>
  53. static object make();
  54. :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type
  55. :Returns: A Python function object to call the overloaded () operator in the struct (in typical usage)
  56. Example(s)
  57. ----------
  58. ::
  59. namespace p = boost::python;
  60. namespace np = boost::python::numpy;
  61. struct UnarySquare
  62. {
  63. typedef double argument_type;
  64. typedef double result_type;
  65. double operator()(double r) const { return r * r;}
  66. };
  67. p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare").def("__call__", np::unary_ufunc<UnarySquare>::make());
  68. p::object inst = ud();
  69. std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ;