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: [mg127811] Re: Non-sequential composition of pure functions
  • From: Earl Mitchell <earl.j.mitchell at gmail.com>
  • Date: Thu, 23 Aug 2012 20:50:34 -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: <20120822062720.4DD8A6850@smc.vnet.net>

I ended up getting the solution to this guy earlier in the thread.  I've
resent it below for reference.

If anyone is interested in taking on a side-M project (creating an M
version of the RandomForest algorithm is the current task) send me a
personal e-mail at earl.j.mitchell at gmail.com.

This group is excellent, thanks again -

Mitch

On Wed, Aug 22, 2012 at 9:16 AM, Earl Mitchell <earl.j.mitchell at gmail.com>wrote:

> 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 at 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 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
>>>
>>
>>
>


  • Prev by Date: Re: How to use Pick[]; Is this a bug?
  • Next by Date: Re: How to use Pick[]; Is this a bug?
  • Previous by thread: Re: Non-sequential composition of pure functions
  • Next by thread: Re: Non-sequential composition of pure functions