# [Numpy-discussion] "Extended" Outer Product

Timothy Hochberg tim.hochberg at ieee.org
Tue Aug 21 15:56:03 EDT 2007

```On 8/21/07, Geoffrey Zhu <zyzhu2000 at gmail.com> wrote:
>
> On 8/21/07, Timothy Hochberg <tim.hochberg at ieee.org> wrote:
> >
> >
> >
> > On 8/21/07, Charles R Harris <charlesr.harris at gmail.com> wrote:
> > >
> > >
> > >
> > > On 8/20/07, Geoffrey Zhu < zyzhu2000 at 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.
> >
> > This is just a general comment on recent threads of this type and not
> > directed specifically at Chuck or anyone else.
> >
> > IMO, the emphasis on avoiding FOR loops at all costs is misplaced. It is
> > often more memory friendly and thus faster to vectorize only the inner
> loop
> > and leave outer loops alone. Everything varies with the specific case of
> > course, but trying to avoid FOR loops on principle is not a good
> strategy.
> >
>
> I agree. My original post asked for solutions without using two nested
> for loops because I already know the two for loop solution. Besides, I
> was hoping that some version of 'outer' will take in a function
> reference and call the function instead of doing multiplifcation.

A specific example would help here. There are ways to deal with certain
subclasses of problems that won't necessarily generalize. For example, are
you aware of the outer methods on ufuncs (add.outer, substract.outer, etc)?
Typical dimensions also matter, since some approaches work well for certain
shapes, but are pretty miserable for others. FWIW, I often have very good
luck with removing the inner for-loop in favor of vector operations. This
tends to be simpler than trying to vectorize everything and often has better
performance since it's often more memory friendly. However, it all depends
on specifics of the problem.

Regards,

-tim

--
.  __
.   |-\
.
.  tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070821/f6210ccc/attachment.html>
```