• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5<html xmlns="http://www.w3.org/1999/xhtml">
6  <head>
7    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
9    <title>unary_ufunc - Boost.Python NumPy extension 1.0 documentation</title>
10    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
11    <link rel="stylesheet" href="../_static/style.css" type="text/css" />
12    <script type="text/javascript">
13      var DOCUMENTATION_OPTIONS = {
14          URL_ROOT:    '../',
15          VERSION:     '1.0',
16          COLLAPSE_MODINDEX: false,
17          FILE_SUFFIX: '.html'
18      };
19    </script>
20    <script type="text/javascript" src="../_static/jquery.js"></script>
21    <script type="text/javascript" src="../_static/underscore.js"></script>
22    <script type="text/javascript" src="../_static/doctools.js"></script>
23    <link rel="index" title="Index" href="../genindex.html" />
24    <link rel="search" title="Search" href="../search.html" />
25    <link rel="top" title="Boost.Python NumPy extension 1.0 documentation" href="../index.html" />
26    <link rel="up" title="Boost.Python NumPy extension Reference" href="index.html" />
27    <link rel="next" title="binary_ufunc" href="binary_ufunc.html" />
28    <link rel="prev" title="ndarray" href="ndarray.html" />
29  </head>
30  <body>
31    <div class="header">
32    <table border="0" cellpadding="7" cellspacing="0" width="100%" summary=
33    "header">
34      <tr>
35        <td valign="top" width="300">
36          <h3><a href="../index.html"><img
37          alt="C++ Boost" src="../_static/bpl.png" border="0"></a></h3>
38        </td>
39
40        <td >
41          <h1 align="center"><a href="../index.html">(NumPy)</a></h1>
42<!--          <h2 align="center">CallPolicies Concept</h2>-->
43        </td>
44	<td>
45      <div id="searchbox" style="display: none">
46        <form class="search" action="../search.html" method="get">
47          <input type="text" name="q" size="18" />
48          <input type="submit" value="Search" />
49          <input type="hidden" name="check_keywords" value="yes" />
50          <input type="hidden" name="area" value="default" />
51        </form>
52      </div>
53      <script type="text/javascript">$('#searchbox').show(0);</script>
54	</td>
55      </tr>
56    </table>
57    </div>
58    <hr/>
59    <div class="content">
60    <div class="navbar" style="text-align:right;">
61
62
63      <a class="prev" title="ndarray" href="ndarray.html"><img src="../_static/prev.png" alt="prev"/></a>
64      <a class="up" title="Boost.Python NumPy extension Reference" href="index.html"><img src="../_static/up.png" alt="up"/></a>
65      <a class="next" title="binary_ufunc" href="binary_ufunc.html"><img src="../_static/next.png" alt="next"/></a>
66
67    </div>
68
69  <div class="section" id="unary-ufunc">
70<h1><a class="toc-backref" href="#id1">unary_ufunc</a></h1>
71<div class="contents topic" id="table-of-contents">
72<p class="topic-title first">Table of Contents</p>
73<ul class="simple">
74<li><a class="reference internal" href="#unary-ufunc" id="id1">unary_ufunc</a><ul>
75<li><a class="reference internal" href="#synopsis" id="id2">synopsis</a></li>
76<li><a class="reference internal" href="#constructors" id="id3">constructors</a></li>
77<li><a class="reference internal" href="#accessors" id="id4">accessors</a></li>
78<li><a class="reference internal" href="#example-s" id="id5">Example(s)</a></li>
79</ul>
80</li>
81</ul>
82</div>
83<p>A <code class="docutils literal"><span class="pre">unary_ufunc</span></code> 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</p>
84<blockquote>
85<div><code class="docutils literal"><span class="pre">&lt;boost/python/numpy/ufunc.hpp&gt;</span></code> contains the <code class="docutils literal"><span class="pre">unary_ufunc</span></code> structure definitions</div></blockquote>
86<div class="section" id="synopsis">
87<h2><a class="toc-backref" href="#id2">synopsis</a></h2>
88<div class="highlight-c++"><div class="highlight"><pre><span class="k">namespace</span> <span class="n">boost</span>
89<span class="p">{</span>
90<span class="k">namespace</span> <span class="n">python</span>
91<span class="p">{</span>
92<span class="k">namespace</span> <span class="n">numpy</span>
93<span class="p">{</span>
94
95<span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="p">,</span>
96          <span class="k">typename</span> <span class="n">TArgument</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">argument_type</span><span class="p">,</span>
97          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
98<span class="k">struct</span> <span class="n">unary_ufunc</span>
99<span class="p">{</span>
100
101  <span class="k">static</span> <span class="n">object</span> <span class="n">call</span><span class="p">(</span><span class="n">TUnaryFunctor</span> <span class="o">&amp;</span> <span class="n">self</span><span class="p">,</span>
102                     <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">input</span><span class="p">,</span>
103                     <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">output</span><span class="p">)</span> <span class="p">;</span>
104
105  <span class="k">static</span> <span class="n">object</span> <span class="nf">make</span><span class="p">();</span>
106
107<span class="p">};</span>
108<span class="p">}</span>
109<span class="p">}</span>
110<span class="p">}</span>
111</pre></div>
112</div>
113</div>
114<div class="section" id="constructors">
115<h2><a class="toc-backref" href="#id3">constructors</a></h2>
116<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">example_unary_ufunc</span>
117<span class="p">{</span>
118  <span class="k">typedef</span> <span class="n">any_valid_type</span> <span class="n">argument_type</span><span class="p">;</span>
119  <span class="k">typedef</span> <span class="n">any_valid_type</span> <span class="n">result_type</span><span class="p">;</span>
120<span class="p">};</span>
121</pre></div>
122</div>
123<table class="docutils field-list" frame="void" rules="none">
124<col class="field-name" />
125<col class="field-body" />
126<tbody valign="top">
127<tr class="field-odd field"><th class="field-name">Requirements:</th><td class="field-body">The <code class="docutils literal"><span class="pre">any_valid</span></code> type must be defined using typedef as a valid C++ type in order to use the struct methods correctly</td>
128</tr>
129<tr class="field-even field"><th class="field-name">Note:</th><td class="field-body">The struct must be exposed as a Python class, and an instance of the class must be created to use the <code class="docutils literal"><span class="pre">call</span></code> method corresponding to the <code class="docutils literal"><span class="pre">__call__</span></code> attribute of the Python object</td>
130</tr>
131</tbody>
132</table>
133</div>
134<div class="section" id="accessors">
135<h2><a class="toc-backref" href="#id4">accessors</a></h2>
136<div class="highlight-c++"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="p">,</span>
137          <span class="k">typename</span> <span class="n">TArgument</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">argument_type</span><span class="p">,</span>
138          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
139<span class="k">static</span> <span class="n">object</span> <span class="n">call</span><span class="p">(</span><span class="n">TUnaryFunctor</span> <span class="o">&amp;</span> <span class="n">self</span><span class="p">,</span>
140                   <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">input</span><span class="p">,</span>
141                   <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">output</span><span class="p">);</span>
142</pre></div>
143</div>
144<table class="docutils field-list" frame="void" rules="none">
145<col class="field-name" />
146<col class="field-body" />
147<tbody valign="top">
148<tr class="field-odd field"><th class="field-name">Requires:</th><td class="field-body">Typenames <code class="docutils literal"><span class="pre">TUnaryFunctor</span></code> and optionally <code class="docutils literal"><span class="pre">TArgument</span></code> for argument type and <code class="docutils literal"><span class="pre">TResult</span></code> for result type</td>
149</tr>
150<tr class="field-even field"><th class="field-name">Effects:</th><td class="field-body">Passes a Python object to the underlying C++ functor after broadcasting its arguments</td>
151</tr>
152</tbody>
153</table>
154<div class="highlight-c++"><div class="highlight"><pre><span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="p">,</span>
155          <span class="k">typename</span> <span class="n">TArgument</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">argument_type</span><span class="p">,</span>
156          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TUnaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
157<span class="k">static</span> <span class="n">object</span> <span class="n">make</span><span class="p">();</span>
158</pre></div>
159</div>
160<table class="docutils field-list" frame="void" rules="none">
161<col class="field-name" />
162<col class="field-body" />
163<tbody valign="top">
164<tr class="field-odd field"><th class="field-name">Requires:</th><td class="field-body">Typenames <code class="docutils literal"><span class="pre">TUnaryFunctor</span></code> and optionally <code class="docutils literal"><span class="pre">TArgument</span></code> for argument type and <code class="docutils literal"><span class="pre">TResult</span></code> for result type</td>
165</tr>
166<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">A Python function object to call the overloaded () operator in the struct (in typical usage)</td>
167</tr>
168</tbody>
169</table>
170</div>
171<div class="section" id="example-s">
172<h2><a class="toc-backref" href="#id5">Example(s)</a></h2>
173<div class="highlight-c++"><div class="highlight"><pre><span class="k">namespace</span> <span class="n">p</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="p">;</span>
174<span class="k">namespace</span> <span class="n">np</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="o">::</span><span class="n">numpy</span><span class="p">;</span>
175
176<span class="k">struct</span> <span class="n">UnarySquare</span>
177<span class="p">{</span>
178  <span class="k">typedef</span> <span class="kt">double</span> <span class="n">argument_type</span><span class="p">;</span>
179  <span class="k">typedef</span> <span class="kt">double</span> <span class="n">result_type</span><span class="p">;</span>
180  <span class="kt">double</span> <span class="nf">operator</span><span class="p">()(</span><span class="kt">double</span> <span class="n">r</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">r</span> <span class="o">*</span> <span class="n">r</span><span class="p">;}</span>
181<span class="p">};</span>
182
183<span class="n">p</span><span class="o">::</span><span class="n">object</span> <span class="n">ud</span> <span class="o">=</span> <span class="n">p</span><span class="o">::</span><span class="n">class_</span><span class="o">&lt;</span><span class="n">UnarySquare</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">UnarySquare</span><span class="o">&gt;</span> <span class="o">&gt;</span><span class="p">(</span><span class="s">&quot;UnarySquare&quot;</span><span class="p">).</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;__call__&quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">::</span><span class="n">unary_ufunc</span><span class="o">&lt;</span><span class="n">UnarySquare</span><span class="o">&gt;::</span><span class="n">make</span><span class="p">());</span>
184<span class="n">p</span><span class="o">::</span><span class="n">object</span> <span class="n">inst</span> <span class="o">=</span> <span class="n">ud</span><span class="p">();</span>
185<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Square of unary scalar 1.0 is &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span> <span class="o">&lt;</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*</span> <span class="o">&gt;</span> <span class="p">(</span><span class="n">p</span><span class="o">::</span><span class="n">str</span><span class="p">(</span><span class="n">inst</span><span class="p">.</span><span class="n">attr</span><span class="p">(</span><span class="s">&quot;__call__&quot;</span><span class="p">)(</span><span class="mf">1.0</span><span class="p">)))</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span> <span class="p">;</span>
186</pre></div>
187</div>
188</div>
189</div>
190
191
192    <div class="navbar" style="text-align:right;">
193
194
195      <a class="prev" title="ndarray" href="ndarray.html"><img src="../_static/prev.png" alt="prev"/></a>
196      <a class="up" title="Boost.Python NumPy extension Reference" href="index.html"><img src="../_static/up.png" alt="up"/></a>
197      <a class="next" title="binary_ufunc" href="binary_ufunc.html"><img src="../_static/next.png" alt="next"/></a>
198
199    </div>
200    </div>
201  </body>
202</html>