Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11472 > unrolled thread
| Started by | Earl Mitchell <earl.j.mitchell@gmail.com> |
|---|---|
| First post | 2012-08-24 00:55 +0000 |
| Last post | 2012-08-24 00:55 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Non-sequential composition of pure functions Earl Mitchell <earl.j.mitchell@gmail.com> - 2012-08-24 00:55 +0000
| From | Earl Mitchell <earl.j.mitchell@gmail.com> |
|---|---|
| Date | 2012-08-24 00:55 +0000 |
| Subject | Re: Non-sequential composition of pure functions |
| Message-ID | <k16jdg$beb$1@smc.vnet.net> |
Thanks again - I think I got it. The solution - slightly modified from
what Albert sent - was this (and if there are obvious improvements to what
I do here I'd love to hear them, learning a lot even though I program in M
every day!):
originalconds = {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] &}
out472:= Join[{originalconds[[1]]},
Table[
With[{
condlist = Take[originalconds, i]
},
With[{
action = First@Last[condlist /. If[c_, a_] :> (a) /. Function -> List]
},
If @@@ (Function @@ {Join[
And @@@
Hold @@ {Flatten[
Hold @@ Cases[condlist, Function[If[c_, a_]] :> Hold[c],
{1}]]},
Hold[action]]})]], {i, 2, Length[originalconds]}]]
out472= {If[#1[[4]] <= 6, 4.52] &,
If[#1[[4]] <= 6 && (#1[[4]] <= 6 && #1[[4]] <= 0), 4.47] &,
If[#1[[4]] <= 6 && (#1[[4]] <= 6 && #1[[4]] <= 0) && (#1[[4]] <= 6 &&
#1[[4]] <= 0 && #1[[5]] <= 0), 4.4] &,
If[#1[[4]] <=
6 && (#1[[4]] <= 6 && #1[[4]] <= 0) && (#1[[4]] <= 6 && #1[[4]] <=
0 && #1[[5]] <= 0) && (#1[[4]] <= 6 && #1[[4]] <= 0 && #1[[5]] <=
0 && #1[[1]] <= 0), 4.02] &,
If[#1[[4]] <=
6 && (#1[[4]] <= 6 && #1[[4]] <= 0) && (#1[[4]] <= 6 && #1[[4]] <=
0 && #1[[5]] <= 0) && (#1[[4]] <= 6 && #1[[4]] <= 0 && #1[[5]] <=
0 && #1[[1]] <= 0) && (#1[[4]] <= 6 && #1[[4]] <= 0 && #1[[5]] <=
0 && #1[[1]] <= 0 && #1[[3]] <= 0), 4.3] &}
Thanks again!
Mitch
On Wed, Aug 22, 2012 at 9:05 AM, Sseziwa Mukasa <mukasa@gmail.com> wrote:
> On approach:
>
> Extract the list of conditions from the functions:
>
> (Debug) In[49]:=
> list = {(If[cond1[#[[1]]], action]) &, (If[cond2[#[[2]]],
> action]) &, (If[cond3[#[[3]]], action]) &};
>
> (Debug) In[50]:= list /. (If[cond_, action_] &) -> (cond &)
>
> (Debug) Out[50]= {cond1[#1[[1]]] &, cond2[#1[[2]]] &,
> cond3[#1[[3]]] &}
>
> Then you can use Through and And to evaluate all the conditions:
>
> (Debug) In[53]:= And @@
> Through[(list /. (If[cond_, action_] &) -> (cond &))[{arg1, arg2,
> arg3}]]
>
> (Debug) Out[53]= cond1[arg1] && cond2[arg2] && cond3[arg3]
>
> At this point all you need to do is extract the action from one of the
> functions, or all of them if they are different, and perform based on the
> result of the conditional. Putting it all together:
>
> (Debug) In[55]:=
> combineConditionalFunctions[functions_ {If[_, _] & ..},
> arguments_ {__}] :=
> Module[{performAction =
> And @@ Through[(functions /. (If[cond_, action_] &) -> (cond &))[
> arguments]],
> action = functions[[1]] /. (If[_, action_]) & -> action},
> If[performAction, action]]
>
> (Debug) In[56]:= combineConditionalFunctions[list, {arg1, arg2, arg3}]
>
> (Debug) Out[56]= If[
> cond1[arg1] && cond2[arg2] && cond3[arg3], action$900]
>
>
> On Wed, Aug 22, 2012 at 2:27 AM, Earl Mitchell <earl.j.mitchell@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
>>
>
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web