patmatch
pattern matching
Calling Sequence
Parameters
Description
Examples
patmatch(expr, pattern)
patmatch(expr, pattern, 's')
expr
-
algebraic expression to be matched
pattern
pattern to match
's'
optional name
The patmatch function returns true if it is able to match expr to pattern, and false otherwise. If true is returned, then s is assigned a list of substitutions such that subs⁡s,patt=expr, where patt is the result of removing the type qualifiers from pattern.
A pattern is an expression, containing variables typed by ::, for example a::radnum means that a is matched to an expression of type radnum. Note that in a sum e.g. a::realcons+x a can be 0, in a product a::realcons⁢x a can be 1 (not 0). This behavior can be avoided using the special keyword nonunit around the type. For example, a::nonunit(realcons)*x does not match x.
Overlapping patterns: if an expression can be matched in different ways with one pattern then we have an overlapping. For example, if we match 2+Pi to a::ℤ+b::realcons then a=0,b=2+π and a=2,b=π can be matched. It is the responsibility of the user to make sure that the pattern is not overlapping.
A note on commutativity. The pattern matcher matches the commutative operations + and *. 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.
We have the special key word conditional to express 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 this kind: ∫a::algebraicⅆx::name=a⁢x,type⁡a,freeof⁡x. This is not the same as ∫a::freeof⁡xⅆx::name since at the point the pattern matcher matches a, x is not known yet. Note that the condition has to be unevaluated or in inert form: use an `_` in front of every name, for example, _type( a, freeof( x ) ) and it is not possible to use `=` or `<>`.
Matching linear patterns and other common patterns: The pattern a::nonunit⁡algebraic+b::nonunit⁡algebraic matches the sum of two or more terms. (Same construct for *). a::nonunit⁡algebraic+b::algebraic matches a single term or the sum of terms. Note that in define we have the key words linear and multilinear generating more efficient code. The expression xn::nonunit⁡ℤ matches an integer power of x but not x.
patmatch⁡x,a::realcons⁢x+b::realcons,la;la
true
a=1,b=0
patmatch⁡sqrt⁡3⁢x−ln⁡4⁢π5−exp⁡1,a::realcons⁢x+b::realcons,la;la
a=3,b=−2⁢ln⁡2⁢π5−ⅇ
patmatch⁡exp⁡12⁢π,exp⁡n::radnum⁢π,la;la
n=12
An example on conditional patterns
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
false
patmatch⁡11⁢x+6,conditional⁡a::integer⁢x+b::integer,b<aand_type⁡a,primeandnota<0,la
A nonunit example
patmatch⁡x2,xn::nonunit⁡integer,la;la
n=2
patmatch⁡x,xn::nonunit⁡integer,la
A commutativity example
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
Examples on matching in sums
patmatch⁡a+b+c,A::nonunit⁡algebraic+B::nonunit⁡algebraic,la;la
A=a,B=b+c
patmatch⁡a,A::nonunit⁡algebraic+B::nonunit⁡algebraic,la
patmatch⁡a+exp⁡x+π,A::nonunit⁡algebraic+B::nonunit⁡algebraic+C::nonunit⁡algebraic,la;la
A=π,B=a,C=ⅇx
patmatch⁡a+exp⁡x,A::nonunit⁡algebraic+B::algebraic,la;la
A=a,B=ⅇx
patmatch⁡a,A::nonunit⁡algebraic+B::algebraic,la;la
A=a,B=0
See Also
:: (double colon)
compiletable
define
match
tablelook
type
type/::
Download Help Document