Message Boards Message Boards

0
|
6336 Views
|
10 Replies
|
4 Total Likes
View groups...
Share
Share this post:

"Fast Introduction for Programmers" - A Printable Copy?!

Posted 8 years ago

Does anyone have, or can there be created, a downloadable PDF or purchasable printed format of the on-line-only tutorial at "http://www.wolfram.com/language/fast-introduction-for-programmers/"? I find it very difficult to learn technical information by paging back and forth through HTML on screen.

As a long-time user, but by no means an expert, of Mathematica, I bought and read through Stephen Wofram's new book, "An Elementary Introduction to the Wolfram Language," for a refresher. Although I did find it enlightening, I was ultimately frustrated. Several times it touts the internal consistency of the language, but nowhere are the underlying structural principles clearly spelled out. Apparently similar commands appear to take their arguments or give results in different formats. There appears to be no clear description of a standard form for arguments, nor even a complete definition of an "iterator."

A few unexplained examples: p.23 about Table[x,20] -- "In earlier versions, this had to be specified as Table[x,{20}]." But curly brackets are still required to control a list of values, so why the change? Just to be consistent with Range, or is the a "range" of commands that behave similarly? p.142 and elsewhere -- The word, "apply," is used to refer to several commands other than Apply. Here, for example, it is used to describe Map. Is there a pedagogical reason for this apparently loose terminology? p.188 about Position -- Why do some functions return double curly brackets, as in Out[15], while others, like Part, do not? (It has always annoyed me that one must use Flatten to find the parts of an expression that are picked out by Position.) p.189 about ReplaceParts -- To replace a single part, no curly brackets are required; but to replace more than one, they are. Why? p.284 about Fold -- In In[13], why do we get to drop the initial value (0) in this case? What's the purpose of this apparently special construction?

I am hopeful that the "Fast Introduction for Programmers" will give a more systematic description of the structure of the language if I can get my hands on a copy. Thanks in advance for any help. -- John Willett

POSTED BY: John Willett
10 Replies

Hi John,

You can get the printed version of An Elementary Introduction to the Wolfram Language which includes most of the topics discussed in "Fast Introduction for Programmers" plus many other examples with a systematic description of the structure of the language.

What CAN be puzzling (in my opinion) is the following example:

data = {};
data[[All, 2]] = 3;
Select[data, #[[2]] == 3 &];
MapAt[f, data, {All, 2}]
BinCounts[data, {0, 5, 1}]
BinCounts[data, {0, 5, 1},{1,5,2}]

So why is line 2 and 3 perfectly working, but line 4 won't? And why is it happily binning an empty list assuming it is a list of 1D elements. But not OK if it is an empty list of '2D elements'?

POSTED BY: Sander Huisman

Sander -- OT, but while you're explaining notation, can you tell me how this works:

ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 [Pi], .5}], PlotLegends -> {"Sin[x]", "Cos[x]"}]

Obviously the "f"'s are necessary here to get the Abscissa for the ListPlot, but I see nothing in the Help for either Table or ListPlot that explains what's going on. (If I had had to do this, I would have used "ListPlot[{Table[Sin[x], {x, 0, 2 [Pi], .5}], Table[Cos[x], {x, 0, 2 [Pi], .5}]}, PlotLegends -> {"Sin[x]", "Cos[x]"}]", which is clearly more cumbersome but also more transparent, to me at least.) -- John Willett

POSTED BY: John Willett

Let's look at the core:

Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 Pi, .5}]

this gets evaluated as (help of Table under 'details'):

Table[Table[f, {x, 0, 2 Pi, .5}],{f, {Sin[x], Cos[x]}}]

So first the first 'f' is selected (in the outer loop), which is Sin[x] and for that value of f the inner loop is evaluated, then after that the last 'f' is selected. So it transforms into:

{Table[Sin[x], {x, 0, 2 Pi, .5}],Table[Cos[x], {x, 0, 2 Pi, .5}]}

Which created two datasets which ListPlot understands.

POSTED BY: Sander Huisman

Table[Table[f, {x, 0, 2 Pi, .5}],{f, {Sin[x], Cos[x]}}]<<

