• 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>binary_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="multi_iter" href="multi_iter.html" />
28    <link rel="prev" title="unary_ufunc" href="unary_ufunc.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="unary_ufunc" href="unary_ufunc.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="multi_iter" href="multi_iter.html"><img src="../_static/next.png" alt="next"/></a>
66
67    </div>
68
69  <div class="section" id="binary-ufunc">
70<h1><a class="toc-backref" href="#id1">binary_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="#binary-ufunc" id="id1">binary_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">binary_ufunc</span></code> 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</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">binary_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">TBinaryFunctor</span><span class="p">,</span>
96          <span class="k">typename</span> <span class="n">TArgument1</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">first_argument_type</span><span class="p">,</span>
97          <span class="k">typename</span> <span class="n">TArgument2</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">second_argument_type</span><span class="p">,</span>
98          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
99
100<span class="k">struct</span> <span class="n">binary_ufunc</span>
101<span class="p">{</span>
102
103  <span class="k">static</span> <span class="n">object</span> <span class="n">call</span><span class="p">(</span><span class="n">TBinaryFunctor</span> <span class="o">&amp;</span> <span class="n">self</span><span class="p">,</span>
104                     <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">input1</span><span class="p">,</span>
105                     <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">input2</span><span class="p">,</span>
106                     <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">output</span><span class="p">);</span>
107
108  <span class="k">static</span> <span class="n">object</span> <span class="nf">make</span><span class="p">();</span>
109<span class="p">};</span>
110
111<span class="p">}</span>
112<span class="p">}</span>
113<span class="p">}</span>
114</pre></div>
115</div>
116</div>
117<div class="section" id="constructors">
118<h2><a class="toc-backref" href="#id3">constructors</a></h2>
119<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">example_binary_ufunc</span>
120<span class="p">{</span>
121  <span class="k">typedef</span> <span class="n">any_valid</span> <span class="n">first_argument_type</span><span class="p">;</span>
122  <span class="k">typedef</span> <span class="n">any_valid</span> <span class="n">second_argument_type</span><span class="p">;</span>
123  <span class="k">typedef</span> <span class="n">any_valid</span> <span class="n">result_type</span><span class="p">;</span>
124<span class="p">};</span>
125</pre></div>
126</div>
127<table class="docutils field-list" frame="void" rules="none">
128<col class="field-name" />
129<col class="field-body" />
130<tbody valign="top">
131<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>
132</tr>
133<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>
134</tr>
135</tbody>
136</table>
137</div>
138<div class="section" id="accessors">
139<h2><a class="toc-backref" href="#id4">accessors</a></h2>
140<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">TBinaryFunctor</span><span class="p">,</span>
141          <span class="k">typename</span> <span class="n">TArgument1</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">first_argument_type</span><span class="p">,</span>
142          <span class="k">typename</span> <span class="n">TArgument2</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">second_argument_type</span><span class="p">,</span>
143          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
144<span class="k">static</span> <span class="n">object</span> <span class="n">call</span><span class="p">(</span><span class="n">TBinaryFunctor</span> <span class="o">&amp;</span> <span class="n">self</span><span class="p">,</span>
145                   <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">input</span><span class="p">,</span>
146                   <span class="n">object</span> <span class="k">const</span> <span class="o">&amp;</span> <span class="n">output</span><span class="p">);</span>
147</pre></div>
148</div>
149<table class="docutils field-list" frame="void" rules="none">
150<col class="field-name" />
151<col class="field-body" />
152<tbody valign="top">
153<tr class="field-odd field"><th class="field-name">Requires:</th><td class="field-body">Typenames <code class="docutils literal"><span class="pre">TBinaryFunctor</span></code> and optionally <code class="docutils literal"><span class="pre">TArgument1</span></code> and <code class="docutils literal"><span class="pre">TArgument2</span></code> for argument type and <code class="docutils literal"><span class="pre">TResult</span></code> for result type</td>
154</tr>
155<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>
156</tr>
157</tbody>
158</table>
159<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">TBinaryFunctor</span><span class="p">,</span>
160          <span class="k">typename</span> <span class="n">TArgument1</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">first_argument_type</span><span class="p">,</span>
161          <span class="k">typename</span> <span class="n">TArgument2</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">second_argument_type</span><span class="p">,</span>
162          <span class="k">typename</span> <span class="n">TResult</span><span class="o">=</span><span class="k">typename</span> <span class="n">TBinaryFunctor</span><span class="o">::</span><span class="n">result_type</span><span class="o">&gt;</span>
163<span class="k">static</span> <span class="n">object</span> <span class="n">make</span><span class="p">();</span>
164</pre></div>
165</div>
166<table class="docutils field-list" frame="void" rules="none">
167<col class="field-name" />
168<col class="field-body" />
169<tbody valign="top">
170<tr class="field-odd field"><th class="field-name">Requires:</th><td class="field-body">Typenames <code class="docutils literal"><span class="pre">TBinaryFunctor</span></code> and optionally <code class="docutils literal"><span class="pre">TArgument1</span></code> and <code class="docutils literal"><span class="pre">TArgument2</span></code> for argument type and <code class="docutils literal"><span class="pre">TResult</span></code> for result type</td>
171</tr>
172<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>
173</tr>
174</tbody>
175</table>
176</div>
177<div class="section" id="example-s">
178<h2><a class="toc-backref" href="#id5">Example(s)</a></h2>
179<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>
180<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>
181
182<span class="k">struct</span> <span class="n">BinarySquare</span>
183<span class="p">{</span>
184  <span class="k">typedef</span> <span class="kt">double</span> <span class="n">first_argument_type</span><span class="p">;</span>
185  <span class="k">typedef</span> <span class="kt">double</span> <span class="n">second_argument_type</span><span class="p">;</span>
186  <span class="k">typedef</span> <span class="kt">double</span> <span class="n">result_type</span><span class="p">;</span>
187
188  <span class="kt">double</span> <span class="nf">operator</span><span class="p">()(</span><span class="kt">double</span> <span class="n">a</span><span class="p">,</span><span class="kt">double</span> <span class="n">b</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="p">(</span><span class="n">a</span><span class="o">*</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">b</span><span class="p">)</span> <span class="p">;</span> <span class="p">}</span>
189<span class="p">};</span>
190
191<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">BinarySquare</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">BinarySquare</span><span class="o">&gt;</span> <span class="o">&gt;</span><span class="p">(</span><span class="s">&quot;BinarySquare&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">binary_ufunc</span><span class="o">&lt;</span><span class="n">BinarySquare</span><span class="o">&gt;::</span><span class="n">make</span><span class="p">());</span>
192<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>
193<span class="n">result_array</span> <span class="o">=</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="n">demo_array</span><span class="p">,</span><span class="n">demo_array</span><span class="p">)</span> <span class="p">;</span>
194<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 list with binary ufunc 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">result_array</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>
195</pre></div>
196</div>
197</div>
198</div>
199
200
201    <div class="navbar" style="text-align:right;">
202
203
204      <a class="prev" title="unary_ufunc" href="unary_ufunc.html"><img src="../_static/prev.png" alt="prev"/></a>
205      <a class="up" title="Boost.Python NumPy extension Reference" href="index.html"><img src="../_static/up.png" alt="up"/></a>
206      <a class="next" title="multi_iter" href="multi_iter.html"><img src="../_static/next.png" alt="next"/></a>
207
208    </div>
209    </div>
210  </body>
211</html>