4 .. contents :: Table of Contents
6 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
8 ``<boost/python/numpy/ufunc.hpp>`` contains the ``unary_ufunc`` structure definitions
23 template <typename TUnaryFunctor,
24 typename TArgument=typename TUnaryFunctor::argument_type,
25 typename TResult=typename TUnaryFunctor::result_type>
29 static object call(TUnaryFunctor & self,
31 object const & output) ;
46 struct example_unary_ufunc
48 typedef any_valid_type argument_type;
49 typedef any_valid_type result_type;
52 :Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly
54 :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
61 template <typename TUnaryFunctor,
62 typename TArgument=typename TUnaryFunctor::argument_type,
63 typename TResult=typename TUnaryFunctor::result_type>
64 static object call(TUnaryFunctor & self,
66 object const & output);
68 :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type
70 :Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments
74 template <typename TUnaryFunctor,
75 typename TArgument=typename TUnaryFunctor::argument_type,
76 typename TResult=typename TUnaryFunctor::result_type>
79 :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type
81 :Returns: A Python function object to call the overloaded () operator in the struct (in typical usage)
90 namespace p = boost::python;
91 namespace np = boost::python::numpy;
95 typedef double argument_type;
96 typedef double result_type;
97 double operator()(double r) const { return r * r;}
100 p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare").def("__call__", np::unary_ufunc<UnarySquare>::make());
101 p::object inst = ud();
102 std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ;