Previous Page Next Page Contents

match -- pattern matching

Introduction

match(expression, pattern) checks whether the syntactical structure of expression matches pattern, and if so, returns a set of replacement equations transforming pattern into expression.

Call(s)

match(expression, pattern <, option1, option2, ...>)

Parameters

expression - a MuPAD expression
pattern - the pattern: a MuPAD expression
option1, option2, ... - optional arguments (see below)

Options

Ass = {f1, f2, ...} - assume that the identifiers f1, f2, ... represent associative operators
Comm = {g1, g2, ...} - assume that the identifiers g1, g2, ... represent commutative operators
Cond = {p1, p2, ...} - conditional matching: consider only matches for which the conditions specified by the procedures p1, p2, ... are satisfied
Const = {c1, c2, ...} - assume that the identifiers c1, c2, ... represent constants
Null = {h1 = e1, h2 = e2, ...} - assume that the identifiers e1, e2, ... represent the neutral elements with respect to the operators h1, h2, ..., respectively

Returns

a set of replacement equations or FAIL.

Related Functions

matchlib::analyze, simplify, subs, subsex, subsop

Details

Option: Ass = {f1, f2, ...}

Option: Comm = {g1, g2, ...}

Option: Cond = {p1, p2, ...}

Option: Const = {c1, c2, ...}

Option: Null = {h1 = e1, h2 = e2, ...}

Example 1

All identifiers of the following pattern are pattern variables:

>> match(f(a, b), f(X, Y))
                           {X = a, Y = b, f = f}

The function f is declared non-replaceable:

>> match(f(a, b), f(X, Y), Const = {f})
                              {X = a, Y = b}

Example 2

The following call contains a condition for the pattern variable X:

>> match(f(a, b), f(X, Y), Const = {f}, Cond = {X -> not has(X, a)})
                                   FAIL

If the function f is declared commutative, the expression matches the given pattern--in contrast to the preceding example:

>> match(f(a, b), f(X, Y), Const = {f}, Comm = {f},
                           Cond = {X -> not has(X, a)})
                              {X = b, Y = a}

Example 3

The following expression cannot be matched since the number of arguments of the expression and the pattern are different:

>> match(f(a, b, c), f(X, Y), Const = {f})              
                                   FAIL

We declare the function f associative with the option Ass. In this case the pattern matches the given expression:

>> match(f(a, b, c), f(X, Y), Const = {f}, Ass = {f})
                           {X = a, Y = f(b, c)}

Example 4

If, however, the function call in the pattern has more arguments than the corresponding function call in the expression, no match is found:

>> match(f(a, b), f(X, Y, Z), Const = {f}, Ass = {f})
                                   FAIL

If the neutral element with respect to the operator f is known, additional matches are possible by substituting it for some of the pattern variables:

>> match(f(a, b), f(X, Y, Z), Const = {f}, Ass = {f}, Null = {f = 0})
                           {X = a, Z = b, Y = 0}

Example 5

Distributivity is not taken into account in general:

>> match(a*x + a*y, a*(X + Y), Const = {a})
                                   FAIL

The next call finds a match, but not the expected one:

>> match(a*(x + y), X + Y)
                          {Y = a (x + y), X = 0}

The following declarations and conditions do not lead to the expected result, either:

>> match(a*(x + y), a*X + a*Y, Const = {a},
                               Cond = {X -> X <> 0, Y -> Y <> 0})
                                   FAIL

Example 6

Automatic simplifications can ``destroy'' the structure of the given expression or pattern:

>> match(sin(-2), sin(X))
                                   FAIL

The result is FAIL, because the first argument sin(-2) is evaluated:

>> sin(-2)
                                  -sin(2)

You can circumvent this problem by using hold:

>> match(hold(sin(-2)), sin(X))  
                                 {X = -2}

Example 7

match returns only one possible match:

>> match(a + b + c + 1, X + Y)
                          {X = a, Y = b + c + 1}

To obtain other solutions, use conditions to exclude the solutions that you already have:

>> match(a + b + c + 1, X + Y, Cond = {X -> X <> a})
                          {X = b, Y = a + c + 1}
>> match(a + b + c + 1, X + Y,
         Cond = {X -> X <> a and X <> b})
                          {X = c, Y = a + b + 1}
>> match(a + b + c + 1, X + Y,
         Cond = {X -> not X in {a, b, c}})
                          {Y = a + b + c, X = 1}

Example 8

Every pattern variable can have at most one condition procedure. Simple conditions can be given by anonymous procedures (->):

>> match(a + b, X + Y, Cond = {X -> X <> a, Y -> Y <> b})
                              {X = b, Y = a}

Several conditions on a pattern variable can be combined in one procedure:

>> Xcond := proc(X) begin
     if domtype(X) = DOM_IDENT then
       X <> a and X <> b
     else
       X <> 0
     end_if
   end_proc:
>> match(sin(a*b), sin(X*Y), Cond = {Xcond})
                             {Y = a b, X = 1}
>> match(sin(a*c), sin(X*Y), Cond = {Xcond})
                              {Y = a, X = c}
>> delete Xcond:

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000