define - Maple Programming Help

define

define characteristics of an operator name

 Calling Sequence define(oper, rule1, rule2, ...) definemore(oper, rule1, rule2, ...) undefine(oper) redefine(oper, rulelist)

Parameters

 oper - name of the operator being defined rule1, rule2, ... - one or more key names of a property, or an equation that defines a rule rulelist - list; a list of equational rules, or property keywords

Description

 • The define command defines procedures by means of rules. To define a procedure by using the define command, you specify the name of the procedure and rules that it must obey using the syntax of patmatch. The left-hand side of each defining equation must contain the name of the defined procedure. In addition, there are several property names that can be used as abbreviations for common constructions.
 • The name passed as the first argument to define must not be assigned. Therefore, to change the definition of a procedure defined using define use instead the redefine command, described below.
 • A procedure defined using define can be specialized further, adding more rules and properties to its definition, by using the definemore command. Its syntax is the same as for the define command, but the rules or properties are added to the definition of the defined procedure rather than replacing them.
 • The define command is not able in all cases to detect if some rules are contradictory, or may result in a non-terminating program.
 • A procedure defined using define does not automatically respect assumptions. However, you can use assumptions within a conditional pattern.

Rules

 • A rule can be an equational rule specified using the syntax of patmatch, or one of the reserved words linear, multilinear, orderless, flat, identity, zero, and diff, which name a property. The meanings of these reserved words are as follows.
 • The linear property causes the defined procedure to be linear in its first argument.
 • The multilinear property causes the defined procedure to be a multilinear function, that is, it is linear in each argument, independently.
 • The orderless property causes the defined procedure to be constant on permutations of its arguments. For two-argument procedures, this corresponds to commutativity.
 • The flat property causes the defined procedure to be associative.
 • The identity property is used to name an identity element for a two-argument procedure (or binary operation). It is used by specifying an equation of the form $\mathrm{identity}=\mathrm{EXPR}$, where $\mathrm{EXPR}$ is the expression that is to serve as an identity element.
 • The zero option is used to specify a zero of the defined procedure. It is used by specifying an equation of the form $\mathrm{zero}=\mathrm{EXPR}$, where $\mathrm{EXPR}$ is the expression that is to be a zero of the defined procedure.
 • The rules defining a procedure may include the specification of the derivative of the defined procedure. If the procedure name being defined is F, then its derivative must be specified in the form $\frac{ⅆ}{ⅆx}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}F\left(x\right)=\mathrm{EXPR}$, where $\mathrm{EXPR}$ is an expression (which may depend on x) for the derivative of F.
 • If a function F has been created with define, you can remove its definition by calling $\mathrm{undefine}\left(F\right)$. (In many cases, simply unassign F will suffice, but in general, undefine must be used.)
 • A function name can be changed by using the redefine command. It takes exactly two arguments: the name of the function to be redefined, and a list of the defining properties that would be passed as subsequent arguments to define in a fresh definition.

Examples

 > $\mathrm{define}\left(f,'\mathrm{linear}',f\left(1\right)=\mathrm{tt}\right)$
 > $f\left(2x+4\right)$
 ${2}{}{f}{}\left({x}\right){+}{4}{}{\mathrm{tt}}$ (1)
 > $\mathrm{define}\left(g,g\left({a::\mathrm{nonunit}\left(\mathrm{algebraic}\right)}^{n::\mathrm{nonunit}\left('\mathrm{integer}'\right)}\right)=ng\left(a\right),g\left(a::\mathrm{realcons}\right)=a\right)$
 > $g\left({x}^{2}\right)$
 ${2}{}{g}{}\left({x}\right)$ (2)
 > $g\left({\left(x-2\right)}^{-1}\right)$
 ${-}{g}{}\left({x}{-}{2}\right)$ (3)
 > $g\left(\mathrm{\pi }\mathrm{sqrt}\left(2\right)\right)$
 ${\mathrm{\pi }}{}\sqrt{{2}}$ (4)

Define a commutative and associative operation.

 > $\mathrm{define}\left(h,'\mathrm{orderless}','\mathrm{flat}'\right)$
 > $h\left(a,b\right)-h\left(b,a\right)$
 ${0}$ (5)
 > $h\left(a,h\left(b,c\right)\right)-h\left(h\left(a,b\right),c\right)$
 ${0}$ (6)

