MATHEMATICA LISTS:
Structured data objects

List groups a collection of 
expressions of any type.

lexp  =  {a, b, c}

We have just defined a symbol lexp to be the list with elements a, b, c, which are themselves symbols. We can then do operations on them.

lexp^2

Sin[{a, b, c}]

f[{a, b, c}]

Most inbuilt functions having the above property are called listable. By default, your own function will not be listable unless you change the attributes of your function.

Attributes[Sin]

SetAttributes[f, Listable]

f[{a, b, c}]

List can be used as vectors:

{a, b, c}  +  {1, 2, 3}

Head[lexp]

First[lexp]

Last[lexp]

To refer to the i-th element in a list, use the double square  bracket:

lexp[[2]]

In Mathematica, the elements in a list are referred from 1, 2, ....

Length[lexp]

lexp[[0]]

lexp[[-1]]

rexp  =  {1, 2, 3}

Join[lexp,  rexp]

Append[lexp,  1]

AppendTo[lexp, 1]

Prepend[lexp,  1]

Lists are used to represent 
matrices as list of lists.

m = {{1, 2}, {0.5, 3}}

MatrixForm[m]

Eigenvalues[m]

Det[m]

Transpose[m]

Inverse[m]

m  .  m

This is how a matrix multiplies a vector:

m  .  {a,  b}

To access the elements an array, use [[i,j]]:

m[[1, 2]]

m[[1]]

List can be used to represent a tree.

c  =  { {a,  b},  {{Sin[x],  1},  2}}

TreeForm[c]

List (or list of lists) v.s. associative array.

To use a list, the elements, size etc has to be defined first.  For example, the following use of two-dimensional array is not correct:

h[[1, 2]]  =  3

Since h as a list of list does not exist yet.  We should say:

(h  =  Table[0, {5}, {5}])  //  MatrixForm

h[[1, 2]]  =  3;  MatrixForm[h]

Associative Array

For an associative array, you need not specify the size of array before hand. We use single square brackets for associative array.

a[0]  =  2;  a[-2]  =  -4;  a[1000]  =  w;  
    a[7, 7]  =  7

Mathematica Expressions

Mathematica expressions are internally always of the form

expr[expr1,  expr2,   ... ]

where expr is called the Head, and expr1 is the first element of the expression, expr2 is the 2nd element. We obtain the expr by applying the Head[ ...] function, we obtain expr1 by First[...].

Head[a + b]

First[a + b]

FullForm[a + b]

Plus[a, b]

FullForm[a*b  +  c (1 + x)^3]

TreeForm[a*b  +  c (1 + x)^3]

You can think of practically any Mathematica expression as a tree.

Assignment and Evaluation

a = 1

a + b

b  =  (1 + x)^2

a + b

Clear[a, b]

a + b

Remove[a, b]

This show you what variables (symbols) have been defined so far.

?Global`*

PROGRAMMING IN MATHEMATICA

Programming Styles

Mathematica supports a variety of programming styles. You can program in the spirit of C or Fortran; you may also do object oriented programming like C++; or you can do functional programming like LISP; and the rule-based programming style is powerful and can be concise. Here we demonstrate few ways to write a factorial function:

\(fac[m_Integer] := 
      Module[\ {i, \ n}, \ 
	
        If[\ m\  < \ 0, \ 
		Return[Infinity]]; 
	
        n\  = \ 1; 
	
        For[i\  = \ 1, \ i\  <= \ m, \ \(++i\), \ 
	
          n\  = \ n*i]; 
\ \ \ \ \ n]\)

Some points: (1) use m only as a constant, (2) declare all variables except the argument as local, (3) return value is the last expression. Other issues relate to:
1. How do we want to display the output?
2. To what detail, do we need to check the validity of input?
We use the function as usual:

fac[6]

fac[-10]

Cheat code is to use built-in function:

fac  =  Factorial

fac[n_Integer]  :=  n!

Or use rule-based programming:

fac[n_Integer]  :=  
        Infinity  /;  n  <  0;
fac[0]  =  1; fac[n_Integer]  := n * fac[n - 1]

Or use functional programming style:

fac[n_Integer]  :=  Times @@  Range[n]

Procedural Programming Constructs

Apart from syntactic differences, many of these constructs are similar to C.  The For loop:

For[initial,  condition,  
      inc, 
    body]

fac[n_]  :=  
      Module[{t = 1, 
          i},                          For[i = 1, 
          i <= n, i++, t *= i];  
t]

i  =  3;  While[ i  > 0,  i--;  Print[i]]

Which is like a sequence of if-else:

\(f[x_]\  := \ 
      Which[x < \(-1\), \ \(-1\), 
		x <= 0, \ \(-1\)/
          2, 
		x > 0\  && \ x\  < 1, \ 
        1/2, 
		x\  >= \ 1, \ 1]\)

Plot[f[x],  {x, -3, 3}]

The If condition is in one of the three forms:

If[test,  then]

If[test,  then,  else]

If[test,  then,  else,  unknown]

Here test is an expression which evaluates to the symbol True or False; if test evaluates to True, the ``then'' expression is evaluated, otherwise, the ``else'' expression is evaluated.  If test is an expression other than True or False, unknown part will be evaluated.

If[True,  a,  b,  c]

Logical expressions can be formed similarly to C:

<                   less than
<= ( ≤ )      less equal
>                   greater than
>=  ( ≥ )     greater equal
==                  equal
!= ( ≠  )     not equal
 && ( ∧ )     AND
 || ( ∨ )     OR
===              identical
=!=              not identical

x = 4; 
    If[x  ≠  5  ∧  x  >  2,  Print[x]]

The last two (===,=!=) is a stronger form of equal or not equal:

{a == b,  a === b}

(1 + x)^2  ===  1  +  2  x  +  x^2

Typing of special symbols

Special symbol like π or α can be typed in 
	(1) using palettes,
	(2) key strokes with ESC code ESC.  
	e.g. π is ESC p ESC, or TEX style input ESC \pi ESC.
Superscripts/subscripts can be typed in with Ctrl-^, Ctrl-_; Control-space get you back to normal type.  e.g.  (Subscript[x, 2.])

(((1  +  α ))^2) //  Expand

N[π]