7
$\begingroup$

Consider the following code.

RegionPlot[Print[{x, y}], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 2]
{-1.996,-1.996}
{Region`Private`RegionVar[1],Region`Private`RegionVar[2]}
{-2.,-2.}
{2.,-2.}
{-2.,2.}
{2.,2.}

My question is: what is RegionPlot doing to create the first two lines of output? I do not understand why the first sample point is {-1.996, -1.996} and why the second sample point is {Region`Private`RegionVar[1],Region`Private`RegionVar[2]}.

I am running this in Mathematica 9. I do not recall this happening in an earlier version.

Edit: I changed the code above to give a better minimal working example. I leave below the code I first gave along with its truncated output.

myFunc[pt_] := Module[ {bool},
  bool = Sin[ pt[[1]] ] > 0;
  Print[pt, bool];
  If[ bool, Return[True], Return[False] ];
];

RegionPlot[myFunc[ {x, y} ], {x, -2, 2}, {y, -2, 2}, PlotPoints -> 2]
{-1.996,-1.996}False

{Region`Private`RegionVar[1],Region`Private`RegionVar[2]}Sin[Region`Private`RegionVar[1]]>0

{-2.,-2.}False

{2.,-2.}True
$\endgroup$
  • 2
    $\begingroup$ I can confirm that this doesn't happen in version 8.0.4.0 but in version 9.0.0.0 - no idea what changed... $\endgroup$ – Jens Feb 8 '13 at 3:10
  • 1
    $\begingroup$ As a clue this resembles the behavior of regular Plot[] which first evaluates the function with a numeric argument, then a symbolic arg, followed by the remaining numeric values. $\endgroup$ – george2079 Feb 8 '13 at 23:23
3
$\begingroup$

According to a representative at Wolfram Research

This behavior doesn't appear to be documented, but also doesn't appear to be a bug of any kind. I apologize, we have no information about this aspect of the internal workings of RegionPlot.

$\endgroup$
1
$\begingroup$

This still happens in Mathematica 10.4, and RegionPlot can fail to produce a plot, with the message Throw::nocatch: "Uncaught Throw[$Failed] returned to top level.".

As a temporary fix one can use NumericQ to evaluate the predicate function only when RegionPlot sends genuine numeric data:

myFunc[pt_] := Module[{bool},
    If[! NumericQ[pt[[1]]], Return[False]];    <-----
    bool = Sin[pt[[1]]] > 0;
    Print[pt,bool];
    If[bool, Return[True], Return[False]];
];

This is not ideal, as the evaluation result can be incorrect in one point (corresponding to the second call of the predicate function by RegionPlot) but at least RegionPlot will output a graph and it worked for me.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.