<br><br><div><span class="gmail_quote">On 8/20/07, <b class="gmail_sendername">Geoffrey Zhu</b> <<a href="mailto:zyzhu2000@gmail.com">zyzhu2000@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi Everyone,<br><br>I am wondering if there is an "extended" outer product. Take the<br>example in "Guide to Numpy." Instead of doing an multiplication, I<br>want to call a custom function for each pair.
<br><br>>>> print outer([1,2,3],[10,100,1000])<br><br>[[ 10 100 1000]<br>[ 20 200 2000]<br>[ 30 300 3000]]<br><br><br>So I want:<br><br>[<br> [f(1,10), f(1,100), f(1,1000)],<br> [f(2,10), f(2, 100), f(2, 1000)],<br>
 [f(3,10), f(3, 100), f(3,1000)]<br>]</blockquote><div><br>You could make two matrices like so:<br><br><span style="font-family: courier new,monospace;">In [46]: a = arange(3)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">In [47]: b = a.reshape(1,3).repeat(3,0)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">In [48]: c = a.reshape(3,1).repeat(3,1)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
In [49]: b</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Out[49]: </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
array([[0, 1, 2],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">       [0, 1, 2],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
       [0, 1, 2]])</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">In [50]: c</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Out[50]: </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">array([[0, 0, 0],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">       [1, 1, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">       [2, 2, 2]])</span><br style="font-family: courier new,monospace;">
<br> which will give you all pairs. You can then make a function of these in various ways, for example<br><br><span style="font-family: courier new,monospace;">In [52]: c**b</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">Out[52]: </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">array([[1, 0, 0],</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">       [1, 1, 1],</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">       [1, 2, 4]])</span><br style="font-family: courier new,monospace;">
<br></div>That is a bit clumsy, though. I don't know how to do what you want in a direct way.<br><br>Chuck<br><br></div><br>