### Classifiers and Canonical Simplifiers

A classifying function for an equivalence ~ has the property ; that is, two elements are equivalent if the result of applying the classifier to both elements is the same. Note that the range of need not be the same as the domain. For example, the length function on symbol names is a classifier for the relation introduced in the section on Equivalences. The length function appeared in all our ordering predicates and equivalence relations and we may wonder whether we could not speed up the code by computing the length once for each element and then working with the lengths only.

A canonical simplifier is a classifier with the property (here, the range is the same as the domain). This condition implies . A canonical simplifier thus allows to identify a unique (simplest) element in every equivalence class. For every , is this simplest element in the equivalence class of .

If the range of a classifier is totally ordered, this order induces a total preorder on the domain: . The equivalence class generated by this preorder is of course the one classified by .

Because Mathematica expressions are totally ordered (by the so-called canonical ordering), every classifier introduces a preorder: . It follows that whenever we can find a classifier or canonical simplifier, we can use the faster method involving sorting to find equivalence classes.

The code for partitioning into equivalence classes using a classifier is similar to the code for `PreorderIndices` above. The main advantage is that we can use the built-in ordering for sorting the list and `SameQ` for splitting, because we can first apply the classifier to all elements just once, instead of repeatedly in the preorder predicate (shown in red).

The default canonical simplifier `Identity` induces canonical ordering and generates the trivial equivalence.

This function was called `Classify` in a related Usenet discussion in 1995 [3], where it was implemented directly, without `ClassifyIndex`. However, the detour through the indices does not slow down the computation (as it did in the examples with `PreorderClasses`). The original solution was rather more complicated, due to the lack of the `Split` function in earlier versions of Mathematica.

This method is much faster than the earlier ones.

The code for computing a transversal (or union) based on classifiers is much faster than the built-in `Union` with a user-defined equivalence relation for large inputs.

If the classifier is a canonical simplifier there is one preferred transversal, where each class is represented by its canonical element. It is particularly easy to code.

Incidentally, we could also express `EquivalenceIndices` in terms of `ClassifyClasses`, using the mapping from indices to list elements as the classifying function: two indices are equivalent if the corresponding list elements are the same.

Converted by Mathematica

[Article Index] [Prev Page][Next Page]