Expressions

Mathematica is a powerfull programming language and supports different programming paradigms. We will summarize its most important features and start with the functional programma paradigm, which is most succesfull in applying to mathematical oriented problems.

Everything in Mathematica is an expression. An expression is an atomic form or the form $f[a_1,...,a_n]$, where $f$ and $a_i$ are again expressions. $f$ is called the head, and the $a_i$ are called the parts or arguments, $n$ is the length of an expression. The atoms can be: an integer, a real number, or a string.

The components of an expressions can be accessed with the operator [[x]], where x is the number ranging from 0 for the head to n for the arguments. There are also explicit commands Head or Part to extract the parts of an expression.


in:={Sin[1/2][[0]], Sin[1/2][[1]]}
out:={Sin, 1/2}
Note that with {} we define a list in Mathematica. Special characters like the double bracket can be entered with ESC c ESC.

If an expression consists of nested expressions, there are multiple levels. The levels can be accessed by [[x]][[y]] or just [[x,y]]. Where x is the xth-part of the first level of expressions, and y the yth-part of the expression formed by x. Instead of the specific elements one can also select the levels by putting [[{x}]]. ositive numbers count from left to right and negative from right to left. To select all levels one can use Infinity. The numeber of levels can be found with Depth.


in:=(x^2 + y^2)[[1]][[2]]
out:=2
in:=(x^2 + y^2)[[{1}]]
out:x^2

Expressions in Mathematica can be written in different forms:

Some operators are by definition in infox form like +,-*,/.

With the command FullForm we can see for any expresion the functional form as internally used by Mathematica:


in:FullForm[x-y]
out: Times[x,Times-1,y]]
in:FullForm[x/y]
out:Times[x,Power[y,-1]]
in: FullForm[{a,b,c}]
out: List[a,b,c]
in: FullForm[x -> y]
out: Rule[x,y]
in: FullForm @ Hold @ [/. x->a]  //we must use Hold to avoid premature evalution of the expression 
out: ReplaceAll[x,Rule[x,a]]
in: FullForm & Hold x=y
out: Set[x,y]
in: FullForm[x:=y]
out: SetDelayed[x,y]
in: FullForm[x[[i]]]]
out: Part[x,i]
in: FullFrom[x_]
out: Pattern[x,Blank[]]
in: FullFrom[x_Integer]
out: Pattern[x,Blank[Integer]]
in: FullForm[#]
out: Slot
in: FullForm[#&]
out: Function[Slot1]
in: FullForm[x[[{1}]]]
out:Level[x,1]

We can also show with TreeForm the tree structure of an expression. Other similar commands are Short or Shallow.

There are a lot of commands that manipulate the arguments of expressions: Drop, Take, Delete, Insert, ReplacePart, Join, RotateLeft, RotateRight, First, Rest, Reverse, Partition, Prepend, Append. These commands leave the original expression unmodified. Commands which change the original expression: PrependTo,AppendTo

Lists,Arrays,Sets

Algorithms need data to work on. Data need to be stored in some kind of data structure. The most important structure in Mathematica is the list. A list is simply defined as follows:


in:=list={1,2.3};
Many functions defined in Mathematica can directly operate on a list by applying the function to each element of the list. Those operators have the attribute Listable

in:list={1,2.3};  // apply Sin to a list
in:Sin[list]
out:{Sin[1], Sin[2], Sin[3]}

To create a list one can also use the Table command. The Table command has as its second argument an iterator. Iterators are used in many places in Mathematica and can have the following form:


in:=Table[Random[Integer,{0,100}],25]; //create a list with 25 Integers randomly selected from 0..100 

The Range command creates a list of values its arguments are the same as for the iterator without the index. The command Interval creates an interval for real numbers.


in:=i=Limit[Cos[x],x->Infinity]; //creates the Interval[{-1,1}]  

A multidimensional list is created with the Table command by specifying more than one index to operate on. This create a lists of lists. With the Flatten command one can merge the sublists.


in:=Table[i+j,{i,1,5},{j,1,5}] (* 2 dimenision list *)

Another command, Outer takes as argument a function with n arguments, and n lists as arguments, then it applies the function to all combinations of the elements of the lists. Thread takes a function, which in turn takes as its arguments lists (including of 1 element), and applies the function to the corresponding entries of the list. Another important command is Inner. Inner applies a function on a Thread applied to another function on lists. Other commands Distribute and Transpose.


in:Outer[Outer[f,{a,b},{c,d}]]; // produces {f[a,c],f[a,d],f[b,c],f[b,d]} 
in:Thread[f[{a,b,c},{d,e,f}]];  // produces {f[a,d],f[b,e],f[c,f]} 
in: var={x,y,z}; p={1,2,3}; Thread[var->p]; // produces {x->1,y->2,z->3} 
in: exp /. Thread[var->p]; // subsitutes p into exp 
in:Inner[f,x[a,b,c],x[d,e,f],g];  // produces g[f[a,d],f[b,e],f[c,f]] 

Arrays are a specific form of lists. The advantage is that the elements of an Array are symbolically named with indices, which can be easily used in algorithms to work on the Array.


in: A=Array[a,{3,4,}];  // 2 dimenisional array 
in:Format[a[i_, j_]] = Subscripted[a[i, j], {1}, {2}]; // subscript the indices, first subscript,second superscript
in:a[i_,j_]=i+j  // fill A with numbers

Sets are like a special form of lists wthout repetition of elements and without order. There are a few commands that operate on lists as if it were sets: Union, Complement,Intersection

References



Copyright 2012 Jacq Krol. All rights reserved. Created ; last updated .