Next: , Previous: , Up: Rewrite Rules   [Contents][Index]

#### 10.11.10 Matching Commands

The a m (`calc-match`) [`match`] function takes a vector of formulas and a rewrite-rule-style pattern, and produces a vector of all formulas which match the pattern. The command prompts you to enter the pattern; as for a r, you can enter a single pattern (i.e., a formula with meta-variables), or a vector of patterns, or a variable which contains patterns, or you can give a blank response in which case the patterns are taken from the top of the stack. The pattern set will be compiled once and saved if it is stored in a variable. If there are several patterns in the set, vector elements are kept if they match any of the patterns.

For example, ‘match(a+b, [x, x+y, x-y, 7, x+y+z])’ will return ‘[x+y, x-y, x+y+z]’.

The `import` mechanism is not available for pattern sets.

The a m command can also be used to extract all vector elements which satisfy any condition: The pattern ‘x :: x>0’ will select all the positive vector elements.

With the Inverse flag [`matchnot`], this command extracts all vector elements which do not match the given pattern.

There is also a function ‘matches(x, p)’ which evaluates to 1 if expression x matches pattern p, or to 0 otherwise. This is sometimes useful for including into the conditional clauses of other rewrite rules.

The function `vmatches` is just like `matches`, except that if the match succeeds it returns a vector of assignments to the meta-variables instead of the number 1. For example, ‘vmatches(f(1,2), f(a,b))’ returns ‘[a := 1, b := 2]’. If the match fails, the function returns the number 0.

Next: , Previous: , Up: Rewrite Rules   [Contents][Index]