next up previous contents
Next: Functions Up: Maple basics Previous: Defining a name

Sequences, lists, sets

Many mathematical expressions in Maple follow universal conventions adopted by many computer algebra and programming systems. For example, a^b means exponentiation $a^b$ as usual. Maple has a fairly unique approach to collections of mathematical expressions, though. The basic construction is a sequence of expressions separated by commas, that is, something like: \begin{displaymath} expr_1,\; expr_2,\; \dots,\; expr_n \end{displaymath} Sequences can be generated by several different means. One can simply type a list of expressions separated by commas, or one can use sequence building commands as follows:

> i^2$i=1..10;

\begin{maplelatex} \begin{displaymath} 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 \end{displaymath}\end{maplelatex}
The expression i^2 is evaluated at each value in the range i=1..10. The $ separates the expression from the range, and is the sequence binary operator.

A slightly more flexible method is the seq command, as in

> seq( (1+x)^i, i=1..5);

\begin{maplelatex} \begin{displaymath} 1 + x, (1 + x)^{2}, (1 + x)^{3}, (1 + x)^{4}, (1 + x)^{5} \end{displaymath}\end{maplelatex}
Note that the x is left as an unspecified symbol, and the binomials are not expanded by default. Simplification is controlled by various Maple commands, which we'll discuss later.

The terms of a sequence are called operands. Any mathematical expression is built out of applying mathematical transformations to a sequence of operands. Maple provides a command op for examining the list of operands in any mathematical expression; for example,

> p1:=(x^2+y^2)^3;

\begin{maplelatex} \begin{displaymath} {\it p1} := (x^{2} + y^{2})^{3} \end{displaymath}\end{maplelatex}
> op(p1);

\begin{maplelatex} \begin{displaymath} x^{2} + y^{2}, 3 \end{displaymath}\end{maplelatex}
We see that the previous algebraic expression has two main operands. This is a very handy command for examining the operands of a complicated expression.

Lists and sets have a little more structure than just a sequence. They are two different ways of packaging up a sequence as a single expression. A list is a sequence enclosed in square brackets [], and means simply list the sequence in order without considering whether or not there are duplicate expressions in the list. A set is a sequence enclosed in curly braces {}, and means that duplicate expressions should be eliminated. In other words, just consider the mathematical set of expressions in the list.

> ss:= 1,2,3,2,1,3,4,2,2,2,3,4;

\begin{maplelatex} \begin{displaymath} {\it ss} := 1, 2, 3, 2, 1, 3, 4, 2, 2, 2, 3, 4 \end{displaymath}\end{maplelatex}
> ll:= [ss];

\begin{maplelatex} \begin{displaymath} {\it ll} := [1, 2, 3, 2, 1, 3, 4, 2, 2, 2, 3 , 4] \end{displaymath}\end{maplelatex}
> mm:={ss};

\begin{maplelatex} \begin{displaymath} {\it mm} := \{1, 2, 3, 4\} \end{displaymath}\end{maplelatex}
Here is the command for counting the terms in either a list or a set (it can be used only on a single expression, hence, not on a sequence):
> nops(ll);

\begin{maplelatex} \begin{displaymath} 12 \end{displaymath}\end{maplelatex}
> nops(mm);

\begin{maplelatex} \begin{displaymath} 4 \end{displaymath}\end{maplelatex}
To retrieve a sequence back from a list or set, use the op command:
> op(mm);

\begin{maplelatex} \begin{displaymath} 1, 2, 3, 4 \end{displaymath}\end{maplelatex}

To use just the $i$-th term of a set or list, just use the index $i$ in square brackets after the name of the list or set.

> mm[2];

\begin{maplelatex} \begin{displaymath} 2 \end{displaymath}\end{maplelatex}
This can also be done with the op command with an optional first argument of the index $i$.
> op(2,mm);

\begin{maplelatex} \begin{displaymath} 2 \end{displaymath}\end{maplelatex}
The op command can be used in this way on any mathematical expression to get at its parts.
> p1:= (x^2+y^2)^3;

\begin{maplelatex} \begin{displaymath} {\it p1} := (x^{2} + y^{2})^{3} \end{displaymath}\end{maplelatex}
> op(1,p1);

\begin{maplelatex} \begin{displaymath} x^{2} + y^{2} \end{displaymath}\end{maplelatex}
op(%);

\begin{maplelatex} \begin{displaymath} x^{2}, y^{2} \end{displaymath}\end{maplelatex}
Here we also used the Maple convention that % refers to the immediately previous expression entered.

There are many operators on sets and lists, for reversing or otherwise permuting the list, concatenating lists, taking unions and intersections of sets, etc. The help page on sets and lists is brought up by the command ?list.


next up previous contents
Next: Functions Up: Maple basics Previous: Defining a name
David J. Wright
2003年03月12日

AltStyle によって変換されたページ (->オリジナル) /