Numerical contour plot of compiled function

Ok I figured it out. You specify the contours you want through the Contours option of the function, surprisingly.

f = Compile[{{x}, {y}}, x^2 + y^2];
ContourPlot[f[x, y], {x, y} \[Element] Disk[{0, 0}, 1], 
 Contours -> {0.1}, ContourShading -> False]

Works beautifully. If you specify an equality in the argument ContourPlot defaults to symbolic solving which is usually way too slow.


Try

f = Compile[{{x, _Real}, {y, _Real}}, x^2 + y^2];
ContourPlot[f[x, y] == 0.1, {x, y} \[Element]Disk[{0, 0}, 1]]

enter image description here


The message you are getting means that ContourPlot internally is calling f with symbolic arguments contrary to its documentation.

ContourPlot has attribute HoldAll, and evaluates the fi and gi only after assigning specific numerical values to x and y.

Nevertheless, many functions expect to be able to evaluate user-supplied functions symbolically. There is an idiomatic way to prevent these calls: tell the pattern matcher that the arguments must be numeric.

Clear[f, fCompiled];
fCompiled = Compile[{{x}, {y}}, x^2 + y^2];
f[x_?NumericQ, y_?NumericQ] := fCompiled[x, y];
ContourPlot[f[x, y] == 0.1, {x, y} \[Element] Disk[{0, 0}, 1]]

This produces no diagnostics.

There are two confusable conditions, ?NumberQ and ?NumericQ. Since NumberQ[Pi] == False, NumericQ[Pi] == True, and fCompiled[Pi,Pi] == 19.739208802178716`, NumberQ is too strict. Use NumericQ.