Maple Professional
Maple Academic
Maple Student Edition
Maple Personal Edition
Maple Player
Maple Player for iPad
MapleSim Professional
MapleSim Academic
Maple T.A. - Testing & Assessment
Maple T.A. MAA Placement Test Suite
Möbius - Online Courseware
Machine Design / Industrial Automation
Aerospace
Vehicle Engineering
Robotics
Power Industries
System Simulation and Analysis
Model development for HIL
Plant Modeling for Control Design
Robotics/Motion Control/Mechatronics
Other Application Areas
Mathematics Education
Engineering Education
High Schools & Two-Year Colleges
Testing & Assessment
Students
Financial Modeling
Operations Research
High Performance Computing
Physics
Live Webinars
Recorded Webinars
Upcoming Events
MaplePrimes
Maplesoft Blog
Maplesoft Membership
Maple Ambassador Program
MapleCloud
Technical Whitepapers
E-Mail Newsletters
Maple Books
Math Matters
Application Center
MapleSim Model Gallery
User Case Studies
Exploring Engineering Fundamentals
Teaching Concepts with Maple
Maplesoft Welcome Center
Teacher Resource Center
Student Help Center
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.
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 is matched to an expression of type radnum. Note that, in a sum such as a::realcons+x, can be ; while in a product, such as a::realcons*x, can be . This behavior can be avoided by wrapping the keyword nonunit around the type: for example, a::nonunit(realcons)*x does not match .
Examples:
Matching a linear expression with real coefficients:
The following pattern matcher looks for type where is a sum of real constants:
keyword nonunit:
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 .
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, , 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 `<>`.
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 but not itself.
Note: for the last result, one may obtain either or . Several outcomes are possible for the following case:
The pattern matcher can also handle lists:
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);
They can easily be used to create lookup tables for integration and other formulas. (See also the integration example in define.)
Now we can use this table:
Return to Index for Example Worksheets
Download Help Document