MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Cartesian product of n sets of lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5940] Re: [mg5860] Cartesian product of n sets of lists
  • From: seanross at worldnet.att.net
  • Date: Tue, 4 Feb 1997 00:04:50 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Jean-Christophe Deschamps wrote:
> 
> PLEASE: be kind to mail answers to <jchd at worldnet.fr> as well.
> I do _not_ receive this list at all here (I wonder why, since I got the
> welcome automated message) and the feed for the Mma newsgroup is utterly
> unreliable at this end, sigh!  Sorry for that unconvenience.
> 
> Using Mma v2.x, I never found the proper way to get the cartesian
> product of several sets whose elements are themselves Lists.
> 
> In other terms, I would like to write something like this (using List as
> operator):
> 
> p =3D CartProd[List, { {a, b, c}, {1, 2, 3} },
>                    { {d, e, f}, {4, 5, 6} }]
> 
> and obtain four matrices:
> 
>   a b c     a b c     d e f     d e f
>   1 2 3     4 5 6     1 2 3     4 5 6
> 
> Outer is of little use since it acts beyond level 1 of its arguments, e.g=
> .:
> 
> Outer[List, { {a, b}, {1, 2} },
>             { {c, d}, {3, 4} }]
> 
> {{{{{a, c}, {a, d}}, {{a, 3}, {a, 4}}}, {{{b, c}, {b, d}}, {{b, 3}, {b,
> 4}}}},=20
>  {{{{1, c}, {1, d}}, {{1, 3}, {1, 4}}}, {{{2, c}, {2, d}}, {{2, 3}, {2,
> 4}}}}}
> 
> Of course, it's possible to define a bunch of functions (one taking 2
> sets, one taking 3 sets, ...) like:
> 
> CartProd[f_, s1_List, S2_List] :=3D Block[{ii,jj},
>   Flatten[ Table[ f[s1[[ii]], s2[[jj]] ],
>                   {ii,Length[s1]}, {jj,Length[s2]} ], 1]]
> 
> but I find it a little ugly.
> 
> Thank you for your hints.
> --
>    jchd at worldnet.fr         | Jean-Chistophe Deschamps
>                             |     14, rue Hermel
>  T=E9l.: (33) 01 42 55 01 93  |      75018 Paris

Have you looked at the MatrixManipulation package?  That may contain 
some functions that might fit your need.  I would also suggest 
experimenting around with various Map and MapThread combinations using 
List or Join as the mapping function.


  • Prev by Date: I can't run mathematica!
  • Next by Date: Laplace Transforms
  • Previous by thread: Cartesian product of n sets of lists
  • Next by thread: Re: Cartesian product of n sets of lists