Syntax PDF
Syntax PDF
Syntax
Michael A. Morrison
(Version 3.4: February 2, 2000)
Contents
1 Basic Syntax: What Everyone Should Know from the Outset. (Beginners) 3
1.1 What are the three most common syntax errorsand how can I avoid them? . . . . . . . . . 3
1.2 What are the ve most important aspects of Mathematica syntax? . . . . . . . . . . . . . . . 3
1.3 I want a summary of the ways Mathematica uses parentheses, brackets, braces, etc. . . . . . . 3
1.4 What do I absolutely have to know about command names? . . . . . . . . . . . . . . . . . . . 4
1.5 How can I exploit Mathematicas naming convention to distinguish my commands from its
commands? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 Mathematica pretends not to know the name of a built-in command I just told it to execute.
Whats wrong? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
3
1 Basic Syntax: What Everyone Should Know from the Outset. (Beginners)
1.1 What are the three most common syntax errorsand how can I avoid them?
1. Multiplication!
The single most common mistake newcomers make is to assume that ab means a times b. It doesnt.
Its the name of the single variable, ab. If you want to write a times b, use the asterisk, as a*b.
Tip Mathematica allows you to indicate multiplication using either a space or an asterisk. Thus 2 Sin[x]
and 2*Sin[x] are identical. Youre on safer ground, though, with the asterisk.
2. Functions!
The second most common mistake is to try to tell Mathematica to evaluate sin x by typing Sin(x). This
wont work! Arguments to functions must be enclosed in square braces, not parentheses, as Sin[x].
3. Equations!
The third most common mistake is to try to write the equation a = b as a=b. Thats not an equation;
its an assignment: it tells Mathematica to expression a identical to the Mathematica expression b.
If you want to type the equation a = b, you must use the double equals notation, as a==b.
1.3 I want a summary of the ways Mathematica uses parentheses, brackets, braces, etc.
Ask and ye shall receive:
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
1.4 What do I absolutely have to know about command names? 4
1.5 How can I exploit Mathematicas naming convention to distinguish my commands from
its commands?
Begin all your variable names, functions denitions, and other expression with a lower case
letter, e.g., psi[x], not Psi[x] and plotWaveFunction[], not PlotWaveFunction.
1.6 Mathematica pretends not to know the name of a built-in command I just told it to
execute. Whats wrong?
If the kernel just returns your command unevaluated, you probably forgot to use capitals
where required in the command. Try using the Help Browser or command completion (in the
Input menu, via the submenu Complete Selection.
It is possible, however, that Mathematica may know your command but be unable to evaluate your
command. If you suspect this, check your command by inquiring via the ? query or poke around in the Help
Browser or try Command Completion
Tip If youre using version 3.0 or higher, use CNTL 6 on your keyboard to get a two-dimensional representation
x3 . Get in the habit of always using this two-dimensional form, as it makes your input and output easier to
understand and check.
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
2.4 I keep telling Mathematica a number is complex, but it persists in treating it as real. Whats wrong? 5
2.4 I keep telling Mathematica a number is complex, but it persists in treating it as real.
Whats wrong?
Probably youre using i to denote 1. In Mathematica, this number is denoted by a
capital letter, as I.
Thus the complex number a + ib is written a + I*b (note the asterisk to denote multiplication).
Warning We are so used to using = to signify equations that its very easy to fall back into this habit in Mathe-
matica. Watch out.
type
A = { {0,1},{1,0} }
3.2 The output from my equation solving commands looks useless. How do I extract the
actual answer from this output?
Commands that solve equations generate lists of replacement rules. You can extract the
solution from such lists by applying the replacement rule with the /. symbol. For instance, to
solve the equation x + 4 = 7, type
soln = Solve[x+4==7, x]
x /. soln
For more about replacement rules, see below.
3.3 When should I use the special Mathematica syntax to refer to the output from a command
I recently executed?
Never!
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
3.4 How do I enter text (e.g., le names, comments) in Mathematica? 6
Tip I urge you to avoid this bad habit. In most notebooks, youll delete cells and forget about them, so what
looks like the previous output may not actually be the one you wantespecially next time you execute the
notebook. Just name your output using an assignment statement, then you can always refer to it.
Okay. If you must, use %n where n is the number in the desired Out[n] generated by your command
In[n].
3.5 Mathematica wont load a package I know it has access to! Am I incorrectly using Needs?
Probably you just forgot that context names (e.g., the names of packages in Needs state-
ments) always must be terminated by a backquote and must be enclosed in quotation marks.
This backquote is part of the context name, as Global.
3.6 Mathematica isnt evaluating pieces of my expression in the right order. How can I
control the evaluation order?
Use parentheses to group commands.
Tip Use parentheses to control the sequence in which Mathematica executes commands in a multi-command
expression. For example, a (b + c) will generate the desired result ab+ac; if, however, you type a b+c, then
Mathematica will calculate ab + c. If youre unsure whether Mathematica will execute individual operations
in your expression in the order you desire, use parentheses to force it to do so. Your parentheses always
override Mathematicas built-in rules, and extraneous parentheses do no harm.
Warning Dont use the hierarchy of grouping symbols standard in mathematics: { [ ( ) ] }. In Mathematica,
square brackets denote arguments and curly brackets denote iterators; only parentheses denote grouping.
type
Clear[potential]
potential[r_ /; r <= a && r >= 0] := -V0
potential[r_ /; r > a] := 0
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
4.2 Whats the dierence between immediate and delayed assignments in function denitions? 7
4.2 Whats the dierence between immediate and delayed assignments in function deni-
tions?
To dene a function and assign a value to a variable, you can use use either the notation
for an immediate assignment, =, or for a delayed assignments, :=. Almost always, you can use
delayed assignment, :=, and just not worry about it.
Heres a brief introduction to the distinction between the two:
immediate assignment: Use = if you want Mathematica to evaluate the right-hand side of the assignment
when you type it in.
delayed assignment: Use := if you do not want Mathematica to evaluate the right-hand side until you
use it.
Warning Remember that in Mathematica, = does not mean the same thing it means in mathematics. In Math-
ematica, a=b+1 means add 1 to the currently stored value of the variable [b], then assign the result to a
variable named a.
Thus in Mathematica we can write assignment expressions that would be senseless in regular mathematics,
such as
b = b + 1
4.5 I told Mathematica to implement a replacement for an expression that occurs several
times but it only made the replacement once! What do I do?
To force Mathematica to invoke the replacement over and over until it can no longer change
the expression, use the notation for repeated replacement, //..
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000
5.2 Whats a handy, powerful shorthand way to dene functions? 8
Tip Most built-in Mathematica functions act on lists element-by-element. That is, Mathematica typically
maps functions across lists. Such functions have the Attribute Listable. If you want to give this attribute
to your own function f, type SetAttributes[f,Listable].
5.3 Can I change the head of an expression without re-dening the expression?
Yep. Thats what Apply is for.
To change the head of an expression, either give the expression to Apply or use the apply symbol
@@. Thus Plus @@ {a,b,c} returns a+b+c, because we changed the head of {a,b,c} from List to Plus.
5.6 I am setting up a sequence of commands. Whats the cleanest, clearest, most straight-
forward way to do this?
Use piping. This is a syntax mode in which the output from one command is piped into
the next command in the sequence. The double backslash symbol // feeds the output from
the command that precedes it into the one that follows it. Thus each command appears in
your notebook in the desired execution sequence.
Tip Getting used to piping takes a little practice, and usually requires the use of anonymous functions. But
its worth it! The increase in clarity of your commands will make it much easier to nd and x errors and
to understand your work months (or years) later when you next need it. Piping is for everybodynot just
advanced users!
To illustrate the improved clarity, consider the following equivalent formulations:
Simplify[ComplexExpand[TrigToComplex[Conjugate[psi[x]]*psi[x]]]]
Version 3.4 c
2000 by Michael A. Morrison February 2, 2000