 patmatch - Maple Programming Help

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

patmatch

pattern matching

 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 $\mathrm{subs}\left(s,\mathrm{patt}\right)=\mathrm{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::\mathrm{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::\mathrm{Typesetting}:-\mathrm{_Hold}\left(\left['\mathrm{integer}'\right]\right)+b::\mathrm{realcons}$ then $a=0,b=2+\mathrm{\pi }$ and $a=2,b=\mathrm{\pi }$ 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::\mathrm{realcons}x+b::\mathrm{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: $\int a::\mathrm{algebraic}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}ⅆx::\mathrm{name}=ax,\mathrm{type}\left(a,\mathrm{freeof}\left(x\right)\right)$. This is not the same as $\int a::\mathrm{freeof}\left(x\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}ⅆx::\mathrm{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::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+b::\mathrm{nonunit}\left(\mathrm{algebraic}\right)$ matches the sum of two or more terms. (Same construct for *). $a::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+b::\mathrm{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 ${x}^{n::\mathrm{nonunit}\left(\mathrm{Typesetting}:-\mathrm{_Hold}\left(\left['\mathrm{integer}'\right]\right)\right)}$ matches an integer power of x but not x.

Examples

 > $\mathrm{patmatch}\left(x,a::\mathrm{realcons}x+b::\mathrm{realcons},'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{a}{=}{1}{,}{b}{=}{0}\right]$ (1)
 > $\mathrm{patmatch}\left(\mathrm{sqrt}\left(3\right)x-\frac{\mathrm{ln}\left(4\right)\mathrm{\pi }}{5}-\mathrm{exp}\left(1\right),a::\mathrm{realcons}x+b::\mathrm{realcons},'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{a}{=}\sqrt{{3}}{,}{b}{=}{-}\frac{{2}{}{\mathrm{ln}}{}\left({2}\right){}{\mathrm{\pi }}}{{5}}{-}{ⅇ}\right]$ (2)
 > $\mathrm{patmatch}\left(\mathrm{exp}\left(\frac{1}{2}\mathrm{\pi }\right),\mathrm{exp}\left(n::\mathrm{radnum}\mathrm{\pi }\right),'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{n}{=}\frac{{1}}{{2}}\right]$ (3)

An example on conditional patterns

 > $\mathrm{patmatch}\left(2x+5,\mathrm{conditional}\left(a::'\mathrm{integer}'x+b::'\mathrm{integer}',{a}^{2}$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{a}{=}{2}{,}{b}{=}{5}\right]$ (4)
 > $\mathrm{patmatch}\left(2x+2,\mathrm{conditional}\left(a::'\mathrm{integer}'x+b::'\mathrm{integer}',{a}^{2}
 ${\mathrm{false}}$ (5)
 > $\mathrm{patmatch}\left(11x+6,\mathrm{conditional}\left(a::'\mathrm{integer}'x+b::'\mathrm{integer}',b
 ${\mathrm{true}}$ (6)

A nonunit  example

 > $\mathrm{patmatch}\left({x}^{2},{x}^{n::\mathrm{nonunit}\left('\mathrm{integer}'\right)},'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{n}{=}{2}\right]$ (7)
 > $\mathrm{patmatch}\left(x,{x}^{n::\mathrm{nonunit}\left('\mathrm{integer}'\right)},'\mathrm{la}'\right)$
 ${\mathrm{false}}$ (8)

A commutativity example

 > $\mathrm{patmatch}\left(f\left(x,y,x+y\right),f\left(a::\mathrm{name},b::\mathrm{name},a::\mathrm{name}+b::\mathrm{name}\right),'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{a}{=}{x}{,}{b}{=}{y}\right]$ (9)
 > $\mathrm{patmatch}\left(f\left(y,x,x+y\right),f\left(a::\mathrm{name},b::\mathrm{name},a::\mathrm{name}+b::\mathrm{name}\right),'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{a}{=}{y}{,}{b}{=}{x}\right]$ (10)

Examples on matching in sums

 > $\mathrm{patmatch}\left(a+b+c,A::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+B::\mathrm{nonunit}\left(\mathrm{algebraic}\right),'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{A}{=}{a}{,}{B}{=}{b}{+}{c}\right]$ (11)
 > $\mathrm{patmatch}\left(a,A::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+B::\mathrm{nonunit}\left(\mathrm{algebraic}\right),'\mathrm{la}'\right)$
 ${\mathrm{false}}$ (12)
 > $\mathrm{patmatch}\left(a+\mathrm{exp}\left(x\right)-\mathrm{\pi },A::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+B::\mathrm{nonunit}\left(\mathrm{algebraic}\right),'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{A}{=}{a}{,}{B}{=}{{ⅇ}}^{{x}}{-}{\mathrm{\pi }}\right]$ (13)
 > $\mathrm{patmatch}\left(a+\mathrm{exp}\left(x\right)-\mathrm{\pi },A::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+B::\mathrm{algebraic},'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{A}{=}{a}{+}{{ⅇ}}^{{x}}{-}{\mathrm{\pi }}{,}{B}{=}{0}\right]$ (14)
 > $\mathrm{patmatch}\left(a,A::\mathrm{nonunit}\left(\mathrm{algebraic}\right)+B::\mathrm{algebraic},'\mathrm{la}'\right);$$\mathrm{la}$
 ${\mathrm{true}}$
 $\left[{A}{=}{a}{,}{B}{=}{0}\right]$ (15)