• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1binary_ufunc
2============
3
4.. contents :: Table of Contents
5
6A ``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
7
8 ``<boost/python/numpy/ufunc.hpp>`` contains the ``binary_ufunc`` structure definitions
9
10
11synopsis
12--------
13
14::
15
16  namespace boost
17  {
18  namespace python
19  {
20  namespace numpy
21  {
22
23  template <typename TBinaryFunctor,
24            typename TArgument1=typename TBinaryFunctor::first_argument_type,
25            typename TArgument2=typename TBinaryFunctor::second_argument_type,
26            typename TResult=typename TBinaryFunctor::result_type>
27
28  struct binary_ufunc
29  {
30
31    static object call(TBinaryFunctor & self,
32                       object const & input1,
33                       object const & input2,
34                       object const & output);
35
36    static object make();
37  };
38
39  }
40  }
41  }
42
43
44constructors
45------------
46
47::
48
49  struct example_binary_ufunc
50  {
51    typedef any_valid first_argument_type;
52    typedef any_valid second_argument_type;
53    typedef any_valid result_type;
54  };
55
56:Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly
57
58: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
59
60accessors
61---------
62
63::
64
65  template <typename TBinaryFunctor,
66            typename TArgument1=typename TBinaryFunctor::first_argument_type,
67            typename TArgument2=typename TBinaryFunctor::second_argument_type,
68            typename TResult=typename TBinaryFunctor::result_type>
69  static object call(TBinaryFunctor & self,
70                     object const & input,
71                     object const & output);
72
73:Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type
74
75:Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments
76
77::
78
79  template <typename TBinaryFunctor,
80            typename TArgument1=typename TBinaryFunctor::first_argument_type,
81            typename TArgument2=typename TBinaryFunctor::second_argument_type,
82            typename TResult=typename TBinaryFunctor::result_type>
83  static object make();
84
85:Requires: Typenames ``TBinaryFunctor`` and optionally ``TArgument1`` and ``TArgument2`` for argument type and ``TResult`` for result type
86
87:Returns: A Python function object to call the overloaded () operator in the struct (in typical usage)
88
89Example(s)
90----------
91
92::
93
94  namespace p = boost::python;
95  namespace np = boost::python::numpy;
96
97  struct BinarySquare
98  {
99    typedef double first_argument_type;
100    typedef double second_argument_type;
101    typedef double result_type;
102
103    double operator()(double a,double b) const { return (a*a + b*b) ; }
104  };
105
106  p::object ud = p::class_<BinarySquare, boost::shared_ptr<BinarySquare> >("BinarySquare").def("__call__", np::binary_ufunc<BinarySquare>::make());
107  p::object inst = ud();
108  result_array = inst.attr("__call__")(demo_array,demo_array) ;
109  std::cout << "Square of list with binary ufunc is " << p::extract <char const * > (p::str(result_array)) << std::endl ;
110
111