Define
With the define command, evaluation and simplification properties for functions and operators can be defined. Properties are specified by both keywords and by equations that use the syntax of the pattern matcher (patmatch command). The define command then creates a procedure that implements the function and its properties. Note that properties are used in the order that they are given; therefore, if one pattern is more general than the other, it has to be specified first: for example, f(x)=x should be defined before fa∷algebraic=ga.
restart
First Examples
In the first example, we use the keyword linear:
definef,linear,f⁡1=tt:
f2⁢x+4
2⁢f⁡x+4⁢tt
defineg,ga∷algebraicn∷nonunitinteger=n ga,ga::realcons=a:
g⁡x2;
2⁢g⁡x
gπ⁢2
π⁢2
To define commutative and associative operations, use the keywords orderless and flat. orderless means that the arguments of a function have no order, and flat means that f(a,f(b,c)) is equal to f(a,b,c).
defineh,orderless,flat:
ha,b−hb,a
0
ha,b,c−hha,b,c
One can use define for functional programming:
definefac,fac⁡0=1,fac⁡n::posint=n⁢fac⁡n−1:
fac5
120
definefib,fib⁡0=1,fib⁡1=1,fib⁡n::posint=fib⁡n−1+fib⁡n−2:
fib7
21
fib15
987
A nice way to define the greatest common divisor of integers:
defineGCD,GCD⁡a::integer,0=a,GCD⁡a::integer,b::integer=GCD⁡b,amodb:
GCD6,3
3
GCD120,12⋅120
GCD13,11
1
The keyword multilinear can be used to define multilinear functions:
defineH,multilinear:
H2⁢x,x+3
2⁢H⁡x,x+6⁢H⁡x,1
Now we use the definemore command to add to the existing definition of H the rule of commutativity (keyword orderless), and a simplification rule for some special arguments:
definemoreH,orderless,H⁡a::realcons,b::algebraic=a⁢H⁡b:
2⁢H⁡x,x+6⁢H⁡x
H⁡2⁢x,x+y
2⁢H⁡x,x+2⁢H⁡x,y
Further Examples
Define an integration procedure: integration is linear, ∫aⅆx equals a⁢x when a does not depend on x .
defineINT,linear,conditionalINT⁡a::algebraic,X::name=a⁢X,_type⁡a,freeof⁡X,INTX::name,X::name=12⋅X2:
INT2⁢x+4,x
x2+4⁢x
INTz+x,z
12⁢z2+x⁢z
We now define the integral of 1a⁢x+b :
definemoreINT,conditional⁡INT⁡1a::algebraic⁢X::name+b::algebraic,X::name=ln⁡a⁢X+ba,_type⁡a,freeof⁡X &and _type⁡b,freeof⁡X:
INT1x,x
ln⁡x
INT12⁢x+3,x
12⁢ln⁡2⁢x+3
And now the integral for powers of x :
definemoreINT,INTx::namen::nonunit⁡integer,x::name=1n+1⋅xn+1:
INT2⁢x2,x
23⁢x3
An example with the keyword diff and the command diff : We define the derivative of P to be 1P:
defineP,diffPx,x=1Px:
diffPx,x
1P⁡x
diffPⅇz,z
ⅇzP⁡ⅇz
diff1/Px2,x
−2⁢xP⁡x23
Define properties of a function f which is linear, has a derivative of a, and for which f⁡1=1.
unassignf:
definef,linear,difffx,x=a
Check the derivative of f:
diff⁢fx,x
a
Given that f is linear:
f⁡0
Now Maple can compute the following integral using the fact that f is linear and has derivative a:
∫f⁡xⅆx
x⁢f⁡x−12⁢x2⁢a
Even nested functions with f can be integrated:
∫ⅇf⁡xⅆx
ⅇf⁡xa
∫f⁡sin⁡xⅆx
x⁢f⁡sin⁡x−a⁢cos⁡x+x⁢sin⁡x
Since the derivative is given, we can compute limit and series:
limx→0f⁡x;
seriesf⁡x,x=0
a⁢x+Ox6
An Example with an Infix Operator
Using the neutral operator &m, we define:
define`&m`,flat,orderless,`&m`a∷realcons, b∷realcons = a b:a &m b
a &m b
3 &m 2 &m a
6 &m a
We can add more properties:
definemore`&m`, `&m`A∷algebraic,A∷algebraic=A: x &m x &m 4
4 &m x
a &m b−b &m a
The syntax for define is described in the help page of define, while the syntax for patterns is described in the help page of patmatch.
Return to Example Worksheets Index
Download Help Document