[Numpy-discussion] "Extended" Outer Product

Charles R Harris charlesr.harris@gmail....
Tue Aug 21 00:14:52 CDT 2007


On 8/20/07, Geoffrey Zhu <zyzhu2000@gmail.com> wrote:
>
> Hi Everyone,
>
> I am wondering if there is an "extended" outer product. Take the
> example in "Guide to Numpy." Instead of doing an multiplication, I
> want to call a custom function for each pair.
>
> >>> print outer([1,2,3],[10,100,1000])
>
> [[ 10 100 1000]
> [ 20 200 2000]
> [ 30 300 3000]]
>
>
> So I want:
>
> [
> [f(1,10), f(1,100), f(1,1000)],
> [f(2,10), f(2, 100), f(2, 1000)],
> [f(3,10), f(3, 100), f(3,1000)]
> ]


You could make two matrices like so:

In [46]: a = arange(3)

In [47]: b = a.reshape(1,3).repeat(3,0)

In [48]: c = a.reshape(3,1).repeat(3,1)

In [49]: b
Out[49]:
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])

In [50]: c
Out[50]:
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])

 which will give you all pairs. You can then make a function of these in
various ways, for example

In [52]: c**b
Out[52]:
array([[1, 0, 0],
       [1, 1, 1],
       [1, 2, 4]])

That is a bit clumsy, though. I don't know how to do what you want in a
direct way.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070820/e2fb4e02/attachment.html 


More information about the Numpy-discussion mailing list