2 * Introduction to Rules and Patterns::
3 * Functions and Variables for Rules and Patterns::
6 @c -----------------------------------------------------------------------------
7 @node Introduction to Rules and Patterns, Functions and Variables for Rules and Patterns, Rules and Patterns, Rules and Patterns
8 @section Introduction to Rules and Patterns
9 @c -----------------------------------------------------------------------------
11 This section describes user-defined pattern matching and simplification rules.
12 There are two groups of functions which implement somewhat different pattern
13 matching schemes. In one group are @code{tellsimp}, @code{tellsimpafter},
14 @code{defmatch}, @code{defrule}, @code{apply1}, @code{applyb1}, and
15 @code{apply2}. In the other group are @code{let} and @code{letsimp}.
16 Both schemes define patterns in terms of pattern variables declared by
19 Pattern-matching rules defined by @code{tellsimp} and @code{tellsimpafter} are
20 applied automatically by the Maxima simplifier. Rules defined by
21 @code{defmatch}, @code{defrule}, and @code{let} are applied by an explicit
24 There are additional mechanisms for rules applied to polynomials by
25 @code{tellrat}, and for commutative and noncommutative algebra in @code{affine}
28 @opencatbox{Categories:}
29 @category{Simplification}
30 @category{Rules and patterns}
33 @c end concepts Rules and Patterns
35 @c -----------------------------------------------------------------------------
36 @node Functions and Variables for Rules and Patterns, , Introduction to Rules and Patterns, Rules and Patterns
37 @section Functions and Variables for Rules and Patterns
38 @c -----------------------------------------------------------------------------
40 @c NEEDS CLARIFICATION AND EXAMPLES
42 @c -----------------------------------------------------------------------------
44 @deffn {Function} apply1 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
46 Repeatedly applies @var{rule_1} to
47 @var{expr} until it fails, then repeatedly applies the same rule to all
48 subexpressions of @var{expr}, left to right, until @var{rule_1} has failed
49 on all subexpressions. Call the result of transforming @var{expr} in this
50 manner @var{expr_2}. Then @var{rule_2} is applied in the same fashion
51 starting at the top of @var{expr_2}. When @var{rule_n} fails on the final
52 subexpression, the result is returned.
54 @code{maxapplydepth} is the depth of the deepest subexpressions processed by
55 @code{apply1} and @code{apply2}.
57 See also @mrefcomma{applyb1} @mref{apply2} and @mrefdot{let}
59 @opencatbox{Categories:}
60 @category{Rules and patterns}
64 @c NEEDS CLARIFICATION AND EXAMPLES
66 @c -----------------------------------------------------------------------------
68 @deffn {Function} apply2 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
70 If @var{rule_1} fails on a given subexpression, then @var{rule_2} is
71 repeatedly applied, etc. Only if all rules fail on a given
72 subexpression is the whole set of rules repeatedly applied to the next
73 subexpression. If one of the rules succeeds, then the same
74 subexpression is reprocessed, starting with the first rule.
76 @code{maxapplydepth} is the depth of the deepest subexpressions processed by
77 @code{apply1} and @code{apply2}.
79 See also @mref{apply1} and @mrefdot{let}
81 @opencatbox{Categories:}
82 @category{Rules and patterns}
86 @c NEEDS CLARIFICATION AND EXAMPLES
88 @c -----------------------------------------------------------------------------
90 @deffn {Function} applyb1 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
92 Repeatedly applies @var{rule_1} to the deepest subexpression of @var{expr}
93 until it fails, then repeatedly applies the same rule one level higher (i.e.,
94 larger subexpressions), until @var{rule_1} has failed on the top-level
95 expression. Then @var{rule_2} is applied in the same fashion to the result of
96 @var{rule_1}. After @var{rule_n} has been applied to the top-level expression,
97 the result is returned.
99 @code{applyb1} is similar to @code{apply1} but works from
100 the bottom up instead of from the top down.
102 @code{maxapplyheight} is the maximum height which @code{applyb1} reaches
105 See also @mrefcomma{apply1} @mref{apply2} and @mrefdot{let}
107 @opencatbox{Categories:}
108 @category{Rules and patterns}
112 @c -----------------------------------------------------------------------------
113 @anchor{current_let_rule_package}
114 @defvr {Option variable} current_let_rule_package
115 Default value: @code{default_let_rule_package}
117 @code{current_let_rule_package} is the name of the rule package that is used by
118 functions in the @code{let} package (@code{letsimp}, etc.) @c NEED TO GIVE AN EXPLICIT LIST HERE (NOT "ETC")
119 if no other rule package is specified.
120 This variable may be assigned the name of any rule package defined
121 via the @code{let} command.
123 If a call such as @code{letsimp (expr, rule_pkg_name)} is made,
124 the rule package @code{rule_pkg_name} is used for that function call only,
125 and the value of @code{current_let_rule_package} is not changed.
127 @opencatbox{Categories:}
128 @category{Rules and patterns}
132 @c -----------------------------------------------------------------------------
133 @anchor{default_let_rule_package}
134 @defvr {Option variable} default_let_rule_package
135 @c DEFAULT BINDING OF default_let_rule_package IS default_let_rule_package (BOUND TO ITSELF)
136 Default value: @code{default_let_rule_package}
138 @c THIS IS SORT OF CONFUSING. PROBABLY NEED TO GIVE MORE DETAIL HERE
139 @code{default_let_rule_package} is the name of the rule package used when one
140 is not explicitly set by the user with @code{let} or by changing the value of
141 @code{current_let_rule_package}.
143 @opencatbox{Categories:}
144 @category{Rules and patterns}
148 @c -----------------------------------------------------------------------------
150 @deffn {Function} defmatch @
151 @fname{defmatch} (@var{progname}, @var{pattern}, @var{x_1}, @dots{}, @var{x_n}) @
152 @fname{defmatch} (@var{progname}, @var{pattern})
154 Defines a function @code{@var{progname}(@var{expr}, @var{x_1}, ..., @var{x_n})}
155 which tests @var{expr} to see if it matches @var{pattern}.
157 @var{pattern} is an expression containing the pattern arguments @var{x_1},
158 @dots{}, @var{x_n} (if any) and some pattern variables (if any). The pattern
159 arguments are given explicitly as arguments to @code{defmatch} while the pattern
160 variables are declared by the @code{matchdeclare} function. Any variable not
161 declared as a pattern variable in @code{matchdeclare} or as a pattern argument
162 in @code{defmatch} matches only itself.
164 The first argument to the created function @var{progname} is an expression to be
165 matched against the pattern and the other arguments are the actual arguments
166 which correspond to the dummy variables @var{x_1}, @dots{}, @var{x_n} in the
169 If the match is successful, @var{progname} returns a list of equations whose
170 left sides are the pattern arguments and pattern variables, and whose right
171 sides are the subexpressions which the pattern arguments and variables matched.
172 The pattern variables, but not the pattern arguments, are assigned the
173 subexpressions they match. If the match fails, @var{progname} returns
176 A literal pattern (that is, a pattern which contains neither pattern arguments
177 nor pattern variables) returns @code{true} if the match succeeds.
179 See also @mrefcomma{matchdeclare} @mrefcomma{defrule} @mref{tellsimp} and
180 @mrefdot{tellsimpafter}
184 Define a function @code{linearp(expr, x)} which
185 tests @code{expr} to see if it is of the form @code{a*x + b}
186 such that @code{a} and @code{b} do not contain @code{x} and @code{a} is nonzero.
187 This match function matches expressions which are linear in any variable,
188 because the pattern argument @code{x} is given to @code{defmatch}.
189 @c HOW HARD WILL MAXIMA TRY TO COLLECT TERMS AND DO OTHER MUNGEING TO FIT THE PATTERN ??
192 @c matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
194 @c defmatch (linearp, a*x + b, x);
195 @c linearp (3*z + (y + 1)*z + y^2, z);
202 (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
207 (%i2) defmatch (linearp, a*x + b, x);
211 (%i3) linearp (3*z + (y + 1)*z + y^2, z);
213 (%o3) [b = y , a = y + 4, x = z]
230 Define a function @code{linearp(expr)} which tests @code{expr}
231 to see if it is of the form @code{a*x + b}
232 such that @code{a} and @code{b} do not contain @code{x} and @code{a} is nonzero.
233 This match function only matches expressions linear in @code{x},
234 not any other variable, because no pattern argument is given to @code{defmatch}.
237 @c matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
239 @c defmatch (linearp, a*x + b);
240 @c linearp (3*z + (y + 1)*z + y^2);
241 @c linearp (3*x + (y + 1)*x + y^2);
245 (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
250 (%i2) defmatch (linearp, a*x + b);
254 (%i3) linearp (3*z + (y + 1)*z + y^2);
258 (%i4) linearp (3*x + (y + 1)*x + y^2);
260 (%o4) [b = y , a = y + 4]
264 Define a function @code{checklimits(expr)} which tests @code{expr}
265 to see if it is a definite integral.
268 @c matchdeclare ([a, f], true);
269 @c constinterval (l, h) := constantp (h - l);
270 @c matchdeclare (b, constinterval (a));
271 @c matchdeclare (x, atom);
273 @c defmatch (checklimits, 'integrate (f, x, a, b));
275 @c 'integrate (sin(t), t, %pi + x, 2*%pi + x);
280 (%i1) matchdeclare ([a, f], true);
284 (%i2) constinterval (l, h) := constantp (h - l);
285 (%o2) constinterval(l, h) := constantp(h - l)
288 (%i3) matchdeclare (b, constinterval (a));
292 (%i4) matchdeclare (x, atom);
300 (%i6) defmatch (checklimits, 'integrate (f, x, a, b));
308 (%i8) 'integrate (sin(t), t, %pi + x, 2*%pi + x);
318 (%i9) checklimits (%);
319 (%o9) [b = x + 2 %pi, a = x + %pi, x = t, f = sin(t)]
323 @opencatbox{Categories:}
324 @category{Rules and patterns}
328 @c NEEDS CLARIFICATION AND EXAMPLES
330 @c -----------------------------------------------------------------------------
332 @deffn {Function} defrule (@var{rulename}, @var{pattern}, @var{replacement})
334 Defines and names a replacement rule for the given pattern. If the rule named
335 @var{rulename} is applied to an expression (by @code{apply1}, @code{applyb1}, or
336 @code{apply2}), every subexpression matching the pattern will be replaced by the
337 replacement. All variables in the replacement which have been
338 assigned values by the pattern match are assigned those values in the
339 replacement which is then simplified.
341 The rules themselves can be
342 treated as functions which transform an expression by one
343 operation of the pattern match and replacement.
344 If the match fails, the rule function returns @code{false}.
346 @opencatbox{Categories:}
347 @category{Rules and patterns}
353 @c -----------------------------------------------------------------------------
355 @deffn {Function} disprule @
356 @fname{disprule} (@var{rulename_1}, @dots{}, @var{rulename_2}) @
357 @fname{disprule} (all)
359 Display rules with the names @var{rulename_1}, @dots{}, @var{rulename_n},
360 as returned by @code{defrule}, @code{tellsimp}, or @code{tellsimpafter},
361 or a pattern defined by @code{defmatch}.
362 Each rule is displayed with an intermediate expression label (@code{%t}).
364 @code{disprule (all)} displays all rules.
366 @code{disprule} quotes its arguments.
367 @code{disprule} returns the list of intermediate expression labels corresponding
368 to the displayed rules.
370 See also @mrefcomma{letrules} which displays rules defined by @mrefdot{let}
375 @c tellsimpafter (foo (x, y), bar (x) + baz (y));
376 @c tellsimpafter (x + y, special_add (x, y));
377 @c defmatch (quux, mumble (x));
378 @c disprule (foorule1, ?\+rule1, quux);
383 (%i1) tellsimpafter (foo (x, y), bar (x) + baz (y));
384 (%o1) [foorule1, false]
387 (%i2) tellsimpafter (x + y, special_add (x, y));
388 (%o2) [+rule1, simplus]
391 (%i3) defmatch (quux, mumble (x));
395 (%i4) disprule (foorule1, ?\+rule1, quux);
396 (%t4) foorule1 : foo(x, y) -> baz(y) + bar(x)
398 (%t5) +rule1 : y + x -> special_add(x, y)
400 (%t6) quux : mumble(x) -> []
402 (%o6) [%t4, %t5, %t6]
406 (%o7) [foorule1 : foo(x, y) -> baz(y) + bar(x),
407 +rule1 : y + x -> special_add(x, y), quux : mumble(x) -> []]
411 @opencatbox{Categories:}
412 @category{Rules and patterns}
413 @category{Display functions}
417 @c -----------------------------------------------------------------------------
419 @deffn {Function} let @
420 @fname{let} (@var{prod}, @var{repl}, @var{predname}, @var{arg_1}, @dots{}, @var{arg_n}) @
421 @fname{let} ([@var{prod}, @var{repl}, @var{predname}, @var{arg_1}, @dots{}, @var{arg_n}], @var{package_name})
423 Defines a substitution rule for @code{letsimp} such that @var{prod} is replaced
424 by @var{repl}. @var{prod} is a product of positive or negative powers of the
429 Atoms which @code{letsimp} will search for literally unless previous to calling
430 @code{letsimp} the @code{matchdeclare} function is used to associate a
431 predicate with the atom. In this case @code{letsimp} will match the atom to
432 any term of a product satisfying the predicate.
434 Kernels such as @code{sin(x)}, @code{n!}, @code{f(x,y)}, etc. As with atoms
435 above @code{letsimp} will look for a literal match unless @code{matchdeclare}
436 is used to associate a predicate with the argument of the kernel.
439 A term to a positive power will only match a term having at least that
440 power. A term to a negative power
441 on the other hand will only match a term with a power at least as
442 negative. In the case of negative powers in @var{prod} the switch
443 @code{letrat} must be set to @code{true}.
444 See also @mrefdot{letrat}
446 If a predicate is included in the @code{let} function followed by a list of
447 arguments, a tentative match (i.e. one that would be accepted if the predicate
448 were omitted) is accepted only if @code{predname (arg_1', ..., arg_n')}
449 evaluates to @code{true} where @var{arg_i'} is the value matched to @var{arg_i}.
450 The @var{arg_i} may be the name of any atom or the argument of any kernel
451 appearing in @var{prod}.
452 @var{repl} may be any rational expression. @c ONLY RATIONAL -- REALLY ??
453 If any of the atoms or arguments from @var{prod} appear in @var{repl} the
454 appropriate substitutions are made. @c SPELL OUT "APPROPRIATE" IN THIS CONTEXT
456 The global flag @code{letrat} controls the simplification of quotients by
457 @code{letsimp}. When @code{letrat} is @code{false}, @code{letsimp} simplifies
458 the numerator and denominator of @var{expr} separately, and does not simplify
459 the quotient. Substitutions such as @code{n!/n} goes to @code{(n-1)!} then
460 fail. When @code{letrat} is @code{true}, then the numerator, denominator, and
461 the quotient are simplified in that order.
463 These substitution functions allow you to work with several rule packages at
464 once. Each rule package can contain any number of @code{let} rules and is
465 referenced by a user-defined name. The command @code{let ([@var{prod},
466 @var{repl}, @var{predname}, @var{arg_1}, ..., @var{arg_n}], @var{package_name})}
467 adds the rule @var{predname} to the rule package @var{package_name}. The
468 command @code{letsimp (@var{expr}, @var{package_name})} applies the rules in
469 @var{package_name}. @code{letsimp (@var{expr}, @var{package_name1},
470 @var{package_name2}, ...)} is equivalent to @code{letsimp (@var{expr},
471 @var{package_name1})} followed by @code{letsimp (%, @var{package_name2})},
474 @code{current_let_rule_package} is the name of the rule package that is
475 presently being used. This variable may be assigned the name of any rule
476 package defined via the @code{let} command. Whenever any of the functions
477 comprising the @code{let} package are called with no package name, the package
478 named by @code{current_let_rule_package} is used. If a call such as
479 @code{letsimp (@var{expr}, @var{rule_pkg_name})} is made, the rule package
480 @var{rule_pkg_name} is used for that @code{letsimp} command only, and
481 @code{current_let_rule_package} is not changed. If not otherwise specified,
482 @code{current_let_rule_package} defaults to @code{default_let_rule_package}.
485 (%i1) matchdeclare ([a, a1, a2], true)$
486 (%i2) oneless (x, y) := is (x = y-1)$
487 (%i3) let (a1*a2!, a1!, oneless, a2, a1);
488 (%o3) a1 a2! --> a1! where oneless(a2, a1)
490 (%i5) let (a1!/a1, (a1-1)!);
492 (%o5) --- --> (a1 - 1)!
494 (%i6) letsimp (n*m!*(n-1)!/m);
496 (%i7) let (sin(a)^2, 1 - cos(a)^2);
498 (%o7) sin (a) --> 1 - cos (a)
499 (%i8) letsimp (sin(x)^4);
501 (%o8) cos (x) - 2 cos (x) + 1
504 @c NEEDS ADDITIONAL EXAMPLES
505 @opencatbox{Categories:}
506 @category{Rules and patterns}
510 @c -----------------------------------------------------------------------------
512 @defvr {Option variable} letrat
513 Default value: @code{false}
515 When @code{letrat} is @code{false}, @code{letsimp} simplifies the
516 numerator and denominator of a ratio separately,
517 and does not simplify the quotient.
519 When @code{letrat} is @code{true},
520 the numerator, denominator, and their quotient are simplified in that order.
523 (%i1) matchdeclare (n, true)$
524 (%i2) let (n!/n, (n-1)!);
526 (%o2) -- --> (n - 1)!
529 (%i4) letsimp (a!/a);
534 (%i6) letsimp (a!/a);
538 @opencatbox{Categories:}
539 @category{Rules and patterns}
545 @c -----------------------------------------------------------------------------
547 @deffn {Function} letrules @
548 @fname{letrules} () @
549 @fname{letrules} (@var{package_name})
551 Displays the rules in a rule package.
552 @code{letrules ()} displays the rules in the current rule package.
553 @code{letrules (@var{package_name})} displays the rules in @var{package_name}.
555 The current rule package is named by @code{current_let_rule_package}.
556 If not otherwise specified, @code{current_let_rule_package}
557 defaults to @code{default_let_rule_package}.
559 See also @mrefcomma{disprule} which displays rules defined by @mref{tellsimp} and
560 @mrefdot{tellsimpafter}
561 @c WHAT ABOUT defmatch AND defrule ??
563 @opencatbox{Categories:}
564 @category{Rules and patterns}
568 @c -----------------------------------------------------------------------------
570 @deffn {Function} letsimp @
571 @fname{letsimp} (@var{expr}) @
572 @fname{letsimp} (@var{expr}, @var{package_name}) @
573 @fname{letsimp} (@var{expr}, @var{package_name_1}, @dots{}, @var{package_name_n})
575 Repeatedly applies the substitution rules defined by @code{let}
576 until no further change is made to @var{expr}.
578 @code{letsimp (@var{expr})} uses the rules from @code{current_let_rule_package}.
580 @code{letsimp (@var{expr}, @var{package_name})} uses the rules from
581 @var{package_name} without changing @code{current_let_rule_package}.
583 @code{letsimp (@var{expr}, @var{package_name_1}, ..., @var{package_name_n})}
584 is equivalent to @code{letsimp (@var{expr}, @var{package_name_1})},
585 followed by @code{letsimp (%, @var{package_name_2})}, and so on.
587 See also @mrefdot{let}
588 For other ways to do substitutions see also @mrefcomma{subst}
589 @mrefcomma{psubst} @mref{at} and @mrefdot{ratsubst}
591 @c NEEDS more or better EXAMPLES
594 @c e0: e(k) = -(9*y(k))/(5*z)-u(k-1)/(5*z)+(4*y(k))/(5*z^2)
595 @c +(3*u(k-1))/(5*z^2)+y(k)-(2*u(k-1))/5;
596 @c matchdeclare(h,any)$
597 @c let(u(h)/z,u(h-1));
598 @c let(y(h)/z, y(h-1));
603 (%i1) e0: e(k) = -(9*y(k))/(5*z)-u(k-1)/(5*z)+(4*y(k))/(5*z^2)
604 +(3*u(k-1))/(5*z^2)+y(k)-(2*u(k-1))/5;
605 9 y(k) u(k - 1) 4 y(k) 3 u(k - 1)
606 (%o1) e(k) = (- ------) - -------- + ------ + ---------- + y(k)
613 (%i2) matchdeclare(h,any)$
615 (%i3) let(u(h)/z,u(h-1));
617 (%o3) ---- --> u(h - 1)
621 (%i4) let(y(h)/z, y(h-1));
623 (%o4) ---- --> y(h - 1)
627 (%i5) e1:letsimp(e0);
628 2 u(k - 1) 3 u(k - 3) 4 y(k - 2)
629 (%o5) e(k) = (- ----------) + y(k) + ---------- + ----------
632 + (- --------) + (- ----------)
636 @opencatbox{Categories:}
637 @category{Rules and patterns}
641 @c -----------------------------------------------------------------------------
642 @anchor{let_rule_packages}
643 @defvr {Option variable} let_rule_packages
644 Default value: @code{[default_let_rule_package]}
646 @code{let_rule_packages} is a list of all user-defined let rule packages
647 plus the default package @code{default_let_rule_package}.
649 @opencatbox{Categories:}
650 @category{Rules and patterns}
654 @c -----------------------------------------------------------------------------
655 @anchor{matchdeclare}
656 @deffn {Function} matchdeclare (@var{a_1}, @var{pred_1}, @dots{}, @var{a_n}, @var{pred_n})
658 Associates a predicate @var{pred_k}
659 with a variable or list of variables @var{a_k}
660 so that @var{a_k} matches expressions
661 for which the predicate returns anything other than @code{false}.
663 A predicate is the name of a function,
664 or a lambda expression,
665 or a function call or lambda call missing the last argument,
666 or @code{true} or @code{all}.
667 Any expression matches @code{true} or @code{all}.
668 If the predicate is specified as a function call or lambda call,
669 the expression to be tested is appended to the list of arguments;
670 the arguments are evaluated at the time the match is evaluated.
671 Otherwise, the predicate is specified as a function name or lambda expression,
672 and the expression to be tested is the sole argument.
673 A predicate function need not be defined when @code{matchdeclare} is called;
674 the predicate is not evaluated until a match is attempted.
676 A predicate may return a Boolean expression as well as @code{true} or
677 @code{false}. Boolean expressions are evaluated by @code{is} within the
678 constructed rule function, so it is not necessary to call @code{is} within the
681 If an expression satisfies a match predicate, the match variable is assigned the
682 expression, except for match variables which are operands of addition @code{+}
683 or multiplication @code{*}. Only addition and multiplication are handled
684 specially; other n-ary operators (both built-in and user-defined) are treated
685 like ordinary functions.
686 @c WOULD BE GREAT TO EXTEND PART+/PART* PROCESSING TO ALL N-ARY OPERATORS
688 In the case of addition and multiplication, the match variable may be assigned a
689 single expression which satisfies the match predicate, or a sum or product
690 (respectively) of such expressions. Such multiple-term matching is greedy:
691 predicates are evaluated in the order in which their associated variables
692 appear in the match pattern, and a term which satisfies more than one predicate
693 is taken by the first predicate which it satisfies. Each predicate is tested
694 against all operands of the sum or product before the next predicate is
695 evaluated. In addition, if 0 or 1 (respectively) satisfies a match predicate,
696 and there are no other terms which satisfy the predicate, 0 or 1 is assigned to
697 the match variable associated with the predicate.
699 The algorithm for processing addition and multiplication patterns makes some
700 match results (for example, a pattern in which a "match anything" variable
701 appears) dependent on the ordering of terms in the match pattern and in the
702 expression to be matched. However, if all match predicates are mutually
703 exclusive, the match result is insensitive to ordering, as one match predicate
704 cannot accept terms matched by another.
706 Calling @code{matchdeclare} with a variable @var{a} as an argument changes the
707 @code{matchdeclare} property for @var{a}, if one was already declared; only the
708 most recent @code{matchdeclare} is in effect when a rule is defined. Later
709 changes to the @code{matchdeclare} property (via @code{matchdeclare} or
710 @code{remove}) do not affect existing rules.
712 @code{propvars (matchdeclare)} returns the list of all variables for which there
713 is a @code{matchdeclare} property. @code{printprops (@var{a}, matchdeclare)}
714 returns the predicate for variable @code{a}.
715 @code{printprops (all, matchdeclare)} returns the list of predicates for all
716 @code{matchdeclare} variables. @code{remove (@var{a}, matchdeclare)} removes
717 the @code{matchdeclare} property from @var{a}.
719 The functions @code{defmatch}, @code{defrule}, @code{tellsimp},
720 @code{tellsimpafter}, and @code{let} construct rules which test expressions
723 @code{matchdeclare} quotes its arguments.
724 @code{matchdeclare} always returns @code{done}.
728 A predicate is the name of a function,
729 or a lambda expression,
730 or a function call or lambda call missing the last argument,
731 or @code{true} or @code{all}.
734 @c matchdeclare (aa, integerp);
735 @c matchdeclare (bb, lambda ([x], x > 0));
736 @c matchdeclare (cc, freeof (%e, %pi, %i));
737 @c matchdeclare (dd, lambda ([x, y], gcd (x, y) = 1) (1728));
738 @c matchdeclare (ee, true);
739 @c matchdeclare (ff, all);
743 (%i1) matchdeclare (aa, integerp);
747 (%i2) matchdeclare (bb, lambda ([x], x > 0));
751 (%i3) matchdeclare (cc, freeof (%e, %pi, %i));
755 (%i4) matchdeclare (dd, lambda ([x, y], gcd (x, y) = 1) (1728));
759 (%i5) matchdeclare (ee, true);
763 (%i6) matchdeclare (ff, all);
768 If an expression satisfies a match predicate,
769 the match variable is assigned the expression.
772 @c matchdeclare (aa, integerp, bb, atom);
773 @c defrule (r1, bb^aa, ["integer" = aa, "atom" = bb]);
778 (%i1) matchdeclare (aa, integerp, bb, atom);
782 (%i2) defrule (r1, bb^aa, ["integer" = aa, "atom" = bb]);
784 (%o2) r1 : bb -> [integer = aa, atom = bb]
788 (%o3) [integer = 8, atom = %pi]
792 In the case of addition and multiplication, the match variable may be assigned
793 a single expression which satisfies the match predicate, or a sum or product
794 (respectively) of such expressions.
797 @c matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
798 @c defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
800 @c r1 (8 + a*b + sin(x));
801 @c defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
803 @c r2 (8 * (a + b) * sin(x));
807 (%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
811 (%i2) defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
813 (%o2) r1 : bb + aa -> [all atoms = aa, all nonatoms = bb]
816 (%i3) r1 (8 + a*b + sin(x));
817 (%o3) [all atoms = 8, all nonatoms = sin(x) + a b]
820 (%i4) defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
822 (%o4) r2 : aa bb -> [all atoms = aa, all nonatoms = bb]
825 (%i5) r2 (8 * (a + b) * sin(x));
826 (%o5) [all atoms = 8, all nonatoms = (b + a) sin(x)]
830 When matching arguments of @code{+} and @code{*},
831 if all match predicates are mutually exclusive,
832 the match result is insensitive to ordering,
833 as one match predicate cannot accept terms matched by another.
836 @c matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
837 @c defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
839 @c r1 (8 + a*b + %pi + sin(x) - c + 2^n);
840 @c defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
842 @c r2 (8 * (a + b) * %pi * sin(x) / c * 2^n);
846 (%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
850 (%i2) defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
852 (%o2) r1 : bb + aa -> [all atoms = aa, all nonatoms = bb]
855 (%i3) r1 (8 + a*b + %pi + sin(x) - c + 2^n);
857 (%o3) [all atoms = %pi + 8, all nonatoms = sin(x) + 2 - c + a b]
860 (%i4) defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
862 (%o4) r2 : aa bb -> [all atoms = aa, all nonatoms = bb]
865 (%i5) r2 (8 * (a + b) * %pi * sin(x) / c * 2^n);
868 (%o5) [all atoms = %pi, all nonatoms = ---------------------]
873 The functions @code{propvars} and @code{printprops} return information about
877 @c matchdeclare ([aa, bb, cc], atom, [dd, ee], integerp);
878 @c matchdeclare (ff, floatnump, gg, lambda ([x], x > 100));
879 @c propvars (matchdeclare);
880 @c printprops (ee, matchdeclare);
881 @c printprops (gg, matchdeclare);
882 @c printprops (all, matchdeclare);
886 (%i1) matchdeclare ([aa, bb, cc], atom, [dd, ee], integerp);
890 (%i2) matchdeclare (ff, floatnump, gg, lambda ([x], x > 100));
894 (%i3) propvars (matchdeclare);
895 (%o3) [aa, bb, cc, dd, ee, ff, gg]
898 (%i4) printprops (ee, matchdeclare);
902 (%i5) printprops (gg, matchdeclare);
903 (%o5) [lambda([x], x > 100, gg)]
906 (%i6) printprops (all, matchdeclare);
907 (%o6) [lambda([x], x > 100, gg), floatnump(ff), integerp(ee),
908 integerp(dd), atom(cc), atom(bb), atom(aa)]
912 @opencatbox{Categories:}
913 @category{Rules and patterns}
914 @category{Declarations and inferences}
920 @c -----------------------------------------------------------------------------
921 @anchor{maxapplydepth}
922 @defvr {Option variable} maxapplydepth
925 @code{maxapplydepth} is the maximum depth to which @code{apply1}
926 and @code{apply2} will delve.
928 @opencatbox{Categories:}
929 @category{Function application}
935 @c -----------------------------------------------------------------------------
936 @anchor{maxapplyheight}
937 @defvr {Option variable} maxapplyheight
940 @code{maxapplyheight} is the maximum height to which @code{applyb1}
941 will reach before giving up.
943 @opencatbox{Categories:}
944 @category{Function application}
948 @c NEEDS CLARIFICATION AND EXAMPLES
950 @c -----------------------------------------------------------------------------
952 @deffn {Function} remlet @
953 @fname{remlet} (@var{prod}, @var{name}) @
955 @fname{remlet} (all) @
956 @fname{remlet} (all, @var{name})
958 Deletes the substitution rule, @code{prod --> repl}, most
959 recently defined by the @code{let} function. If name is supplied the rule is
960 deleted from the rule package name.
962 @code{remlet()} and @code{remlet(all)} delete all substitution rules from the
963 current rule package. If the name of a rule package is supplied, e.g.
964 @code{remlet (all, @var{name})}, the rule package @var{name} is also deleted.
966 If a substitution is to be changed using the same
967 product, @code{remlet} need not be called, just redefine the substitution
968 using the same product (literally) with the @code{let} function and the new
969 replacement and/or predicate name. Should @code{remlet (@var{prod})} now be
970 called the original substitution rule is revived.
972 See also @mrefcomma{remrule} which removes a rule defined by @mref{tellsimp} or
973 @mrefdot{tellsimpafter}
975 @opencatbox{Categories:}
976 @category{Rules and patterns}
980 @c -----------------------------------------------------------------------------
982 @deffn {Function} remrule @
983 @fname{remrule} (@var{op}, @var{rulename}) @
984 @fname{remrule} (@var{op}, all)
986 Removes rules defined by @code{tellsimp} or @code{tellsimpafter}.
988 @code{remrule (@var{op}, @var{rulename})}
989 removes the rule with the name @var{rulename} from the operator @var{op}.
990 When @var{op} is a built-in or user-defined operator
991 (as defined by @code{infix}, @code{prefix}, etc.),
992 @var{op} and @var{rulename} must be enclosed in double quote marks.
994 @code{remrule (@var{op}, all)} removes all rules for the operator @var{op}.
996 See also @mrefcomma{remlet} which removes a rule defined by @mrefdot{let}
1001 @c tellsimp (foo (aa, bb), bb - aa);
1002 @c tellsimpafter (aa + bb, special_add (aa, bb));
1004 @c tellsimp (aa @@ bb, bb/aa);
1005 @c tellsimpafter (quux (%pi, %e), %pi - %e);
1006 @c tellsimpafter (quux (%e, %pi), %pi + %e);
1007 @c [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
1009 @c remrule (foo, foorule1);
1010 @c remrule ("+", ?\+rule1);
1011 @c remrule ("@@", ?\@\@rule1);
1012 @c remrule (quux, all);
1013 @c [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
1018 (%i1) tellsimp (foo (aa, bb), bb - aa);
1019 (%o1) [foorule1, false]
1022 (%i2) tellsimpafter (aa + bb, special_add (aa, bb));
1023 (%o2) [+rule1, simplus]
1026 (%i3) infix ("@@@@");
1030 (%i4) tellsimp (aa @@@@ bb, bb/aa);
1031 (%o4) [@@@@rule1, false]
1034 (%i5) tellsimpafter (quux (%pi, %e), %pi - %e);
1035 (%o5) [quuxrule1, false]
1038 (%i6) tellsimpafter (quux (%e, %pi), %pi + %e);
1039 (%o6) [quuxrule2, quuxrule1, false]
1042 (%i7) [foo (aa, bb), aa + bb, aa @@@@ bb, quux (%pi, %e),
1045 (%o7) [bb - aa, special_add(aa, bb), --, %pi - %e, %pi + %e]
1049 (%i8) remrule (foo, foorule1);
1053 (%i9) remrule ("+", ?\+rule1);
1057 (%i10) remrule ("@@@@", ?\@@\@@rule1);
1061 (%i11) remrule (quux, all);
1065 (%i12) [foo (aa, bb), aa + bb, aa @@@@ bb, quux (%pi, %e),
1067 (%o12) [foo(aa, bb), bb + aa, aa @@@@ bb, quux(%pi, %e),
1072 @opencatbox{Categories:}
1073 @category{Rules and patterns}
1077 @c NEEDS EXPANSION OR MAYBE JUST APPROPRIATE REFS TO tellsimpafter
1079 @c -----------------------------------------------------------------------------
1081 @deffn {Function} tellsimp (@var{pattern}, @var{replacement})
1083 is similar to @code{tellsimpafter} but places
1084 new information before old so that it is applied before the built-in
1085 simplification rules.
1087 @code{tellsimp} is used when it is important to modify
1088 the expression before the simplifier works on it, for instance if the
1089 simplifier "knows" something about the expression, but what it returns
1090 is not to your liking.
1091 If the simplifier "knows" something about the
1092 main operator of the expression, but is simply not doing enough for
1093 you, you probably want to use @code{tellsimpafter}.
1095 The pattern may not be a
1096 sum, product, single variable, or number.
1098 The system variable @code{rules} is the list of rules defined by
1099 @code{defrule}, @code{defmatch}, @code{tellsimp}, and @code{tellsimpafter}.
1104 (%i1) matchdeclare (x, freeof (%i));
1106 (%i2) %iargs: false$
1107 (%i3) tellsimp (sin(%i*x), %i*sinh(x));
1108 (%o3) [sinrule1, simp-%sin]
1109 (%i4) trigexpand (sin (%i*y + x));
1110 (%o4) sin(x) cos(%i y) + %i cos(x) sinh(y)
1112 (%i6) errcatch(0^0);
1114 0 has been generated
1116 (%i7) ev (tellsimp (0^0, 1), simp: false);
1117 (%o7) [^rule1, simpexpt]
1120 (%i9) remrule ("^", %th(2)[1]);
1122 (%i10) tellsimp (sin(x)^2, 1 - cos(x)^2);
1123 (%o10) [^rule2, simpexpt]
1124 (%i11) (1 + sin(x))^2;
1129 (%o12) 2 sin(x) - cos (x) + 2
1133 (%i14) kill (rules);
1135 (%i15) matchdeclare (a, true);
1137 (%i16) tellsimp (sin(a)^2, 1 - cos(a)^2);
1138 (%o16) [^rule3, simpexpt]
1144 @opencatbox{Categories:}
1145 @category{Rules and patterns}
1149 @c -----------------------------------------------------------------------------
1150 @anchor{tellsimpafter}
1151 @deffn {Function} tellsimpafter (@var{pattern}, @var{replacement})
1153 Defines a simplification rule which the Maxima simplifier applies after built-in
1154 simplification rules. @var{pattern} is an expression, comprising pattern
1155 variables (declared by @code{matchdeclare}) and other atoms and operators,
1156 considered literals for the purpose of pattern matching. @var{replacement} is
1157 substituted for an actual expression which matches @var{pattern}; pattern
1158 variables in @var{replacement} are assigned the values matched in the actual
1161 @var{pattern} may be any nonatomic expression in which the main operator is not
1162 a pattern variable; the simplification rule is associated with the main
1163 operator. The names of functions (with one exception, described below), lists,
1164 and arrays may appear in @var{pattern} as the main operator only as literals
1165 (not pattern variables); this rules out expressions such as @code{aa(x)} and
1166 @code{bb[y]} as patterns, if @code{aa} and @code{bb} are pattern variables.
1167 Names of functions, lists, and arrays which are pattern variables may appear as
1168 operators other than the main operator in @var{pattern}.
1170 There is one exception to the above rule concerning names of functions.
1171 The name of a subscripted function in an expression such as @code{aa[x](y)}
1172 may be a pattern variable, because the main operator is not @code{aa} but rather
1173 the Lisp atom @code{mqapply}. This is a consequence of the representation of
1174 expressions involving subscripted functions.
1176 @c LET'S NOT GO INTO DETAILS ABOUT MAIN OPERATORS HERE; BUT PRESERVE THIS FOR REFERENCE
1177 @c The main operator of an expression @code{expr} is @code{caar $expr}.
1178 @c For most kinds of expressions,
1179 @c the main operator is the operator returned by @code{op (@var{pattern})};
1180 @c the sole exception is the operator @code{mqapply},
1181 @c which appears in indexed function expressions (e.g., @code{foo[i](x)}).
1183 @c NEED TO REVIEW THIS PARAGRAPH FOR ACCURACY
1184 Simplification rules are applied after evaluation
1185 (if not suppressed through quotation or the flag @code{noeval}).
1186 Rules established by @code{tellsimpafter} are applied in the order they were
1187 defined, and after any built-in rules.
1188 Rules are applied bottom-up, that is,
1189 applied first to subexpressions before application to the whole expression.
1190 @c NO IT IS MORE COMPLICATED THAN THIS, ALTHOUGH IN SOME CIRCUMSTANCE IT APPEARS TO BE THE CASE:
1191 @c For a given expression, at most one rule per operator is applied.
1192 It may be necessary to repeatedly simplify a result (for example, via the
1193 quote-quote operator @code{'@w{}'} or the flag @code{infeval})
1194 to ensure that all rules are applied.
1196 Pattern variables are treated as local variables in simplification rules.
1197 Once a rule is defined, the value of a pattern variable
1198 does not affect the rule, and is not affected by the rule.
1199 An assignment to a pattern variable which results from a successful rule match
1200 does not affect the current assignment (or lack of it) of the pattern variable.
1201 However, as with all atoms in Maxima, the properties of pattern variables (as
1202 declared by @code{put} and related functions) are global.
1204 The rule constructed by @code{tellsimpafter} is named after the main operator of
1205 @var{pattern}. Rules for built-in operators, and user-defined operators defined
1206 by @code{infix}, @code{prefix}, @code{postfix}, @code{matchfix}, and
1207 @code{nofix}, have names which are Lisp identifiers.
1208 @c SLIGHTLY TOO MUCH DETAIL
1209 @c (that is, the name is written with a leading question mark @code{?} in Maxima).
1210 Rules for other functions have names which are Maxima identifiers.
1211 @c SLIGHTLY TOO MUCH DETAIL
1212 @c (that is, the name begins with dollar sign @code{$}).
1214 The treatment of noun and verb forms is slightly confused. @c THIS IS A BUG.
1215 If a rule is defined for a noun (or verb) form
1216 and a rule for the corresponding verb (or noun) form already exists,
1217 the newly-defined rule applies to both forms (noun and verb).
1218 If a rule for the corresponding verb (or noun) form does not exist,
1219 the newly-defined rule applies only to the noun (or verb) form.
1221 The rule constructed by @code{tellsimpafter} is an ordinary Lisp function.
1222 If the name of the rule is @code{$foorule1},
1223 the construct @code{:lisp (trace $foorule1)} traces the function,
1224 and @code{:lisp (symbol-function '$foorule1)} displays its definition.
1226 @code{tellsimpafter} quotes its arguments.
1227 @code{tellsimpafter} returns the list of rules for the main operator of
1228 @var{pattern}, including the newly established rule.
1229 @c WHAT IS simpfoo THAT SOMETIMES APPEARS, AND WHY DOES false SOMETIMES APPEAR IN RETURN VALUE ??
1231 See also @mrefcomma{matchdeclare} @mrefcomma{defmatch} @mrefcomma{defrule} @mrefcomma{tellsimp}
1232 @mrefcomma{let} @mrefcomma{kill} @mref{remrule} and @mrefdot{clear_rules}
1236 @var{pattern} may be any nonatomic expression in which the
1237 main operator is not a pattern variable.
1240 @c matchdeclare (aa, atom, [ll, mm], listp, xx, true)$
1241 @c tellsimpafter (sin (ll), map (sin, ll));
1242 @c sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi);
1243 @c tellsimpafter (ll^mm, map ("^", ll, mm));
1244 @c [a, b, c]^[1, 2, 3];
1245 @c tellsimpafter (foo (aa (xx)), aa (foo (xx)));
1246 @c foo (bar (u - v));
1249 (%i1) matchdeclare (aa, atom, [ll, mm], listp, xx, true)$
1251 (%i2) tellsimpafter (sin (ll), map (sin, ll));
1252 (%o2) [sinrule1, simp-%sin]
1255 (%i3) sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi);
1257 (%o3) [-, -------, -------, 1, 0]
1261 (%i4) tellsimpafter (ll^mm, map ("^", ll, mm));
1262 (%o4) [^rule1, simpexpt]
1265 (%i5) [a, b, c]^[1, 2, 3];
1270 (%i6) tellsimpafter (foo (aa (xx)), aa (foo (xx)));
1271 (%o6) [foorule1, false]
1274 (%i7) foo (bar (u - v));
1275 (%o7) bar(foo(u - v))
1279 Rules are applied in the order they were defined.
1280 If two rules can match an expression,
1281 the rule which was defined first is applied.
1284 @c matchdeclare (aa, integerp);
1285 @c tellsimpafter (foo (aa), bar_1 (aa));
1286 @c tellsimpafter (foo (aa), bar_2 (aa));
1291 (%i1) matchdeclare (aa, integerp);
1295 (%i2) tellsimpafter (foo (aa), bar_1 (aa));
1296 (%o2) [foorule1, false]
1299 (%i3) tellsimpafter (foo (aa), bar_2 (aa));
1300 (%o3) [foorule2, foorule1, false]
1308 Pattern variables are treated as local variables in simplification rules.
1309 (Compare to @code{defmatch}, which treats pattern variables as global
1313 @c matchdeclare (aa, integerp, bb, atom);
1314 @c tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1321 (%i1) matchdeclare (aa, integerp, bb, atom);
1325 (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1326 (%o2) [foorule1, false]
1334 (%o4) bar(aa = 42, bb = %e)
1342 As with all atoms, properties of pattern variables are global even though values
1343 are local. In this example, an assignment property is declared via
1344 @code{define_variable}. This is a property of the atom @code{bb} throughout
1348 @c matchdeclare (aa, integerp, bb, atom);
1349 @c tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1351 @c define_variable (bb, true, boolean);
1356 (%i1) matchdeclare (aa, integerp, bb, atom);
1360 (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1361 (%o2) [foorule1, false]
1365 (%o3) bar(aa = 42, bb = %e)
1368 (%i4) define_variable (bb, true, boolean);
1373 translator: bb was declared with mode boolean, but it has value:
1375 -- an error. To debug this try: debugmode(true);
1379 Rules are named after main operators.
1380 Names of rules for built-in and user-defined operators are Lisp identifiers,
1381 while names for other functions are Maxima identifiers.
1384 @c tellsimpafter (foo (%pi + %e), 3*%pi);
1385 @c tellsimpafter (foo (%pi * %e), 17*%e);
1386 @c tellsimpafter (foo (%i ^ %e), -42*%i);
1387 @c tellsimpafter (foo (9) + foo (13), quux (22));
1388 @c tellsimpafter (foo (9) * foo (13), blurf (22));
1389 @c tellsimpafter (foo (9) ^ foo (13), mumble (22));
1391 @c foorule_name: first (%o1);
1392 @c plusrule_name: first (%o4);
1393 @c remrule (foo, foorule1);
1394 @c remrule ("^", ?\^rule1);
1399 (%i1) tellsimpafter (foo (%pi + %e), 3*%pi);
1400 (%o1) [foorule1, false]
1403 (%i2) tellsimpafter (foo (%pi * %e), 17*%e);
1404 (%o2) [foorule2, foorule1, false]
1407 (%i3) tellsimpafter (foo (%i ^ %e), -42*%i);
1408 (%o3) [foorule3, foorule2, foorule1, false]
1411 (%i4) tellsimpafter (foo (9) + foo (13), quux (22));
1412 (%o4) [+rule1, simplus]
1415 (%i5) tellsimpafter (foo (9) * foo (13), blurf (22));
1416 (%o5) [*rule1, simptimes]
1419 (%i6) tellsimpafter (foo (9) ^ foo (13), mumble (22));
1420 (%o6) [^rule1, simpexpt]
1424 (%o7) [foorule1, foorule2, foorule3, +rule1, *rule1, ^rule1]
1427 (%i8) foorule_name: first (%o1);
1431 (%i9) plusrule_name: first (%o4);
1435 (%i10) remrule (foo, foorule1);
1439 (%i11) remrule ("^", ?\^rule1);
1444 (%o12) [foorule2, foorule3, +rule1, *rule1]
1448 A worked example: anticommutative multiplication.
1451 @c gt (i, j) := integerp(j) and i < j;
1452 @c matchdeclare (i, integerp, j, gt(i));
1453 @c tellsimpafter (s[i]^^2, 1);
1454 @c tellsimpafter (s[i] . s[j], -s[j] . s[i]);
1455 @c s[1] . (s[1] + s[2]);
1457 @c factor (expand (sum (s[i], i, 0, 9)^^5));
1461 (%i1) gt (i, j) := integerp(j) and i < j;
1462 (%o1) gt(i, j) := integerp(j) and (i < j)
1465 (%i2) matchdeclare (i, integerp, j, gt(i));
1469 (%i3) tellsimpafter (s[i]^^2, 1);
1470 (%o3) [^^rule1, simpncexpt]
1473 (%i4) tellsimpafter (s[i] . s[j], -s[j] . s[i]);
1474 (%o4) [.rule1, simpnct]
1477 (%i5) s[1] . (s[1] + s[2]);
1487 (%i7) factor (expand (sum (s[i], i, 0, 9)^^5));
1488 (%o7) 100 (s + s + s + s + s + s + s + s + s + s )
1493 @opencatbox{Categories:}
1494 @category{Rules and patterns}
1498 @c -----------------------------------------------------------------------------
1499 @anchor{clear_rules}
1500 @deffn {Function} clear_rules ()
1502 Executes @code{kill (rules)} and then resets the next rule number to 1
1503 for addition @code{+}, multiplication @code{*}, and exponentiation @code{^}.
1505 @opencatbox{Categories:}
1506 @category{Rules and patterns}