Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Explanation to Hold?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113931] Explanation to Hold?
  • From: fd <fdimer at gmail.com>
  • Date: Thu, 18 Nov 2010 07:06:35 -0500 (EST)

Dear Group,

While doing some work in Mathematica I stumbled into something that
has let me quite intrigued. Indeed, I'm having some difficulties to
understand how hold works. I'm an unexperienced user, so any
clarification on this would be greatly appreciated.

I'm using Mathematica to generate a report where I display points of
minimum for two different function and a set of 4 parameters.

I start by defining the functions like this


fnA = Function[{x}, x^2 + x]

fnB = Function[{x}, x^4 - x^2]

results = {fnA, fnB};

pointsOfInterest = {1, 2, 3, 4};


And then I generate the points of minimum using the following command

Thread[NArgMin[#, x] & /@ ((# - (pointsOfInterest))^2) & /@
   Through[results[x]]] // Grid

All works fine, but then, when I use compiled functions

fnAC = Compile[{x}, fnA[x]];

fnBC = Compile[{x}, fnB[x]];

resultsC = {fnAC, fnBC}

and use the same command as before

Thread[NArgMin[#, x] & /@ ((#[x] - (pointsOfInterest))^2) & /@
resultsC]

I get the following message, along with the answer I had before.

CompiledFunction::cfsa: Argument x at position 1 should be a machine-
size real number.

Well, I thought that functions like NArgMin would first assign a real
value to x and then evaluate the function, as Plot would do. When I
hold the compiled function

NArgMin[Hold[(fnAC[x] - 3)^2], x]

I do get the result without any warning message.

If I plot the compiled functions I don't get any message either.

Funnier still, when I use the mapping to plot everything at once

Thread[Plot[#, {x, -10, 10}] & /@ ((#[x] - (pointsOfInterest))^2) & /@
   resultsC]

I do get the  same message

CompiledFunction::cfsa: Argument x at position 1 should be a machine-
size real number

But in this case I can remedy by using Hold and ReleaseHold


Thread[Plot[
      ReleaseHold[#], {x, -10,
       10}] & /@ ((Hold[#[x]] - (pointsOfInterest))^2) & /@ resultsC]


But this same trick will not work with NArgMin, which is puzzling to
me.

Well, any comments?

Best Regards















  • Prev by Date: JLink Java exception reading netcdf file via input
  • Next by Date: Re: Problem with Series
  • Previous by thread: Re: JLink Java exception reading netcdf file via input
  • Next by thread: maximum of a series