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.
In the first example, we use the keyword linear:
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).
One can use define for functional programming:
A nice way to define the greatest common divisor of integers:
The keyword multilinear can be used to define multilinear functions:
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:
Define an integration procedure: integration is linear, ∫aⅆx equals a⁢x when a does not depend on x .
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:
And now the integral for powers of x :
An example with the keyword diff and the command diff :
We define the derivative of P to be 1P:
Define properties of a function f which is linear, has a derivative of a, and for which f⁡1=1.
Check the derivative of f:
Given that f is linear:
Now Maple can compute the following integral using the fact that f is linear and has derivative a:
Even nested functions with f can be integrated:
Since the derivative is given, we can compute limit and series:
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
What kind of issue would you like to report? (Optional)