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}
29 @category{Simplification} @category{Rules and patterns}
32 @c end concepts Rules and Patterns
34 @c -----------------------------------------------------------------------------
35 @node Functions and Variables for Rules and Patterns, , Introduction to Rules and Patterns, Rules and Patterns
36 @section Functions and Variables for Rules and Patterns
37 @c -----------------------------------------------------------------------------
39 @c NEEDS CLARIFICATION AND EXAMPLES
41 @c -----------------------------------------------------------------------------
43 @deffn {Function} apply1 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
45 Repeatedly applies @var{rule_1} to
46 @var{expr} until it fails, then repeatedly applies the same rule to all
47 subexpressions of @var{expr}, left to right, until @var{rule_1} has failed
48 on all subexpressions. Call the result of transforming @var{expr} in this
49 manner @var{expr_2}. Then @var{rule_2} is applied in the same fashion
50 starting at the top of @var{expr_2}. When @var{rule_n} fails on the final
51 subexpression, the result is returned.
53 @code{maxapplydepth} is the depth of the deepest subexpressions processed by
54 @code{apply1} and @code{apply2}.
56 See also @code{applyb1}, @code{apply2}, and @code{let}.
59 @category{Rules and patterns}
63 @c NEEDS CLARIFICATION AND EXAMPLES
65 @c -----------------------------------------------------------------------------
67 @deffn {Function} apply2 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
69 If @var{rule_1} fails on a given subexpression, then @var{rule_2} is
70 repeatedly applied, etc. Only if all rules fail on a given
71 subexpression is the whole set of rules repeatedly applied to the next
72 subexpression. If one of the rules succeeds, then the same
73 subexpression is reprocessed, starting with the first rule.
75 @code{maxapplydepth} is the depth of the deepest subexpressions processed by
76 @code{apply1} and @code{apply2}.
78 See also @code{apply1} and @code{let}.
81 @category{Rules and patterns}
85 @c NEEDS CLARIFICATION AND EXAMPLES
87 @c -----------------------------------------------------------------------------
89 @deffn {Function} applyb1 (@var{expr}, @var{rule_1}, @dots{}, @var{rule_n})
91 Repeatedly applies @var{rule_1} to the deepest subexpression of @var{expr}
92 until it fails, then repeatedly applies the same rule one level higher (i.e.,
93 larger subexpressions), until @var{rule_1} has failed on the top-level
94 expression. Then @var{rule_2} is applied in the same fashion to the result of
95 @var{rule_1}. After @var{rule_n} has been applied to the top-level expression,
96 the result is returned.
98 @code{applyb1} is similar to @code{apply1} but works from
99 the bottom up instead of from the top down.
101 @code{maxapplyheight} is the maximum height which @code{applyb1} reaches
104 See also @code{apply1}, @code{apply2}, and @code{let}.
107 @category{Rules and patterns}
111 @c -----------------------------------------------------------------------------
112 @anchor{current_let_rule_package}
113 @defvr {Option variable} current_let_rule_package
114 Default value: @code{default_let_rule_package}
116 @code{current_let_rule_package} is the name of the rule package that is used by
117 functions in the @code{let} package (@code{letsimp}, etc.) @c NEED TO GIVE AN EXPLICIT LIST HERE (NOT "ETC")
118 if no other rule package is specified.
119 This variable may be assigned the name of any rule package defined
120 via the @code{let} command.
122 If a call such as @code{letsimp (expr, rule_pkg_name)} is made,
123 the rule package @code{rule_pkg_name} is used for that function call only,
124 and the value of @code{current_let_rule_package} is not changed.
127 @category{Rules and patterns}
131 @c -----------------------------------------------------------------------------
132 @anchor{default_let_rule_package}
133 @defvr {Option variable} default_let_rule_package
134 @c DEFAULT BINDING OF default_let_rule_package IS default_let_rule_package (BOUND TO ITSELF)
135 Default value: @code{default_let_rule_package}
137 @c THIS IS SORT OF CONFUSING. PROBABLY NEED TO GIVE MORE DETAIL HERE
138 @code{default_let_rule_package} is the name of the rule package used when one
139 is not explicitly set by the user with @code{let} or by changing the value of
140 @code{current_let_rule_package}.
143 @category{Rules and patterns}
147 @c -----------------------------------------------------------------------------
149 @deffn {Function} defmatch @
150 @fname{defmatch} (@var{progname}, @var{pattern}, @var{x_1}, @dots{}, @var{x_n}) @
151 @fname{defmatch} (@var{progname}, @var{pattern})
153 Defines a function @code{@var{progname}(@var{expr}, @var{x_1}, ..., @var{x_n})}
154 which tests @var{expr} to see if it matches @var{pattern}.
156 @var{pattern} is an expression containing the pattern arguments @var{x_1},
157 @dots{}, @var{x_n} (if any) and some pattern variables (if any). The pattern
158 arguments are given explicitly as arguments to @code{defmatch} while the pattern
159 variables are declared by the @code{matchdeclare} function. Any variable not
160 declared as a pattern variable in @code{matchdeclare} or as a pattern argument
161 in @code{defmatch} matches only itself.
163 The first argument to the created function @var{progname} is an expression to be
164 matched against the pattern and the other arguments are the actual arguments
165 which correspond to the dummy variables @var{x_1}, @dots{}, @var{x_n} in the
168 If the match is successful, @var{progname} returns a list of equations whose
169 left sides are the pattern arguments and pattern variables, and whose right
170 sides are the subexpressions which the pattern arguments and variables matched.
171 The pattern variables, but not the pattern arguments, are assigned the
172 subexpressions they match. If the match fails, @var{progname} returns
175 A literal pattern (that is, a pattern which contains neither pattern arguments
176 nor pattern variables) returns @code{true} if the match succeeds.
178 See also @code{matchdeclare}, @code{defrule}, @code{tellsimp}, and
179 @code{tellsimpafter}.
183 Define a function @code{linearp(expr, x)} which
184 tests @code{expr} to see if it is of the form @code{a*x + b}
185 such that @code{a} and @code{b} do not contain @code{x} and @code{a} is nonzero.
186 This match function matches expressions which are linear in any variable,
187 because the pattern argument @code{x} is given to @code{defmatch}.
188 @c HOW HARD WILL MAXIMA TRY TO COLLECT TERMS AND DO OTHER MUNGEING TO FIT THE PATTERN ??
191 @c matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
193 @c defmatch (linearp, a*x + b, x);
194 @c linearp (3*z + (y + 1)*z + y^2, z);
200 (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
203 (%i2) defmatch (linearp, a*x + b, x);
205 (%i3) linearp (3*z + (y + 1)*z + y^2, z);
207 (%o3) [b = y , a = y + 4, x = z]
217 Define a function @code{linearp(expr)} which tests @code{expr}
218 to see if it is of the form @code{a*x + b}
219 such that @code{a} and @code{b} do not contain @code{x} and @code{a} is nonzero.
220 This match function only matches expressions linear in @code{x},
221 not any other variable, because no pattern argument is given to @code{defmatch}.
224 @c matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
226 @c defmatch (linearp, a*x + b);
227 @c linearp (3*z + (y + 1)*z + y^2);
228 @c linearp (3*x + (y + 1)*x + y^2);
231 (%i1) matchdeclare (a, lambda ([e], e#0 and freeof(x, e)), b,
234 (%i2) defmatch (linearp, a*x + b);
236 (%i3) linearp (3*z + (y + 1)*z + y^2);
238 (%i4) linearp (3*x + (y + 1)*x + y^2);
240 (%o4) [b = y , a = y + 4]
243 Define a function @code{checklimits(expr)} which tests @code{expr}
244 to see if it is a definite integral.
247 @c matchdeclare ([a, f], true);
248 @c constinterval (l, h) := constantp (h - l);
249 @c matchdeclare (b, constinterval (a));
250 @c matchdeclare (x, atom);
252 @c defmatch (checklimits, 'integrate (f, x, a, b));
254 @c 'integrate (sin(t), t, %pi + x, 2*%pi + x);
258 (%i1) matchdeclare ([a, f], true);
260 (%i2) constinterval (l, h) := constantp (h - l);
261 (%o2) constinterval(l, h) := constantp(h - l)
262 (%i3) matchdeclare (b, constinterval (a));
264 (%i4) matchdeclare (x, atom);
268 (%i6) defmatch (checklimits, 'integrate (f, x, a, b));
272 (%i8) 'integrate (sin(t), t, %pi + x, 2*%pi + x);
282 (%i9) checklimits (%);
283 (%o9) [b = x + 2 %pi, a = x + %pi, x = t, f = sin(t)]
287 @category{Rules and patterns}
291 @c NEEDS CLARIFICATION AND EXAMPLES
293 @c -----------------------------------------------------------------------------
295 @deffn {Function} defrule (@var{rulename}, @var{pattern}, @var{replacement})
297 Defines and names a replacement rule for the given pattern. If the rule named
298 @var{rulename} is applied to an expression (by @code{apply1}, @code{applyb1}, or
299 @code{apply2}), every subexpression matching the pattern will be replaced by the
300 replacement. All variables in the replacement which have been
301 assigned values by the pattern match are assigned those values in the
302 replacement which is then simplified.
304 The rules themselves can be
305 treated as functions which transform an expression by one
306 operation of the pattern match and replacement.
307 If the match fails, the rule function returns @code{false}.
310 @category{Rules and patterns}
316 @c -----------------------------------------------------------------------------
318 @deffn {Function} disprule @
319 @fname{disprule} (@var{rulename_1}, @dots{}, @var{rulename_2}) @
320 @fname{disprule} (all)
322 Display rules with the names @var{rulename_1}, @dots{}, @var{rulename_n},
323 as returned by @code{defrule}, @code{tellsimp}, or @code{tellsimpafter},
324 or a pattern defined by @code{defmatch}.
325 Each rule is displayed with an intermediate expression label (@code{%t}).
327 @code{disprule (all)} displays all rules.
329 @code{disprule} quotes its arguments.
330 @code{disprule} returns the list of intermediate expression labels corresponding
331 to the displayed rules.
333 See also @code{letrules}, which displays rules defined by @code{let}.
338 @c tellsimpafter (foo (x, y), bar (x) + baz (y));
339 @c tellsimpafter (x + y, special_add (x, y));
340 @c defmatch (quux, mumble (x));
341 @c disprule (foorule1, "+rule1", quux);
345 (%i1) tellsimpafter (foo (x, y), bar (x) + baz (y));
346 (%o1) [foorule1, false]
347 (%i2) tellsimpafter (x + y, special_add (x, y));
348 (%o2) [+rule1, simplus]
349 (%i3) defmatch (quux, mumble (x));
351 (%i4) disprule (foorule1, "+rule1", quux);
352 (%t4) foorule1 : foo(x, y) -> baz(y) + bar(x)
354 (%t5) +rule1 : y + x -> special_add(x, y)
356 (%t6) quux : mumble(x) -> []
358 (%o6) [%t4, %t5, %t6]
360 (%o6) [foorule1 : foo(x, y) -> baz(y) + bar(x),
361 +rule1 : y + x -> special_add(x, y), quux : mumble(x) -> []]
365 @category{Rules and patterns} @category{Display functions}
369 @c -----------------------------------------------------------------------------
371 @deffn {Function} let @
372 @fname{let} (@var{prod}, @var{repl}, @var{predname}, @var{arg_1}, @dots{}, @var{arg_n}) @
373 @fname{let} ([@var{prod}, @var{repl}, @var{predname}, @var{arg_1}, @dots{}, @var{arg_n}], @var{package_name})
375 Defines a substitution rule for @code{letsimp} such that @var{prod} is replaced
376 by @var{repl}. @var{prod} is a product of positive or negative powers of the
381 Atoms which @code{letsimp} will search for literally unless previous to calling
382 @code{letsimp} the @code{matchdeclare} function is used to associate a
383 predicate with the atom. In this case @code{letsimp} will match the atom to
384 any term of a product satisfying the predicate.
386 Kernels such as @code{sin(x)}, @code{n!}, @code{f(x,y)}, etc. As with atoms
387 above @code{letsimp} will look for a literal match unless @code{matchdeclare}
388 is used to associate a predicate with the argument of the kernel.
391 A term to a positive power will only match a term having at least that
392 power. A term to a negative power
393 on the other hand will only match a term with a power at least as
394 negative. In the case of negative powers in @var{prod} the switch
395 @code{letrat} must be set to @code{true}.
396 See also @code{letrat}.
398 If a predicate is included in the @code{let} function followed by a list of
399 arguments, a tentative match (i.e. one that would be accepted if the predicate
400 were omitted) is accepted only if @code{predname (arg_1', ..., arg_n')}
401 evaluates to @code{true} where @var{arg_i'} is the value matched to @var{arg_i}.
402 The @var{arg_i} may be the name of any atom or the argument of any kernel
403 appearing in @var{prod}.
404 @var{repl} may be any rational expression. @c ONLY RATIONAL -- REALLY ??
405 If any of the atoms or arguments from @var{prod} appear in @var{repl} the
406 appropriate substitutions are made. @c SPELL OUT "APPROPRIATE" IN THIS CONTEXT
408 The global flag @code{letrat} controls the simplification of quotients by
409 @code{letsimp}. When @code{letrat} is @code{false}, @code{letsimp} simplifies
410 the numerator and denominator of @var{expr} separately, and does not simplify
411 the quotient. Substitutions such as @code{n!/n} goes to @code{(n-1)!} then
412 fail. When @code{letrat} is @code{true}, then the numerator, denominator, and
413 the quotient are simplified in that order.
415 These substitution functions allow you to work with several rule packages at
416 once. Each rule package can contain any number of @code{let} rules and is
417 referenced by a user-defined name. The command @code{let ([@var{prod},
418 @var{repl}, @var{predname}, @var{arg_1}, ..., @var{arg_n}], @var{package_name})}
419 adds the rule @var{predname} to the rule package @var{package_name}. The
420 command @code{letsimp (@var{expr}, @var{package_name})} applies the rules in
421 @var{package_name}. @code{letsimp (@var{expr}, @var{package_name1},
422 @var{package_name2}, ...)} is equivalent to @code{letsimp (@var{expr},
423 @var{package_name1})} followed by @code{letsimp (%, @var{package_name2})},
426 @code{current_let_rule_package} is the name of the rule package that is
427 presently being used. This variable may be assigned the name of any rule
428 package defined via the @code{let} command. Whenever any of the functions
429 comprising the @code{let} package are called with no package name, the package
430 named by @code{current_let_rule_package} is used. If a call such as
431 @code{letsimp (@var{expr}, @var{rule_pkg_name})} is made, the rule package
432 @var{rule_pkg_name} is used for that @code{letsimp} command only, and
433 @code{current_let_rule_package} is not changed. If not otherwise specified,
434 @code{current_let_rule_package} defaults to @code{default_let_rule_package}.
437 (%i1) matchdeclare ([a, a1, a2], true)$
438 (%i2) oneless (x, y) := is (x = y-1)$
439 (%i3) let (a1*a2!, a1!, oneless, a2, a1);
440 (%o3) a1 a2! --> a1! where oneless(a2, a1)
442 (%i5) let (a1!/a1, (a1-1)!);
444 (%o5) --- --> (a1 - 1)!
446 (%i6) letsimp (n*m!*(n-1)!/m);
448 (%i7) let (sin(a)^2, 1 - cos(a)^2);
450 (%o7) sin (a) --> 1 - cos (a)
451 (%i8) letsimp (sin(x)^4);
453 (%o8) cos (x) - 2 cos (x) + 1
456 @c NEEDS ADDITIONAL EXAMPLES
458 @category{Rules and patterns}
462 @c -----------------------------------------------------------------------------
464 @defvr {Option variable} letrat
465 Default value: @code{false}
467 When @code{letrat} is @code{false}, @code{letsimp} simplifies the
468 numerator and denominator of a ratio separately,
469 and does not simplify the quotient.
471 When @code{letrat} is @code{true},
472 the numerator, denominator, and their quotient are simplified in that order.
475 (%i1) matchdeclare (n, true)$
476 (%i2) let (n!/n, (n-1)!);
478 (%o2) -- --> (n - 1)!
481 (%i4) letsimp (a!/a);
486 (%i6) letsimp (a!/a);
491 @category{Rules and patterns}
497 @c -----------------------------------------------------------------------------
499 @deffn {Function} letrules @
500 @fname{letrules} () @
501 @fname{letrules} (@var{package_name})
503 Displays the rules in a rule package.
504 @code{letrules ()} displays the rules in the current rule package.
505 @code{letrules (@var{package_name})} displays the rules in @var{package_name}.
507 The current rule package is named by @code{current_let_rule_package}.
508 If not otherwise specified, @code{current_let_rule_package}
509 defaults to @code{default_let_rule_package}.
511 See also @code{disprule}, which displays rules defined by @code{tellsimp} and
512 @code{tellsimpafter}.
513 @c WHAT ABOUT defmatch AND defrule ??
516 @category{Rules and patterns}
520 @c -----------------------------------------------------------------------------
522 @deffn {Function} letsimp @
523 @fname{letsimp} (@var{expr}) @
524 @fname{letsimp} (@var{expr}, @var{package_name}) @
525 @fname{letsimp} (@var{expr}, @var{package_name_1}, @dots{}, @var{package_name_n})
527 Repeatedly applies the substitution rules defined by @code{let}
528 until no further change is made to @var{expr}.
530 @code{letsimp (@var{expr})} uses the rules from @code{current_let_rule_package}.
532 @code{letsimp (@var{expr}, @var{package_name})} uses the rules from
533 @var{package_name} without changing @code{current_let_rule_package}.
535 @code{letsimp (@var{expr}, @var{package_name_1}, ..., @var{package_name_n})}
536 is equivalent to @code{letsimp (@var{expr}, @var{package_name_1})},
537 followed by @code{letsimp (%, @var{package_name_2})}, and so on.
541 @category{Rules and patterns}
545 @c -----------------------------------------------------------------------------
546 @anchor{let_rule_packages}
547 @defvr {Option variable} let_rule_packages
548 Default value: @code{[default_let_rule_package]}
550 @code{let_rule_packages} is a list of all user-defined let rule packages
551 plus the default package @code{default_let_rule_package}.
554 @category{Rules and patterns}
558 @c -----------------------------------------------------------------------------
559 @anchor{matchdeclare}
560 @deffn {Function} matchdeclare (@var{a_1}, @var{pred_1}, @dots{}, @var{a_n}, @var{pred_n})
562 Associates a predicate @var{pred_k}
563 with a variable or list of variables @var{a_k}
564 so that @var{a_k} matches expressions
565 for which the predicate returns anything other than @code{false}.
567 A predicate is the name of a function,
568 or a lambda expression,
569 or a function call or lambda call missing the last argument,
570 or @code{true} or @code{all}.
571 Any expression matches @code{true} or @code{all}.
572 If the predicate is specified as a function call or lambda call,
573 the expression to be tested is appended to the list of arguments;
574 the arguments are evaluated at the time the match is evaluated.
575 Otherwise, the predicate is specified as a function name or lambda expression,
576 and the expression to be tested is the sole argument.
577 A predicate function need not be defined when @code{matchdeclare} is called;
578 the predicate is not evaluated until a match is attempted.
580 A predicate may return a Boolean expression as well as @code{true} or
581 @code{false}. Boolean expressions are evaluated by @code{is} within the
582 constructed rule function, so it is not necessary to call @code{is} within the
585 If an expression satisfies a match predicate, the match variable is assigned the
586 expression, except for match variables which are operands of addition @code{+}
587 or multiplication @code{*}. Only addition and multiplication are handled
588 specially; other n-ary operators (both built-in and user-defined) are treated
589 like ordinary functions.
590 @c WOULD BE GREAT TO EXTEND PART+/PART* PROCESSING TO ALL N-ARY OPERATORS
592 In the case of addition and multiplication, the match variable may be assigned a
593 single expression which satisfies the match predicate, or a sum or product
594 (respectively) of such expressions. Such multiple-term matching is greedy:
595 predicates are evaluated in the order in which their associated variables
596 appear in the match pattern, and a term which satisfies more than one predicate
597 is taken by the first predicate which it satisfies. Each predicate is tested
598 against all operands of the sum or product before the next predicate is
599 evaluated. In addition, if 0 or 1 (respectively) satisfies a match predicate,
600 and there are no other terms which satisfy the predicate, 0 or 1 is assigned to
601 the match variable associated with the predicate.
603 The algorithm for processing addition and multiplication patterns makes some
604 match results (for example, a pattern in which a "match anything" variable
605 appears) dependent on the ordering of terms in the match pattern and in the
606 expression to be matched. However, if all match predicates are mutually
607 exclusive, the match result is insensitive to ordering, as one match predicate
608 cannot accept terms matched by another.
610 Calling @code{matchdeclare} with a variable @var{a} as an argument changes the
611 @code{matchdeclare} property for @var{a}, if one was already declared; only the
612 most recent @code{matchdeclare} is in effect when a rule is defined. Later
613 changes to the @code{matchdeclare} property (via @code{matchdeclare} or
614 @code{remove}) do not affect existing rules.
616 @code{propvars (matchdeclare)} returns the list of all variables for which there
617 is a @code{matchdeclare} property. @code{printprops (@var{a}, matchdeclare)}
618 returns the predicate for variable @code{a}.
619 @code{printprops (all, matchdeclare)} returns the list of predicates for all
620 @code{matchdeclare} variables. @code{remove (@var{a}, matchdeclare)} removes
621 the @code{matchdeclare} property from @var{a}.
623 The functions @code{defmatch}, @code{defrule}, @code{tellsimp},
624 @code{tellsimpafter}, and @code{let} construct rules which test expressions
627 @code{matchdeclare} quotes its arguments.
628 @code{matchdeclare} always returns @code{done}.
632 A predicate is the name of a function,
633 or a lambda expression,
634 or a function call or lambda call missing the last argument,
635 or @code{true} or @code{all}.
638 @c matchdeclare (aa, integerp);
639 @c matchdeclare (bb, lambda ([x], x > 0));
640 @c matchdeclare (cc, freeof (%e, %pi, %i));
641 @c matchdeclare (dd, lambda ([x, y], gcd (x, y) = 1) (1728));
642 @c matchdeclare (ee, true);
643 @c matchdeclare (ff, all);
646 (%i1) matchdeclare (aa, integerp);
648 (%i2) matchdeclare (bb, lambda ([x], x > 0));
650 (%i3) matchdeclare (cc, freeof (%e, %pi, %i));
652 (%i4) matchdeclare (dd, lambda ([x, y], gcd (x, y) = 1) (1728));
654 (%i5) matchdeclare (ee, true);
656 (%i6) matchdeclare (ff, all);
660 If an expression satisfies a match predicate,
661 the match variable is assigned the expression.
664 @c matchdeclare (aa, integerp, bb, atom);
665 @c defrule (r1, bb^aa, ["integer" = aa, "atom" = bb]);
669 (%i1) matchdeclare (aa, integerp, bb, atom);
671 (%i2) defrule (r1, bb^aa, ["integer" = aa, "atom" = bb]);
673 (%o2) r1 : bb -> [integer = aa, atom = bb]
675 (%o3) [integer = 8, atom = %pi]
678 In the case of addition and multiplication, the match variable may be assigned
679 a single expression which satisfies the match predicate, or a sum or product
680 (respectively) of such expressions.
683 @c matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
684 @c defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
686 @c r1 (8 + a*b + sin(x));
687 @c defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
689 @c r2 (8 * (a + b) * sin(x));
692 (%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
694 (%i2) defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
696 bb + aa partitions `sum'
697 (%o2) r1 : bb + aa -> [all atoms = aa, all nonatoms = bb]
698 (%i3) r1 (8 + a*b + sin(x));
699 (%o3) [all atoms = 8, all nonatoms = sin(x) + a b]
700 (%i4) defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
702 bb aa partitions `product'
703 (%o4) r2 : aa bb -> [all atoms = aa, all nonatoms = bb]
704 (%i5) r2 (8 * (a + b) * sin(x));
705 (%o5) [all atoms = 8, all nonatoms = (b + a) sin(x)]
708 When matching arguments of @code{+} and @code{*},
709 if all match predicates are mutually exclusive,
710 the match result is insensitive to ordering,
711 as one match predicate cannot accept terms matched by another.
714 @c matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
715 @c defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
717 @c r1 (8 + a*b + %pi + sin(x) - c + 2^n);
718 @c defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
720 @c r2 (8 * (a + b) * %pi * sin(x) / c * 2^n);
723 (%i1) matchdeclare (aa, atom, bb, lambda ([x], not atom(x)));
725 (%i2) defrule (r1, aa + bb, ["all atoms" = aa, "all nonatoms" =
727 bb + aa partitions `sum'
728 (%o2) r1 : bb + aa -> [all atoms = aa, all nonatoms = bb]
729 (%i3) r1 (8 + a*b + %pi + sin(x) - c + 2^n);
731 (%o3) [all atoms = %pi + 8, all nonatoms = sin(x) + 2 - c + a b]
732 (%i4) defrule (r2, aa * bb, ["all atoms" = aa, "all nonatoms" =
734 bb aa partitions `product'
735 (%o4) r2 : aa bb -> [all atoms = aa, all nonatoms = bb]
736 (%i5) r2 (8 * (a + b) * %pi * sin(x) / c * 2^n);
739 (%o5) [all atoms = 8 %pi, all nonatoms = -----------------]
743 The functions @code{propvars} and @code{printprops} return information about
747 @c matchdeclare ([aa, bb, cc], atom, [dd, ee], integerp);
748 @c matchdeclare (ff, floatnump, gg, lambda ([x], x > 100));
749 @c propvars (matchdeclare);
750 @c printprops (ee, matchdeclare);
751 @c printprops (gg, matchdeclare);
752 @c printprops (all, matchdeclare);
755 (%i1) matchdeclare ([aa, bb, cc], atom, [dd, ee], integerp);
757 (%i2) matchdeclare (ff, floatnump, gg, lambda ([x], x > 100));
759 (%i3) propvars (matchdeclare);
760 (%o3) [aa, bb, cc, dd, ee, ff, gg]
761 (%i4) printprops (ee, matchdeclare);
763 (%i5) printprops (gg, matchdeclare);
764 (%o5) [lambda([x], x > 100, gg)]
765 (%i6) printprops (all, matchdeclare);
766 (%o6) [lambda([x], x > 100, gg), floatnump(ff), integerp(ee),
767 integerp(dd), atom(cc), atom(bb), atom(aa)]
771 @category{Rules and patterns} @category{Declarations and inferences}
777 @c -----------------------------------------------------------------------------
778 @anchor{maxapplydepth}
779 @defvr {Option variable} maxapplydepth
782 @code{maxapplydepth} is the maximum depth to which @code{apply1}
783 and @code{apply2} will delve.
786 @category{Function application}
792 @c -----------------------------------------------------------------------------
793 @anchor{maxapplyheight}
794 @defvr {Option variable} maxapplyheight
797 @code{maxapplyheight} is the maximum height to which @code{applyb1}
798 will reach before giving up.
801 @category{Function application}
805 @c NEEDS CLARIFICATION AND EXAMPLES
807 @c -----------------------------------------------------------------------------
809 @deffn {Function} remlet @
810 @fname{remlet} (@var{prod}, @var{name}) @
812 @fname{remlet} (all) @
813 @fname{remlet} (all, @var{name})
815 Deletes the substitution rule, @var{prod} --> repl, most
816 recently defined by the @code{let} function. If name is supplied the rule is
817 deleted from the rule package name.
819 @code{remlet()} and @code{remlet(all)} delete all substitution rules from the
820 current rule package. If the name of a rule package is supplied, e.g.
821 @code{remlet (all, @var{name})}, the rule package @var{name} is also deleted.
823 If a substitution is to be changed using the same
824 product, @code{remlet} need not be called, just redefine the substitution
825 using the same product (literally) with the @code{let} function and the new
826 replacement and/or predicate name. Should @code{remlet (@var{prod})} now be
827 called the original substitution rule is revived.
829 See also @code{remrule}, which removes a rule defined by @code{tellsimp} or
830 @code{tellsimpafter}.
833 @category{Rules and patterns}
837 @c -----------------------------------------------------------------------------
839 @deffn {Function} remrule @
840 @fname{remrule} (@var{op}, @var{rulename}) @
841 @fname{remrule} (@var{op}, all)
843 Removes rules defined by @code{tellsimp} or @code{tellsimpafter}.
845 @code{remrule (@var{op}, @var{rulename})}
846 removes the rule with the name @var{rulename} from the operator @var{op}.
847 When @var{op} is a built-in or user-defined operator
848 (as defined by @code{infix}, @code{prefix}, etc.),
849 @var{op} and @var{rulename} must be enclosed in double quote marks.
851 @code{remrule (@var{op}, all)} removes all rules for the operator @var{op}.
853 See also @code{remlet}, which removes a rule defined by @code{let}.
858 @c tellsimp (foo (aa, bb), bb - aa);
859 @c tellsimpafter (aa + bb, special_add (aa, bb));
861 @c tellsimp (aa @@ bb, bb/aa);
862 @c tellsimpafter (quux (%pi, %e), %pi - %e);
863 @c tellsimpafter (quux (%e, %pi), %pi + %e);
864 @c [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
866 @c remrule (foo, foorule1);
867 @c remrule ("+", ?\+rule1);
868 @c remrule ("@@", ?\@\@rule1);
869 @c remrule (quux, all);
870 @c [foo (aa, bb), aa + bb, aa @@ bb, quux (%pi, %e),
874 (%i1) tellsimp (foo (aa, bb), bb - aa);
875 (%o1) [foorule1, false]
876 (%i2) tellsimpafter (aa + bb, special_add (aa, bb));
877 (%o2) [+rule1, simplus]
878 (%i3) infix ("@@@@");
880 (%i4) tellsimp (aa @@@@ bb, bb/aa);
881 (%o4) [@@@@rule1, false]
882 (%i5) tellsimpafter (quux (%pi, %e), %pi - %e);
883 (%o5) [quuxrule1, false]
884 (%i6) tellsimpafter (quux (%e, %pi), %pi + %e);
885 (%o6) [quuxrule2, quuxrule1, false]
886 (%i7) [foo (aa, bb), aa + bb, aa @@@@ bb, quux (%pi, %e),
889 (%o7) [bb - aa, special_add(aa, bb), --, %pi - %e, %pi + %e]
891 (%i8) remrule (foo, foorule1);
893 (%i9) remrule ("+", ?\+rule1);
895 (%i10) remrule ("@@@@", ?\@@\@@rule1);
897 (%i11) remrule (quux, all);
899 (%i12) [foo (aa, bb), aa + bb, aa @@@@ bb, quux (%pi, %e),
901 (%o12) [foo(aa, bb), bb + aa, aa @@@@ bb, quux(%pi, %e),
906 @category{Rules and patterns}
910 @c NEEDS EXPANSION OR MAYBE JUST APPROPRIATE REFS TO tellsimpafter
912 @c -----------------------------------------------------------------------------
914 @deffn {Function} tellsimp (@var{pattern}, @var{replacement})
916 is similar to @code{tellsimpafter} but places
917 new information before old so that it is applied before the built-in
918 simplification rules.
920 @code{tellsimp} is used when it is important to modify
921 the expression before the simplifier works on it, for instance if the
922 simplifier "knows" something about the expression, but what it returns
923 is not to your liking.
924 If the simplifier "knows" something about the
925 main operator of the expression, but is simply not doing enough for
926 you, you probably want to use @code{tellsimpafter}.
928 The pattern may not be a
929 sum, product, single variable, or number.
931 The system variable @code{rules} is the list of rules defined by
932 @code{defrule}, @code{defmatch}, @code{tellsimp}, and @code{tellsimpafter}.
937 (%i1) matchdeclare (x, freeof (%i));
940 (%i3) tellsimp (sin(%i*x), %i*sinh(x));
941 (%o3) [sinrule1, simp-%sin]
942 (%i4) trigexpand (sin (%i*y + x));
943 (%o4) sin(x) cos(%i y) + %i cos(x) sinh(y)
949 (%i7) ev (tellsimp (0^0, 1), simp: false);
950 (%o7) [^rule1, simpexpt]
953 (%i9) remrule ("^", %th(2)[1]);
955 (%i10) tellsimp (sin(x)^2, 1 - cos(x)^2);
956 (%o10) [^rule2, simpexpt]
957 (%i11) (1 + sin(x))^2;
962 (%o12) 2 sin(x) - cos (x) + 2
968 (%i15) matchdeclare (a, true);
970 (%i16) tellsimp (sin(a)^2, 1 - cos(a)^2);
971 (%o16) [^rule3, simpexpt]
978 @category{Rules and patterns}
982 @c -----------------------------------------------------------------------------
983 @anchor{tellsimpafter}
984 @deffn {Function} tellsimpafter (@var{pattern}, @var{replacement})
986 Defines a simplification rule which the Maxima simplifier applies after built-in
987 simplification rules. @var{pattern} is an expression, comprising pattern
988 variables (declared by @code{matchdeclare}) and other atoms and operators,
989 considered literals for the purpose of pattern matching. @var{replacement} is
990 substituted for an actual expression which matches @var{pattern}; pattern
991 variables in @var{replacement} are assigned the values matched in the actual
994 @var{pattern} may be any nonatomic expression in which the main operator is not
995 a pattern variable; the simplification rule is associated with the main
996 operator. The names of functions (with one exception, described below), lists,
997 and arrays may appear in @var{pattern} as the main operator only as literals
998 (not pattern variables); this rules out expressions such as @code{aa(x)} and
999 @code{bb[y]} as patterns, if @code{aa} and @code{bb} are pattern variables.
1000 Names of functions, lists, and arrays which are pattern variables may appear as
1001 operators other than the main operator in @var{pattern}.
1003 There is one exception to the above rule concerning names of functions.
1004 The name of a subscripted function in an expression such as @code{aa[x](y)}
1005 may be a pattern variable, because the main operator is not @code{aa} but rather
1006 the Lisp atom @code{mqapply}. This is a consequence of the representation of
1007 expressions involving subscripted functions.
1009 @c LET'S NOT GO INTO DETAILS ABOUT MAIN OPERATORS HERE; BUT PRESERVE THIS FOR REFERENCE
1010 @c The main operator of an expression @code{expr} is @code{caar $expr}.
1011 @c For most kinds of expressions,
1012 @c the main operator is the operator returned by @code{op (@var{pattern})};
1013 @c the sole exception is the operator @code{mqapply},
1014 @c which appears in indexed function expressions (e.g., @code{foo[i](x)}).
1016 @c NEED TO REVIEW THIS PARAGRAPH FOR ACCURACY
1017 Simplification rules are applied after evaluation
1018 (if not suppressed through quotation or the flag @code{noeval}).
1019 Rules established by @code{tellsimpafter} are applied in the order they were
1020 defined, and after any built-in rules.
1021 Rules are applied bottom-up, that is,
1022 applied first to subexpressions before application to the whole expression.
1023 @c NO IT IS MORE COMPLICATED THAN THIS, ALTHOUGH IN SOME CIRCUMSTANCE IT APPEARS TO BE THE CASE:
1024 @c For a given expression, at most one rule per operator is applied.
1025 It may be necessary to repeatedly simplify a result (for example, via the
1026 quote-quote operator @code{'@w{}'} or the flag @code{infeval})
1027 to ensure that all rules are applied.
1029 Pattern variables are treated as local variables in simplification rules.
1030 Once a rule is defined, the value of a pattern variable
1031 does not affect the rule, and is not affected by the rule.
1032 An assignment to a pattern variable which results from a successful rule match
1033 does not affect the current assignment (or lack of it) of the pattern variable.
1034 However, as with all atoms in Maxima, the properties of pattern variables (as
1035 declared by @code{put} and related functions) are global.
1037 The rule constructed by @code{tellsimpafter} is named after the main operator of
1038 @var{pattern}. Rules for built-in operators, and user-defined operators defined
1039 by @code{infix}, @code{prefix}, @code{postfix}, @code{matchfix}, and
1040 @code{nofix}, have names which are Lisp identifiers.
1041 @c SLIGHTLY TOO MUCH DETAIL
1042 @c (that is, the name is written with a leading question mark @code{?} in Maxima).
1043 Rules for other functions have names which are Maxima identifiers.
1044 @c SLIGHTLY TOO MUCH DETAIL
1045 @c (that is, the name begins with dollar sign @code{$}).
1047 The treatment of noun and verb forms is slightly confused. @c THIS IS A BUG.
1048 If a rule is defined for a noun (or verb) form
1049 and a rule for the corresponding verb (or noun) form already exists,
1050 the newly-defined rule applies to both forms (noun and verb).
1051 If a rule for the corresponding verb (or noun) form does not exist,
1052 the newly-defined rule applies only to the noun (or verb) form.
1054 The rule constructed by @code{tellsimpafter} is an ordinary Lisp function.
1055 If the name of the rule is @code{$foorule1},
1056 the construct @code{:lisp (trace $foorule1)} traces the function,
1057 and @code{:lisp (symbol-function '$foorule1)} displays its definition.
1059 @code{tellsimpafter} quotes its arguments.
1060 @code{tellsimpafter} returns the list of rules for the main operator of
1061 @var{pattern}, including the newly established rule.
1062 @c WHAT IS simpfoo THAT SOMETIMES APPEARS, AND WHY DOES false SOMETIMES APPEAR IN RETURN VALUE ??
1064 See also @code{matchdeclare}, @code{defmatch}, @code{defrule}, @code{tellsimp},
1065 @code{let}, @code{kill}, @code{remrule}, and @code{clear_rules}.
1069 @var{pattern} may be any nonatomic expression in which the
1070 main operator is not a pattern variable.
1073 @c matchdeclare (aa, atom, [ll, mm], listp, xx, true)$
1074 @c tellsimpafter (sin (ll), map (sin, ll));
1075 @c sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi);
1076 @c tellsimpafter (ll^mm, map ("^", ll, mm));
1077 @c [a, b, c]^[1, 2, 3];
1078 @c tellsimpafter (foo (aa (xx)), aa (foo (xx)));
1079 @c foo (bar (u - v));
1082 (%i1) matchdeclare (aa, atom, [ll, mm], listp, xx, true)$
1083 (%i2) tellsimpafter (sin (ll), map (sin, ll));
1084 (%o2) [sinrule1, simp-%sin]
1085 (%i3) sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi);
1087 (%o3) [-, -------, -------, 1, 0]
1089 (%i4) tellsimpafter (ll^mm, map ("^", ll, mm));
1090 (%o4) [^rule1, simpexpt]
1091 (%i5) [a, b, c]^[1, 2, 3];
1094 (%i6) tellsimpafter (foo (aa (xx)), aa (foo (xx)));
1095 (%o6) [foorule1, false]
1096 (%i7) foo (bar (u - v));
1097 (%o7) bar(foo(u - v))
1100 Rules are applied in the order they were defined.
1101 If two rules can match an expression,
1102 the rule which was defined first is applied.
1105 @c matchdeclare (aa, integerp);
1106 @c tellsimpafter (foo (aa), bar_1 (aa));
1107 @c tellsimpafter (foo (aa), bar_2 (aa));
1111 (%i1) matchdeclare (aa, integerp);
1113 (%i2) tellsimpafter (foo (aa), bar_1 (aa));
1114 (%o2) [foorule1, false]
1115 (%i3) tellsimpafter (foo (aa), bar_2 (aa));
1116 (%o3) [foorule2, foorule1, false]
1121 Pattern variables are treated as local variables in simplification rules.
1122 (Compare to @code{defmatch}, which treats pattern variables as global
1126 @c matchdeclare (aa, integerp, bb, atom);
1127 @c tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1133 (%i1) matchdeclare (aa, integerp, bb, atom);
1135 (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1136 (%o2) [foorule1, false]
1140 (%o4) bar(aa = 42, bb = %e)
1145 As with all atoms, properties of pattern variables are global even though values
1146 are local. In this example, an assignment property is declared via
1147 @code{define_variable}. This is a property of the atom @code{bb} throughout
1151 @c matchdeclare (aa, integerp, bb, atom);
1152 @c tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1154 @c define_variable (bb, true, boolean);
1158 (%i1) matchdeclare (aa, integerp, bb, atom);
1160 (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb));
1161 (%o2) [foorule1, false]
1163 (%o3) bar(aa = 42, bb = %e)
1164 (%i4) define_variable (bb, true, boolean);
1167 Error: bb was declared mode boolean, has value: %e
1168 -- an error. Quitting. To debug this try debugmode(true);
1171 Rules are named after main operators.
1172 Names of rules for built-in and user-defined operators are Lisp identifiers,
1173 while names for other functions are Maxima identifiers.
1176 @c tellsimpafter (foo (%pi + %e), 3*%pi);
1177 @c tellsimpafter (foo (%pi * %e), 17*%e);
1178 @c tellsimpafter (foo (%i ^ %e), -42*%i);
1179 @c tellsimpafter (foo (9) + foo (13), quux (22));
1180 @c tellsimpafter (foo (9) * foo (13), blurf (22));
1181 @c tellsimpafter (foo (9) ^ foo (13), mumble (22));
1183 @c foorule_name: first (%o1);
1184 @c plusrule_name: first (%o4);
1185 @c remrule (foo, foorule1);
1186 @c remrule ("^", ?\^rule1);
1190 (%i1) tellsimpafter (foo (%pi + %e), 3*%pi);
1191 (%o1) [foorule1, false]
1192 (%i2) tellsimpafter (foo (%pi * %e), 17*%e);
1193 (%o2) [foorule2, foorule1, false]
1194 (%i3) tellsimpafter (foo (%i ^ %e), -42*%i);
1195 (%o3) [foorule3, foorule2, foorule1, false]
1196 (%i4) tellsimpafter (foo (9) + foo (13), quux (22));
1197 (%o4) [+rule1, simplus]
1198 (%i5) tellsimpafter (foo (9) * foo (13), blurf (22));
1199 (%o5) [*rule1, simptimes]
1200 (%i6) tellsimpafter (foo (9) ^ foo (13), mumble (22));
1201 (%o6) [^rule1, simpexpt]
1203 (%o7) [foorule1, foorule2, foorule3, +rule1, *rule1, ^rule1]
1204 (%i8) foorule_name: first (%o1);
1206 (%i9) plusrule_name: first (%o4);
1208 (%i10) remrule (foo, foorule1);
1210 (%i11) remrule ("^", ?\^rule1);
1213 (%o12) [foorule2, foorule3, +rule1, *rule1]
1216 A worked example: anticommutative multiplication.
1219 @c gt (i, j) := integerp(j) and i < j;
1220 @c matchdeclare (i, integerp, j, gt(i));
1221 @c tellsimpafter (s[i]^^2, 1);
1222 @c tellsimpafter (s[i] . s[j], -s[j] . s[i]);
1223 @c s[1] . (s[1] + s[2]);
1225 @c factor (expand (sum (s[i], i, 0, 9)^^5));
1228 (%i1) gt (i, j) := integerp(j) and i < j;
1229 (%o1) gt(i, j) := integerp(j) and i < j
1230 (%i2) matchdeclare (i, integerp, j, gt(i));
1232 (%i3) tellsimpafter (s[i]^^2, 1);
1233 (%o3) [^^rule1, simpncexpt]
1234 (%i4) tellsimpafter (s[i] . s[j], -s[j] . s[i]);
1235 (%o4) [.rule1, simpnct]
1236 (%i5) s[1] . (s[1] + s[2]);
1242 (%i7) factor (expand (sum (s[i], i, 0, 9)^^5));
1243 (%o7) 100 (s + s + s + s + s + s + s + s + s + s )
1248 @category{Rules and patterns}
1252 @c -----------------------------------------------------------------------------
1253 @anchor{clear_rules}
1254 @deffn {Function} clear_rules ()
1256 Executes @code{kill (rules)} and then resets the next rule number to 1
1257 for addition @code{+}, multiplication @code{*}, and exponentiation @code{^}.
1260 @category{Rules and patterns}