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>Ufuncs - 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 Tutorial" href="index.html" /> 27 <link rel="next" title="How to access data using raw pointers" href="fromdata.html" /> 28 <link rel="prev" title="Creating ndarrays" 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="Creating ndarrays" href="ndarray.html"><img src="../_static/prev.png" alt="prev"/></a> 64 <a class="up" title="Boost.Python NumPy extension Tutorial" href="index.html"><img src="../_static/up.png" alt="up"/></a> 65 <a class="next" title="How to access data using raw pointers" href="fromdata.html"><img src="../_static/next.png" alt="next"/></a> 66 67 </div> 68 69 <div class="section" id="ufuncs"> 70<h1>Ufuncs</h1> 71<p>Ufuncs or universal functions operate on ndarrays element by element, and support array broadcasting, type casting, and other features.</p> 72<p>Lets try and see how we can use the binary and unary ufunc methods</p> 73<p>After the neccessary includes</p> 74<div class="highlight-c++"><div class="highlight"><pre><span class="cp">#include</span> <span class="cpf"><boost/python/numpy.hpp></span><span class="cp"></span> 75<span class="cp">#include</span> <span class="cpf"><iostream></span><span class="cp"></span> 76 77<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> 78<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> 79</pre></div> 80</div> 81<p>Now we create the structs necessary to implement the ufuncs. The typedefs <em>must</em> be made as the ufunc generators take these typedefs as inputs and return an error otherwise</p> 82<div class="highlight-c++"><div class="highlight"><pre><span class="k">struct</span> <span class="n">UnarySquare</span> 83<span class="p">{</span> 84 <span class="k">typedef</span> <span class="kt">double</span> <span class="n">argument_type</span><span class="p">;</span> 85 <span class="k">typedef</span> <span class="kt">double</span> <span class="n">result_type</span><span class="p">;</span> 86 87 <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> 88<span class="p">};</span> 89 90<span class="k">struct</span> <span class="n">BinarySquare</span> 91<span class="p">{</span> 92 <span class="k">typedef</span> <span class="kt">double</span> <span class="n">first_argument_type</span><span class="p">;</span> 93 <span class="k">typedef</span> <span class="kt">double</span> <span class="n">second_argument_type</span><span class="p">;</span> 94 <span class="k">typedef</span> <span class="kt">double</span> <span class="n">result_type</span><span class="p">;</span> 95 96 <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> 97<span class="p">};</span> 98</pre></div> 99</div> 100<p>Initialise the Python runtime and the numpy module</p> 101<div class="highlight-c++"><div class="highlight"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="n">argv</span><span class="p">)</span> 102<span class="p">{</span> 103 <span class="n">Py_Initialize</span><span class="p">();</span> 104 <span class="n">np</span><span class="o">::</span><span class="n">initialize</span><span class="p">();</span> 105</pre></div> 106</div> 107<p>Now expose the struct UnarySquare to Python as a class, and let ud be the class object.</p> 108<div class="highlight-c++"><div class="highlight"><pre><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"><</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"><</span><span class="n">UnarySquare</span><span class="o">></span> <span class="o">></span><span class="p">(</span><span class="s">"UnarySquare"</span><span class="p">);</span> 109<span class="n">ud</span><span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"__call__"</span><span class="p">,</span> <span class="n">np</span><span class="o">::</span><span class="n">unary_ufunc</span><span class="o"><</span><span class="n">UnarySquare</span><span class="o">>::</span><span class="n">make</span><span class="p">());</span> 110</pre></div> 111</div> 112<p>Let inst be an instance of the class ud</p> 113<div class="highlight-c++"><div class="highlight"><pre><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> 114</pre></div> 115</div> 116<p>Use the “__call__” method to call the overloaded () operator and print the value</p> 117<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Square of unary scalar 1.0 is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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">"__call__"</span><span class="p">)(</span><span class="mf">1.0</span><span class="p">)))</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 118</pre></div> 119</div> 120<p>Create an array in C++</p> 121<div class="highlight-c++"><div class="highlight"><pre><span class="kt">int</span> <span class="n">arr</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">}</span> <span class="p">;</span> 122</pre></div> 123</div> 124<p>..and use it to create the ndarray in Python</p> 125<div class="highlight-c++"><div class="highlight"><pre><span class="n">np</span><span class="o">::</span><span class="n">ndarray</span> <span class="n">demo_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">::</span><span class="n">from_data</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="n">np</span><span class="o">::</span><span class="n">dtype</span><span class="o">::</span><span class="n">get_builtin</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="p">(),</span> 126 <span class="n">p</span><span class="o">::</span><span class="n">make_tuple</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> 127 <span class="n">p</span><span class="o">::</span><span class="n">make_tuple</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> 128 <span class="n">p</span><span class="o">::</span><span class="n">object</span><span class="p">());</span> 129</pre></div> 130</div> 131<p>Print out the demo array</p> 132<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Demo array is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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">demo_array</span><span class="p">))</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 133</pre></div> 134</div> 135<p>Call the “__call__” method to perform the operation and assign the value to result_array</p> 136<div class="highlight-c++"><div class="highlight"><pre><span class="n">p</span><span class="o">::</span><span class="n">object</span> <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">"__call__"</span><span class="p">)(</span><span class="n">demo_array</span><span class="p">);</span> 137</pre></div> 138</div> 139<p>Print the resultant array</p> 140<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Square of demo array is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 141</pre></div> 142</div> 143<p>Lets try the same with a list</p> 144<div class="highlight-c++"><div class="highlight"><pre><span class="n">p</span><span class="o">::</span><span class="n">list</span> <span class="n">li</span><span class="p">;</span> 145<span class="n">li</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span> 146<span class="n">li</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="mi">7</span><span class="p">);</span> 147</pre></div> 148</div> 149<p>Print out the demo list</p> 150<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Demo list is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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">li</span><span class="p">))</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 151</pre></div> 152</div> 153<p>Call the ufunc for the list</p> 154<div class="highlight-c++"><div class="highlight"><pre><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">"__call__"</span><span class="p">)(</span><span class="n">li</span><span class="p">);</span> 155</pre></div> 156</div> 157<p>And print the list out</p> 158<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Square of demo list is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 159</pre></div> 160</div> 161<p>Now lets try Binary ufuncs. Again, expose the struct BinarySquare to Python as a class, and let ud be the class object</p> 162<div class="highlight-c++"><div class="highlight"><pre><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"><</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"><</span><span class="n">BinarySquare</span><span class="o">></span> <span class="o">></span><span class="p">(</span><span class="s">"BinarySquare"</span><span class="p">);</span> 163<span class="n">ud</span><span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"__call__"</span><span class="p">,</span> <span class="n">np</span><span class="o">::</span><span class="n">binary_ufunc</span><span class="o"><</span><span class="n">BinarySquare</span><span class="o">>::</span><span class="n">make</span><span class="p">());</span> 164</pre></div> 165</div> 166<p>And initialise ud</p> 167<div class="highlight-c++"><div class="highlight"><pre><span class="n">inst</span> <span class="o">=</span> <span class="n">ud</span><span class="p">();</span> 168</pre></div> 169</div> 170<p>Print the two input lists</p> 171<div class="highlight-c++"><div class="highlight"><pre><span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"The two input list for binary ufunc are "</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span> 172 <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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">demo_array</span><span class="p">))</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span> 173 <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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">demo_array</span><span class="p">))</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 174</pre></div> 175</div> 176<p>Call the binary ufunc taking demo_array as both inputs</p> 177<div class="highlight-c++"><div class="highlight"><pre><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">"__call__"</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> 178</pre></div> 179</div> 180<p>And print the output</p> 181<div class="highlight-c++"><div class="highlight"><pre> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="s">"Square of list with binary ufunc is "</span> <span class="o"><<</span> <span class="n">p</span><span class="o">::</span><span class="n">extract</span><span class="o"><</span><span class="kt">char</span> <span class="k">const</span> <span class="o">*></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"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> 182<span class="p">}</span> 183</pre></div> 184</div> 185</div> 186 187 188 <div class="navbar" style="text-align:right;"> 189 190 191 <a class="prev" title="Creating ndarrays" href="ndarray.html"><img src="../_static/prev.png" alt="prev"/></a> 192 <a class="up" title="Boost.Python NumPy extension Tutorial" href="index.html"><img src="../_static/up.png" alt="up"/></a> 193 <a class="next" title="How to access data using raw pointers" href="fromdata.html"><img src="../_static/next.png" alt="next"/></a> 194 195 </div> 196 </div> 197 </body> 198</html>