You can use define to program recursive procedures.

 > $\mathrm{define}\left(\mathrm{fac},\mathrm{fac}\left(0\right)=1,\mathrm{fac}\left(n::'\mathrm{posint}'\right)=n\mathrm{fac}\left(n-1\right)\right)$
 > $\mathrm{fac}\left(5\right)$
 ${120}$ (7)
 > $\mathrm{define}\left(\mathrm{fib},\mathrm{fib}\left(0\right)=1,\mathrm{fib}\left(1\right)=1,\mathrm{fib}\left(n::'\mathrm{posint}'\right)=\mathrm{fib}\left(n-1\right)+\mathrm{fib}\left(n-2\right)\right)$
 > $\mathrm{fib}\left(7\right)$
 ${21}$ (8)
 > $\mathrm{define}\left(H,'\mathrm{multilinear}'\right)$
 > $H\left(2x,x+3\right)$
 ${2}{}{H}{}\left({x}{,}{x}\right){+}{6}{}{H}{}\left({x}{,}{1}\right)$ (9)
 > $\mathrm{definemore}\left(H,'\mathrm{orderless}',H\left(a::\mathrm{realcons},b::\mathrm{algebraic}\right)=aH\left(b\right)\right)$
 > $H\left(2x,x+3\right)$
 ${2}{}{H}{}\left({x}{,}{x}\right){+}{6}{}{H}{}\left({x}\right)$ (10)

An example of programming a one-line GCD.

 > $\mathrm{define}\left(\mathrm{GCD},\mathrm{GCD}\left(a::'\mathrm{integer}',0\right)=a,\mathrm{GCD}\left(a::'\mathrm{integer}',b::'\mathrm{integer}'\right)=\mathrm{GCD}\left(b,a\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{mod}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}b\right)\right)$
 > $\mathrm{GCD}\left(6,3\right)$
 ${3}$ (11)
 > $\mathrm{GCD}\left(120,12\cdot 120\right)$
 ${120}$ (12)
 > $\mathrm{GCD}\left(13,11\right)$
 ${1}$ (13)

The identity property can be used to define a monoid operation.

 > $\mathrm{define}\left('\mathrm{&x}','\mathrm{flat}','\mathrm{identity}'=1\right)$
 > $\left(a\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}b\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}c$
 ${\mathrm{&x}}{}\left({a}{,}{b}{,}{c}\right)$ (14)
 > $a\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}1$
 ${a}$ (15)
 > $1\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&x\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}b$
 ${b}$ (16)

A simple integrator can be defined as follows.

 > $\mathrm{define}\left(\mathrm{INT},'\mathrm{linear}','\mathrm{conditional}'\left(\mathrm{INT}\left(a::\mathrm{algebraic},X::\mathrm{name}\right)=aX,\mathrm{_type}\left(a,\mathrm{freeof}\left(X\right)\right)\right),\mathrm{INT}\left(X::\mathrm{name},X::\mathrm{name}\right)=\frac{{X}^{2}}{2}\right)$

This definition works for any variable.

 > $\mathrm{INT}\left(2x+4,x\right)$
 ${{x}}^{{2}}{+}{4}{}{x}$ (17)
 > $\mathrm{INT}\left(3z-\mathrm{\pi },z\right)$
 $\frac{{3}}{{2}}{}{{z}}^{{2}}{-}{\mathrm{\pi }}{}{z}$ (18)

