File: unary_ufunc.rst

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (103 lines) | stat: -rw-r--r-- 2,790 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
unary_ufunc
===========

.. contents :: Table of Contents

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

 ``<boost/python/numpy/ufunc.hpp>`` contains the ``unary_ufunc`` structure definitions


synopsis
--------

::

  namespace boost 
  {
  namespace python
  {
  namespace numpy 
  {

  template <typename TUnaryFunctor, 
            typename TArgument=typename TUnaryFunctor::argument_type, 
            typename TResult=typename TUnaryFunctor::result_type>
  struct unary_ufunc 
  {

    static object call(TUnaryFunctor & self, 
                       object const & input, 
                       object const & output) ;

    static object make(); 

  };
  }
  }
  }


constructors
------------

::

  struct example_unary_ufunc
  {
    typedef any_valid_type argument_type;
    typedef any_valid_type result_type;
  };

:Requirements: The ``any_valid`` type must be defined using typedef as a valid C++ type in order to use the struct methods correctly

: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

accessors
---------

::

  template <typename TUnaryFunctor, 
            typename TArgument=typename TUnaryFunctor::argument_type,
            typename TResult=typename TUnaryFunctor::result_type>
  static object call(TUnaryFunctor & self, 
                     object const & input, 
                     object const & output);

:Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type

:Effects: Passes a Python object to the underlying C++ functor after broadcasting its arguments

::

  template <typename TUnaryFunctor, 
            typename TArgument=typename TUnaryFunctor::argument_type,
            typename TResult=typename TUnaryFunctor::result_type>
  static object make(); 

:Requires: Typenames ``TUnaryFunctor`` and optionally ``TArgument`` for argument type and ``TResult`` for result type

:Returns: A Python function object to call the overloaded () operator in the struct (in typical usage)



Example(s)
----------

::

  namespace p = boost::python;
  namespace np = boost::python::numpy;

  struct UnarySquare 
  {
    typedef double argument_type;
    typedef double result_type;
    double operator()(double r) const { return r * r;}
  };

  p::object ud = p::class_<UnarySquare, boost::shared_ptr<UnarySquare> >("UnarySquare").def("__call__", np::unary_ufunc<UnarySquare>::make());
  p::object inst = ud();
  std::cout << "Square of unary scalar 1.0 is " << p::extract <char const * > (p::str(inst.attr("__call__")(1.0))) << std::endl ;