       Re: Manipulating Slot objects in Compile

• To: mathgroup at smc.vnet.net
• Subject: [mg24604] Re: Manipulating Slot objects in Compile
• From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
• Date: Fri, 28 Jul 2000 17:23:42 -0400 (EDT)
• Organization: Universitaet Leipzig
• References: <8lj87k\$8ot@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi,

can you tell me for what you need the Slot[] objects ?
You whant to generate cArg[[i]] and no Slot[[i]]

The implementation is simple
f[{x_, y_}] := x^2 y^3
arg = Table[Unique[], {2}];
grad = Map[D[f[arg], #] &, arg];
arg -> Table[
Hold[ cArg[[i]] ] /. i -> k, {k, 1,
2}] ]

c = Compile @@ {{{cArg, _Real, 1}}, Hold @@ {grad /. rules}} /.
Hold[a_Part] :> a /. Hold[a_] :> Unevaluated[a]

I explained yesterday how it works.

Regards
Jens

Johannes Ludsteck wrote:
>
> Dear Mathgroup Members,
> I want to write a program which computes the gradient of any
> function and then compiles this expression. I tried to do this by
> manipulating Slot objects.
> Unfortunately there is the following problem:
> First I define an example function.
>
> f[{x_, y_}] := x^2 y^3,
>
> Then I generate a list of (unique) variables for differentiation:
>
> arg = Table[Unique[], {2}]
> {\$1,\$2}
>
> grad = Map[D[f[arg], #] &, arg]
> {2 \$1 \$2^3, 3 \$1^2 \$2^2}
>
> Then I generate a set of replacement rules
> rules = Table[arg[[i]] -> Slot[][[i]], {i, 2}]
>
> and use them in order to get compiled code by
>
> c = Compile[{{cArg, _Real, 1}},
> Unfortunately, Mathematica doesn't replace the Slot[] object by
> cArg and I get the following Output:
>
> CompiledFunction[{cArg},
>  ( { 2 Slot[][] Slot[][]^3, 3 Slot[][]^2 Slot[][]^2 } & )[cArg],
> -CompiledCode-]
>
> Thus I tried to circumvent the problem by using Slot instead:
> rules = Table[arg[[i]] -> Slot[][[i]], {i, 2}]
>
> Now the Slot object disappeared, but Slot[] was replaced
> by 1 and I got again the false result:
>
> c = Compile[{{cArg, _Real, 1}},
> now evaluates to
>
> CompiledFunction[{cArg},
>         { 2 cArg[]^3, 3 cArg[]^2}, -CompiledCode-]
>
> The Problem here is that Slot[] is replaced by 1.
> How can I force Mathematica to replace Slot[][[i]] by cArg[[i]] or
> to replace Slot[][] by cArg[] instead, or is there any other
> way to get a compiled gradient from any function?
>
> Any suggestions?
>
> Of course, the seemingly awkward usage of Unique[]
> and Tables of rules is in order to collect the pieces of code into
> one Module which does the job for any function of any dimension
> (lenArg is the dimension of the argument list of function fun):
>
>     arg = Table[Unique[], {lenArg}];
>     grad = Map[D[fun[arg], #] &, arg];
>     rules = Table[arg[[i]] -> Slot[][[i]], {i, lenArg}];
>     Compile[{{cArg, _Real, 1}},
>
> Thank you and best regards,
>         Johannes Ludsteck
>
> Johannes Ludsteck
> Centre for European Economic Research (ZEW)
> Department of Labour Economics,
> Human Resources and Social Policy
> Phone (+49)(0)621/1235-157
> Fax (+49)(0)621/1235-225
>
> P.O.Box 103443
> D-68034 Mannheim
> GERMANY
>
> Email: ludsteck at zew.de

```

• Prev by Date: reporting solution
• Next by Date: Re: Re: More about l`Hopital`s rule
• Previous by thread: Manipulating Slot objects in Compile
• Next by thread: Re: Manipulating Slot objects in Compile