Use definemore to improve the integrator.

 > $\mathrm{definemore}\left(\mathrm{INT},'\mathrm{conditional}'\left(\mathrm{INT}\left(\frac{1}{a::\mathrm{algebraic}X::\mathrm{name}+b::\mathrm{algebraic}},X::\mathrm{name}\right)=\mathrm{ln}\left(aX+b\right),\mathrm{_type}\left(a,\mathrm{freeof}\left(X\right)\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&and\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{_type}\left(b,\mathrm{freeof}\left(X\right)\right)\right)\right)$
 > $\mathrm{INT}\left(\frac{1}{x},x\right)$
 ${\mathrm{ln}}{}\left({x}\right)$ (19)
 > $\mathrm{INT}\left(\frac{1}{2x+3},x\right)$
 ${\mathrm{ln}}{}\left({2}{}{x}{+}{3}\right)$ (20)
 > $\mathrm{definemore}\left(\mathrm{INT},\mathrm{INT}\left({x::\mathrm{name}}^{n::\mathrm{nonunit}\left('\mathrm{integer}'\right)},x::\mathrm{name}\right)=\frac{1}{n+1}{x}^{n+1}\right)$
 > $\mathrm{INT}\left(2{x}^{2},x\right)$
 $\frac{{2}{}{{x}}^{{3}}}{{3}}$ (21)
 > $\mathrm{INT}\left(\frac{1}{x},x\right)$
 ${\mathrm{ln}}{}\left({x}\right)$ (22)
 > $\mathrm{definemore}\left(\mathrm{INT},'\mathrm{conditional}'\left(\mathrm{INT}\left(\mathrm{sin}\left(a::\mathrm{algebraic}X::\mathrm{name}+b::\mathrm{algebraic}\right),X::\mathrm{name}\right)=-\frac{1}{a}\mathrm{cos}\left(ax+b\right),\mathrm{_type}\left(a,\mathrm{freeof}\left(X\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}&and\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{_type}\left(b,\mathrm{freeof}\left(X\right)\right)\right)\right)\right)$

Note: If INT does not recognize the pattern, it returns unevaluated.

 > $\mathrm{INT}\left(\mathrm{sin}\left(x\right)+\mathrm{cos}\left(x\right),x\right)$
 ${-}{\mathrm{cos}}{}\left({x}\right){+}{\mathrm{INT}}{}\left({\mathrm{cos}}{}\left({x}\right){,}{x}\right)$ (23)

The following example defined the procedure P by specifying its derivative.

 > $\mathrm{define}\left(P,\mathrm{diff}\left(P\left(x\right),x\right)=\frac{1}{P\left(x\right)}\right)$
 > $\mathrm{diff}\left(P\left(x\right),x\right)$
 $\frac{{1}}{{P}{}\left({x}\right)}$ (24)
 > $\mathrm{diff}\left(P\left(\mathrm{exp}\left(z\right)\right),z\right)$
 $\frac{{{ⅇ}}^{{z}}}{{P}{}\left({{ⅇ}}^{{z}}\right)}$ (25)
 > $\mathrm{diff}\left(\frac{1}{P\left({x}^{2}\right)},x\right)$
 ${-}\frac{{2}{}{x}}{{{P}{}\left({{x}}^{{2}}\right)}^{{3}}}$ (26)

If the derivative is specified for a procedure defined using the linear property, then Maple can take advantage of this.

 > $\mathrm{redefine}\left(f,\left[\mathrm{linear},\mathrm{diff}\left(f\left(x\right),x\right)=a\right]\right)$
 > $\mathrm{diff}\left(f\left(x\right),x\right)$
 ${a}$ (27)
 > $f\left(0\right)$
 ${0}$ (28)
 > $\mathrm{int}\left(f\left(x\right),x\right)$
 ${x}{}{f}{}\left({x}\right){-}\frac{{{x}}^{{2}}{}{a}}{{2}}$ (29)
 > $\mathrm{int}\left(\mathrm{exp}\left(f\left(x\right)\right),x\right)$
 $\frac{{{ⅇ}}^{{f}{}\left({x}\right)}}{{a}}$ (30)
 > $\mathrm{int}\left(f\left(\mathrm{sin}\left(x\right)\right),x\right)$
 ${x}{}{f}{}\left({\mathrm{sin}}{}\left({x}\right)\right){-}{a}{}\left({\mathrm{cos}}{}\left({x}\right){+}{x}{}{\mathrm{sin}}{}\left({x}\right)\right)$ (31)
 > $\mathrm{limit}\left(f\left(x\right),x=0\right)$
 ${0}$ (32)

Use undefine to completely remove the definition of a procedure defined using the define command.

 > $\mathrm{undefine}\left(f\right)$
 > $\mathrm{diff}\left(f\left(x\right),x\right)$
 $\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)$ (33)
 > $\mathrm{limit}\left(f\left(x\right),x=0\right)$
 $\underset{{x}{\to }{0}}{{lim}}{}{f}{}\left({x}\right)$ (34)