# Cannot avoid redundant function evaluations in RegionPlot

I am trying to use RegionPlot to visualize the regions where a function has different integer values. For example

RegionPlot[{Round[x + y] == 0, Round[x + y] == 1, Round[x + y] == 2, Round[x + y] > 2}, {x, 0, 3}, {y, 0, 3}]


In my actual problem, the function is time-consuming to compute, and I'd like to avoid evaluating it, redundantly, for each of the cases. I know could define it using this construct

f[x_,y_]:= f[x,y] = (function body)


so that the values are saved, but I'd prefer to avoid this, as in my experience it leads to bugs and errors later when I'm developing further and I forget that the values are being saved.

All my attempts fail when trying to replace the list argument of RegionPlot with something that requires one function evaluation; usually it ends up giving something equivalent to plotting the first equality only, i.e., as if I had used

RegionPlot[Round[x + y] == 0, {x, 0, 3}, {y, 0, 3}]


I tried, for example

myfun[x_, y_] := Module[{z = Round[x + y]}, {z == 0, z == 1, z == 2, z > 2}]
RegionPlot[myfun[x, y], {x, 0, 3}, {y, 0, 3}]


which gives the first-equality-only result. Based on an answer to a similar question, I also tried

RegionPlot[Evaluate[Hold[myfun[x, y][[#]]] & /@ Range[4]], {x, 0, 1}, {y, 0, 1}]


which gives an error and returns an empty plot.

As it is now, the plot takes a couple of hours to generate, and I have a total of 9 cases that I check, so I think I could reduce the time quite a bit by avoiding these redundant calculations. If I can't find a better way, I'll go ahead and use the function form where the results are stored.

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jul 2 '16 at 18:46
• Would something like DensityPlot[Round[x + y], {x, 0, 3}, {y, 0, 3}] work better? – mikado Jul 2 '16 at 19:16
• I don't think it's possible to do it with just one function evaluation at each point - it would require RegionPlot to refine multiple regions in parallel and AFAIK it's just not written that way. The memoization approach is the right way to go - you could always clear the stored values afterwards if you think they will cause problems later. – Simon Woods Jul 2 '16 at 21:21
• Thanks! This gives me a better idea of what RegionPlot is doing. It isn't looking for a list of booleans at each x,y point, but is working with each function independently, refining each as needed. So it isn't evaluating all nine functions at every x,y, but is picking and choosing different ones as it refines the mesh for each. That means there aren't really redundant evaluations at all, and there's not much hope for improvement this way. Maybe I'll take mikado's suggestion and look at other plot functions, or tweak the parameters (eg MaxRecursion) and take a lower-quality plot. – NiMo Jul 2 '16 at 22:25