2

I am looking for a way to speed up code and thus would like to use a compiled function in an ImplicitRegion. Here is an example of a region that can be plotted normally, but fails when I try to use a compiled function. Is there a way to make this work?

RegionPlot3D[
 ImplicitRegion[-x  y z > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]]
(*works*)

TEST=Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, -x  y z];
RegionPlot3D[
 ImplicitRegion[TEST > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]]
(*fails*)
BenP1192
  • 934
  • 6
  • 13
  • Have you tried RegionPlot3D[ ImplicitRegion[TEST[x,y,z] > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]]? – dr.blochwave Jul 24 '15 at 13:44
  • If you are unhappy with the warning message try this cf = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, -x y z]; TEST[x_?NumericQ, y_?NumericQ, z_?NumericQ] := cf[x, y, z]; RegionPlot3D[ ImplicitRegion[ TEST[x, y, z] > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]] – PlatoManiac Jul 24 '15 at 13:45
  • Doesn't RegionPlot3D compile its function anyway? – Michael E2 Jul 24 '15 at 14:36
  • @MichaelE2 I'm not sure. I don't see anything about that on the documentation page, but you might be right – BenP1192 Jul 24 '15 at 14:40
  • A lot of computationally intense functions do automatically compile in appropriate circumstances. Check the timings RegionPlot3D[ImplicitRegion[-x y z > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]] // RepeatedTiming etc. I found this original plot was slightly faster than blockwave's compiled version. – Michael E2 Jul 24 '15 at 14:45

1 Answers1

2

This works without any error messages:

TEST = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, -x y z];

TESTfn[x_?NumericQ, y_?NumericQ, z_?NumericQ] := TEST[x, y, z];

RegionPlot3D[ImplicitRegion[TESTfn[x, y, z] > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]]

The second function, TESTfn, is necessary to ensure you only pass numeric (and not symbolic) arguments to the compiled function, see for example Using a compiled function inside NIntegrate gives "CompiledFunction::cfsa" message.

enter image description here

dr.blochwave
  • 8,768
  • 3
  • 42
  • 76