Re: array ordered
- To: mathgroup at smc.vnet.net
- Subject: [mg3203] Re: array ordered
- From: villegas (Robert Villegas)
- Date: Sat, 17 Feb 1996 14:15:40 -0500
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
In article <4fhh6k$963 at dragonfly.wolfram.com> TTCJ34A at prodigy.com (DR JOHN C ERB) writes: > I have six pieces of plastic of thickness: > thick={0.48,0.71,1.41,3.45,5.61,13.49}; > > By using these in various combinations > (i.e., 1 piece, 2 pieces,... 6 pieces), > I can have: > numberthick={1,2,3,4,5,6}; > total = Apply[Plus,Map[Binomial[6,#]&,numbthick]]; > total = 63 > 63 different thickness. > > How can I use Mathematica to get an array, ordered by > increasing thicknesses of plastic, giving the > 63 different thicknesses, and combination of pieces > needed to give each thickness? This problem amounts to finding all subsets of the list of thicknesses T, and for each subset adding up its elements. Any subset of T can be described by giving a status to each of T's elements: absent or present (as noted in Jorma Virtamo's solution). In terms of a contribution to the total thickness, the ith element adds 0 if it is absent, or adds ti if it is present. Thus, if you take the Cartesian product of these ordered pairs: {0, t1} x {0, t2} x . . . x {0, tn} you get all possible combinations of plates, e.g. {t1, 0, 0, t4, t5, ...}, and you can add the elements of each combination. 'Distribute' is perfect for forming Cartesian products and doing what you want with the n-tuples that result (such as adding their elements). Let's look at an example where it's easy to see that the result is indeed all possible thicknesses: consecutive powers of 2. In[1]:= thicknesses = 2^Range[0, 4] Out[1]= {1, 2, 4, 8, 16} Every element contributes 0 or its thickness value: In[2]:= presentOrNot = Thread[{0, thicknesses}] Out[2]= {{0, 1}, {0, 2}, {0, 4}, {0, 8}, {0, 16}} Indeed, the possible thickness are 0 through (2^5 - 1): In[3]:= Distribute[presentOrNot, List, List, List, Plus] //Sort Out[3]= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, > 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} The "List, List, List, Plus" arguments of Distribute deserve some explanation. The second argument of Distribute gives the heads of the "sets" whose elements you are putting together in combinations. In our case, the sets are {0, 1}, {0, 2}, etc., which have head 'List'. The third argument of Distribute gives the overall head of the expression in argument 1 of Distribute. This can be thought of as the operator connecting the sets. This operator will surround each n-tuple at the end. Here is a short example to show the meaning of arguments 2 and 3: In[6]:= Distribute[f[g[a, b], g[x, y]], g, f] Out[6]= g[f[a, x], f[a, y], f[b, x], f[b, y]] It happens that often you want to replace f and g after the Distribute is over. For instance, you can replace f with some function that will operate on the n-tuples, and you could replace g with something that will act on the collection of all results. The fourth and fifth arguments allow you to give these replacement functions. That is, arg 2 -> arg 4, and arg3 -> arg5. Let's say I want to add the n-tuples and then multiply the sums: In[7]:= Distribute[f[g[a, b], g[x, y]], g, f, Times, Plus] Out[7]= (a + x) (b + x) (a + y) (b + y) Or use Power on the n-tuples and add the results: In[8]:= Distribute[f[g[a, b], g[x, y]], g, f, Plus, Power] x y x y Out[8]= a + a + b + b In our case, we want to add subsets of thicknesses (Plus) and collect the results in a list (List). Robby Villegas ==== [MESSAGE SEPARATOR] ====