Tuesday, March 21, 2006

NKS and the world of (not so) simple programs

I started getting into the details of NKS and going through the Mathematica code related to NKS. One of the titles of a chapter in NKS is something like, 'The world of simple programs'. Well, conceptually, these programs are simple, and the implementation in Mathmatica is only a few lines long, but understanding the code is really pretty difficult unless you are experienced with Mathematica.

I mean, look at any Java, C++, C# code, you can follow it pretty well without knowing the language. Look at any well commented code, and you can follow it completely.

Here is the code that generates the famous 'Rule 30' pattern.

Clear[CAStep]
CenterList[n_Integer]:=ReplacePart[Table[0,{n}],1,Ceiling[n/2]]
ElementaryRule[num_Integer]:=IntegerDigits[num,2,8]
CAStep[rule_List,a_List]:=rule
[[8-(RotateLeft[a]+2 (a+2 RotateRight[a]))]]
CAEvolveList[rule_,init_List,t_Integer]:=NestList[CAStep[rule,#]&,init,t]
CAGraphics[history_List]:=Graphics[Raster[1-Reverse[history]],AspectRatio
®Automatic]

------------------------------------------------------------------------------

Show[CAGraphics[CAEvolveList[ElementaryRule[30],CenterList[103],50]]]


Its pretty compact, eh? Just looking at it, not knowing anything about Mathematica, it's obviously a bunch of nested functions. Nothing difficult about that. However, there are a few lines that a just gibberish. For example, the CAStep function line.

CAStep[rule_List,a_List]:=rule[[8-(RotateLeft[a]+2 (a+2 RotateRight[a]))]]

Just looking at it, you might say 'HUH'? Well it turns out, this line translates an array of 0s and 1s through the rule used with the numbering system defined outside the program. The numbering system in base two corresponds to if you should use a 1 or 0 in response to a pattern. This is really creative and is absolutely critical to the performance of the program. And, there is no explanation of this, no comments about how this works, just an output of what happens. The relation of the rule number to what pattern is being accessed to what the output should be in that case, its all in one little step, or CAStep, if you prefer, and completely without comments.

Note the algebraic expression (with RotateLeft and such) will translate an array of three members of a base two number into base 10. The double brackets are shorthand for the part function which takes an array and replaces part of that array with the specified part.

The CAEvolve function is no better with the # and & signs included in the function. Once you understand it, you might wonder why the brevity of 3-4 more characters was valued over clarity. I won't go into that.

This program is trivial if you are one of the smartest people in the world and spent 5 years developing an environment so you could create programs like this. It would be easy for everyone if there were comments. Ah, Steven Wolfram! Why make it difficult at all to understand this code? The code isn't important - the results are!!

1 Comments:

At 7:03 AM , Blogger Justin Lyon said...

Mickslam,

Thanks for the kind words on my blog Simulation Science. I looked for your email address, but was unable to find it on your blog Critical Path. Can you send it to me at justin1028@yahoo.com so that we can correspond via email?

Best,
Justin

 

Post a Comment

<< Home