Re: Non-sequential composition of pure functions
- To: mathgroup at smc.vnet.net
- Subject: [mg127797] Re: Non-sequential composition of pure functions
- From: Ray Koopman <koopman at sfu.ca>
- Date: Thu, 23 Aug 2012 02:53:23 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
----- On Wed, Aug 22, 2012 at 08:04 AM, Earl Mitchell <earl.j.mitchell at gmail.com> wrote: > Thanks Ray, > > This doesn't quite answer my question (though it is due to the lack of > clarity I had in my original example). The exact problem I have is this: > > I have the following list of functions: > > list1={If[#1[[4]] <= 6, 4.52] &, If[#1[[4]] <= 0, 4.47] &, If[#1[[5]] <= 0, > 4.40] &, > If[#1[[1]] <= 0, 4.02`] &, If[#1[[3]] <= 0, 4.30] &} > > I want to produce the following list of functions: > > targetlist= { > If[#1[[4]] <= 6, 4.52] &, > If[(#1[[4]] <= 6) && (#1[[4]] <= 0), 4.47] &, > > If[(#1[[4]] <= 6) && (#1[[4]] <= 0) && (#1[[5]] <= 0), 4.40] &, > If[(#1[[4]] <= 6) && (#1[[4]] <= 0) && (#1[[5]] <= 0) && (#1[[1]] <= > 0), 4.02`] &, > > If[(#1[[4]] <= 6) && (#1[[4]] <= 0) && (#1[[5]] <= 0) && (#1[[1]] <= 0) && > (#1[[3]] <= 0), 4.30] & > > } > > Quick aside - I recognize that given the function in position two, the > function in position one is irrelevant (redundant). If the composition > algorithm could check for and discard any *sequential* irrelevancies that > would be even better. If, however, the third function were put sequentially > between functions one and two, then all three statements would be relevant. > > Thanks! > > Mitch In[1]:= list1 = { If[#[[4]] <= 6, 4.52]&, If[#[[4]] <= 0, 4.47]&, If[#[[5]] <= 0, 4.40]&, If[#[[1]] <= 0, 4.02]&, If[#[[3]] <= 0, 4.30]&}; In[2]:= f = Table[With[{ positions = list1[[Range@j,1,1,1,2]], comparands = list1[[Range@j,1,1,2]], result = list1[[ j,1,2]] }, Function[ If[Inner[LessEqual,#[[positions]],comparands,And], result]]], {j,5}] Out[2]= {If[Inner[LessEqual, #1[[{4}]], {6}, And], 4.52]&, If[Inner[LessEqual, #1[[{4,4}]], {6,0}, And], 4.47]&, If[Inner[LessEqual, #1[[{4,4,5}]], {6,0,0}, And], 4.4]&, If[Inner[LessEqual, #1[[{4,4,5,1}]], {6,0,0,0}, And], 4.02]&, If[Inner[LessEqual, #1[[{4,4,5,1,3}]], {6,0,0,0,0}, And], 4.3]&} In[3]:= f[[#]]@{x1,x2,x3,x4,x5}& /@ Range@5 Out[3]= {If[x4 =E2=89=A4 6, 4.52], If[x4 =E2=89=A4 6 && x4 =E2=89=A4 0, 4.47], If[x4 =E2=89=A4 6 && x4 =E2=89=A4 0 && x5 =E2=89=A4 0, 4.4], If[x4 =E2=89=A4 6 && x4 =E2=89=A4 0 && x5 =E2=89=A4 0 && x1 =E2=89=A4 0, 4.02], If[x4 =E2=89=A4 6 && x4 =E2=89=A4 0 && x5 =E2=89=A4 0 && x1 =E2=89=A4 0 && x3 =E2=89=A4 0, 4.3]} > > On Wed, Aug 22, 2012 at 3:19 AM, Ray Koopman <koopman at sfu.ca> wrote: > >> On Aug 21, 11:26 pm, Earl Mitchell <earl.j.mitch... at gmail.com> wrote: >>> Hi All, >>> >>> I have a list of pure functions (If[Conditional[#[[1]]],Action]& >>> statements) and I want to compose them into one large pure function >>> >>> If[FirstConditional[#[[1]]]&&SecondConditional[#[[6]]]&&ThirdConditional[#[[foo]]],Action]&. >>> >>> They are to be applied to a list of values, and the conditionals checked >>> for multiple columns for a given element of a list. >>> >>> I'm having problems joining these things together - to the point that >>> I've considered converting them all to strings and doing the tedious >>> (hackie) string manipulations to get the final function in the right form. >>> Any recommendations on how to do this? I found Composition[] but it >>> nests the functions - and I want to combine them. >>> >>> Thanks! >>> >>> Mitch >> >> In[1]:= f1 = If[c1@#, a1]&; >> f2 = If[c2@#, a2]&; >> f3 = If[c3@#, a3]&; >> >> In[4]:= If[And @@ MapThread[#1@#2&, >> { {f1,f2,f3}[[All,1,1,0]], {x1,x2,x3} }], action] >> >> Out[4]= If[c1[x1] && c2[x2] && c3[x3], action]