Re: How to construct all possible orderings
- To: mathgroup at smc.vnet.net
- Subject: [mg15648] Re: [mg15588] How to construct all possible orderings
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sat, 30 Jan 1999 04:28:43 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
There are lots of ways to do this using Outer and List. For example this function arrange[l_List,k_]:=Flatten[Outer[List,Apply[Sequence,Table[l,{k}]]],k-1] will list all arranements with repetitions of k objects our of the list l. In[28]:= arrange[{a,b,c},3] Out[28]= {{a,a,a},{a,a,b},{a,a,c},{a,b,a},{a,b,b},{a,b,c},{a,c,a},{a,c,b},{a,c,c},{b,a, a},{b,a,b},{b,a,c},{b,b,a},{b,b,b},{b,b,c},{b,c,a},{b,c,b},{b,c,c},{c,a, a},{c,a,b},{c,a,c},{c,b,a},{c,b,b},{c,b,c},{c,c,a},{c,c,b},{c,c,c}} There is also an "illegal" (meaning strongly discouraged by wri) way of solving many such problems, which personally I rather like. You first clear the Orderless attribute in Times. ClearAttributes[Times, Orderless] Next, note that if you simply evaluate In[29]:= Expand[(a+b+c)^3] Out[29]= 2 2 2 2 a b a + c b a + b a + a c a + b c a + c a + b a + c a + 3 2 2 2 a + b a b + c a b + a b + a c b + b c b + c b + a b + 2 3 2 2 c b + b + b a c + c a c + a c + a b c + c b c + b c + 2 2 3 a c + b c + c you have essentially got your answer. If you want to get it in the usual form you can do something like In[30]:= Expand[(a+b+c)^3]/.{Plus->List,Times->List, Power[x_,n_]:>Apply[Sequence,Table[x,{n}]]} Out[30]= {{a, b, a}, {c, b, a}, {b, b, a}, {a, c, a}, {b, c, a}, {c, c, a}, {b, a, a}, {c, a, a}, a, a, a, {b, a, b}, {c, a, b}, {a, a, b}, {a, c, b}, {b, c, b}, {c, c, b}, {a, b, b}, {c, b, b}, b, b, b, {b, a, c}, {c, a, c}, {a, a, c}, {a, b, c}, {c, b, c}, {b, b, c}, {a, c, c}, {b, c, c}, c, c, c} The reason why this is discouraged is that users ar enot supposed to change attributes of basic functions like Times. One can, this sort of thing quite legitimatley using the Non-commutative algebra package available from MathSource, or define your own NonCommutativeExpand. On Thu, Jan 28, 1999, Carlos Carreto <ccarreto at ipg.pt> wrote: >Hello, > >I am trying to construct all possible orderings of for example 3 >elements, where each element can be A, B or C. > >The result will be something like this: AAA >AAB >AAC >ABA >. . . >CCC > >How can I do this? > >Thank you. > >-:- Carlos > > Andrzej Kozlowski Toyama International University JAPAN http://sigma.tuins.ac.jp/ http://eri2.tuins.ac.jp/