2
$\begingroup$

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*)
$\endgroup$
  • $\begingroup$ Have you tried RegionPlot3D[ ImplicitRegion[TEST[x,y,z] > 0, {{x, -5, 5}, {y, -5, 5}, {z, -5, 5}}]]? $\endgroup$ – dr.blochwave Jul 24 '15 at 13:44
  • $\begingroup$ 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}}]] $\endgroup$ – PlatoManiac Jul 24 '15 at 13:45
  • $\begingroup$ Doesn't RegionPlot3D compile its function anyway? $\endgroup$ – Michael E2 Jul 24 '15 at 14:36
  • $\begingroup$ @MichaelE2 I'm not sure. I don't see anything about that on the documentation page, but you might be right $\endgroup$ – BenP1192 Jul 24 '15 at 14:40
  • $\begingroup$ 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. $\endgroup$ – Michael E2 Jul 24 '15 at 14:45
2
$\begingroup$

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

$\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.