Defining your own words

Maple's mathematical vocabulary is extensive, but it can never be complete. That's because we are always discovering new ways to do things. The methods can then be defined in a Maple procedure.

One way to develop the definition is in a series of input cells: For example, suppose we wanted to define a word to solve a quadratic equation a [Maple Math] . We want the inputs to be the coefficients a,b,c of the equation, and the output to be the roots and a message describing the natrue of the roots. So given the inputs values in an input cell. Now develop the body of the procedure in an input cell below it. Thus the nature of the roots is determined by the discriminant [Maple Math] , so we need to use a conditional statement here.

> a := 3 : b:= 0: c := 5:

> if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;

[Maple Math]

After changing the values of a, b, and c in the input cell and testing the body of the procudure, we can define the procedure in an input cell. Use the copy and paste item in the edit menu to put the body into the cell below then at the top put the 'proc line' and at the bottom put the 'end line'.

> quadroots := proc(a,b,c) # the proc line
if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;
end; # the end line

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

Now, test the procedure again

> quadroots(2,3,4);

[Maple Math]

> quadroots(2,sqrt(32),4);

[Maple Math]

[Maple Math]

> quadroots(2,10,4);

[Maple Math]

[Maple Math]

At this point, you have added a word to the Maple language. It can be used just like any of the other words. It is not a permanent addition, however. The next time you run Maple, it won't be there, unless you make some special effort.

One way to keep all your procedures together is to put them into a "package". A package in Maple is a table whose indices are names of procedures and whose entries are the bodies of those procedures. For example, in this worksheet, we have defined two procedures that we might like to keep in a package, quadroots and one further back myabs.

We could call the package 'mystuff'.

> mystuff := table([]);

[Maple Math]
[Maple Math]

> mystuff[quadroots]:= proc(a,b,c) # the proc line
if b^2 - 4*a*c < 0 then print("negative discriminant. no real roots.")
elif b^2 - 4*a*c = 0 then print("discriminant is 0. 1 real root.");
(-b +sqrt(b^2-4*a*c))/(2*a)
else print("discriminant is positive, two real roots.");
(-b +sqrt(b^2-4*a*c))/(2*a),(-b -sqrt(b^2-4*a*c))/(2*a) fi;
end: # the end line
mystuff[myabs]:= proc(x) if x > 0 then x else -x fi end:
with(mystuff);

[Maple Math]

Now in order to use the procedures in the 'mystuff' package, load a worksheet containing the package and execute the cell containing the definitions. Include the line with(mystuff) at the bottom to complete the loading process.