Maple Professionel
Maple Académique
Maple Edition Étudiant
Maple Personal Edition
Maple Player
Maple Player for iPad
MapleSim Professionel
MapleSim Académique
Maple T.A. - Suite d'examens de classement
Maple T.A. MAA Placement Test Suite
Möbius - Didacticiels de mathématiques en ligne
Machine Design / Industrial Automation
Aéronautique
Ingénierie des véhicules
Robotics
Energie
System Simulation and Analysis
Model development for HIL
Modélisation du procédé pour la conception de systèmes de contrôle
Robotics/Motion Control/Mechatronics
Other Application Areas
Enseignement des mathématiques
Enseignement de l’ingénierie
Enseignement secondaire et supérieur (CPGE, BTS)
Tests et évaluations
Etudiants
Modélisation financière
Recherche opérationnelle
Calcul haute performance
Physique
Webinaires en direct
Webinaires enregistrés
Agenda des évènements
Forum MaplePrimes
Blog Maplesoft
Membres Maplesoft
Maple Ambassador Program
MapleCloud
Livres blancs techniques
Bulletin électronique
Livres Maple
Math Matters
Portail des applications
Galerie de modèles MapleSim
Cas d'Etudes Utilisateur
Exploring Engineering Fundamentals
Concepts d’enseignement avec Maple
Centre d’accueil utilisateur Maplesoft
Centre de ressources pour enseignants
Centre d’assistance aux étudiants
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