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

MathGroup Archive 1995

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

Search the Archive

Re: I'm looking for an algorithm: Cartesian Product

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1676] Re: [mg1644] I'm looking for an algorithm: Cartesian Product
  • From: Lou Talman <me at talmanl.mscd.edu>
  • Date: Tue, 11 Jul 1995 06:07:21 -0400

H. Skaf wants to compute Cartesian products.

The Outer operator finds Cartesian products  fairly well.  The main
problem is cleaning  up the extraneous {} pairs:

In[1]:=
  CleanUp[X_List] := Flatten[X, Depth[X] - 3];

In[2]:=
  f[List1_List, Lists__List] := 
              CleanUp[Outer[List, List1, Lists]];

In[3]:= 
  l1 = {a1, a2};
  l2 = {b1, b2, b3};
  l3 = {c1, c2, c3, c4};
  l4 = {d1, d2};

In[4]:=
  f[l1, l2]

Out[4]=
  {{a1, b1}, {a1, b2}, {a1, b3}, {a2, b1}, {a2, b2}, {a2, b3}}

In[5]:=
  f[l1, l2, l3]

Out[5]=
  {{a1, b1, c1}, {a1, b1, c2}, {a1, b1, c3}, {a1, b1, c4}, 
 
  {a1, b2, c1}, {a1, b2, c2}, {a1, b2, c3}, {a1, b2, c4}, 
 
  {a1, b3, c1}, {a1, b3, c2}, {a1, b3, c3}, {a1, b3, c4}, 
 
  {a2, b1, c1}, {a2, b1, c2}, {a2, b1, c3}, {a2, b1, c4}, 
 
  {a2, b2, c1}, {a2, b2, c2}, {a2, b2, c3}, {a2, b2, c4}, 
 
  {a2, b3, c1}, {a2, b3, c2}, {a2, b3, c3}, {a2, b3, c4}}

In[6]:=
  f[l1, l2, l3, l4]

Out[6]=
  {{a1, b1, c1, d1}, {a1, b1, c1, d2}, {a1, b1, c2, d1}, 
 
  {a1, b1, c2, d2}, {a1, b1, c3, d1}, {a1, b1, c3, d2}, 
 
  {a1, b1, c4, d1}, {a1, b1, c4, d2}, {a1, b2, c1, d1}, 
 
  {a1, b2, c1, d2}, {a1, b2, c2, d1}, {a1, b2, c2, d2}, 
 
  {a1, b2, c3, d1}, {a1, b2, c3, d2}, {a1, b2, c4, d1}, 
 
  {a1, b2, c4, d2}, {a1, b3, c1, d1}, {a1, b3, c1, d2}, 
 
  {a1, b3, c2, d1}, {a1, b3, c2, d2}, {a1, b3, c3, d1}, 
 
  {a1, b3, c3, d2}, {a1, b3, c4, d1}, {a1, b3, c4, d2}, 
 
  {a2, b1, c1, d1}, {a2, b1, c1, d2}, {a2, b1, c2, d1}, 
 
  {a2, b1, c2, d2}, {a2, b1, c3, d1}, {a2, b1, c3, d2}, 
 
  {a2, b1, c4, d1}, {a2, b1, c4, d2}, {a2, b2, c1, d1}, 
 
  {a2, b2, c1, d2}, {a2, b2, c2, d1}, {a2, b2, c2, d2}, 
 
  {a2, b2, c3, d1}, {a2, b2, c3, d2}, {a2, b2, c4, d1}, 
 
  {a2, b2, c4, d2}, {a2, b3, c1, d1}, {a2, b3, c1, d2}, 
 
  {a2, b3, c2, d1}, {a2, b3, c2, d2}, {a2, b3, c3, d1}, 
 
  {a2, b3, c3, d2}, {a2, b3, c4, d1}, {a2, b3, c4, d2}}
--------------

--Lou Talman
  Metropolitan State College of Denver


  • Prev by Date: Q: Mathematica & minimal surfaces
  • Next by Date: Re: I'm looking for an algorithm: Cartesian Product
  • Previous by thread: Re: I'm looking for an algorithm: Cartesian Product
  • Next by thread: Re: I'm looking for an algorithm: Cartesian Product