6-Data Structures - Maple Help

Home : Support : Online Help : Getting Started : Tutorials : 6-Data Structures

Part 6: Data Structures

 Introduction Maple tutorials are designed to help you get started with Maple, learn about the key tools available in Maple, and lead you through a series of problems.   In Part 6: Data Structures, you will learn about Maple data structures.   To try this material on your own, start with an empty Maple document. Perform the steps described in the left column of each table below.  The results of the steps are displayed in the right column for reference.   Refer to Help>Quick Reference for basic getting started tips.   Note for non-Windows users: The keystrokes given in this document are for Windows.  There will be differences for other platforms. If you are using a different platform, see Shortcut Keys.

Data Structures in Maple

Maple has a variety of data structures, providing you with the tools for any task.  You have seen matrices already.  Other data structures include sequences, sets, lists, arrays, tables, and modules.

Sequences

Steps

Results

Expression sequences (or simply sequences) are created using the comma operator.

Example:

$s≔1,2,3,a,b,c$ assigns the name $s$ to the sequence.

In Maple, sequences form the basis of many data types. In particular, they appear in function calls, lists, sets, and subscripts. 



To select members of a sequence, use index notation: refers to the $i$th element of the sequence $s$.

You can also use subscript notation, ${s}_{i}$, to refer to the $i$th element.  Use Ctrl + underscore (_) to enter the subscript.

Note:  Index and subscript notation are available when using the 2-D math editor. In places where Maple syntax is required, such as the start-up code region, index notation must be used.









$s≔1,2,3,a,b,c$

 ${s}{≔}{1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}$ (2.1.1)







$s\left[1\right]$

 ${1}$ (2.1.2)





${s}_{2}$

 ${2}$ (2.1.3)

${s}_{4}$

 ${a}$ (2.1.4)



The seq Command

The seq command is used to construct a sequence of values.

The calling sequence used here is seq(f(i), i=1..n), which generates the sequence f(1), f(2), ..., f(n).  For more information, see seq.





$\mathrm{seq}\left(i,i=1..10\right)$

 ${1}{,}{2}{,}{3}{,}{4}{,}{5}{,}{6}{,}{7}{,}{8}{,}{9}{,}{10}$ (2.1.5)

$\mathrm{seq}\left({k}^{2},k=1..5\right)$

 ${1}{,}{4}{,}{9}{,}{16}{,}{25}$ (2.1.6)



Sets

Steps

Results

A set is an unordered sequence of distinct expressions enclosed in braces {}.

Note: Maple removes duplicate members from a set. The sequence can be empty, so {} represents an empty set.

Notice that when the set $\mathrm{S1}$$≔\left\{a,b,x,y,z,a\right\}$ is evaluated, the duplicate $a$ is removed.



$\mathrm{S1}≔\left\{a,b,x,y,z,a\right\}$

 ${\mathrm{S1}}{≔}\left\{{a}{,}{b}{,}{x}{,}{y}{,}{z}\right\}$ (2.2.1)

$\mathrm{S2}≔\left\{1,2,b,c\right\}$

 ${\mathrm{S2}}{≔}\left\{{1}{,}{2}{,}{b}{,}{c}\right\}$ (2.2.2)

${}$

Set Arithmetic

To perform set arithmetic, use the symbols from the Common Symbols palette or use the operators $\mathbf{union}$, $\mathbf{intersect}$, and $\mathbf{minus}$.

Example:

Find the union, intersection and set difference of the sets $\mathrm{S1}$ and $\mathrm{S2}$.

Testing Set Membership

Example:

To test for set membership, use the command member.

Alternatively, use the set notation to form the statement  then use evalb (evaluate boolean) to evaluate the expression to get true or false. The keyword $\mathbf{in}$ can be used instead of the ∈ symbol.











$\mathrm{S1}\bigcup \mathrm{S2}$ = $\left\{{1}{,}{2}{,}{a}{,}{b}{,}{c}{,}{x}{,}{y}{,}{z}\right\}$

