patmatch - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Operations : Substitution : patmatch

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 the patmatch is successful, s is assigned a substitution set such that subss,pattern=expr.

• 

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::integer+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: inta::algebraic&comma;x::name&equals;ax&comma;typea&comma;freeofx. This is not the same as inta::freeofx&comma;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&plus;b::nonunitalgebraic matches the sum of two or more terms. (Same construct for *). a::nonunitalgebraic&plus;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::nonunitinteger matches an integer power of x but not x.

Examples

patmatchx&comma;a::realconsx&plus;b::realcons&comma;&apos;la&apos;&semi;la

true

a&equals;1&comma;b&equals;0

(1)

patmatch3xln4&pi;5&ExponentialE;&comma;a::realconsx&plus;b::realcons&comma;&apos;la&apos;&semi;la

true

a&equals;3&comma;b&equals;25ln2&pi;&ExponentialE;

(2)

patmatch&ExponentialE;1&pi;2&comma;&ExponentialE;n::radnum&pi;&comma;&apos;la&apos;&semi;la

true

n&equals;12

(3)

An example on conditional patterns

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

true

a&equals;2&comma;b&equals;5

(4)

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

false

(5)

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

true

(6)

A nonunit  example

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

true

n&equals;2

(7)

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

false

(8)

A commutativity example

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

true

a&equals;x&comma;b&equals;y

(9)

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

true

a&equals;y&comma;b&equals;x

(10)

Examples on matching in sums

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

true

A&equals;a&comma;B&equals;b&plus;c

(11)

patmatcha&comma;A::nonunitalgebraic&plus;B::nonunitalgebraic&comma;&apos;la&apos;

false

(12)

patmatcha&plus;&ExponentialE;x&pi;&comma;A::nonunitalgebraic&plus;B::nonunitalgebraic&comma;&apos;la&apos;&semi;la

true

A&equals;a&comma;B&equals;&ExponentialE;x&pi;

(13)

patmatcha&plus;&ExponentialE;x&pi;&comma;A::nonunitalgebraic&plus;B::algebraic&comma;&apos;la&apos;&semi;la

true

A&equals;a&plus;&ExponentialE;x&pi;&comma;B&equals;0

(14)

patmatcha&comma;A::nonunitalgebraic&plus;B::algebraic&comma;&apos;la&apos;&semi;la

true

A&equals;a&comma;B&equals;0

(15)

See Also

:: (double colon)

compiletable

define

match

tablelook

type

type/::

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam