Is $2x$ documented?
From Your First Wolfram Language Calculations:
2x
means2*x
.
Examples of no-space multiplication from Times
:
0x
evaluates to0
, and0.0x
evaluates to0.0
.
...
Times threads element-wise over lists:In[1]:= 2{x,y,z} Out[1]= {2 x,2 y,2 z} In[2]:= {{a,b},{c,d}}{x,y} Out[2]= {{a x,b x},{c y,d y}}
Update:
As Sjoerd notes, according to Symbol
, a symbol name cannot begin with a digit. This implies 2x
would either be a syntax error or not; if not, then what? The standard mathematical interpretation is an obvious choice. J.M. notes further that it would be confusing to have symbol names begin with a number. I'm not sure this all captures the argument in the most natural way. I think humans do a pretty good job getting used to whatever the rules are, and identifiers starting with a number wouldn't be that bad. I think the starting point for the argument is that it was desired from the beginning to interpret 2x
as Times[2,x]
, which implies you cannot have symbols start with a number (which nobody wants anyway, if I may alter J.M.'s remark a little). But now we're wading into the opinion-based territory for which this Q&A was originally closed. It's also clear that (almost) everyone wants xy
to be interpreted as Times[x,y]
, but that poses lexical analysis problems that couldn't be solved satisfactorily.
It is not a bug.
See for example the documentation of Mathematica 2 chapter 1.1.6 :
...It also allows multiplication to be indicated without an explicit * or other character. As a result, Mathematica can handle expressions like 2x and a x or a (1+x) , treating them just as in standard mathematical notation
This is also written in the other Mathematica books (The last one is for Mathematica 5.2)
No, you are right; there "does not seem to be a document page that tells us that "2x is valid"" but there should be (if not a page, a line of clarification). If it is good enough for the documentation to confirm the mathematically obvious 0x = 0 then one might reasonably expect that it also be good enough to state:
cx
parses toTimes[c, x]
whenc
appears as an atomic, real number (but not as basic constants like\[Pi]
and E).
With such an insertion it becomes clear, for example, why (possibly against initial expectations) each member of
does not evaluate to 0
unlike the expression
The last example illustrates how "mathematical obviousness" is not always the clear, disambiguating arbiter that might be initially presumed. This is further reinforced by the following elements not evaluating to 0
in apparent disregard of multiplicative commutativity (despite Times
being Orderless
) and BIMDAS' precedence rules (despite Times
's parsing generally conforming to it):
Of course, the above exemplifies the extra challenges faced by a computational system compared with traditional mathematical notation but it is also an argument for the importance of documentation clarity. While the particular case 2x
does appear elsewhere in the documentation, it is not foreshadowed in the initial specifications (as redundant as this may appear to experienced users) nor does its particular appearance necessarily infer the more general case.
The instructiveness of the OP's question seems to be less about the number 2
, a little more about how numbers/letters are parsed to the (fundamental) Times
operation but most of all it seems to be about users' relationship to the Help Documentation. The OP's question is probing to what extent the documentation should be taken as a formal description of the WL. The remarkable aspect of WL's documentation is how closely it does come to describing a formal grammar while retaining a natural expressiveness and consistency. Part of this involves completeness together with pedagogically progressing from the general to the particular and a general awareness of this constancy, IMO, invariably holds new users in good stead.