Orderless

Orderless

is an attribute that can be assigned to a symbol f to indicate that the elements ei in expressions of the form f[e1,e2,] should automatically be sorted into canonical order. This property is accounted for in pattern matching.

Details

  • The Orderless attribute for a function corresponds to the mathematical property of commutativity.
  • Functions with the Orderless attribute use canonical order as described in the notes for Sort.
  • For an object that represents a matrix or a tensor, the Orderless attribute represents symmetry among indices.
  • Functions like Plus and Times are Orderless.
  • In matching patterns with Orderless functions, all possible orders of arguments are tried.
  • The Orderless attribute must be assigned before defining any values for an Orderless function.

Examples

open allclose all

Basic Examples  (3)

Plus sorts its arguments because it has attribute Orderless:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Any orderless function will sort its arguments:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

The pattern matcher reorders arguments of Orderless functions to obtain matches:

In[1]:=
Click for copyable input
Out[1]=

Note that Plus itself does not reorder these particular arguments:

In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=

Properties & Relations  (2)

Possible Issues  (1)

See Also

Sort  OrderedQ  Flat  OneIdentity  OrderlessPatternSequence

Tutorials

Introduced in 1988
(1.0)