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.
restart
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 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.
Examples:
Matching a linear expression with real coefficients:
patmatch⁡x,a∷realcons ⋅ x+b∷realcons,'la';la
true
a=1,b=0
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
keyword nonunit:
patmatch⁡x2,xn::nonunit⁡integer,'la';la
n=2
patmatch⁡x,xn::nonunit⁡integer,'la'
false
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.
patmatch⁡f⁡x,y,x+y,f⁡a::name,b::name,a::name+b::name,'la';la
a=x,b=y
patmatch⁡f⁡y,x,x+y,f⁡a::name,b::name,a::name+b::name,'la';la
a=y,b=x
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
a=2,b=5
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
a=11,b=6
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.
patmatch⁡a,A::nonunit⁡algebraic+B::nonunit⁡algebraic,'la'
patmatch⁡a+ⅇx,A::nonunit⁡algebraic+B::nonunit⁡algebraic,'la';la
A=a,B=ⅇx
patmatch⁡a,A::nonunit⁡algebraic+B::algebraic,'la';la
A=a,B=0
patmatch⁡a+ⅇx,A::nonunit⁡algebraic+B::algebraic,'la';la
A=a+ⅇx,B=0
Note: for the last result, one may obtain either A=a or A=a+ⅇx. Several outcomes are possible for the following case:
patmatch⁡a+ⅇx−π,A::nonunit⁡algebraic+B::algebraic,'la';la
A=a+ⅇx−π,B=0
The pattern matcher can also handle lists:
patmatch⁡ⅇ2⁢x,2,2,ⅇa::integer⁢x,a::integer,b::integer,'la';la
a=2,b=2
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);
pat:=compiletable⁡fa::nonunit⁡integer ⋅ x=a⁢fx,gb::nonunit⁡integer ⋅⁢x= 1b ⋅ gx:
tablelook⁡f⁡2⁢x,pat
2⁢f⁡x
tablelook⁡g⁡4⁢x,pat
14⁢g⁡x
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:
tablelook⁡ⅇx3,p
3⁢ⅇx
tablelook⁡12+x,p
ln⁡2+x
Return to Index for Example Worksheets
Download Help Document