MathGroup Archive 2000

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

Search the Archive

Manipulating Slot objects in Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24584] Manipulating Slot objects in Compile
  • From: "Johannes Ludsteck" <ludsteck at zew.de>
  • Date: Tue, 25 Jul 2000 00:56:16 -0400 (EDT)
  • Organization: Zentr. f. Europ. Wirtschaftsforsch
  • Sender: owner-wri-mathgroup at wolfram.com

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}
and compute the gradient:

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
applying r to grad

c = Compile[{{cArg, _Real, 1}},
    Evaluate[Evaluate[grad /. r] &[cArg]]]
Unfortunately, Mathematica doesn't replace the Slot[] object by 
cArg and I get the following Output:

CompiledFunction[{cArg},
 ( { 2 Slot[][[1]] Slot[][[2]]^3, 3 Slot[][[1]]^2 Slot[][[2]]^2 } & )[cArg],
-CompiledCode-]

Thus I tried to circumvent the problem by using Slot[1] instead:
rules = Table[arg[[i]] -> Slot[][[i]], {i, 2}]

Now the Slot[1] object disappeared, but Slot[1][[1]] was replaced
by 1 and I got again the false result:

c = Compile[{{cArg, _Real, 1}},
    Evaluate[Evaluate[grad /. r] &[cArg]]]
now evaluates to

CompiledFunction[{cArg},
	{ 2 cArg[[2]]^3, 3 cArg[[2]]^2}, -CompiledCode-]

The Problem here is that Slot[1][[1]] is replaced by 1.
How can I force Mathematica to replace Slot[][[i]] by cArg[[i]] or
to replace Slot[[1]][[1]] by cArg[[1]] 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):

compiledGradient[fun_, lenArg_] :=
  Module[{arg, rules, grad},
    arg = Table[Unique[], {lenArg}];
    grad = Map[D[fun[arg], #] &, arg];
    rules = Table[arg[[i]] -> Slot[][[i]], {i, lenArg}];
    Compile[{{cArg, _Real, 1}},
      Evaluate[Evaluate[grad /. rules] &[cArg]]]] 

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: Re: Malfunction of Position or where am I wrong?
  • Next by Date: Re: Speeding up Replacement Rules
  • Previous by thread: Re: NIntegrate within FixedPoint (HoldAll)
  • Next by thread: Re: Manipulating Slot objects in Compile