3

If you evaluate the following code twice, results will be different. Can anyone explain what's going on?

findHull[points_] := Module[{},
   Needs["ComputationalGeometry`"];
   ConvexHull[points]
   ];
findHull[RandomReal[1, {10, 2}]];
Remove["Global`ConvexHull"];
findHull[RandomReal[1, {10, 2}]]

2 Answers 2

6

The problem is that even though the module is not evaluated untill you call findHull, the symbols are resolved when you define findHull (i.e.: The new downvalue for findHull is stored in terms of symbols, not text). This means that during the first round, ConvexHull resolves to Global`ConvexHull because the Needs is not evaluated. During the second round, ComputationalGeometry is on $ContextPath and so ConvexHull resolves as you intended.

If you really cannot bear to load ComputationalGeometry beforehand, just refer to ConvexHull by its full name: ComputationalGeometry`ConvexHull. See also this related answer.

HTH

3
  • Thanks, makes sense. Another context gotcha I found is that functions defined inside functions inside package MyPackage are put into Private' rather than MyPackage'Private' Feb 14, 2011 at 5:30
  • @Yaroslav. Can't reproduce that one -- did you remember to use Begin["`Private`"] instead of Begin["Private`"] (because if you didn't, that would lead to exactly the behaviour you describe)?
    – Janus
    Feb 14, 2011 at 5:57
  • I'll try again with the backtick quoting :) You should be using Begin["`Private`"].
    – Janus
    Feb 14, 2011 at 6:04
2

Not a direct answer to the question, but a bit too large for a comment. As another alternative, a general way to delay the symbol parsing until run-time is to use Symbol["your-symbol-name"]. In your case, you can replace ConvexHull on the r.h.s. of your definition by Symbol["ConvexHull"]:

findHull[points_] := 
 Module[{}, 
    Needs["ComputationalGeometry`"];
    Symbol["ConvexHull"][points]];

This solution is not very elegant though, since Symbol["ConvexHull"] will be executed every time afresh. This can also be somewhat error-prone, if you do non-trivial manipulations with $ContextPath. Here is a modified version, combined with a generally useful trick with self-redefinition, that I use in similar cases:

Clear[findHull];
findHull[points_] :=
Module[{},
  Needs["ComputationalGeometry`"];
  With[{ch = Symbol["ConvexHull"]},
    findHull[pts_] := ch[pts];
    findHull[points]]];

For example,

findHull[RandomReal[1, {10, 2}]]

{4, 10, 9, 1, 6, 2, 5}

What happens is that the first time the function is called, the original definition with Module gets replaced by the inner one, and that happens already after the needed package is loaded and its context placed on the $ContextPath. Here we exploit the fact that Mathematica replaces an old definition with a new one if it can determine that the patterns are the same - as it can in such cases.

Other instances when self-redefinition trick is useful are cases when, for example, a function call results in some expensive computation, which we want to cache, but we are not sure whether or not the function will be called at all. Then, such construct allows to cache the computed (say, symbolically) result automatically upon the first call to the function.

3
  • Nice trick! And some extra words for the sake of character counting :)
    – Janus
    Feb 15, 2011 at 3:10
  • @Janus That was spot on, verbosity is one of my sins :) Feb 15, 2011 at 18:51
  • Apologies for cryptic comment: the "extra words" were put in because "Nice trick!" is not long enough to qualify as an SO comment -- I think your answer is at just the right level of verbosity :)
    – Janus
    Feb 17, 2011 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.