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)]
]

Maybe something like

In [15]: f = lambda x,y : x*sin(y)

In [16]: a = array([[f(i,j) for i in range(3)] for j in range(3)])

In [17]: a
Out[17]:
array([[ 0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.84147098,  1.68294197],
       [ 0.        ,  0.90929743,  1.81859485]])

I don't know if nested list comprehensions are faster than two nested loops, but at least they avoid array indexing.

Chuck