Maple V by Example

Data Types and Operations Thereof

Variables do not need to be declared in Maple. The assignment operator := will give to a variable the data structure of what is to the right of it. Data type errors never occur during assignment but only if you try to apply a function to an incompatible data type.


Numerical

Maple handles integers and decimal numbers (real or complex) differently. Maple can perform calculations on integers with as many digits as you like; it is only limited by computer memory (RAM) and how Maple utilizes that memory. Furthermore, if an expression is input in terms of integers (and Pi and maybe some other constants), Maple doesn't spit out a decimal expression unless specifically told to do so. For example, sqrt(3); doesn't put out 1.732050808 but leaves it as the square root of 3 in standard math notation. In fact, the output of cos(Pi/6); is the square root of 3 over 2, in standard math notation.

To get a decimal expression of a mathematical expression use, evalf. evalf(expr,n) requires the expression expr and outputs a decimal expansion with n digits. If n is not provided, evalf utilizes for n the value stored in the internal variable Digits which can be changed simply by typing Digits:=30;for example. Here are a few examples of evalf:
evalf(Pi); Evaluates Pi to the 10th decimal place (assuming that Digits=10, the default value.)
evalf(5/3*exp(-2+3*I)*sin(Pi/4),15); Displays: -.157898022493763+.225078172647505e-1*I


Algebraic expresions

One of the powers of Maple comes from the fact that it can manipulate algebraic expressions. One can say for example, f:=cos(x)^2-1. There are various packages that simplify the manipulation of algebraic, trigonmetric and other expressions. Furthermore, having defined f with respect to the variable x.


Functions

Here are examples of declaration of functions:
f := x -> x^2-x+1; f is the function that maps x (integer, real or complex) to x2-x+1.
f := x -> 8*x^4-8*x^2+1;
g := (x,y) -> f(x)+f(y);
f is a single variable function but g is a function from 2 variables to a single variable.
tor1 := (u,v) -> [cos(u)*(2+cos(v)), sin(u)*(2+cos(v)),sin(v)]; This is a parametrization of a torus in 3 dimensions. Note that the output is surrounded by brackets [ ] and not parentheses.

In Maple, you can quickly and easily compose functions as long as data types of inputs and outputs of the functions match up. Furthermore, one can use the "@@" for iterated composition. For example:

f := x -> cos(x);
g:=x->1-x^2;
      Then type:
(g@f)(x);
The output of the third line is:
1-cos(x)2
f:=x->x^2-1;
(f@@5)(0.5);
The output ofter the second line gives the value obtained by repeatedly applying f to 0.5, five times. That is to say f(f(f(f(f(0.5))))).
 
The following example is useful in studying discrete dynamical systems:
G := x-> x^2-1;
solve((G@@2)(x)=x,x);
The output of the last line gives expressions for fixed points of the function G2(x). These solutions represent fixed points of the function G as well as the points which are on a periodic orbit of period 2.

Maple can also handle function that are defined in a piecewise manner. Examples:
ABS := proc(x)
   if x<0 then
      -x;
   else
      x;
   fi;
end:
This construction gives us the absolute value function. Note that fi; indicates to Maple the end of the conditional if statement.
HAT := proc(x)
   if type(x, numeric) then
      if x<=0 then 0;
      elif x<=1 then x;
      elif x<=2 then 2-x;
      else 0;
      fi;
    else
       'HAT'(x);
    fi;
end:
This procedure first checks that the parameter value x is of type numeric. If x isn't a numeric, then the procedure simply returns HAT(x) as an algebraic expression. Note that elif is an "else-if" command. This construct is one of the easier ways of working with cases in Maple.


Booleans

Booleans are easy. They can be declared as so:
isdone:=false; Booleans like this will typically be used when programming.


Strings

Strings are also easy. They can be declared as so:
str:="Hello"; These aren't used all that often but have some use in programming.
A.1:=(x-1)^2+1; The "." between A and 1 means string concatenation. The construct shown to the left creates a variable name of A1 and assigns the algebraic expression (x-1)2+1 to it.


