This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Functional Operations  /

2.2.10 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.
This section describes various operations that you can explicitly perform on expressions. Section 2.5.3 will describe 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.
  • In[1]:= Sort[ f[c, a, b] ]


    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.
  • In[2]:= Sort[ {5, 1, 8, 2}, (#2 < #1)& ]


  • This sorting criterion puts elements that do not depend on x before those that do.
  • In[3]:= Sort[ {x^2, y, x+y, y-2}, FreeQ[#1, x]& ]


    Flattening out expressions.

  • Flatten removes nested occurrences of a function.
  • In[4]:= Flatten[ f[a, f[b, c], f[f[d]]] ]


  • You can use Flatten to "splice" sequences of elements into lists or other expressions.
  • In[5]:= Flatten[ {a, f[b, c], f[a, b, d]}, 1, f ]


    You can use Flatten to implement the mathematical property of associativity. The function Distribute allows you to implement properties such as distributivity and linearity.

    Applying distributive laws.

  • This "distributes" f over a+b.
  • In[6]:= Distribute[ f[a + b] ]


  • Here is a more complicated example.
  • In[7]:= Distribute[ f[a + b, c + d] ]


    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.
  • In[8]:= Expand[ (a + b) (c + d) ]


  • This applies distributivity over lists, rather than sums. The result contains all possible pairs of arguments.
  • In[9]:= Distribute[ f[{a, b}, {c, d}], List ]


  • This distributes over lists, but does so only if the head of the whole expression is f.
  • In[10]:= Distribute[ f[{a, b}, {c, d}], List, f ]


  • 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.
  • In[11]:= Distribute[ f[{a, b}, {c, d}], List, f, gp, fp ]


    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.

    Functions for threading expressions.

  • Here is a function whose arguments are lists.
  • In[12]:= f[{a1, a2}, {b1, b2}]


  • Thread applies the function "in parallel" to each element of the lists.
  • In[13]:= Thread[%]


  • Arguments that are not lists get repeated.
  • In[14]:= Thread[ f[{a1, a2}, {b1, b2}, c, d] ]


    As mentioned in Section 1.8.1, and discussed in more detail in Section 2.5.3, 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.
  • In[15]:= Log[{a, b, c}]


  • Log is, however, not automatically threaded over equations.
  • In[16]:= Log[x == y]


  • You can use Thread to get functions applied to both sides of an equation.
  • In[17]:= Thread[%, Equal]


    Generalized outer and inner products.



    ] 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 Section 3.7.11


  • Outer forms all possible combinations of elements, and applies f to them.
  • In[18]:= Outer[f, {a, b}, {1, 2, 3}]


  • Here Outer produces a lower-triangular Boolean matrix.
  • In[19]:= Outer[ Greater, {1, 2, 3}, {1, 2, 3} ]


  • You can use Outer on any sequence of expressions with the same head.
  • In[20]:= Outer[ g, f[a, b], f[c, d] ]


    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.
  • In[21]:= Inner[f, {a, b}, {c, d}, g]


  • Inner is a generalization of Dot.
  • In[22]:= Inner[Times, {a, b}, {c, d}, Plus]