Message Boards Message Boards

Differences in handling compiled functions between Plot3D and VectorPlot

Posted 11 years ago
I have run into an interesting situation using a compiled function with VectorPlot (and StreamPlot) to plot 2D slices of a 3D vector field, wherein I get the following error:
CompiledFunction::cfsa: Argument x at position 1 should be a machine-size real number. >>
but the operation still produces the desired results.

Consider a function ff (simplified for the example's sake)
ff[{x_, y_, z_}] := {x, y, z}
ffcomp = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, ff[{x, y, z}]]

Plot3D:
Plot3D[ff[{x, 0, y}][[{1, 3}]], {x, 0, 2}, {y, 0, 2}]
Plot3D[ffcomp[x, 0, y][[{1, 3}]], {x, 0, 2}, {y, 0, 2}]
produce identical results with no error messages.

VectorPlot (or StreamPlot)
VectorPlot[ff[{x, 0, y}][[{1, 3}]], {x, 0, 2}, {y, 0, 2}]
VectorPlot[ffcomp[x, 0, y][[{1, 3}]], {x, 0, 2}, {y, 0, 2}]
Produce the same results but the second case gives the error message shown above. What is the difference in how VectorPlot and Plot3D treat their arguments that causes this?

BTW, working from a suggestion I found on stackexchange, if I define the function
ff2[x_?NumericQ, y_?NumericQ, z_?NumericQ] := ffcomp[x, y, z]
VectorPlot[ff2[x, 0, y][[{1, 3}]], {x, 0, 2}, {y, 0, 2}]
no error message is produced.

So, in summary, while I can get Mathematica to do what I need it to, I'm still trying to fully understand how the world of Hold/Release/Evaluate work.
POSTED BY: T Saab
3 Replies
Some Mathematica functions do a symbolic evaluation first.  I believe there is an option for Compile, EvaluateSymbolically which can be set to True.
POSTED BY: Frank Kampas
Thanks Frank,
However, setting EvaluateSymbolically to either True or False does not change the behavior. I have been able to use these function so far (within Show) by wrapping them in Quiet.
POSTED BY: T Saab
I should have been more explicit.  It's RuntimeOptions -> {"EvaluateSymbolically -> True}
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract