Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11452

Re: Non-sequential composition of pure functions

From awnl <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Non-sequential composition of pure functions
Date 2012-08-23 06:51 +0000
Message-ID <k14jtg$7e6$1@smc.vnet.net> (permalink)
References <k11u1r$t8m$1@smc.vnet.net> <20120822091941.291ED6852@smc.vnet.net> <CAFAKiUS=A9Qyj_ReXSqKUC+TvOA5MGYb1TeNYapFwaCw3OZ53g@mail.gmail.com>
Organization Time-Warner Telecom

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Non-sequential composition of pure functions Earl Mitchell <earl.j.mitchell@gmail.com> - 2012-08-22 06:26 +0000
  Re: Non-sequential composition of pure functions Ray Koopman <koopman@sfu.ca> - 2012-08-22 09:20 +0000
  Re: Non-sequential composition of pure functions awnl <awnl@gmx-topmail.de> - 2012-08-22 09:21 +0000
  Re: Non-sequential composition of pure functions awnl <awnl@gmx-topmail.de> - 2012-08-23 06:51 +0000
  Re: Non-sequential composition of pure functions Ralph Dratman <ralph.dratman@gmail.com> - 2012-08-23 06:53 +0000
  Re: Non-sequential composition of pure functions Earl Mitchell <earl.j.mitchell@gmail.com> - 2012-08-23 06:54 +0000

csiph-web