Expression Sequences, List, Arrays and Sets

We describe each of these related data types by examples in the following table:
Data TypeExample
Expression Sequence
As its names suggests, this data type is essential just a list of objects without any further structure. Each object is seperated by a comma.
s:= 1, x, y;
List
This data type is an ordered list of objects. The elements of the list can be accessed using [ index ] notation as in many other computer languages except that the indexing starts at 1 rather than at 0..
s:= [1, x, y];
s[2];

This last command will return x.

pts:=[[0,0],[1,0],[1,1],[0,1]];
This line defines a list of four points, each point being a list one its own.

Arrays
A list of a list does not in general have the structure of a matrix which is sometimes desired. The examples show how to make lists into matrix arrays. One typically needs the package linalg.
with(linalg):

A1:=array([[0.5,0],[0,0.5]]);
p1:=vector([0,0]);

A1 is a 2-by-2 matrix and p1 is a column vector.
Sets
A set is an ordered list and hence specific elements cannot be referenced as with the list structure. However, it is more natural to use a set than a list for some applications: mapping a function on a set of numbers, deciding set membership, displaying more than one plot structure at a time,...
set:={1,2,3,4,5};
set2:={x^2,x,x^2-2};
set3:={[0,1],[0,0],[1,0]};

There are a number of commands that are special to lists and set structures. One shouldn't underestimate their usefulness since they often allow one to do something in one line that would otherwise require a seperate procedure.
CommandExplanationExamples
seq Takes a sequence of integers and applies a function integer, returning a list. seq(i^2, i=1..5);

X:={seq(i,i=0..6)};
map The map command applies a function to the operands of an expression. (Takes time getting used to.) map(f,x+y*z);
f(x)+f(yz)

map(f,{a,b,c});
{f(a),f(b),f(c)}
op Can extract one or more element from a list. op(2,u); returns the second element in the list u.
op(u);turns an ordered list into just a sequence.
nops(u); returns the number of elements in the list u.


The Range Data Type

The range data type is declared like so: r:=0..10;. Depending on context (and whether the left end and right end of the range are integers) the range data type stands for a sequence of integers or a whole range of real numbers. Examples:
int(f(x), x=1..3.5); This command attempts to evaluate the integral of some function f between 1 and 3.5.
x.(1..5); This command returns the list of expressions:
x1,x2,x3,x4,x5


Plot Structures

In Maple, one should consider a plot structure as a data type in its own right. This is because one often needs to utilizes sets or list of plot objects in order to display them simultaneously or to create an animated gif file, for example. The plot object is in fact a complicated array but for all intents and purposes, we don't need to know the internal structure of the plot array.

There is no unified way to create a plot object in Maple. Some methods are more common than others, but depending on one's goal one command many be more helpful (or perhaps required). The output list that Maple displays following the command with(plots); often gives a good indication of what plot functions are already available. For many plot related commands, there are numerous settings and options which can be modified such as: accuracy (grid), axes style, color, etc... See the help files for more information on all these different options.

Here are a number of examples which illustrate various ways of creating a plot object:
plot(cos(x)+sin(x),x=0..Pi); This displays the graph of the function cos(x)+sin(x) over the range 0 to p.
f:=x->x^2-x+1;
plot(f(x),x=1..3);
This plots the user defined function f over the specified interval.
f:=x->x^2-x+1;
plot(f,1..3);
When working with functions of one variable, it isn't necessary to specify the variable in the plot function.
plot(tan(x),x=-2*Pi..2*Pi, y=-4..4, discont=true); The discont option can be used when we knoe that the function has discontinuities over the specified interval. Note that the range for y tells Maple when to cut off the graph of tan.
plot([sin(x),x-x^3/6], x=0..2, color=[red,blue], style=[point,line]); This plot command displays the graph of two functions: 1) it displays sin(x) in red and using only points, i.e. it doesn't connect the points; 2) the second function in blue, with points connected.
plot3d(sin(x+y),x=-1..1,y=-1..1); This plots the two dimensional graph in 3-space of the two variable function sin(x+y).
A1:=implicitplot3d(x^3+y^3+z^3-1, x=-3..3, y=-3..3, z=-3..3, grid=[20,20,20]):
A2:=plot3d(x^2+y^2, x=-2..2, y=-1..3):
display({A1,A2});
This sequence of commands defines one three dimensional plot object using the implicitplot3d command and then defines a second three dimensional plot object by displaying the graph of a function. Both these plot objects are assigned to variables and neither are displayed. The display command takes a list or set of plot objects and combines them into a single image and shows the image.
One can construct and manipulate a plot object directly by using the PLOT and PLOT3D commands. To get a very good feel for these, one should consult a Maple programming book.
pts:=[[0,0],[1,0],[1,1],[0,1],[0,0]];
PLOT(CURVES(pts));
This plots the corners of the unit square and draws lines between each successive point.
PLOT(POINTS(seq([i,i^2],i=-2..2), SYMBOL(DIAMOND))); This command plots just the points [-2,4], [-1,1],...,[2,4] but uses a diamond symbol to indicate them.

