The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Explanation to Hold?
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  

Posts: 43
Registered: 7/24/09
Explanation to Hold?
Posted: Nov 18, 2010 7:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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) & /@

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) & /@

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

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

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

Well, any comments?

Best Regards

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.