WOLFRAM LANGUAGE TUTORIAL

Reducing Expressions to Their Standard Form

The builtin functions in the Wolfram Language operate in a wide variety of ways. But many of the mathematical functions share an important approach: they are set up so as to reduce classes of mathematical expressions to standard forms.

The builtin definitions for the Plus function, for example, are set up to write any sum of terms in a standard unparenthesized form. The associativity of addition means that expressions like , and are all equivalent. But for many purposes it is convenient for all these forms to be reduced to the single standard form . The builtin definitions for Plus are set up to do this.

Through the builtin definitions for Plus, this expression is reduced to a standard unparenthesized form.
In[1]:=
Click for copyable input
Out[1]=

Whenever the Wolfram Language knows that a function is associative, it tries to remove parentheses (or nested invocations of the function) to get the function into a standard "flattened" form.

A function like addition is not only associative, but also commutative, which means that expressions like and with terms in different orders are equal. Once again, the Wolfram Language tries to put all such expressions into a "standard" form. The standard form it chooses is the one in which all the terms are in a definite order, corresponding roughly to alphabetical order.

The Wolfram Language sorts the terms in this sum into a standard order.
In[2]:=
Click for copyable input
Out[2]=
flat (associative) is equivalent to , etc.
orderless (commutative) is equivalent to , etc.

Two important properties that the Wolfram Language uses in reducing certain functions to standard form.

There are several reasons to try to put expressions into standard forms. The most important is that if two expressions are really in standard form, it is obvious whether or not they are equal.

When the two sums are put into standard order, they are immediately seen to be equal, so that two 's cancel, leaving the result .
In[3]:=
Click for copyable input
Out[3]=

You could imagine finding out whether was equal to by testing all possible orderings of each sum. It is clear that simply reducing both sums to standard form is a much more efficient procedure.

One might think that the Wolfram Language should somehow automatically reduce all mathematical expressions to a single standard canonical form. With all but the simplest kinds of expressions, however, it is quite easy to see that you do not want the same standard form for all purposes.

For polynomials, for example, there are two obvious standard forms, which are good for different purposes. The first standard form for a polynomial is a simple sum of terms, as would be generated in the Wolfram Language by applying the function Expand. This standard form is most appropriate if you need to add and subtract polynomials.

There is, however, another possible standard form that you can use for polynomials. By applying Factor, you can write any polynomial as a product of irreducible factors. This canonical form is useful if you want to do operations like division.

Expanded and factored forms are in a sense both equally good standard forms for polynomials. Which one you decide to use simply depends on what you want to use it for. As a result, the Wolfram Language does not automatically put polynomials into one of these two forms. Instead, it gives you functions like Expand and Factor that allow you explicitly to put polynomials in whatever form you want.

Here is a list of two polynomials that are mathematically equal.
In[4]:=
Click for copyable input
Out[4]=
You can write both of them in expanded form just by applying Expand. In this form, the equality of the polynomials is obvious.
In[5]:=
Click for copyable input
Out[5]=
You can also see that the polynomials are equal by writing them both in factored form.
In[6]:=
Click for copyable input
Out[6]=

Although it is clear that you do not always want expressions reduced to the same standard form, you may wonder whether it is at least possible to reduce all expressions to some standard form.

There is a basic result in the mathematical theory of computation which shows that this is, in fact, not always possible. You cannot guarantee that any finite sequence of transformations will take any two arbitrarily chosen expressions to a standard form.

In a sense, this is not particularly surprising. If you could in fact reduce all mathematical expressions to a standard form, then it would be quite easy to tell whether any two expressions were equal. The fact that so many of the difficult problems of mathematics can be stated as questions about the equality of expressions suggests that this can in fact be difficult.