MathematicaÒ programming: an advanced introduction Leonid
Shifrin
Part I: The core language
Version 1.01
2
Mathematica programming: an advanced introduction Leonid Shifrin Copyright © Leonid Shifrin, 2008
This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
MathematicaTM is a registered trademark of Wolfram Research Inc. Other symbols, where used, respective owners.
Leonid Shifrin
Digitally signed by Leonid Shifrin DN: cn=Leonid Shifrin, o=Brunel University, ou,
[email protected] ming-intro.org, c=GB Date: 2009.02.04 11:30:22 -08'00'
are registered trademarks of their
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........................