Cartesian product of n sets of lists
- To: mathgroup at smc.vnet.net
- Subject: [mg5860] Cartesian product of n sets of lists
- From: Jean-Christophe Deschamps <jchd at worldnet.fr>
- Date: Sun, 2 Feb 1997 01:30:35 -0500
- Sender: owner-wri-mathgroup at wolfram.com
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