pattern matching - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


patmatch

pattern matching

 

Calling Sequence

Parameters

Description

Examples

Calling Sequence

patmatch(expr, pattern)

patmatch(expr, pattern,  's')

Parameters

expr

-

algebraic expression to be matched

pattern

-

pattern to match

's'

-

optional name

Description

• 

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 subss,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::realconsx 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::realconsx+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&DifferentialD;x::name=ax,typea&comma;freeofx. This is not the same as a::freeofx&DifferentialD;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::nonunitalgebraic+b::nonunitalgebraic matches the sum of two or more terms. (Same construct for *). a::nonunitalgebraic+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.

Examples

patmatchx&comma;a::realconsx+b::realcons&comma;la&semi;la

true

a=1&comma;b=0

(1)

patmatchsqrt3xln4π5exp1&comma;a::realconsx+b::realcons&comma;la&semi;la

true

a=3&comma;b=2ln2π5&ExponentialE;

(2)

patmatchexp12π&comma;expn::radnumπ&comma;la&semi;la

true

n=12

(3)

An example on conditional patterns

patmatch2x+5&comma;conditionala::integerx+b::integer&comma;a2<b&comma;la&semi;la

true

a=2&comma;b=5

(4)

patmatch2x+2&comma;conditionala::integerx+b::integer&comma;a2<b&comma;la

false

(5)

patmatch11x+6&comma;conditionala::integerx+b::integer&comma;b<aand_typea&comma;primeandnota<0&comma;la

true

(6)

A nonunit  example

patmatchx2&comma;xn::nonunitinteger&comma;la&semi;la

true

n=2

(7)

patmatchx&comma;xn::nonunitinteger&comma;la

false

(8)

A commutativity example

patmatchfx&comma;y&comma;x+y&comma;fa::name&comma;b::name&comma;a::name+b::name&comma;la&semi;la

true

a=x&comma;b=y

(9)

patmatchfy&comma;x&comma;x+y&comma;fa::name&comma;b::name&comma;a::name+b::name&comma;la&semi;la

true

a=y&comma;b=x

(10)

Examples on matching in sums

patmatcha+b+c&comma;A::nonunitalgebraic+B::nonunitalgebraic&comma;la&semi;la

true

A=a&comma;B=b+c

(11)

patmatcha&comma;A::nonunitalgebraic+B::nonunitalgebraic&comma;la

false

(12)

patmatcha+expx+π&comma;A::nonunitalgebraic+B::nonunitalgebraic+C::nonunitalgebraic&comma;la&semi;la

true

A=π&comma;B=a&comma;C=&ExponentialE;x

(13)

patmatcha+expx&comma;A::nonunitalgebraic+B::algebraic&comma;la&semi;la

true

A=a&comma;B=&ExponentialE;x

(14)

patmatcha&comma;A::nonunitalgebraic+B::algebraic&comma;la&semi;la

true

A=a&comma;B=0

(15)

See Also

:: (double colon)

compiletable

define

match

tablelook

type

type/::