Sponsored links: Algebra eBooks ### The Maxima on-line user's manual

Algebra Calculator

#### Search: #### Defmatch

Function: defmatch (<progname>, <pattern>, <x_1>, ..., <x_n>) Function: defmatch (<progname>, <pattern>) Defines a function `<progname>(<expr>, <x_1>, ..., <x_n>)` which tests <expr> to see if it matches <pattern>.

<pattern> is an expression containing the pattern arguments <x_1>, ..., <x_n> (if any) and some pattern variables (if any). The pattern arguments are given explicitly as arguments to `defmatch` while the pattern variables are declared by the `matchdeclare` function. Any variable not declared as a pattern variable in `matchdeclare` or as a pattern argument in `defmatch` matches only itself.

The first argument to the created function <progname> is an expression to be matched against the pattern and the other arguments are the actual arguments which correspond to the dummy variables <x_1>, ..., <x_n> in the pattern.

If the match is successful, <progname> returns a list of equations whose left sides are the pattern arguments and pattern variables, and whose right sides are the subexpressions which the pattern arguments and variables matched. The pattern variables, but not the pattern arguments, are assigned the subexpressions they match. If the match fails, <progname> returns `false`.

A literal pattern (that is, a pattern which contains neither pattern arguments nor pattern variables) returns `true` if the match succeeds.

See also `matchdeclare`, `defrule`, `tellsimp`, and `tellsimpafter`.

Examples:

Define a function `linearp(expr, x)` which tests `expr` to see if it is of the form `a*x + b` such that `a` and `b` do not contain `x` and `a` is nonzero. This match function matches expressions which are linear in any variable, because the pattern argument `x` is given to `defmatch`.

```          (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
freeof(x));
(%o1)                         done
(%i2) defmatch (linearp, a*x + b, x);
(%o2)                        linearp
(%i3) linearp (3*z + (y + 1)*z + y^2, z);
2
(%o3)              [b = y , a = y + 4, x = z]
(%i4) a;
(%o4)                         y + 4
(%i5) b;
2
(%o5)                          y
(%i6) x;
(%o6)                           x```

Define a function `linearp(expr)` which tests `expr` to see if it is of the form `a*x + b` such that `a` and `b` do not contain `x` and `a` is nonzero. This match function only matches expressions linear in `x`, not any other variable, because no pattern argument is given to `defmatch`.

```          (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
freeof(x));
(%o1)                         done
(%i2) defmatch (linearp, a*x + b);
(%o2)                        linearp
(%i3) linearp (3*z + (y + 1)*z + y^2);
(%o3)                         false
(%i4) linearp (3*x + (y + 1)*x + y^2);
2
(%o4)                  [b = y , a = y + 4]```

Define a function `checklimits(expr)` which tests `expr` to see if it is a definite integral.

```          (%i1) matchdeclare ([a, f], true);
(%o1)                         done
(%i2) constinterval (l, h) := constantp (h - l);
(%o2)        constinterval(l, h) := constantp(h - l)
(%i3) matchdeclare (b, constinterval (a));
(%o3)                         done
(%i4) matchdeclare (x, atom);
(%o4)                         done
(%i5) simp : false;
(%o5)                         false
(%i6) defmatch (checklimits, integrate (f, x, a, b));
(%o6)                      checklimits
(%i7) simp : true;
(%o7)                         true
(%i8) integrate (sin(t), t, %pi + x, 2*%pi + x);
x + 2 %pi
/
[
(%o8)                 I          sin(t) dt
]
/
x + %pi
(%i9) checklimits (%);
(%o9)    [b = x + 2 %pi, a = x + %pi, x = t, f = sin(t)]```

```(%o1)                                true
(%i2) ```

### Related Examples

##### defmatch-lambda-matchdeclare

matchdeclare (a, lamb...

eq...

lin...

Calculate

##### defmatch-freeof-matchdeclare

matchdeclare (a, free...

defmatch (linearp, a*...

linearp (3*x + (y + ...

Calculate

##### defmatch-lambda-letrules-matchdeclare

matchdeclare (a, lamb...

eq...

lin...

Calculate

##### defmatch-lambda-matchdeclare

matchdeclare (a, lamb...

de...

lin...

Calculate

##### defmatch-freeof-matchdeclare

L:[a,b];

matchdeclare ([a,b], ...

defmatch (linearp, a*...

Calculate

##### defmatch-lambda-letrules-matchdeclare

matchdeclare (a, lamb...

eq...

lin...

Calculate

##### defmatch-lambda-matchdeclare

matchdeclare (a, lamb...

de...

lin...

Calculate

? defmatch;

Calculate

##### defmatch-lambda-matchdeclare

matchdeclare (a, lamb...

eq...

lin...

Calculate

##### defmatch-freeof-matchdeclare

matchdeclare (a, free...

defmatch (linearp, a*...

linearp (3*x + (y + ...

Calculate 