Re: Programing operations with lists
- To: mathgroup at smc.vnet.net
- Subject: [mg50867] Re: [mg50856] Programing operations with lists
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 25 Sep 2004 01:55:05 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Guillermo Sanchez [mailto:guillerm at aida.usal.es] To: mathgroup at smc.vnet.net >Sent: Friday, September 24, 2004 10:42 AM >To: mathgroup at smc.vnet.net >Subject: [mg50867] [mg50856] Programing operations with lists > > >Dear group, > >I have two lists with this pattern > >list1={{a1 , b1}, ..., {ai , bi}, ...}; > >list2={c1, ..., c(j-1), cj, ...}; > >and a function f[x] (ie: f[x_] = 0.2 x); > > >I would like grouping and sum the product bi f[ai] where >C(j-1)<ai< cj > > >I show it with an example: > >list1 = {{0.71, 7378}, {1.6, 768}, {2, 2269}, {2.05, 0}, {2.35, 0}, >{2.4, 2608}, {2.6, 193}, {2.8, 4187}, {2.85, 204}, {2.95, 134}, >{3.2,776}, {3.25, 24}, {3.6, 6209}, {3.65, 0}, {3.7, 2809}, >{3.95, 1923}, >{4,2627}, {4.1, 25}, {4.15, 1}, {4.2, 0}, {4.25, -15}, {4.35, 1863}, >{4.4, 1797}, {4.45, 7533}, {4.5, 0}, {4.55, 53}, {4.7, 12045}, >{4.9, 7304}, >{4.95, 34}}; > > >list2 = {0.71, 3.2, 4, 4.5, 5} > > >The solution should be: > >{{7378 f[0.71] + .. +134 f[2.95]}, ... , {53 f[4.55]+...+34 f[4.95]}} > >Thanks > >Guillermo > > Is it this what you want (from your example, not quite exactly what you specified)? In[9]:= (Plus @@ (#2 f[#1] &) @@@ Cases[list1, {x_, _} /; #1 <= x < #2] &) @@@ Partition[list2, 2, 1] Out[9]= {7378 f[0.71] + 768 f[1.6] + 2269 f[2] + 2608 f[2.4] + 193 f[2.6] + 4187 f[2.8] + 204 f[2.85] + 134 f[2.95], 776 f[3.2] + 24 f[3.25] + 6209 f[3.6] + 2809 f[3.7] + 1923 f[3.95], 2627 f[4] + 25 f[4.1] + f[4.15] - 15 f[4.25] + 1863 f[4.35] + 1797 f[4.4] + 7533 f[4.45], 53 f[4.55] + 12045 f[4.7] + 7304 f[4.9] + 34 f[4.95]} I assume ordering of list2 as with your example. -- Hartmut Wolf P.S.: I tried an idea using Split, but only reached a near miss: In[101]:= list0 = Transpose[list1][[1]] In[119]:= i = 1; Split[list0, (If[# < list2[[i]], True, i++; False] &)] Out[119]= {{0.71}, {1.6, 2, 2.05, 2.35, 2.4, 2.6, 2.8, 2.85, 2.95, 3.2}, {3.25, 3.6, 3.65, 3.7, 3.95, 4}, {4.1, 4.15, 4.2, 4.25, 4.35, 4.4, 4.45, 4.5}, {4.55, 4.7, 4.9, 4.95}} In[121]:= i = 5; Split[Reverse[list0], (If[# > list2[[i]], True, i--; False] &)] Out[121]= {{4.95}, {4.9, 4.7, 4.55, 4.5}, {4.45, 4.4, 4.35, 4.25, 4.2, 4.15, 4.1, 4}, {3.95, 3.7, 3.65, 3.6, 3.25, 3.2}, {2.95, 2.85, 2.8, 2.6, 2.4, 2.35, 2.05, 2, 1.6, 0.71}} Can someone help me? (Split before, not after the fact; I mean to start a new grouping when test becomes false not endeng the preceeding)