$\mathrm{S1}\bigcap \mathrm{S2}$ = $\left\{{b}\right\}$

$\mathrm{S1}\setminus \mathrm{S2}$ = $\left\{{a}{,}{x}{,}{y}{,}{z}\right\}$



= $\left\{{1}{,}{2}{,}{a}{,}{b}{,}{c}{,}{x}{,}{y}{,}{z}\right\}$

= $\left\{{b}\right\}$

= $\left\{{a}{,}{x}{,}{y}{,}{z}\right\}$







$\mathrm{member}\left(x,\mathrm{S1}\right)$

 ${\mathrm{true}}$ (2.2.3)

$\mathrm{member}\left(1,\mathrm{S1}\right)$

 ${\mathrm{false}}$ (2.2.4)



$\mathrm{evalb}\left(x\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}∈\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\mathrm{S1}\right)$

 ${\mathrm{true}}$ (2.2.5)

 ${\mathrm{false}}$ (2.2.6)







Selecting Elements

Use the same selection notation as for sequences: $s\left[i\right]$ and ${s}_{i}$ both represent the $i$th element in the set $s$.

Example:

Select the first and third elements of $\mathrm{S1}$











$\mathrm{S1}\left[1\right]$

 ${a}$ (2.2.7)

${\mathrm{S1}}_{3}$

 ${x}$ (2.2.8)



Maple has a type-checking function.

The commands whattype returns the Maple type.  The command type does type-checking.

Tip:  Type-checking is useful in programming Maple procedures.





$\mathrm{whattype}\left(\mathrm{S1}\right)$

 ${\mathrm{set}}$ (2.2.9)

$\mathrm{type}\left(\mathrm{S1},\mathrm{set}\right)$

 ${\mathrm{true}}$ (2.2.10)



Lists

Steps

Results

A list is an ordered sequence of expressions enclosed between square brackets [ ]. The ordering of the list is the same as the sequence ordering. Also unlike sets, duplicate members are retained in the list. In the case where a sequence is empty, [ ] represents an empty list.

Lists can be created by enclosing a defined sequence in square brackets.

Example:

Create a list from the sequence $s$.

The elements of a list can be any expressions, even other lists.

Maple gives nested lists whose inner lists have the same number of elements a special name, listlist.



$\mathrm{L1}≔\left[x,y,z,x\right]$

 ${\mathrm{L1}}{≔}\left[{x}{,}{y}{,}{z}{,}{x}\right]$ (2.3.1)

${}$









$s$

 ${1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}$ (2.3.2)

$\mathrm{L2}≔\left[s\right]$

 ${\mathrm{L2}}{≔}\left[{1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}\right]$ (2.3.3)

$\mathrm{L3}≔\left[\mathrm{L1},\left[1,2\right],3,\mathrm{L2}\right]$

 ${\mathrm{L3}}{≔}\left[\left[{x}{,}{y}{,}{z}{,}{x}\right]{,}\left[{1}{,}{2}\right]{,}{3}{,}\left[{1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}\right]\right]$ (2.3.4)

$M≔\left[\left[a,b\right],\left[1,2\right],\left[4,5\right],\left[3,7\right]\right]$

 ${M}{≔}\left[\left[{a}{,}{b}\right]{,}\left[{1}{,}{2}\right]{,}\left[{4}{,}{5}\right]{,}\left[{3}{,}{7}\right]\right]$ (2.3.5)

$\mathrm{whattype}\left(M\right)$

 ${\mathrm{list}}$ (2.3.6)

$\mathrm{type}\left(M,\mathrm{listlist}\right)$

 ${\mathrm{true}}$ (2.3.7)

$\mathrm{type}\left(\mathrm{L3},\mathrm{listlist}\right)$

 ${\mathrm{false}}$ (2.3.8)



Selecting Elements

Use the same selection notation as for sequences and sets: $s\left[i\right]$ and ${s}_{i}$ both represent the $i$th element in the list $s.$

Selection operations can be combined to select elements from nested lists.

