The Commutative Match Objects

There are two commutative matching objects, MatchCommutative and MatchParamCommutative. The first extends the Match object and the second extends the MatchParameters objects. The matching for these two objects is extended to also generate matches that take into account terms that are commutative.

An example where it makes sense to use the MatchCommutative object might be:

Because we have used MatchCommutative, this clause will match expressions like -x + x as well as x + (-x).

Another example of the use of the MatchCommutative object might this clause used in the Expand command:

Here we are first matching to any expression that has the form "a/b", then we are testing to see if the expression bound to "a" is a multiplication and if so, we are testing to see if any of the terms in the multiply have the form "d + f".

If we used Match instead of MatchCommutative in this expression, we would only be testing for the form "d + f" in the first parameter of the multiplied terms. When we change to MatchCommutative, all of the terms in the multiplication will be matched against "d + f", and in each case all of the remaining terms in the multiplication will be bound to the variable "c". The MatchCommutative clause could be written equivalently as:

Note that a commutative pattern should contain at most one ListVariable object.