MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Fundamentals of the Mathematica Programmnbg Language note set by Richard J. Gaylord

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49330] Fundamentals of the Mathematica Programmnbg Language note set by Richard J. Gaylord
  • From: gaylord at uiuc.edu (richard j. gaylord)
  • Date: Wed, 14 Jul 2004 07:29:37 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

my pdf and Mathematica note set are now available at:

http://library.wolfram.com/infocenter/MathSource/5216/

to give you a flavor of what i am trying to emphasize in the note set,
here is an excerpt.


Mathematica Programming Fundamentals:
Lecture Notes

Richard Gaylord

These notes form the basis of a series of lectures given by the
author, in which the fundamental principles underlying
Mathematica'sprogramming language are discussed and illustrated with
carefully chosen examples. This is not a transcription ofthose
lectures, but the note set was used to create a set of transparencies
which he showed and spoke about during his lectures. These notes
formed the basis for both a single 6 hour one-day lecture and a series
of four 90 minute lectures, delivered to professionals and to
students.

Introduction
In order to use Mathematica efficiently, you need to understand how
the Mathematica programming language works. This tutorial (which has
been extensively field-tested in university courses) is intended to
provide you with the background that's needed to write your own code.

Note: While this material can't replace the use of The Mathematica
Book by Wolfram or Mathematica's online help (which give definitions
of built-in functions and simple illustrations of their use), it can
help to make you more comfortable with the Mathematica programming
language which often seems obscure, even enigmatic, when first
encountered by someone whoseprogramming experience is with one of the
traditional procedural languages.

In this note set, the following aspects of the Mathematica programming
language are emphasized: the nature of expressions,how expressions are
evaluated, how pattern-matching works, creating rewrite rules, and
using higher-order functions.


How expressions are evaluated
Mathematica is a term rewriting system (TRS). Whenever an expression
is entered, it is evaluated by term rewriting usingrewrite rules.
These rules consist of two parts: a pattern on the left-hand side and
a replacement text on the right-hand side. When the lhs of a rewrite
rule is found to pattern-match part of the expression, that part is
replaced by the rhs of the rule, after substituting values in the
expression which match labelled blanks in the pattern into the rhs of
the rule. Evaluation then proceeds by searching for further matching
rules until no more are found.

The evaluation process in Mathematica can be easily understood with
the following analogy:
Think of your experiences with using a handbook of mathematical
formulas, such as the integral tables of Gradshteyn andRyzhik. In
order to solve an integral, you consult the handbook which contains
formulas consisting of a left-hand side (lhs) and a right-hand side
(rhs), separated by an 'equals' sign. You look for an integration
formula in the handbook whose left-hand side has the same form as your
integral.

Note: While no two formulas in the handbook have the identical lhs,
there may be several whose lhs have the same form as your integral
(eg., one lhs might have specific values in the integration limits of
in the integrand, while another lhs has unspecified (dummy) variables
for these quantities). When this happens, you use the formula whose
lhs gives the closest fit to your integral. You then replace your
integral with the right-hand side of the matching lhs and you
substitute the specific values in your integral for the corresponding
variable symbols in the rhs. Finally, you look through the handbook
for formulas (eg., trigonometric identities or algebraic manipulation)
that can be used to change the answer further.

This depiction provides an excellent description of the Mathematica
evaluation process.

However, the application of the term rewriting process to a
Mathematica expression requires a bit more discussion because a
Mathematica expression consists of parts, a head and zero or more
arguments which are themselves expressions. expr@expr1, expr2, ?,
exprnD. It is therefore necessary to understand the order in which the
various parts of an expression are evaluated by term rewriting.

The implementation of the evaluation procedure is (with a few
exceptions) straightforward:

1. If the expression is a number or a string, it isn't changed.

2. If the expression is a symbol, it is rewritten if there is an
applicable rewrite rule in the global rule base; otherwise, it is
unchanged.

3. If the expression is not a number, string or symbol, its parts are
evaluated in a specific order:
a. The head of the expression is evaluated.
b. The arguments of the expression are evaluated from left to right in
order. An exception to this occurs when the head is a symbol with a
hold attribute (eg., HoldFirst, HoldRest, or HoldAll), so that some of
its arguments are left in their unevaluated forms (unless they, in
turn, have the head Evaluate). For example, the Set or SetDelayed
function which we will discuss in a moment.

4. After the head and arguments of an expression are each completely
evaluated, the expression consisting of the evaluated head and
arguments is rewritten, after making any necessary changes to the
arguments based on the Attributes (such asFlat, Listable, Orderless)
of the head, if there is an applicable rewrite rule in the global rule
base.

5. After carrying out the previous steps, the resulting expression is
evaluated in the same way and then the result of that evaluation is
evaluated, and so on until there are no more applicable rewrite rules.

The details of the term-rewriting process in steps 2 and 4 are as
follows:
a. part of an expression is pattern-matched by the lhs of a rewrite
rule
b. the values which match labelled blanks in the pattern are
substituted into the rhs of the rewrite rule and evaluated.
c. the pattern-matched part of the expression is replaced with the
evaluated result.

note that while i am a strong proponent of the functional style of
programming, the fundamental (in my opinion) nature of the Mathematica
programming language is that it is a TERM REWRITING SYSTEM (aka an
equational programming language).

i would be interested in your general response to this note set,
though i can't answer specific questions as that would eat into my
retirment time.


  • Prev by Date: Re: Normal distribtion
  • Next by Date: definite integral
  • Previous by thread: Re: User-defined or filled symbols for Points in ListPlots and Epilogs?
  • Next by thread: definite integral