Selecting partial lists

Select a range of elements from a list by using $i..j$ to specify a range of indices. Leaving off an end point means "from the beginning" or "until the end."





$\mathrm{L1}$

 $\left[{x}{,}{y}{,}{z}{,}{x}\right]$ (2.3.9)

$\mathrm{L1}\left[3\right]$

 ${z}$ (2.3.10)

$M\left[1\right]$

 $\left[{a}{,}{b}\right]$ (2.3.11)

$M\left[1\right]\left[2\right]$

 ${b}$ (2.3.12)



$\mathrm{L1}\left[2..3\right]$

 $\left[{y}{,}{z}\right]$ (2.3.13)

$\mathrm{L1}\left[..3\right]$

 $\left[{x}{,}{y}{,}{z}\right]$ (2.3.14)

$\mathrm{L1}\left[3..\right]$

 $\left[{z}{,}{x}\right]$ (2.3.15)

${}$

Testing List Membership

Example:

To test for list membership, use the command member.

You can also determine the position of an element in a list. If member returns true and a third argument, for example 'i', is included in the calling sequence, the element's position in the list is assigned to i.





$\mathrm{member}\left(x,\mathrm{L1}\right)$

 ${\mathrm{true}}$ (2.3.16)











$\mathrm{member}\left(y,\mathrm{L1},'i'\right)$

 ${\mathrm{true}}$ (2.3.17)

$i$

 ${2}$ (2.3.18)





The op and numelems Commands

The op command can be used to extract elements (or operands) from any Maple data structure, including lists. It is particularly useful when modifying existing lists.

The numelems command returns the number of elements.

Example:

Extract all the elements from the list L1.

Find the number of elements in L1.

Concatenating Lists

Use the op command to extract the expression sequence of elements from individual lists. Concatenate two lists by creating a new list from the expression sequences.

The same method is used to add an element to a list.







$\mathrm{L1}$

 $\left[{x}{,}{y}{,}{z}{,}{x}\right]$ (2.3.19)





$\mathrm{op}\left(\mathrm{L1}\right)$

 ${x}{,}{y}{,}{z}{,}{x}$ (2.3.20)



$\mathrm{numelems}\left(\mathrm{L1}\right)$

 ${4}$ (2.3.21)





$\mathrm{op}\left(\mathrm{L1}\right)$

 ${x}{,}{y}{,}{z}{,}{x}$ (2.3.22)

$\mathrm{op}\left(\mathrm{L2}\right)$

 ${1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}$ (2.3.23)

$\left[\mathrm{op}\left(\mathrm{L1}\right),\mathrm{op}\left(\mathrm{L2}\right)\right]$

 $\left[{x}{,}{y}{,}{z}{,}{x}{,}{1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}\right]$ (2.3.24)



$\left[X,Y,\mathrm{op}\left(\mathrm{L2}\right)\right]$

 $\left[{X}{,}{Y}{,}{1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}\right]$ (2.3.25)



Replacing Elements in a List

Replace an element in a list by assigning a new value to that position.

If a list has duplicates and you want to replace all occurrences, use the eval command.

Example:

Replace "a" by "A" everywhere it appears in the list L4.



$\mathrm{L1}$

 $\left[{x}{,}{y}{,}{z}{,}{x}\right]$ (2.3.26)

${\mathrm{L1}}_{4}≔\mathrm{α}$

 ${\mathrm{L1}}{≔}\left[{x}{,}{y}{,}{z}{,}{\mathrm{\alpha }}\right]$ (2.3.27)

$\mathrm{L1}$

 $\left[{x}{,}{y}{,}{z}{,}{\mathrm{\alpha }}\right]$ (2.3.28)







$\mathrm{L4}≔\left[a,b,a,c,d,a\right]$

 ${\mathrm{L4}}{≔}\left[{a}{,}{b}{,}{a}{,}{c}{,}{d}{,}{a}\right]$ (2.3.29)

