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(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 a is matched to an expression of type radnum. Note that, in a sum such as a::realcons+x, a can be 0; while in a product, such as a::realcons*x, a can be 1. This behavior can be avoided by wrapping the keyword nonunit around the type: for example, a::nonunit(realcons)*x does not match x.
Matching a linear expression with real coefficients:
patmatch⁡x,a∷realcons ⋅ x+b∷realcons,'la';la
The following pattern matcher looks for type a⁢x+b where b is a sum of real constants:
patmatch3⁢x−ln⁡4⁢π5−ⅇ,a∷realcons ⋅ x+b∷realcons,'ls';la
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 b.
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, a, x 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 `<>`.
patmatch⁡2⁢x+5,conditional⁡a∷integer ⋅ x+b∷integer,a2<b,'la';la
patmatch⁡2⁢x+2,conditional⁡a∷integer ⋅ x+b∷integer,a2<b,'la'
patmatch⁡11⁢x+6,conditional⁡a∷integer ⋅ x+b∷integer,b<aand_type⁡a,primeandnota<0,'la';la
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 x but not x itself.
Note: for the last result, one may obtain either A=a or A=a+ⅇx. Several outcomes are possible for the following case:
The pattern matcher can also handle lists:
With compiletable and tablelook, Maple has the ability to create tables of patterns that are merged for efficient look-up.
compiletable([pattern1=rhs,pattern2=rhs,...]); and tablelook(expr,pattern);
pat:=compiletable⁡fa::nonunit⁡integer ⋅ x=a⁢fx,gb::nonunit⁡integer ⋅⁢x= 1b ⋅ gx:
They can easily be used to create lookup tables for integration and other formulas. (See also the integration example in define.)
tab≔lna∷radnum ⋅ _X+b∷radnum=lna⁢x+b⁢x+1a ⋅ lna x + bb −x−⁢1a ⋅ b,ⅇa∷radnum ⋅ _X=⁢1a ⅇx,ⅇa∷radnum ⋅ _X2+b::radnum=12π12−a12 ⋅ erf−a12⋅ x ⋅ ⅇb,_X+a::radnum−1=lnx+a,a∷radnum ⋅_Xn::integer=an+1 ⋅ xn+1:tab:=subs⁡_X=x::name,tab:p≔compiletable⁡tab:
Now we can use this table:
Return to Index for Example Worksheets
Download Help Document
What kind of issue would you like to report? (Optional)