Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

RE: Speeding up Replacement Rules

  • To: mathgroup at
  • Subject: [mg24686] RE: [mg24527] Speeding up Replacement Rules
  • From: Wolf Hartmut <hwolf at>
  • Date: Fri, 4 Aug 2000 01:19:08 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

a partial answer (I hope) at 

> -----Original Message-----
> From:	Johannes Ludsteck [SMTP:ludsteck at]
To: mathgroup at
> Sent:	Monday, July 24, 2000 9:04 AM
> To:	mathgroup at
> Subject:	[mg24527] Speeding up Replacement Rules
> Dear MathGroup Members,
> I use Mathematica to compute the hessian of a complicated 
> function of a vector of about 50 variables. My problem with the job 
> is that I need the mean of the hessian for about 50000 sets of 
> vector values.
> Of course, it is simpe to compute a symbolic expression of the 
> hessian in two steps:
> g=Map[D[f[args],#]&,args];
> h=Map[D[g,#]&,args];
> and to use this to compute the mean by defining a list of 500000 
> replacement rules, and to replace the stuff with
> (Plus@@(h/.rules))/50000;
> This works fine but very sloooooooow. Since I have to redo the 
> computation of the mean some hundred times, I nead a drastic 
> gain in speed. I think that the main reason for the poor performance 
> of my strategy is that the replacement operation is slow. I think it 
> should be possible to generate a Compiled function object which is 
> much faster. Since I expect that this will require some time, I would 
> like to know whether the increase in speed will compensate me for 
> the pains of the implementation.
> Of course, if someone has Mathematica code which takes a vector 
> valued function and generates a Compiled gradient or hessian 
> function, I will accept it gratefully.
[Hartmut Wolf]  

you may get your compiled Hessian just along the lines of my reply to
"[mg24584] Manipulating Slot objects in Compile":

f[{x_, y_}] := x^2 y^3

dimension = 2;



cArgList = Table[cArg[[i]], {i, dimension}]

hessian = 
  Compile[{{cArg, _Real, 1}}, 
    Evaluate[Map[D[Map[D[f[cArgList], #] &, cArgList], #] &, cArgList]]]

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

(This is not the output of the computation, but generated from that to make
it visible here). Compare to

D[f[{x, y}], x, x]
D[f[{x, y}], x, y]
D[f[{x, y}], y, y]

hessian[{0.5, 0.7}]

{{0.686, 1.47}, {1.47, 1.05}}

> The simple advice to compute the hessian by hand and to put this 
> in a Compiled function is worthless for me, since I have to apply 
> the mean hessian computation to a variety of different functions.
> Thank you,
> 	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
> Email: ludsteck at

  • Prev by Date: Test for On/Off (was: RE: Re: Manipulating Slot objects in Compile)
  • Next by Date: Re: mathematica gets a simple limit wrong?
  • Previous by thread: Test for On/Off (was: RE: Re: Manipulating Slot objects in Compile)
  • Next by thread: Re: mathematica gets a simple limit wrong?