$\mathrm{eval}\left(\mathrm{L4},a=A\right)$

 $\left[{A}{,}{b}{,}{A}{,}{c}{,}{d}{,}{A}\right]$ (2.3.30)



Sorting a List

The sort command sorts the elements of a numerical list into ascending order.





 $\left[{-3.7}{,}{1}{,}{1}{,}{2}{,}{4.2}{,}{5}{,}{8}\right]$ (2.3.31)



For more commands for with lists, see ListTools.

MultiSets

Steps

Result

A MultiSet is a data structure that can be used to manage unordered collections of data while accounting for multiple occurrences of particular members.

Example:

a collector of baseball cards might have 3 "Ty Cobb" cards, 1 "George Brett", and 2 "Nolan Ryan". This collection can be represented as a MultiSet:

 ${\mathrm{MyCards}}{≔}\left\{\left[{"Ty Cobb"}{,}{3}\right]{,}\left[{"George Brett"}{,}{1}\right]{,}\left[{"Nolan Ryan"}{,}{2}\right]\right\}$ (2.4.1)

If the collector then obtains 2 more "Ty Cobb" cards and a "Reggie Jackson", these cards can be added to the collection with the  command to the right:

 ${\mathrm{MyCards}}{≔}\left\{\left[{"Ty Cobb"}{,}{5}\right]{,}\left[{"George Brett"}{,}{1}\right]{,}\left[{"Nolan Ryan"}{,}{2}\right]{,}\left[{"Reggie Jackson"}{,}{1}\right]\right\}$ (2.4.2)

For a standard MultiSet, the multiplicity of any element must be a non-negative integer (elements with multiplicity 0 are removed from the MultiSet).  A more general structure, allowing any real number to represent the "multiplicity" is available by attaching the index generalized to the MultiSet call:

 ${M}{≔}\left\{\left[{a}{,}\frac{{1}}{{2}}\right]{,}\left[{b}{,}{-3.1415}\right]{,}\left[{c}{,}{2}\right]\right\}$ (2.4.3)

This kind of MultiSet can be produced by the convert command when applied to a rational function:

 $\left\{\left[{x}{-}{2}{,}{2}\right]{,}\left[{{x}}^{{2}}{+}{1}{,}{3}\right]{,}\left[{{x}}^{{2}}{+}{3}{}{x}{-}{1}{,}{-4}\right]\right\}$ (2.4.4)

Applying a Function to the Elements of a Set or List

Steps

Result

To apply a function or procedure to the elements of a set or list, use map or zip.

Example:

Apply sin to all the elements in S1.

Square all the elements of S1.

To apply a function of two variables f(x,y) to the elements of two lists, use the zip command.  The syntax is zip(f, list1, list2).

Example:

Multiply the elements of A1 and A2 together.

Find the pairwise greatest common denominator of the elements of the lists A1 and A2.  Use igcd to find the greatest common denominator for the integers.



$\mathrm{S1}$

 $\left\{{a}{,}{b}{,}{x}{,}{y}{,}{z}\right\}$ (2.5.1)

$\mathrm{map}\left(f,\mathrm{S1}\right)$

 $\left\{{f}{}\left({a}\right){,}{f}{}\left({b}\right){,}{f}{}\left({x}\right){,}{f}{}\left({y}\right){,}{f}{}\left({z}\right)\right\}$ (2.5.2)







$\mathrm{map}\left(\mathrm{sin},\mathrm{S1}\right)$

 $\left\{{\mathrm{sin}}{}\left({a}\right){,}{\mathrm{sin}}{}\left({b}\right){,}{\mathrm{sin}}{}\left({x}\right){,}{\mathrm{sin}}{}\left({y}\right){,}{\mathrm{sin}}{}\left({z}\right)\right\}$ (2.5.3)

$\mathrm{map}\left(x\to {x}^{2},\mathrm{S1}\right)$

 $\left\{{{a}}^{{2}}{,}{{b}}^{{2}}{,}{{x}}^{{2}}{,}{{y}}^{{2}}{,}{{z}}^{{2}}\right\}$ (2.5.4)









