]>
Commit | Line | Data |
---|---|---|
1 | unary_ufunc | |
2 | =========== | |
3 | ||
4 | .. contents :: Table of Contents | |
5 | ||
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 | |
7 | ||
8 | ``<boost/python/numpy/ufunc.hpp>`` contains the ``unary_ufunc`` structure definitions | |
9 | ||
10 | ||
11 | synopsis | |
12 | -------- | |
13 | ||
14 | :: | |
15 | ||
16 | namespace boost | |
17 | { | |
18 | namespace python | |
19 | { | |
20 | namespace numpy | |
21 | { | |
22 | ||
23 | template <typename TUnaryFunctor, | |
24 | typename TArgument=typename TUnaryFunctor::argument_type, | |
25 | typename TResult=typename TUnaryFunctor::result_type> | |
26 | struct unary_ufunc | |
27 | { | |
28 | ||
29 | static object call(TUnaryFunctor & self, | |
30 | object const & input, | |
31 | object const & output) ; | |
32 | ||
33 | static object make(); | |
34 | ||
35 | }; | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
40 | ||
41 | constructors | |
42 | ------------ | |
43 | ||
44 | :: | |
45 | ||
46 | struct example_unary_ufunc | |
47 | { | |
48 | typedef any_valid_type argument_type; | |
49 | typedef any_valid_type result_type; | |
50 | }; | |
51 | ||
52 | :Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly | |
53 | ||
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 | |
55 | ||
56 | accessors | |
57 | --------- | |
58 | ||
59 | :: | |
60 | ||
61 | template <typename TUnaryFunctor, | |
62 | typename TArgument=typename TUnaryFunctor::argument_type, | |
63 | typename TResult=typename TUnaryFunctor::result_type> | |
64 | static object call(TUnaryFunctor & self, | |
65 | object const & input, | |
66 | object const & output); | |
67 | ||
68 | :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type | |
69 | ||
70 | :Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments | |
71 | ||
72 | :: | |
73 | ||
74 | template <typename TUnaryFunctor, | |
75 | typename TArgument=typename TUnaryFunctor::argument_type, | |
76 | typename TResult=typename TUnaryFunctor::result_type> | |
77 | static object make(); | |
78 | ||
79 | :Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type | |
80 | ||
81 | :Returns: A Python function object to call the overloaded () operator in the struct (in typical usage) | |
82 | ||
83 | ||
84 | ||
85 | Example(s) | |
86 | ---------- | |
87 | ||
88 | :: | |
89 | ||
90 | namespace p = boost::python; | |
91 | namespace np = boost::python::numpy; | |
92 | ||
93 | struct UnarySquare | |
94 | { | |
95 | typedef double argument_type; | |
96 | typedef double result_type; | |
97 | double operator()(double r) const { return r * r;} | |
98 | }; | |
99 | ||
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 ; | |
103 |