Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1997
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg5906] Re: [mg5860] Cartesian product of n sets of lists
  • From: "w.meeussen" <w.meeussen at solair4b.eunet.be>
  • Date: Tue, 4 Feb 1997 00:03:24 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

At 01:30 2-02-97 -0500, you 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
>
>
>
>
hi,

In Mma 3.0, your usage of Outer[] has been included:

?Outer
    "Outer[f, list1, list2, ... ] gives the generalized outer product of the 
    listi, forming all possible combinations of the lowest-level elements in
each
    of them. Outer[f, list1, list2, ... , n] treats as separate elements only
    sublists at level n in the listi. Outer[f, list1, list2, ... , n1, n2, ... ]
    treats as separate elements only sublists at level ni in the corresponding
    listi."

In Mma 2.2.3, I would use a simple inert wrapping function like dummy[] to
protect the innermost list:

li1={ {a, b, c}, {1, 2, 3} }
{{a, b, c}, {1, 2, 3}}

li2={ {d, e, f}, {4, 5, 6} }
{{d, e, f}, {4, 5, 6}}

{it1,it2}= Transpose at {li1,li2}

it1
{{a, b, c}, {d, e, f}}

it2
{{1, 2, 3}, {4, 5, 6}}

Outer[List,dummy@@it1,dummy@@it2]/.dummy->List
{{{{a, b, c}, {1, 2, 3}}, 
   {{a, b, c}, {4, 5, 6}}}, 
  {{{d, e, f}, {1, 2, 3}}, 
   {{d, e, f}, {4, 5, 6}}}}

%//TableForm
a b c   a b c
1 2 3   4 5 6

d e f   d e f
1 2 3   4 5 6

If you want the four smaller lists sequentially, then do
Flatten[Outer[List,my@@it1,my@@it2]/.my->List,1]

I hope this is what you meant.
Dr. Wouter L. J. MEEUSSEN
w.meeussen at solair4b.eunet.be



  • Prev by Date: Re: How do you plot Exp?????
  • Next by Date: Surface Interpolation
  • Previous by thread: Re: Cartesian product of n sets of lists
  • Next by thread: Re: Re: Cartesian product of n sets of lists