MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Algorithm Analysis Course: Should I use Mathematica for projects?

  • To: mathgroup at
  • Subject: [mg127299] Re: Algorithm Analysis Course: Should I use Mathematica for projects?
  • From: Andrzej Kozlowski <akozlowski at>
  • Date: Sun, 15 Jul 2012 04:25:26 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • References: <jtkuhh$2a$> <> <jtoglu$qk8$> <> <> <>

Time difference and Daniel's comments have made most of what I would have said obsolete so here is only what remains.

On 14 Jul 2012, at 07:28, Richard Fateman wrote:
>> It is indeed true (and, in fact, pretty obvious) that when Mathematica evaluates M[[i]] (i.e. Part[M,i]) it must evaluate M, since the attributes of Part are:
>> Attributes[Part]
>> {NHoldRest,Protected,ReadProtected}
>> in other words, they don't include HoldFirst or HoldAll. We can see it simply here:
>> l = Range[10];
>> Trace[l[[2]]]
>> {{l,{1,2,3,4,5,6,7,8,9,10}},{1,2,3,4,5,6,7,8,9,10}[[2]],2}
>> So l was indeed evaluated, in some sense anyway.
> It was completely evaluated, not "in some sense".

Well, it seems to me that I am the one who was careful and you are contradicting yourself. Note that later in the same post you say:

> this is evidence that the Mathematica system has determined that the
> value of  l1
> depends on nothing that has changed, e.g. not the definition of any
> functions or the values of any
> variables) and therefore it need not be evaluated (again).

But just a little earlier you claim that the list is "completely evaluated". How are these two statements compatible?

I pointed out that Mathematica's "official" semantics seems to imply that when you evaluate Part[list,n]  list will be evaluated. Trace seems to confirm this. However, evaluating Part of a very long list shows no difference compared with evaluating Part of a very short list, suggesting constant time retrieval. This is exactly why I wrote "in some sense" - I was not sure exactly how this high performance is achieved.  Anyway, this seems to me at least more accurate than saying that the list is both "completely evaluated" and "not evaluated at all".

Another thing, all your examples of length dependent retrieval involve changing the list in w way that could alter it's length. In such cases Mathematica obviously has to re-evaluate the list (in the ordinary sense). In particular consider this:

B = Table[i, {i, 0, 1000}];
B[[5]] = j; Timing[Do[B[[50]]++, {i, 0, 100000}]]

Out[39]= {0.911279,Null}

B = Table[i, {i, 0, 1000}];
B[[5]] = 3; Timing[Do[B[[50]]++, {i, 0, 100000}]]


Of course using a symbol here was crucial in creating this effect. But this proves nothing in relation to "ordinary programming languages" since most of them would no allow you to do this sort of thing at all.

Finally, I would like to mention that I have taken no position at all on the question in the subject of this thread. Clearly using a tool which was designed for a purpose other than the one you have in mind is rarely the optimal approach unless there are some benefits that compensate for the almost inevitable deficiencies. Mathematica was not designed primarily as an all purpose programming language so it is not the best tool for the study of the workings of such languages. In particular, it uses various techniques, some of which are not fully documented to speed up certain frequently used computations, sometimes at the expense of others that are less common (for most Mathematica users). On the other hand compared with standard programming languages it has many "compensating features" that have already been mentioned a number of times.
The balance will depend on such things as the intended audience for the course, the knowledge of the instructor etc.

Perhaps on a somewhat related note, at the university where I am now working, computer science is in the same department as mathematics. Our course on Mathematica is taken by both mathematics and computer science students. The latter already know many "ordinary" programming languages but most of them seem to show a lot of interest in the inner workings of Mathematica. So while Mathematica may not be always the right tool for teaching computer science  it is certainly something of interest to many computer scientists.

Andrzej Kozlowski

  • Prev by Date: Using Mathematica to Plot Business Business Data By Zip Code
  • Next by Date: Re: Algorithm Analysis Course: Should I use Mathematica for projects?
  • Previous by thread: Re: Algorithm Analysis Course: Should I use Mathematica for projects?
  • Next by thread: Re: Algorithm Analysis Course: Should I use Mathematica for projects?