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


Groups > comp.soft-sys.math.mathematica > #11445 > unrolled thread

Non-sequential composition of pure functions

Started byEarl Mitchell <earl.j.mitchell@gmail.com>
First post2012-08-22 06:26 +0000
Last post2012-08-23 06:54 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#11445 — Non-sequential composition of pure functions

FromEarl Mitchell <earl.j.mitchell@gmail.com>
Date2012-08-22 06:26 +0000
SubjectNon-sequential composition of pure functions
Message-ID<k11u1r$t8m$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#11448

FromRay Koopman <koopman@sfu.ca>
Date2012-08-22 09:20 +0000
Message-ID<k1289d$10p$1@smc.vnet.net>
In reply to#11445
On Aug 21, 11:26 pm, Earl Mitchell <earl.j.mitch...@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]

[toc] | [prev] | [next] | [standalone]


#11450

Fromawnl <awnl@gmx-topmail.de>
Date2012-08-22 09:21 +0000
Message-ID<k128a1$10u$1@smc.vnet.net>
In reply to#11445
Hi,

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

it is not clear what you mean by "combining" the functions, but whatever 
you want to do, I would never suggest to convert to string and back. It 
is one of the great strengths of Mathematica that you can treat 
Mathematica code just a any other expression, although the evaluation 
scheme makes it somewhat tricky to manipulate code without evaluating 
it. Here is an example which does something along the lines you described:

condlist={If[#[[1]]>0,Print["1"]]&,If[#[[2]]>0,Print["2"]]&}

If@@@(Function@@{Join[
And@@@Hold@@{Flatten[Hold@@Cases[condlist,Function[If[c_,a_]]:>Hold[c],{1}]]},
Hold[Print["3"]]
]})

There might be somewhat simpler or clearer ways to do such 
manipulations. Which "tricks" you use to avoid the evaluation of the 
code you manipulate are also a matter of taste, it's usually some 
combination of Hold, Unevaluated, With that you'll need. If you want a 
solution for your very problem, you would need to send a full example of 
input and desired output...

hth,

albert

[toc] | [prev] | [next] | [standalone]


#11452

Fromawnl <awnl@gmx-topmail.de>
Date2012-08-23 06:51 +0000
Message-ID<k14jtg$7e6$1@smc.vnet.net>
In reply to#11445
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

[toc] | [prev] | [next] | [standalone]


#11460

FromRalph Dratman <ralph.dratman@gmail.com>
Date2012-08-23 06:53 +0000
Message-ID<k14k1l$7g9$1@smc.vnet.net>
In reply to#11445
I have developed some relevant techniques for handling this sort of
problem, but in order to suggest what to do, I need to see more
details of what the desired result should look like. Could you please
illustrate that with an example of (manually) combining two or three
pure functions, showing what you would like the resulting function to
look like?

Thank you.

Ralph Dratman


On Wed, Aug 22, 2012 at 5:19 AM, awnl <awnl@gmx-topmail.de> wrote:
> Hi,
>
>> 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.
>
> it is not clear what you mean by "combining" the functions, but whatever
> you want to do, I would never suggest to convert to string and back. It
> is one of the great strengths of Mathematica that you can treat
> Mathematica code just a any other expression, although the evaluation
> scheme makes it somewhat tricky to manipulate code without evaluating
> it. Here is an example which does something along the lines you described:
>
> condlist={If[#[[1]]>0,Print["1"]]&,If[#[[2]]>0,Print["2"]]&}
>
> If@@@(Function@@{Join[
> And@@@Hold@@{Flatten[Hold@@Cases[condlist,Function[If[c_,a_]]:>Hold[c],{1}]]},
> Hold[Print["3"]]
> ]})
>
> There might be somewhat simpler or clearer ways to do such
> manipulations. Which "tricks" you use to avoid the evaluation of the
> code you manipulate are also a matter of taste, it's usually some
> combination of Hold, Unevaluated, With that you'll need. If you want a
> solution for your very problem, you would need to send a full example of
> input and desired output...
>
> hth,
>
> albert
>

[toc] | [prev] | [next] | [standalone]


#11462

FromEarl Mitchell <earl.j.mitchell@gmail.com>
Date2012-08-23 06:54 +0000
Message-ID<k14k34$7gv$1@smc.vnet.net>
In reply to#11445
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
Mitch



On Wed, Aug 22, 2012 at 3:19 AM, Ray Koopman <koopman@sfu.ca> wrote:

> On Aug 21, 11:26 pm, Earl Mitchell <earl.j.mitch...@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]
>
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web