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.
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 doDynamic[r]thenr=nf[pt]? – Nasser Jul 12 '14 at 08:44LocatorPaneto moveptaround, containing aGraphicsthat showed a line fromptto the nearest point, and aRegionPlotof the intersection. I've simplified it to find the root problem, and only posted that. You can imagine aLocatormovingptaround somewhere else in the notebook, but it doesn't really change anything. – Niki Estner Jul 12 '14 at 09:46Refresh– Rojo Jul 13 '14 at 00:40Dynamic, reported that too – Niki Estner Jul 13 '14 at 08:24