Go to the first, previous, next, last section, table of contents.
This section describes user-defined pattern matching and
simplification rules.
There are two groups of functions which implement somewhat different pattern matching schemes.
In one group are tellsimp
, tellsimpafter
, defmatch
, defrule
,
apply1
, applyb1
, and apply2
.
In the other group are let
and letsimp
.
Both schemes define patterns in terms of pattern variables declared by matchdeclare
.
Pattern-matching rules defined by tellsimp
and tellsimpafter
are applied automatically
by the Maxima simplifier.
Rules defined by defmatch
, defrule
, and let
are applied
by an explicit function call.
There are additional mechanisms for rules applied to polynomials by tellrat
,
and for commutative and noncommutative algebra in affine
package.
maxapplydepth
is the depth of the deepest subexpressions processed by
apply1
and apply2
.
See also applyb1
, apply2
, and let
.
maxapplydepth
is the depth of the deepest subexpressions processed by
apply1
and apply2
.
See also apply1
and let
.
applyb1
is similar to apply1
but works from
the bottom up instead of from the top down.
maxapplyheight
is the maximum height which applyb1
reaches
before giving up.
See also apply1
, apply2
, and let
.
default_let_rule_package
current_let_rule_package
is the name of the rule package that is used by
functions in the let
package (letsimp
, etc.)
if no other rule package is specified.
This variable may be assigned the name of any rule package defined
via the let
command.
If a call such as letsimp (expr, rule_pkg_name)
is made,
the rule package rule_pkg_name
is used for that function call only,
and the value of current_let_rule_package
is not changed.
default_let_rule_package
default_let_rule_package
is the name of the rule package used when one
is not explicitly set by the user with let
or by changing the value of
current_let_rule_package
.
progname (expr, y_1, ..., y_n)
which tests expr to see if it matches pattern.
pattern is an expression
containing the pattern variables x_1, ..., x_n
and pattern parameters, if any.
The pattern variables are given
explicitly as arguments to defmatch
while the pattern parameters
are declared by the matchdeclare
function.
The first argument to the created function progname is an expression to be matched against the pattern and the other arguments are the actual variables y_1, ..., y_n in the expression 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 variables and pattern parameters, and whose right sides are the expressions
which the pattern variables and parameters matched.
The pattern parameters, but not the variables, are assigned the subexpressions they match.
If the match fails, progname returns false
.
Any variables not declared as pattern parameters in matchdeclare
or as
variables in defmatch
match only themselves.
A pattern which contains no pattern variables or parameters
returns true
if the match succeeds.
See also matchdeclare
, defrule
, tellsimp
, and tellsimpafter
.
Examples:
This defmatch
defines the function linearp (expr, y)
, which
tests expr
to see if it is of the form a*y + b
such that a
and b
do not contain y
.
(%i1) matchdeclare (a, freeof(x), b, freeof(x))$ (%i2) defmatch (linearp, a*x + b, x)$ (%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
If the third argument to defmatch
in line (%i2) had
been omitted, then linear
would only match expressions linear in x,
not in any other variable.
(%i1) matchdeclare ([a, f], true)$ (%i2) constinterval (l, h) := constantp (h - l)$ (%i3) matchdeclare (b, constinterval (a))$ (%i4) matchdeclare (x, atom)$ (%i5) (remove (integrate, outative), defmatch (checklimits, 'integrate (f, x, a, b)), declare (integrate, outative))$ (%i6) 'integrate (sin(t), t, %pi + x, 2*%pi + x); x + 2 %pi / [ (%o6) I sin(t) dt ] / x + %pi (%i7) checklimits (%); (%o7) [b = x + 2 %pi, a = x + %pi, x = t, f = sin(t)] (%i8) a; (%o8) x + %pi (%i9) b; (%o9) x + 2 %pi (%i10) f; (%o10) sin(t) (%i11) x; (%o11) t
apply1
, applyb1
, or apply2
), every
subexpression matching the pattern will be replaced by the
replacement. All variables in the replacement which have been
assigned values by the pattern match are assigned those values in the
replacement which is then simplified.
The rules themselves can be treated as functions which transform an expression by one operation of the pattern match and replacement. If the match fails, the original expression is returned.
defrule
, tellsimp
, or tellsimpafter
,
or a pattern defined by defmatch
.
For example, the first rule modifying sin
is named sinrule1
.
disprule (all)
displays all rules.
See also letrules
, which displays rules defined by let
.
letsimp
such that prod is replaced by repl.
prod is a product of positive or negative powers of the following terms:
letsimp
will search for literally unless previous
to calling letsimp
the matchdeclare
function is used to associate a
predicate with the atom. In this case letsimp
will match the atom to
any term of a product satisfying the predicate.
sin(x)
, n!
, f(x,y)
, etc. As with atoms above
letsimp
will look for a literal match unless matchdeclare
is used to
associate a predicate with the argument of the kernel.
A term to a positive power will only match a term having at least that
power. A term to a negative power
on the other hand will only match a term with a power at least as
negative. In the case of negative powers in prod the switch
letrat
must be set to true
.
See also letrat
.
If a predicate is included in the let
function followed by a list of
arguments, a tentative match (i.e. one that would be accepted if the
predicate were omitted) is accepted only if
predname (arg_1', ..., arg_n')
evaluates to true
where arg_i' is the value
matched to arg_i. The arg_i may be the name of any atom or the argument
of any kernel appearing in prod.
repl may be any rational expression.
If any of the atoms or arguments from prod appear in repl the
appropriate substitutions are made.
The global flag letrat
controls the simplification of quotients by letsimp
.
When letrat
is false
,
letsimp
simplifies the numerator and
denominator of expr separately, and does not simplify the quotient.
Substitutions such as n!/n
goes to (n-1)!
then fail.
When letrat
is true
, then the numerator,
denominator, and the quotient are simplified in that order.
These substitution functions allow you to work with several rule packages at once.
Each rule package can contain any number of let
rules and is referenced by a user-defined name.
let ([prod, repl, predname, arg_1, ..., arg_n], package_name)
adds the rule predname to the rule package package_name.
letsimp (expr, package_name)
applies the rules in package_name.
letsimp (expr, package_name1, package_name2, ...)
is equivalent to letsimp (expr, package_name1)
followed by letsimp (%, package_name2)
, ....
current_let_rule_package
is the name of the rule package that is
presently being used.
This variable may be assigned the name of
any rule package defined via the let
command.
Whenever any of the functions comprising the let
package are called with no package name,
the package named by current_let_rule_package
is used.
If a call such as letsimp (expr, rule_pkg_name)
is made,
the rule package rule_pkg_name is used for that letsimp
command only,
and current_let_rule_package
is not changed.
If not otherwise specified,
current_let_rule_package
defaults to default_let_rule_package
.
(%i1) matchdeclare ([a, a1, a2], true)$ (%i2) oneless (x, y) := is (x = y-1)$ (%i3) let (a1*a2!, a1!, oneless, a2, a1); (%o3) a1 a2! --> a1! where oneless(a2, a1) (%i4) letrat: true$ (%i5) let (a1!/a1, (a1-1)!); a1! (%o5) -- --> (a1 - 1)! a1 (%i6) letsimp (n*m!*(n-1)!/m); (%o6) (m - 1)! n! (%i7) let (sin(a)^2, 1 - cos(a)^2); 2 2 (%o7) sin (a) --> 1 - cos (a) (%i8) letsimp (sin(x)^4); 4 2 (%o8) cos (x) - 2 cos (x) + 1
false
When letrat
is false
, letsimp
simplifies the
numerator and denominator of a ratio separately,
and does not simplify the quotient.
When letrat
is true
,
the numerator, denominator, and their quotient are simplified in that order.
(%i1) matchdeclare (n, true)$ (%i2) let (n!/n, (n-1)!); n! (%o2) -- --> (n - 1)! n (%i3) letrat: false$ (%i4) letsimp (a!/a); a! (%o4) -- a (%i5) letrat: true$ (%i6) letsimp (a!/a); (%o6) (a - 1)!
letrules ()
displays the rules in the current rule package.
letrules (package_name)
displays the rules in package_name
.
The current rule package is named by current_let_rule_package
.
If not otherwise specified, current_let_rule_package
defaults to default_let_rule_package
.
See also disprule
, which displays rules defined by tellsimp
and tellsimpafter
.
let
until no further change is made to expr.
letsimp (expr)
uses the rules from current_let_rule_package
.
letsimp (expr, package_name)
uses the rules from package_name
without changing current_let_rule_package
.
letsimp (expr, package_name_1, ..., package_name_n)
is equivalent to letsimp (expr, package_name_1
,
followed by letsimp (%, package_name_2)
, and so on.
[default_let_rule_package]
let_rule_packages
is a list of all user-defined let rule packages
plus the default package default_let_rule_package
.
false
.
The predicate is the name of a function,
a function call missing the last argument,
or true
.
Any expression matches true
.
If the predicate is specified as a function call,
the expression to be tested is appended to the list of arguments;
the arguments are evaluated at the time the match is evaluated.
Otherwise, the predicate is specified as a function name,
and the expression to be tested is the sole argument.
A predicate function need not be defined when matchdeclare
is called;
the predicate is not evaluated until a match is attempted.
A matchdeclare
predicate cannot be any kind of expression other than a function name or function call.
In particular, a predicate cannot be a lambda
or block
.
If an expression satisfies a match predicate,
the match variable is assigned the expression,
except for match variables which are operands of addition +
or multiplication *
.
Only addition and multiplication are handled specially;
other n-ary operators (both built-in and user-defined) are treated like ordinary functions.
In the case of addition and multiplication, the match variable may be assigned a single expression which satisfies the match predicate, or a sum or product (respectively) of such expressions. Such multiple-term matching is greedy: predicates are evaluated in the order in which their associated variables appear in the match pattern, and a term which satisfies more than one predicate is taken by the first predicate which it satisfies. Each predicate is tested against all operands of the sum or product before the next predicate is evaluated. In addition, if 0 or 1 (respectively) satisfies a match predicate, and there are no other terms which satisfy the predicate, 0 or 1 is assigned to the match variable associated with the predicate.
The algorithm for processing addition and multiplication patterns makes some match results (for example, a pattern in which a "match anything" variable appears) dependent on the ordering of terms in the match pattern and in the expression to be matched. However, if all match predicates are mutually exclusive, the match result is insensitive to ordering, as one match predicate cannot accept terms matched by another.
Calling matchdeclare
with a variable a as an argument
changes the matchdeclare
property for a, if one was already declared;
only the most recent matchdeclare
is in effect when a rule is defined,
Later changes to the matchdeclare
property
(via matchdeclare
or remove
)
do not affect existing rules.
propvars (matchdeclare)
returns the list of all variables
for which there is a matchdeclare
property.
printprops (a, matchdeclare)
returns the predicate for variable a
.
printprops (all, matchdeclare)
returns the list of predicates for all matchdeclare
variables.
remove (a, matchdeclare)
removes the matchdeclare
property from a.
The functions
defmatch
, defrule
, tellsimp
, tellsimpafter
, and let
construct rules which test expressions against patterns.
matchdeclare
quotes its arguments.
matchdeclare
always returns done
.
Examples:
q
matches an expression not containing x
or %e
.
(%i1) matchdeclare (q, freeof (x, %e))$
A "matchfix" operator is a function of any number of arguments,
such that the arguments occur between matching left and right delimiters.
The delimiters may be any strings, so long as the parser can
distinguish the delimiters from the operands
and other expressions and operators.
In practice this rules out unparseable delimiters such as
%
, ,
, $
and ;
,
and may require isolating the delimiters with white space.
The right delimiter can be the same or different from the left delimiter.
A left delimiter can be associated with only one right delimiter; two different matchfix operators cannot have the same left delimiter.
An existing operator may be redeclared as a matchfix operator
without changing its other properties.
In particular, built-in operators such as addition +
can
be declared matchfix,
but operator functions cannot be defined for built-in operators.
matchfix (ldelimiter, rdelimiter, arg_pos, pos)
declares the argument part-of-speech arg_pos
and result part-of-speech pos,
and the delimiters ldelimiter and rdelimiter.
The function to carry out a matchfix operation is an ordinary
user-defined function.
The operator function is defined
in the usual way
with the function definition operator :=
or define
.
The arguments may be written between the delimiters,
or with the left delimiter as a quoted string and the arguments
following in parentheses.
dispfun (ldelimiter)
displays the function definition.
The only built-in matchfix operator is the list constructor [ ]
.
Parentheses ( )
and double-quotes " "
act like matchfix operators,
but are not treated as such by the Maxima parser.
matchfix
evaluates its arguments.
matchfix
returns its first argument, ldelimiter.
Examples:
(%i1) matchfix ("@", "~"); (%o1) "@" (%i2) @ a, b, c ~; (%o2) @a, b, c~ (%i3) matchfix (">>", "<<"); (%o3) ">>" (%i4) >> a, b, c <<; (%o4) >>a, b, c<< (%i5) matchfix ("foo", "oof"); (%o5) "foo" (%i6) foo a, b, c oof; (%o6) fooa, b, coof (%i7) >> w + foo x, y oof + z << / @ p, q ~; >>z + foox, yoof + w<< (%o7) ---------------------- @p, q~
(%i1) matchfix ("!-", "-!"); (%o1) "!-" (%i2) !- x, y -! := x/y - y/x; x y (%o2) !-x, y-! := - - - y x (%i3) define (!-x, y-!, x/y - y/x); x y (%o3) !-x, y-! := - - - y x (%i4) define ("!-" (x, y), x/y - y/x); x y (%o4) !-x, y-! := - - - y x (%i5) dispfun ("!-"); x y (%t5) !-x, y-! := - - - y x (%o5) done (%i6) !-3, 5-!; 16 (%o6) - -- 15 (%i7) "!-" (3, 5); 16 (%o7) - -- 15
let
function. If name is supplied the rule is
deleted from the rule package name.
remlet()
and remlet(all)
delete all substitution rules from the current rule package.
If the name of a rule package is supplied,
e.g. remlet (all, name)
, the rule package name is also deleted.
If a substitution is to be changed using the same
product, remlet
need not be called, just redefine the substitution
using the same product (literally) with the let
function and the new
replacement and/or predicate name. Should remlet (prod)
now be
called the original substitution rule is revived.
See also remrule
, which removes a rule defined by tellsimp
or tellsimpafter
.
defrule
, defmatch
, tellsimp
, or tellsimpafter
.
remrule (op, rulename)
removes the rule with the name rulename
from the operator op.
remrule (function, all)
removes all rules for the operator op.
See also remlet
, which removes a rule defined by let
.
tellsimpafter
but places
new information before old so that it is applied before the built-in
simplification rules.
tellsimp
is used when it is important to modify
the expression before the simplifier works on it, for instance if the
simplifier "knows" something about the expression, but what it returns
is not to your liking.
If the simplifier "knows" something about the
main operator of the expression, but is simply not doing enough for
you, you probably want to use tellsimpafter
.
The pattern may not be a sum, product, single variable, or number.
rules
is the list of rules defined by
defrule
, defmatch
, tellsimp
, and tellsimpafter
.
Examples:
(%i1) matchdeclare (x, freeof (%i)); (%o1) done (%i2) %iargs: false$ (%i3) tellsimp (sin(%i*x), %i*sinh(x)); (%o3) [sinrule1, simp-%sin] (%i4) trigexpand (sin (%i*y + x)); (%o4) sin(x) cos(%i y) + %i cos(x) sinh(y) (%i5) %iargs:true$ (%i6) errcatch(0^0); 0 0 has been generated (%o6) [] (%i7) ev (tellsimp (0^0, 1), simp: false); (%o7) [^rule1, simpexpt] (%i8) 0^0; (%o8) 1 (%i9) remrule ("^", %th(2)[1]); (%o9) ^ (%i10) tellsimp (sin(x)^2, 1 - cos(x)^2); (%o10) [^rule2, simpexpt] (%i11) (1 + sin(x))^2; 2 (%o11) (sin(x) + 1) (%i12) expand (%); 2 (%o12) 2 sin(x) - cos (x) + 2 (%i13) sin(x)^2; 2 (%o13) 1 - cos (x) (%i14) kill (rules); (%o14) done (%i15) matchdeclare (a, true); (%o15) done (%i16) tellsimp (sin(a)^2, 1 - cos(a)^2); (%o16) [^rule3, simpexpt] (%i17) sin(y)^2; 2 (%o17) 1 - cos (y)
matchdeclare
)
and other atoms and operators, considered literals for the purpose of pattern matching.
replacement is substituted for an actual expression which matches pattern;
pattern variables in replacement are assigned the values matched in the actual expression.
pattern may be any nonatomic expression
in which the main operator is not a pattern variable;
the simplification rule is associated with the main operator.
The names of functions (with one exception, described below), lists, and arrays
may appear in pattern as the main operator only as literals (not pattern variables);
this rules out expressions such as aa(x)
and bb[y]
as patterns,
if aa
and bb
are pattern variables.
Names of functions, lists, and arrays which are pattern variables may appear as operators
other than the main operator in pattern.
There is one exception to the above rule concerning names of functions.
The name of a subscripted function in an expression such as aa[x](y)
may be a pattern variable,
because the main operator is not aa
but rather the Lisp atom mqapply
.
This is a consequence of the representation of expressions involving subscripted functions.
Simplification rules are applied after evaluation
(if not suppressed through quotation or the flag noeval
).
Rules established by tellsimpafter
are applied in the order they were defined,
and after any built-in rules.
Rules are applied bottom-up, that is,
applied first to subexpressions before application to the whole expression.
It may be necessary to repeatedly simplify a result
(for example, via the quote-quote operator '
' or the flag infeval
)
to ensure that all rules are applied.
Pattern variables are treated as local variables in simplification rules.
Once a rule is defined, the value of a pattern variable
does not affect the rule, and is not affected by the rule.
An assignment to a pattern variable which results from a successful rule match
does not affect the current assignment (or lack of it) of the pattern variable.
However,
as with all atoms in Maxima,
the properties of pattern variables (as declared by put
and related functions) are global.
The rule constructed by tellsimpafter
is named after the main operator of pattern
.
Rules for built-in operators,
and user-defined operators
defined by infix
, prefix
, postfix
, matchfix
, and nofix
,
have names which are Maxima strings.
Rules for other functions have names which are ordinary Maxima identifiers.
The treatment of noun and verb forms is slightly confused. If a rule is defined for a noun (or verb) form and a rule for the corresponding verb (or noun) form already exists, the newly-defined rule applies to both forms (noun and verb). If a rule for the corresponding verb (or noun) form does not exist, the newly-defined rule applies only to the noun (or verb) form.
The rule constructed by tellsimpafter
is an ordinary Lisp function.
If the name of the rule is $foorule1
,
the construct :lisp (trace $foorule1)
traces the function,
and :lisp (symbol-function '$foorule1
displays its definition.
tellsimpafter
quotes its arguments.
tellsimpafter
returns the list of rules for the main operator of pattern,
including the newly established rule.
See also matchdeclare
, defmatch
, defrule
, tellsimp
, let
,
kill
, remrule
, and clear_rules
.
Examples:
pattern may be any nonatomic expression in which the main operator is not a pattern variable.
(%i1) matchdeclare (aa, atom, [ll, mm], listp, xx, true)$ (%i2) tellsimpafter (sin (ll), map (sin, ll)); (%o2) [sinrule1, simp-%sin] (%i3) sin ([1/6, 1/4, 1/3, 1/2, 1]*%pi); 1 sqrt(2) sqrt(3) (%o3) [-, -------, -------, 1, 0] 2 2 2 (%i4) tellsimpafter (ll^mm, map ("^", ll, mm)); (%o4) [^rule1, simpexpt] (%i5) [a, b, c]^[1, 2, 3]; 2 3 (%o5) [a, b , c ] (%i6) tellsimpafter (foo (aa (xx)), aa (foo (xx))); (%o6) [foorule1, false] (%i7) foo (bar (u - v)); (%o7) bar(foo(u - v))
Rules are applied in the order they were defined. If two rules can match an expression, the rule which was defined first is applied.
(%i1) matchdeclare (aa, integerp); (%o1) done (%i2) tellsimpafter (foo (aa), bar_1 (aa)); (%o2) [foorule1, false] (%i3) tellsimpafter (foo (aa), bar_2 (aa)); (%o3) [foorule2, foorule1, false] (%i4) foo (42); (%o4) bar_1(42)
Pattern variables are treated as local variables in simplification rules.
(Compare to defmatch
, which treats pattern variables as global variables.)
(%i1) matchdeclare (aa, integerp, bb, atom); (%o1) done (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb)); (%o2) [foorule1, false] (%i3) bb: 12345; (%o3) 12345 (%i4) foo (42, %e); (%o4) bar(aa = 42, bb = %e) (%i5) bb; (%o5) 12345
As with all atoms, properties of pattern variables are global even though values are local.
In this example, an assignment property is declared via define_variable
.
This is a property of the atom bb
throughout Maxima.
(%i1) matchdeclare (aa, integerp, bb, atom); (%o1) done (%i2) tellsimpafter (foo(aa, bb), bar('aa=aa, 'bb=bb)); (%o2) [foorule1, false] (%i3) foo (42, %e); (%o3) bar(aa = 42, bb = %e) (%i4) define_variable (bb, true, boolean); (%o4) true (%i5) foo (42, %e); Error: bb was declared mode boolean, has value: %e -- an error. Quitting. To debug this try debugmode(true);
Rules are named after main operators. Names of rules for built-in and user-defined operators are strings, while names for other functions are ordinary identifiers.
(%i1) tellsimpafter (foo (%pi + %e), 3*%pi); (%o1) [foorule1, false] (%i2) tellsimpafter (foo (%pi * %e), 17*%e); (%o2) [foorule2, foorule1, false] (%i3) tellsimpafter (foo (%i ^ %e), -42*%i); (%o3) [foorule3, foorule2, foorule1, false] (%i4) tellsimpafter (foo (9) + foo (13), quux (22)); (%o4) [+rule1, simplus] (%i5) tellsimpafter (foo (9) * foo (13), blurf (22)); (%o5) [*rule1, simptimes] (%i6) tellsimpafter (foo (9) ^ foo (13), mumble (22)); (%o6) [^rule1, simpexpt] (%i7) rules; (%o7) [trigrule0, trigrule1, trigrule2, trigrule3, trigrule4, htrigrule1, htrigrule2, htrigrule3, htrigrule4, foorule1, foorule2, foorule3, +rule1, *rule1, ^rule1] (%i8) foorule_name: first (%o1); (%o8) foorule1 (%i9) plusrule_name: first (%o4); (%o9) +rule1 (%i10) [?mstringp (foorule_name), symbolp (foorule_name)]; (%o10) [false, true] (%i11) [?mstringp (plusrule_name), symbolp (plusrule_name)]; (%o11) [true, true] (%i12) remrule (foo, foorule1); (%o12) foo (%i13) remrule ("^", "^rule1"); (%o13) ^
kill (rules)
and then resets the next rule number to 1
for addition +
, multiplication *
, and exponentiation ^
.
Go to the first, previous, next, last section, table of contents.