Manipulating Sets of Transformation Rules

You can manipulate lists of transformation rules in the Wolfram Language just like other symbolic expressions. It is common to assign a name to a rule or set of rules.

This assigns the "name" to the trigonometric expansion rule.
In[1]:=
Click for copyable input
Out[1]=
You can now request the rule "by name".
In[2]:=
Click for copyable input
Out[2]=

You can use lists of rules to represent mathematical and other relations. Typically you will find it convenient to give names to the lists, so that you can easily specify the list you want in a particular case.

In most situations, it is only one rule from any given list that actually applies to a particular expression. Nevertheless, the operator tests each of the rules in the list in turn. If the list is very long, this process can take a long time.

The Wolfram Language allows you to preprocess lists of rules so that can operate more quickly on them. You can take any list of rules and apply the function Dispatch to them. The result is a representation of the original list of rules, but including dispatch tables which allow to "dispatch" to potentially applicable rules immediately, rather than testing all the rules in turn.

Here is a list of rules for the first five factorials.
In[3]:=
Click for copyable input
Out[3]=
This sets up dispatch tables that make the rules faster to use.
In[4]:=
Click for copyable input
Out[4]=
You can apply the rules using the operator.
In[5]:=
Click for copyable input
Out[5]=
Dispatch[rules]create a representation of a list of rules that includes dispatch tables
expr/.drulesapply rules that include dispatch tables

Creating and using dispatch tables.

For long lists of rules, you will find that setting up dispatch tables makes replacement operations much faster. This is particularly true when your rules are for individual symbols or other expressions that do not involve pattern objects. Once you have built dispatch tables in such cases, you will find that the operator takes a time that is more or less independent of the number of rules you have. Without dispatch tables, however, will take a time directly proportional to the total number of rules.