Message Boards Message Boards

1
|
3050 Views
|
0 Replies
|
1 Total Likes
View groups...
Share
Share this post:

The Infix Format

Posted 11 years ago

The Infix Format

In my search for examples on how to use Mathematica I have come across those which use infix notation. As we know, besides the infix format, Mathematica also supports prefix and postfix formats. The latter are probably the most commonly used formats but there are cases when the infix format provides a notational advantage by making shorter or more intuitive expressions. For example, {a, b} ~ Join ~ {c, d, e} gives a somewhat more intuitive idea of what’s taking place than, Join[{a, b},{c, d, e}].

Unfortunately, Mathematica’s help on infix is sketchy and a little vague. It starts out as follows: “Infix[f[a,b,c]] prints with f[e1,e2,…] given in default infix form: e1 ~ f ~ e2 ~ f ~ e3 ~ …”

Now, to my naïve reading, this gives the impression that e1 ~ f ~ e2 ~ f ~ e3 ~ … is an equivalent notational format for f[e1,e2,…]. It isn't immediately clear that this is the case but I hope to show it is. In doing so I beg the indulgence of the more sophisticated readers of math and computer science to whom all of this is obvious and elementary.

Again, in my naïve understanding, I said to myself, “well, if the two forms of notation are equivalent, then when I type a ~ f ~ b ~ f ~ c into an input cell Mathematica should return f[a,b,c].” Instead, it returns the initially baffling expression, f[f[a,b],c]. In fact, it is easy to come up with examples for f where the two forms f[a,b,c] and f[f[a,b],c] are quite different. For example, if f is replaced by List we get {a,b,c} for the former expression and {{a,b},c} for the latter, which are different. Another example is to replace f by Subtract. This is slightly different from the example using List as Mathematica does not allow the expression Subtract[a,b,c]. However the expression a ~ Subtract ~ b ~ Subtract ~ c returns a – b – c which is what one would get from the expression Subtract[Subtract[a,b],c]. This gave me the final hint as to what’s going on. What we are dealing with is the issue of associativity. The expression a ~ f ~ b ~ f ~ c will yield f[a,b,c] only if f is associative. Thus if f is replaced by an associative operation such as Plus or Times, things work as one would expected. We have for example for the former, a + b + c = (a + b) + c i.e., Plus[a,b,c]=Plus[Plus[a,b],c]. Or, more generally, f[a,b,c] = f[f[a,b],c].

If f is not associative then Mathematica, seems to follow the usual convention of parsing the expression from left to right. By means of this it will encounter the first infix and translate it as f[a,b] which then becomes the first member of the second infix to give f[f[a,b],c].

I subsequently found that Mathematica allows one to assign associativity to a symbol, f, by means of the expression SetAttributes[f, Flat]. For a symbol, f, with the Flat attribute we have f[a,b,c] = f[f[a,b],c].

As a postscript, I have two final comments. First, Mathematica’s help on Infix doesn’t make it clear that the expression produced as output from Infix[f[a,b,c]] is not an editable expression. (You can see this by clicking the output cell and typing ctrl-E.) Nor, if you copy it and paste it in a new input cell, will it evaluate to either f[f[a,b],c] or f[a,b,c] but remains unchanged. I suppose this is because Mathematica does not assume that we “really” want to convert the expression to, say, prefix format any more than we would want f[a,b,c] changed to infix format. But this still doesn’t explain why the output from Infix[f[a,b,c]] would not be editable. Question: what is the rationale for this?

Second, the expression e1 ~ f ~ e2 ~ f ~ e3 ~… is equivalent to Fold[f,e1,{e2,e3,…}] which gives, I think, a nice alternative way to visualize the folding operation for we can clearly imagine the expression being folded at the symbol f just like a map.

Attached is a Mathematica notebook illustrating some of the above.

Attachments:
POSTED BY: Douglas Skinner
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