[Numpy-discussion] Tuple outer product?

Gökhan Sever gokhansever@gmail....
Fri Sep 25 13:36:02 CDT 2009


On Fri, Sep 25, 2009 at 1:05 PM, Mads Ipsen <mpi@comxnet.dk> wrote:

> Gökhan Sever wrote:
> >
> >
> > On Fri, Sep 25, 2009 at 12:45 PM, Mads Ipsen <mpi@comxnet.dk
> > <mailto:mpi@comxnet.dk>> wrote:
> >
> >     Is there a numpy operation on two arrays, say [1,2,3] and [4,5,6],
> >     that
> >     will yield:
> >
> >     [[(1,4),(1,5),(1,6)],[(2,4),(2,5),(2,6)],[(3,4),(3,5),(3,6)]]
> >
> >     Any suggestions are most welcome.
> >
> >     Mads
> >
> >
> >
> > I don't know if there is a function in numpy library, but it is a
> > simple one isn't it?
> >
> > ipython --pylab
> >
> > I[1]: a = array([1,2,3])
> >
> > I[2]: b = array([4,5,6])
> >
> > I[3]: [zip(ones(a.size, dtype=int)*a[i], b) for i in range(len(a))]
> > O[3]: [[(1, 4), (1, 5), (1, 6)], [(2, 4), (2, 5), (2, 6)], [(3, 4),
> > (3, 5), (3, 6)]]
> >
> >
> >
> >
> >     --
> >     +------------------------------------------------------------+
> >     | Mads Ipsen, Scientific developer                           |
> >     +------------------------------+-----------------------------+
> >     | QuantumWise A/S              | phone:         +45-29716388 |
> >     | Nørresøgade 27A              | www:    www.quantumwise.com
> >     <http://www.quantumwise.com> |
> >     | DK-1370 Copenhagen, Denmark  | email:  mpi@quantumwise.com
> >     <mailto:mpi@quantumwise.com> |
> >     +------------------------------+-----------------------------+
> >
> >
> >     _______________________________________________
> >     NumPy-Discussion mailing list
> >     NumPy-Discussion@scipy.org <mailto:NumPy-Discussion@scipy.org>
> >     http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
> >
> >
> > --
> > Gökhan
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> Sure, and you could do the same thing using two nested for loops. But
> its bound to be slow when the arrays become large (and they will). The
> question is whether it can be done in pure numpy. An output like
>
> [[[1,4],[1,5],[1,6]],[[2,4],[2,5],[2,6]],[[3,4],[3,5],[3,6]]]
>
> is also OK.
>
> Mads
>
>
You are right, it takes a lot of time when the arrays get bigger:

I[1]: a = arange(1000)

I[2]: b = arange(1000)

I[3]: %time [zip(ones(a.size, dtype=int)*a[i], b) for i in range(len(a))];
CPU times: user 1.73 s, sys: 0.06 s, total: 1.79 s
Wall time: 1.86 s

I[5]: b = arange(2000)

I[6]: a = arange(2000)

I[7]: %time [zip(ones(a.size, dtype=int)*a[i], b) for i in range(len(a))];
CPU times: user 12.45 s, sys: 0.15 s, total: 12.61 s
Wall time: 13.23 s

I[9]: b = arange(3000)

I[10]: a = arange(3000)

I[11]: %time [zip(ones(a.size, dtype=int)*a[i], b) for i in range(len(a))];
CPU times: user 49.29 s, sys: 0.32 s, total: 49.60 s
Wall time: 51.25 s

I[13]: from itertools import product

I[14]: %time list(product(a,b));
CPU times: user 45.74 s, sys: 0.11 s, total: 45.84 s
Wall time: 48.26 s


To me, this is a very nice case to include Cython in your code. However you
might need to seek further advice in this :)




>
> --
> +------------------------------------------------------------+
> | Mads Ipsen, Scientific developer                           |
> +------------------------------+-----------------------------+
> | QuantumWise A/S              | phone:         +45-29716388 |
> | Nørresøgade 27A              | www:    www.quantumwise.com |
> | DK-1370 Copenhagen, Denmark  | email:  mpi@quantumwise.com |
> +------------------------------+-----------------------------+
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090925/db6b6aa9/attachment.html 


More information about the NumPy-Discussion mailing list