Structural Operations
Mathematica contains some powerful primitives for making structural changes to expressions. You can use these primitives both to implement mathematical properties such as associativity and distributivity, and to provide the basis for some succinct and efficient programs.
Here we describe various operations that you can explicitly perform on expressions.
"Attributes" describes how some of these operations can be performed automatically on all expressions with a particular head by assigning appropriate attributes to that head.
You can use the
Mathematica function
Sort[expr] to sort elements not only of lists, but of expressions with any head. In this way, you can implement the mathematical properties of commutativity or symmetry for arbitrary functions.
You can use Sort to put the arguments of any function into a standard order.
| Out[1]= |  |
|
| Sort[expr] | sort the elements of a list or other expression into a standard order |
| Sort[expr,pred] | sort using the function pred to determine whether pairs are in order |
| Ordering[expr] | give the ordering of elements when sorted |
| Ordering[expr,n] | give the ordering of the first n elements when sorted |
| Ordering[expr,n,pred] | use the function pred to determine whether pairs are in order |
| OrderedQ[expr] | give True if the elements of expr are in standard order, and False otherwise |
| Order[expr1,expr2] | give 1 if expr1 comes before expr2 in standard order, and -1 if it comes after |
Sorting into order.
The second argument to Sort is a function used to determine whether pairs are in order. This sorts numbers into descending order.
| Out[2]= |  |
|
This sorting criterion puts elements that do not depend on x before those that do.
| Out[3]= |  |
|
| Flatten[expr] | flatten out all nested functions with the same head as expr |
| Flatten[expr,n] | flatten at most n levels of nesting |
| Flatten[expr,n,h] | flatten functions with head h |
| FlattenAt[expr,i] | flatten only the ith element of expr |
Flattening out expressions.
Flatten removes nested occurrences of a function.
| Out[4]= |  |
|
You can use Flatten to "splice" sequences of elements into lists or other expressions.
| Out[5]= |  |
|
You can use
Flatten to implement the mathematical property of associativity. The function
Distribute allows you to implement properties such as distributivity and linearity.
| Distribute[f[a+b+...,...]] | distribute f over sums to give f[a, ...]+f[b, ...]+... |
| Distribute[f[args],g] | distribute f over any arguments which have head g |
| Distribute[expr,g,f] | distribute only when the head is f |
| Distribute[expr,g,f,gp,fp] | distribute f over g, replacing them with fp and gp, respectively |
Applying distributive laws.
This "distributes" f over a+b.
| Out[6]= |  |
|
Here is a more complicated example.
| Out[7]= |  |
|
In general, if
f is distributive over
Plus, then an expression like
f[a+b] can be "expanded" to give
f[a]+f[b]. The function
Expand does this kind of expansion for standard algebraic operators such as
Times.
Distribute allows you to perform the same kind of expansion for arbitrary operators.
Expand uses the distributivity of Times over Plus to perform algebraic expansions.
| Out[8]= |  |
|
This applies distributivity over lists, rather than sums. The result contains all possible pairs of arguments.
| Out[9]= |  |
|
This distributes over lists, but does so only if the head of the whole expression is f.
| Out[10]= |  |
|
This distributes over lists, making sure that the head of the whole expression is f. In the result, it uses gp in place of List, and fp in place of f.
| Out[11]= |  |
|
Related to
Distribute is the function
Thread. What
Thread effectively does is to apply a function in parallel to all the elements of a list or other expression.
| Thread[f[{a1,a2},{b1,b2}]] |
| thread f over lists to give {f[a1, b1], f[a2, b2]} |
| Thread[f[args],g] | thread f over objects with head g in args |
Functions for threading expressions.
Here is a function whose arguments are lists.
| Out[12]= |  |
|
Thread applies the function "in parallel" to each element of the lists.
| Out[13]= |  |
|
Arguments that are not lists get repeated.
| Out[14]= |  |
|
As mentioned in
"Collecting Objects Together", and discussed in more detail in
"Attributes", many built-in
Mathematica functions have the property of being "listable", so that they are automatically threaded over any lists that appear as arguments.
Built-in mathematical functions such as Log are listable, so that they are automatically threaded over lists.
| Out[15]= |  |
|
Log is, however, not automatically threaded over equations.
| Out[16]= |  |
|
You can use Thread to get functions applied to both sides of an equation.
| Out[17]= |  |
|
| Outer[f,list1,list2] | generalized outer product |
| Inner[f,list1,list2,g] | generalized inner product |
Generalized outer and inner products.
Outer[f, list1, list2] takes all possible combinations of elements from
list1 and
list2, and combines them with
f.
Outer can be viewed as a generalization of a Cartesian product for tensors, as discussed in
"Tensors".
Outer forms all possible combinations of elements, and applies f to them.
| Out[18]= |  |
|
Here Outer produces a lower-triangular Boolean matrix.
| Out[19]= |  |
|
You can use Outer on any sequence of expressions with the same head.
| Out[20]= |  |
|
Outer, like
Distribute, constructs all possible combinations of elements. On the other hand,
Inner, like
Thread, constructs only combinations of elements that have corresponding positions in the expressions it acts on.
Here is a structure built by Inner.
| Out[21]= |  |
|
| Out[22]= |  |
|