MathGroup Archive 2003

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

Search the Archive

RE: Pure Functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42577] RE: [mg42551] Pure Functions
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Wed, 16 Jul 2003 09:13:38 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Oliver Ruebenkoenig [mailto:ruebenko at imtek.uni-freiburg.de]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, July 15, 2003 8:54 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg42577] [mg42551] Pure Functions
>
>
>Hi,
>
>I am looking for the solution to the following problem and am grateful
>for any hints:
>
>Consider the pure function:
>
>In[1] := f = Function[ { x, y },
>    Function[ { a, b, c, d, e, f }, a*x + b *y  + c + d + e + f] ];
>
>I can then
>
>In[2]:= f[ 2, 2 ]
>
>Out[2]= Function[{a$, b$, c$, d$, e$, f$}, a$ 2 + b$ 2 + c$ + d$ + e$ + f$]
>
>The result is, as expected, another function. A complete call 
>would look like this:
>
>In[3]:= f[ 2, 2 ][ 1, 2, 3, 4, 5, 6 ]
>
>Out[3]= 24
>
>Fine. Now i can for example differentiate
>
>In[4]:= D[ f[ x, y ], { x, 1 } ]
>
>Out[4]= Function[{a$, b$, c$, d$, e$, f$}, a$]
>
>and a function is returned. Nice.
>
>The problem is that i would like to use constructors and selectors to
>create a new level of abstraction. For this reason i define
>
>In[5]:= MakeMyHead[a_,b_,c_,d_,e_,f_] := myHead[a,b,c,d,e,f];
>
>and
>
>In[6]:= GetTestValues[ myHead[a_,b_,c_,d_,e_,f_]] := {a,b,c,d,e,f};
>
>This works as follows:
>In[7]:= example = MakeMyHead[ 1, 2, 3, 4, 5, 6 ]
>
>Out[7]= myHead[1, 2, 3, 4, 5, 6]
>
>and
>
>In[8]:= GetTestValues[ example ]
>
>Out[8]= {1, 2, 3, 4, 5, 6}
>
>The problem is now that i would like a code that in pseudo 
>code does this:
>(* pseudo code
>f = Function[ { x, y },
>    Function[ { mh_myHead },
>      Module[ { a, b, c, d, e, f }, { a, b, c, d, e, f } =
>          GetTestValues[ mh ];  a*x + b *y  + c + d + e + f] ] ]
>*)
>
>a) Is it possible to hand over specific data types to functions, like
myHead?
>b) How can i create additional local variables in a function.
>
>Differentiating the expression a*x + b*y + c... should be 
>independent of the value of the variables a,b,c,...
>
>Any suggestions on how to do this? Much thanks in advance.
>
>Oliver
>
>NEW Phone number!
>
>Oliver Ruebenkoenig, <ruebenko at imtek.de>
>   Phone: ++49 +761 203 7385
>

Oliver,

I just try to answer to your question a):

As stated: no. If you want to check for a "data type" or any condition you
have to make a definition and use pattern matching of the evaluator.

I'll show how to set up such a definition, to come close to your pseudo
code.

With your example data

In[10]:= GetTestValues[example]
Out[10]= {1, 2, 3, 4, 5, 6}
 
You can make your function f (to make another function)

In[11]:=
f = Function[{x, y}, 
      Function[{mh}, 
        Module[{a, b, c, d, e, f}, {a, b, c, d, e, f} = GetTestValues[mh]; 
          a*x + b*y + c + d + e + f]]];



In[12]:= g = f[2, 3];

In[13]:= g[example]
Out[13]= 26
 
In[14]:= Clear[g]


Now the idea is, instead of assigning f[2,3] to g, we make a definition for
g essentially wrapping the arguments to f[2, 3] with some pattern tests or
conditions of convenience. This is a function which does that:

In[16]:= Attributes[headWrap] = {HoldRest}

In[17]:=
headWrap[f_Function, s_Symbol, patts__] := With[{pp = Sequence[patts]},
    (s[pp] := f[##]) &[
      Sequence @@ 
        Cases[HoldPattern[{patts}], Verbatim[Pattern][var_, _] -> var, 
          Infinity]]]

The transfer of the patterns via local variable pp prevents renaming of the
pattern variables within the patterns. The argument expression to the
function that makes  the definition is just a way to extract the names
(symbols) from the patterns; they must not have a value, however (which is
in your hands; a somewhat more elaborate form of headWrap would ignore
this).


In[18]:= headWrap[f[2, 3], g, y_myHead]

In[20]:= g[example]
Out[20]= 26

In[21]:= g[List @@ example]
Out[21]= g[{1, 2, 3, 4, 5, 6}]


But, I fear, the idea to reassign the GetTestValues to local variables
breaks your ideas about differentiation (which I don't comprehend). You may
however just use your original definition for f (from In[1] above), and then
define g as:

In[423]:= headWrap[f[2, 3], g, myHead[y__]]

In[425]:= DownValues[g]
Out[425]=
{HoldPattern[g[myHead[y__]]] :> 
    Function[{a$, b$, c$, d$, e$, f$}, a$ 2 + b$ 3 + c$ + d$ + e$ + f$][y]}


--
Hartmut Wolf



  • Prev by Date: Re: --Parallel Computing w/beowulf cluster
  • Next by Date: solving a system of equations involving numerical integration
  • Previous by thread: Pure Functions
  • Next by thread: GUI interface help