Normally, when you use Dynamic with a CPU-intensive expression, the expression only gets evaluated when a symbol in the expression changes. This doesn't seem to work with RegionNearest:

d = RegionIntersection[Ellipsoid[{2., 2.}, {5, 3}], 
   Ellipsoid[{5., 3.}, {4, 3}]];    
nf = RegionNearest[d];
pt = {0, 0};

Timing[nf[pt]] tells me that this takes .6s on my PC (side question: why does this take so long? I thought this would be equivalent to finding the nearest point to each of the two ellipsoids, then checking if that point is contained in the other.)

Now when I write:


The Mathematica kernel goes to 25% CPU utilization (one core) and stays there, even when nothing changes.

Is this a bug?

ADD: as @Nasser remarked in a comment, Dynamic[nf[pt]] doesn't make much sense on it's own. My actual code was:

  Graphics[Dynamic[Line[{pt, nf[pt]}]]],

But Dynamic[nf[pt]] is the simplest way to reproduce the problem.

  • $\begingroup$ I do not know why the cpu goes full speed, strange things happen when Dynamic are not used correctly. Your use of Dynamic is not right really. I am not sure what you are trying to do with Dynamic[nf[pt]]. One should use Dynamic around objects that gets displayed by front end, so that the FE updates them automatically without the kernel having to do anything, and not around the computation itself that goes to the kernel. May be you wanted to do Dynamic[r] then r=nf[pt] ? $\endgroup$ – Nasser Jul 12 '14 at 8:44
  • $\begingroup$ @Nasser: My original code had a LocatorPane to move pt around, containing a Graphics that showed a line from pt to the nearest point, and a RegionPlot of the intersection. I've simplified it to find the root problem, and only posted that. You can imagine a Locator moving pt around somewhere else in the notebook, but it doesn't really change anything. $\endgroup$ – Niki Estner Jul 12 '14 at 9:46
  • $\begingroup$ Related: link $\endgroup$ – eldo Jul 12 '14 at 10:30
  • $\begingroup$ @Nasser: It crashed my kernel a few times, too. But every time I restarted the kernel and executed the same code, it worked. So I have no code to reproduce that problem. $\endgroup$ – Niki Estner Jul 12 '14 at 10:36
  • $\begingroup$ me too. It crashed it few times. I thought may be it was just random dynamic thing. Was not sure.... something strange seems to be going on with this RegionIntersection function, I never used it before.... $\endgroup$ – Nasser Jul 12 '14 at 10:45

I'd say it's a bug worth reporting. In the meantime you can work around it using Refresh. Perhaps something like the following

 rnf : _RegionNearestFunction[___] /; ! TrueQ[inside] := 
  Block[{inside = True}, Refresh[rnf, None]]

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.