The Pattern Matcher in Maple
This worksheet demonstrates the functionality of the Maple pattern matcher. To check an expression for a match to a single pattern, the patmatch function is used. An efficient facility for matching an expression to one of several patterns is provided by the compiletable and tablelook functions.
>
|
|
|
Patmatch
|
|
|
Basic Functionality
|
|
Syntax:
patmatch(expr,pattern); or patmatch(expr, pattern, 's');
expr: the expression to be matched.
pattern: the pattern.
s: the returned variable with the substitution.
The patmatch function returns true if it can match expr to pattern, and returns false otherwise. If the matching is successful, s is assigned to a substitution set such that subs(s, pattern) = expr.
A pattern is an expression containing variables with type defined by "::"; for example, a::radnum means that is matched to an expression of type radnum. Note that, in a sum such as a::realcons+x, can be ; while in a product, such as a::realcons*x, can be . This behavior can be avoided by wrapping the keyword nonunit around the type: for example, a::nonunit(realcons)*x does not match .
Examples:
Matching a linear expression with real coefficients:
>
|
|
| (1.1.1) |
The following pattern matcher looks for type where is a sum of real constants:
>
|
|
| (1.1.2) |
keyword nonunit:
>
|
|
| (1.1.3) |
>
|
|
| (1.1.4) |
|
|
A Note on Commutativity
|
|
The pattern matcher matches the commutative operations `+` and `*`; for example, the pattern a::realcons*x+b::algebraic will look for a term of the form realcons*x, and then bind the rest of the sum to .
>
|
|
| (1.2.1) |
>
|
|
| (1.2.2) |
|
|
Patterns with Conditions
|
|
The special keyword conditional is used to specify patterns having additional conditions. This is used for programming patterns in tables with additional conditions on the pattern. The syntax is conditional(pattern, condition) and conditional(pattern=right_hand_side, condition) for rules in tables or define. For example, it can be used for patterns of type int(a::algebraic,x::name)=a*x,_type(a,freeof(x)). This is not the same as int(a::freeof(x),x::name), because at the point that the pattern matcher matches, , is not known yet. Note that the condition has to be unevaluated or in inert form, meaning that you must use an underscore '_' in front of every name; for example, _type(a,freeof(x)).
Note: You cannot use `=` or `<>`.
Examples:
>
|
|
| (1.3.1) |
>
|
|
| (1.3.2) |
>
|
|
| (1.3.3) |
|
|
Linear and Other Common Patterns
|
|
Matching linear patterns and other common patterns: the pattern a::nonunit(algebraic)+b::nonunit(algebraic) matches the sum of two or more terms. (The same construct as for *.) a::nonunit(algebraic)+b::algebraic matches a single term or the sum of terms. Note that in define (see the help page of define) we have the keywords linear and multilinear, which generate more efficient code. x^nonunit(n::integer) matches an integer power of but not itself.
Examples:
>
|
|
| (1.4.1) |
>
|
|
| (1.4.2) |
>
|
|
| (1.4.3) |
>
|
|
| (1.4.4) |
Note: for the last result, one may obtain either or . Several outcomes are possible for the following case:
>
|
|
| (1.4.5) |
The pattern matcher can also handle lists:
| (1.4.6) |
|
|
|
Table Lookup
|
|
With compiletable and tablelook, Maple has the ability to create tables of patterns that are merged for efficient look-up.
Syntax:
compiletable([pattern1=rhs,pattern2=rhs,...]); and tablelook(expr,pattern);
Examples:
>
|
|
>
|
|
| (2.1) |
>
|
|
| (2.2) |
They can easily be used to create lookup tables for integration and other formulas. (See also the integration example in define.)
Now we can use this table:
>
|
|
| (2.3) |
>
|
|
| (2.4) |
|
Return to Index for Example Worksheets
|