MathGroup Archive 2012

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

Search the Archive

Re: Non-sequential composition of pure functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127791] Re: Non-sequential composition of pure functions
  • From: awnl <awnl at gmx-topmail.de>
  • Date: Thu, 23 Aug 2012 02:51:22 -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
  • References: <k11u1r$t8m$1@smc.vnet.net> <20120822091941.291ED6852@smc.vnet.net> <CAFAKiUS=A9Qyj_ReXSqKUC+TvOA5MGYb1TeNYapFwaCw3OZ53g@mail.gmail.com>

Hi Earl,
>
> This is very close to what I need. ... 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.

The following should do what you want. I decided to store the 
intermediate results as I think it is somewhat easier to follow what 
happens. This is just what I came up with at first try, as usual there 
are plenty possibilities to achieve the same thing, some of which might 
be much more elegant. The code is also not bullet proof, but I didn't 
want to introduce additional complexity for making it more safe or 
general. Anyway I hope it is good enough to get you started:

(*extract the held conditions and bodies (in this case just values) *)
heldConditions=Cases[list1,Function[If[c_,_]]:>Hold[c]]
heldBodies=Cases[list1,Function[If[_,b_]]:>Hold[b]]

(* create the "accumulated" conditions, still wrapped with Hold *)
accumulatedConditions=ReplaceAll[
   Hold/@Rest[FoldList[And,True,heldConditions]],
   Hold[LessEqual[x___]]:>LessEqual[x]
]

(* finally compose the resulting functions from the accumulated 
conditions and the stored "bodies" *)
targetList=Function/@Flatten/@Hold@@@Transpose[{accumulatedConditions,heldBodies}]/.Hold->If

hth,

albert



  • Prev by Date: Re: Problem with NIntegrate
  • Next by Date: Re: Drawing a Line From One Plot to Another
  • Previous by thread: Re: Non-sequential composition of pure functions
  • Next by thread: Re: Non-sequential composition of pure functions