# Structural Operations

The Wolfram System 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 Wolfram System 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.

In[1]:= |

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[expr_{1},expr_{2}] | give if comes before in standard order, and if it comes after |

In[2]:= |

Out[2]= |

In[3]:= |

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 i element of expr |

In[4]:= |

Out[4]= |

In[5]:= |

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 |

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 |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In general, if f is distributive over Plus, then an expression like can be "expanded" to give . 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.

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

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[{a_{1},a_{2}},{b_{1},b_{2}}]] | thread f over lists to give |

Thread[f[args],g] | thread f over objects with head g in args |

Functions for threading expressions.

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

Out[14]= |

As mentioned in "Collecting Objects Together", and discussed in more detail in "Attributes", many built‐in Wolfram System functions have the property of being "listable", so that they are automatically threaded over any lists that appear as arguments.

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

Outer[f,list_{1},list_{2}] | generalized outer product |

Inner[f,list_{1},list_{2},g] | generalized inner product |

Generalized outer and inner products.

Outer[f,list_{1},list_{2}] takes all possible combinations of elements from and , and combines them with f. Outer can be viewed as a generalization of a Cartesian product for tensors, as discussed in "Tensors".

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

In[20]:= |

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.

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |