[Numpy-discussion] Tuple outer product?

Mads Ipsen mpi@comxnet...
Fri Sep 25 13:05:04 CDT 2009


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


-- 
+------------------------------------------------------------+
| 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 |
+------------------------------+-----------------------------+




More information about the NumPy-Discussion mailing list