Dynamic with RegionNearest keeps one CPU core at 100% - Is this a bug?

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:

Dynamic[nf[pt]]


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:

LocatorPane[Dynamic[pt],
Show[
Graphics[Dynamic[Line[{pt, nf[pt]}]]],
RegionPlot[d]]]


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

• 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] ? – Nasser Jul 12 '14 at 8:44
• @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. – Niki Estner Jul 12 '14 at 9:46
• Related: link – eldo Jul 12 '14 at 10:30
• @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. – Niki Estner Jul 12 '14 at 10:36
• 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.... – Nasser Jul 12 '14 at 10:45

1 Answer

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

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