Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 8
    $\begingroup$ Great analysis!! I'm a bit disappointed that MemberQ unpacks. There is a clear room for optimization here. Merely knowing the type of a packed array should be enough for the system to conclude that the array is free of symbols. Such optimizations are used with some other pattern matching functions. $\endgroup$ Commented Mar 14, 2012 at 5:21
  • $\begingroup$ Getting rid of that check brings the evaluation time down to 3.0 seconds here. $\endgroup$ Commented Mar 14, 2012 at 5:49
  • $\begingroup$ I updated the question with a potential fix to the problem. $\endgroup$ Commented Mar 14, 2012 at 19:55
  • $\begingroup$ +1. One question: why the definition mq:MemberQ[args___]/;(Print@HoldForm[mq];True):=mq does not lead to circularity while the definition MemberQ[args___] /; (Print@HoldForm[mq]; True) := MemberQ[args] does? $\endgroup$ Commented Mar 26, 2012 at 2:30
  • 1
    $\begingroup$ @AlexeyPopkov because in the first case the evaluation of this definition is finished after the first term rewriting iteration. It's the same as for any expression that doesn't change under evaluation: symbols without definitions don't trigger infinite recursion. In the second case, if we replace the matched pattern with something new, evaluation is not known to be finished until that has also been evaluated, which gives rise to problems. The internal details of this are likely related to how Mathematica implements tail call optimization. $\endgroup$ Commented Mar 26, 2012 at 2:41