Outputing a plot structure to a file:
Maple has the nice feature of being able to output the plot displays to a file. To get comprehensive information on this, you should look up the Maple help files on the key words interface or plotdevice. However, I can provide a brief introduction here. The main command to use looks like the following:
      interface(plotdevice=gif, plotoutput=myfile2, plotoptions="transparent=true");

After executing this command, all Maple commands for displaying or plotting an object will put the output to a gif file, with the name myfile2, and with a transparent background. (I'm not always sure where to look for myfile2; I believe it gets created in the directory from which the Maple file was opened.) One can change file style by changing the value of plotdevice. See the help file for more file types. Also, various file types can take different plotoptions. An additional option that is nearly standard to all file types is the width and height of the output image.

Notice that if you use the animate or animate3d command to create your plot structure, outputting to a gif file will create an animated gif file.

The following code creates the 3D animated Triple Tchebycheff surface that is shown in the Visualizing in 4D part of my personal webpage.
with(plots):
f:=x->8*x^4-8*x^2+1;
Pict := (n,c) -> implicitplot3d(f(x)+f(y)+f(z)=n, x=-1.2..1.2, y=-1.2..1.2, z=-1.2..1.2, grid=[30,30,30], scaling=unconstrained, color=c, style=patchnogrid, light=[45,45,1,1,1]):
A1:=Pict(2,COLOR(HUE,1.0)):
A2:=Pict(1.5,COLOR(HUE,0.9)):
A3:=Pict(1,COLOR(HUE,0.8)):
A4:=Pict(0.5,COLOR(HUE,0.7)):
A5:=Pict(0,COLOR(HUE,0.6)):
A6:=Pict(-0.5,COLOR(HUE,0.5)):
A7:=Pict(-1,COLOR(HUE,0.4)):
A8:=Pict(-1.5,COLOR(HUE,0.3)):
A9:=Pict(-2,COLOR(HUE,0.2)):
A10:=Pict(-2.5,COLOR(HUE,0.1)):
A11:=Pict(-2.99,COLOR(HUE,0)):
interface(plotdevice=gif, plotoutput=myfile2, plotoptions="transparent=true");
display([A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11],insequence=true);


The Procedure Object

The more advanced Maple user can extend the capabilities of Maple by programming his or her own procedures. Maple programming is a long subject in and of itself. All I can do here is refer the reader to some of the examples of procedures given below. As always, one can think of procedures as a data type of its own


Programming Binary Operators

OperatorMeaning
+addition
-subtraction
*multiplication
/division
^exponentiation
**exponentiation
$sequence operator
@composition
@@repeated composition
::type declaration (in procedures)
&stringneutral operator
.string concatenation
.decimal point
..ellipsis (ranges)
,expression seperator
:=assignment
OperatorMeaning
<less then
<=less than or =
>greater than
>=greater than or =
=equal
<>not equal
->arrow operator
modmodulo
unionunion of sets
minusset difference
intersectset intersection
andlogical and
orlogical or
&*non-commutative multiplication
::pattern binding




Previous     Top     Table of Contents     Next