OK, Sander, I think I see what you're saying. Two questions:

1) Are there other places, perhaps many, where this kind of structure may be used? Has it become part of the standard 'iterator' form?

2) Is there any way to play around with the pieces of this structure to "force" my intuition about it? For example, can one get a look at the values of f or explicitly convert the structure into the transformations you are indicating?

Best Regards -- John Willett

POSTED BY: John Willett
  1. yes this is part of the standard iterator form. It can be used in Sum, Do, Table, Integrate et cetera (Range is the exception perhaps, as a n-Dimensional range is weird to imagine!)

  2. You could run:

    Table[f, {f, {Sin[x], Cos[x]}}]

though that is exactly the iterator specification:

Table[f, {f,2,12,3}]

The transformation is done automatically as per the details in the help:

Do[expr,Subscript[spec, 1],Subscript[spec, 2]] is effectively equivalent to Do[Do[expr,Subscript[spec, 2]],Subscript[spec, 1]].

notice that is says effectively equivalent. This probably indicates that while the end result is exactly the same. The single 'Do' as opposed to the nested 'Do' is probably more efficient and has optimizations. Though I can only speculate, but seems very likely. There is no function that does this conversion, this is done internally, automatically. The evaluation structure is different for both specifications:

Table[Table[f, {x, 0, 2 Pi, .5}], {f, {Sin[x], Cos[x]}}] // Trace
Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 Pi, .5}] // Trace
POSTED BY: Sander Huisman

Thanks, Sander! -- I think I've finally got it. Stupid of me: Looking at "{f, {Sin[x], Cos[x]}}", I didn't realize that this was the first of two iterators -- fooled by the fact that f was given a list of functions rather than the usual "ladder" of values.

"firstIterator=Table[f, {f, {Sin[x], Cos[x]}}]" does indeed produce "{Sin[x], Cos[x]}", which is then acted on by the second iterator, "{x, 2, 12, 3}", to produce the desired input for ListPlot. The trick to remember is that the first iterator produces the outermost list, giving a list of Sin followed by a list of Cos. I guess you could apply the second iterator to the above result as as "Table[#, {x, 0, 2 Pi, .5}] & /@ firstIterator" to get the correct result -- just my backward way of thinking...

Have I finally got it right? -- John Willett

POSTED BY: John Willett

I think that that is equivalent indeed. Generally there are multiple ways of doing things...

POSTED BY: Sander Huisman

I don't think there is an 'offline' version available, maybe in the future!

For the standard iterator notation is meant:

{iterator,min,max,delta}

{iterator,min,max} goes automatically to {iterator,min,max,1}

{iterator,n} start automatically from 1 (indexing is base 1 in Mathematica, so that makes sense).

{n} could not possibly mean {iterator} because then there aren't any bounds. so it must be a number of times. Later on they added the 'shortcut' so you can just write n, just to make it easier for starters I guess.

This notation works in Sum, Product, Integrate, Table, Do, et cetera...of course iterator in integrate has a slightly different meaning...

I'm not sure what you mean with "But curly brackets are still required to control a list of values".

The reason why position gives double curly braces back is quite easy: This needs to be like this because Position can also work on deeper 'levels', so the only solution is to do it the way they do it now; otherwise you can't always tell if they mean several parts or a single part multiple levels deep. It also designed to work along with Extract, rather than Part. It says this in the documentation:

Position returns a list of positions in a form suitable for use in Extract, ReplacePart, and MapAt. The form is different from the one used in Part.

Regarding ReplacePart, the second argument can be given as {3->x}, but 3->x as well, again this is just a shortcut, The difference here with Position is that there is no ambiguity, you can't interpret either of them in a 'wrong' way.

I'm not sure what you mean with your Fold example, but probably also logically explainable...

POSTED BY: Sander Huisman

Sander -- Thanks for the helpful answers. I probably should have left my "examples" out -- just asked for the printable copy -- but it does seem to me that some unifying structural overview is missing from the book...

Tech Support suggested I post my request here, perhaps because some of the Wolfram people would read it? Best Regards. -- John Willett

POSTED BY: John Willett
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract