Mathematica programming: an
advanced introduction
Leonid Shifrin
Part I: The core language
Version 1.01
2
3
To my parents
4
Contents
Preface.......................................................................................18
I. Introduction..............................................................................26
1.1 First principle: everything is an expression................................................................26
1.1.1 Atoms and the built-in AtomQ predicate......................................................................26
1.1.2 Mathematica normal (composite) expressions............................................................26
1.1.3 Literal equivalents of built-in functions, and FullForm command................................26
1.1.4. All normal expressions are trees - TreeForm command............................................ .27
1.1.5. Heads of expressions and the Head command...........................................................27
1.1.6 Accessing individual parts of expressions through indexing ......................................28
1.1.7 Levels of expressions and the Level command...........................................................28
1.2 Second principle: pattern-matching and rule substitution...........................................30
1.2.1 Rewrite Rules..............................................................................................................30
1.2.2 An example of a simple pattern-defined function........................................................30
1.2.3 Functions are really rules : DownValues command....................................................31
1.2.4 Example of a function based on a restricted pattern...................................................31
1.2.5 A bit about evaluation..................................................................................................31
1.2.6 Patterns allow for multiple definitions of the same function.........................................31
1.2.7 Non - commutativity of rules substitution.....................................................................32
1.2.8 Automatic rule reordering.............................................................................................32
1.3 Third principle: expression evaluation........................................................................33
Summary...............................................................................................................................33
II. Elementary operations..............................................................34
2.1 Introduction..................................................................................................................34
2.2 Symbols and variables............................................................................................. ..34
2.2.1 Legitimate symbol names............................................................................................. 34
2.2.2 Getting information about symbols
5
2.2.2 Getting information about symbols.................................................................................35
2.2.3 "Proper" variables and OwnValues.................................................................................36
2.2.4 Indexed variables and DownValues................................................................................36
2.2.5 Composite variables and SubValues..............................................................................37
2.2.6 Clearing variables...........................................................................................................37
2.2.7 Does a variable have a value? ValueQ.......................................................................39
2.2.8 Assignments attach rules to Symbol-s...........................................................................39
2.2.9 Summary........................................................................................................................40
2.3 Dynamic data typing.......................................................................................................40
2.4 Assignments...................................................................................................................41
2.4.1 Immediate and delayed assignments: Set and SetDelayed...........................................41
2.4.2 The difference between Set and SetDelayed : an example...........................................41
2.4.3 Chain assignments.........................................................................................................42
2.4.4 Don’ t use SetDelayed with more than two arguments...................................................43
2.4.5 Set and SetDelayed : when which one is used...............................................................44
2.5 Equality checks............................................................................................................ ..44
2.5.1 Equal...............................................................................................................................44
2.5.2 Beware: Equal may return "unevaluated".......................................................................44
2.5.3 Equal is used by built-in solvers to build equations..........................................................45
2.5.4 Assigning the result of Equal to a variable, and more on evaluation..............................45
2.5.5 The role of side effects...................................................................................................46
2.5.6 A digression: standard and non-standard evaluation........................................................46
2.5.7 Pass by reference semantics - possible to imitate..........................................................47
2.5.8 SameQ............................................................................................................................47
2.5.9 TrueQ..............................................................................................................................48
2.6 Logical operators..........................................................................................................49
2.7 Conditionals...................................................................................................................50
2.7.1 The If operator................................................................................................................50
2.7.2 If may return "unevaluated"............................................................................................50
2.7.3 If returns a value
6
2.7.3 If returns a value..............................................................................................................50
2.7.4 Operators Which and Switch...........................................................................................51
2.8 Loops.............................................................................................................................52
2.8.1 For loop...........................................................................................................................52
2.8.2 While loop.......................................................................................................................52
2.8.3 Do loop............................................................................................................................52
2.8.4 Side effects induced by loops.........................................................................................53
2.8.5 Blocks of operators - the CompoundExpression command............................................53
2.8.6 Local goto statements: Break, Continue, Return............................................................53
2.8.7 Programs using loops are often inefficient in Mathematica............................................54
2.9 Four types of brackets in Mathematica.......................................................................55
2.9.1 Parentheses ()................................................................................................................55
2.9.2 Curly braces {}................................................................................................................55
2.9.3 Single square brackets []............................................................................................... 55
2.9.4 Double square brackets [[ ]]............................................................................................56
Summary................................................................................................................................ .57
III. Lists......................................................................................... .58
3.1 Introduction.................................................................................................58
3.2 The main rule of thumb when working with lists in Mathematica.................58
3.3 The content of lists......................................................................................58
3.4 Generation of lists.......................................................................................58
3.4.1 Generating a list by hand................................................................................................58
3.4.2 Generation of lists of equidistant numbers by the Range command..............................59
3.4.3 Generation of lists with the Table command...................................................................59
3.4.4 A comment on universality of Range..............................................................................60
3.4.5 Generation of lists inside loops.......................................................................................62
3.5 Internal (full) form of lists.............................................................................64
3.6 Working with lists and their parts
7
3.6 Working with lists and their parts................................................................64
3.6.1 List indexing and element extraction with the Part command.........................................64
3.6.2 Extract.............................................................................................................................66
3.6.3 Take and Drop................................................................................................................66
3.6.4 First, Rest, Last and Most...............................................................................................67
3.6.5 Length.............................................................................................................................67
3.6.6 Modification of list elements by direct indexing (using Part)...........................................68
3.6.7 ReplacePart....................................................................................................................69
3.6.8 Position...........................................................................................................................70
3.7 Adding elements to the list and removing them from the list.....................75
3.7.1 Append, Prepend, AppendTo and PrependTo................................................................75
3.7.2 Insert and Delete.............................................................................................................76
3.8 Working with nested lists.........................................................................77
3.8.1 Partition...........................................................................................................................77
3.8.2 Transpose.......................................................................................................................81
3.8.3 Flatten ...........................................................................................................................82
3.9 Working with several lists.........................................................................84
3.9.1 The Join command.........................................................................................................85
3.9.2 The Intersection command.............................................................................................85
3.9.3 The Complement command...........................................................................................85
3.10 Functions related to list sorting ..............................................................86
3.10.1 The Sort command........................................................................................................86
3.10.2 The Union command.....................................................................................................88
3.10.3 The Split command........................................................................................................89
Summary..........................................................................................................91
IV. Rules, patterns and functions..................................................92
4.1 Introduction..............................................................................................92
4.2 Rules and patterns
8
4.2 Rules and patterns..................................................................................92
4.2.1 Rule, RuleDelayed, Replace and ReplaceAll commands...........................................92
4.2.2 Rule substitution is not commutative...........................................................................95
4.2.3 An interplay between rules and evaluation process.....................................................96
4.2.4 Rules and simple (unrestricted) patterns.....................................................................98
4.2.5 Applying rules repeatedly - the ReplaceRepeated function........................................111
4.2.6 Conditional (restricted) patterns..................................................................................114
4.2.7 Alternative patterns.....................................................................................................120
4.2.8 Giving names to entire patterns - the Pattern command ............................................121
4.2.9 Optional patterns.........................................................................................................121
4.2.10 Repeated patterns.......................................................................................................122
4.3 Built-in functions that use patterns........................................................123
4.3.1 Cases...........................................................................................................................123
4.3.2 DeleteCases.................................................................................................................128
4.3.3 MemberQ......................................................................................................................130
4.3.4 Position - a second look...............................................................................................132
4.3.5 Count.............................................................................................................................133
4.3.6 FreeQ............................................................................................................................134
4.3.7 A note on the Heads option...........................................................................................134
4.3.8 A more complicated example - finding subsequences..................................................134
4.4 Functions - starting examples and syntax..............................................138
4.4.1 A definition and a simple example................................................................................138
4.4.2 More on function names and evaluation surprises.......................................................139
4.4.3 On the necessity of patterns.........................................................................................139
4.4.4 More on the correct syntax of the function calls............................................................140
4.4.5 On function definitions and assignment operators........................................................141
4.4.6 Assigning values to function symbols (names).............................................................143
4.4.7 Advanced topic: parameter passing............................................................................144
4.4.8 Function calls: prefix and postfix syntax .......................................................................149
4.4.9 Function name conventions...........................................................................................151
4.5 Examples of functions of a single argument