$\mathrm{A1}≔\left[2,6,9\right]:$

$\mathrm{A2}≔\left[2,7,6\right]:$







$\mathrm{zip}\left(\left(x,y\right)\to x\cdot y,\mathrm{A1},\mathrm{A2}\right)$

 $\left[{4}{,}{42}{,}{54}\right]$ (2.5.5)

$\mathrm{zip}\left(\mathrm{igcd},\mathrm{A1},\mathrm{A2}\right)$

 $\left[{2}{,}{1}{,}{3}\right]$ (2.5.6)









Using a for loop.

You can also use a conditional statement for ... do ... to perform an operation repeatedly, once for each element in the set.  The results are printed to the screen.

A for loop can be used to run through a sequence.

Note:  Maple's built-in commands such as map and zip can be significantly faster than using a for loop.  For efficiency, whenever possible, you should use these commands instead.  For more information, see efficiency.

Tip: For more information and examples of conditional statements, see do or the Basic Programming chapter of Maple's User Manual.



$\mathrm{S1}$

 $\left\{{a}{,}{b}{,}{x}{,}{y}{,}{z}\right\}$ (2.5.7)



 ${{z}}^{{3}}$ (2.5.8)

$s$

 ${1}{,}{2}{,}{3}{,}{a}{,}{b}{,}{c}$ (2.5.9)



 ${{c}}^{{2}}{}{\mathrm{sin}}{}\left(\frac{{c}{}{\mathrm{\pi }}}{{3}}\right)$ (2.5.10)



Arrays, Matrices and Vectors

Arrays, Matrices, and Vectors are built on the same structure, rtable, which are mutable dense arrays.  Lists are convenient for small datasets, such as a collection of arguments.  However, lists are not mutable.  Use arrays or other rtable-based data structures when you will need to change the data.

Matrices and vectors are covered in the tutorial 5-WorkingWithMatrices. The rest of this section deals with arrays.

Steps

Results

Define an array.

Example:

Define an array with a list of entries.

Example:

Define a 3 x 3 array with values from 1 to 9.

The calling sequence used here is Array(rows, columns, entries), where the rows and columns are given as ranges and the entries are given as a nested list.







 $\left[\begin{array}{cccc}1.1& 1.2& 1.3& 1.4\end{array}\right]$ (2.6.1)







$\mathrm{A2}≔\mathrm{Array}\left(1..3,1..3,\left[\left[1,2,3\right],\left[4,5,6\right],\left[7,8,9\right]\right]\right)$

 $\left[\begin{array}{rrr}1& 2& 3\\ 4& 5& 6\\ 7& 8& 9\end{array}\right]$ (2.6.2)





Indexing and Dynamically Growing Arrays

Extract entries

Example:

Extract a single entry from A1.

Extract the entry in the second row and first column of A2.

You can extract entries using either [] or () brackets.

The first method of index selection you have seen used for other data structures, such as lists.  The second method, (), which only works on rtables, is called programmer indexing.  Programmer indexing allows for more flexible and powerful indexing.   For example, you can grow an array by assigning to an element outside the initial boundaries.

Dynamically Grow an Array

Example:

Grow an array with () brackets.











$\mathrm{A1}\left[2\right]$

 ${1.2}$ (2.6.3)

$\mathrm{A2}\left[2,1\right]$

 ${4}$ (2.6.4)







$\mathrm{A1}\left(2\right)$

 ${1.2}$ (2.6.5)



$\mathrm{A1}\left(2..3\right)$

 $\left[\begin{array}{cc}1.2& 1.3\end{array}\right]$ (2.6.6)









$\mathrm{A1}\left(5\right)≔1.5$

 $\left[\begin{array}{ccccc}1.1& 1.2& 1.3& 1.4& 1.5\end{array}\right]$ (2.6.7)

$\mathrm{A1}$

 $\left[\begin{array}{ccccc}1.1& 1.2& 1.3& 1.4& 1.5\end{array}\right]$ (2.6.8)