Part 6: Data Structures
Go to Maple Portal Previous Tutorial Next Tutorial

Introduction


Maple's 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's 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 nonWindows 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 Help>Quick Help for the list of the most common keystrokes.


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:
assigns the name 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 th element of the sequence .
You can also use subscript notation, , to refer to the th element. Use Ctrl + underscore (_) to enter the subscript.
Note: Index and subscript notation are available when using the 2D math editor. In places where Maple syntax is required, such as the startup code region, index notation must be used.

 (2.1.1) 
 (2.1.2) 
 (2.1.3) 
 (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.

 (2.1.5) 
 (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 is evaluated, the duplicate is removed.

 (2.2.1) 
 (2.2.2) 

Set Arithmetic
To perform set arithmetic, use the symbols from the Common Symbols palette or use the operators , , and .
Example:
Find the union, intersection and set difference of the sets and .
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 can be used instead of the ∈ symbol.

=
=
=
=
=
=
 (2.2.3) 
 (2.2.4) 
 (2.2.5) 
 (2.2.6) 

Selecting Elements
Use the same selection notation as for sequences: and both represent the th element in the set .
Example:
Select the first and third elements of

 (2.2.7) 
 (2.2.8) 

Maple has a typechecking function.
The commands whattype returns the Maple type. The command type does typechecking.
Tip: Typechecking is useful in programming Maple procedures.

 (2.2.9) 
 (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 .
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.

 (2.3.1) 
 (2.3.2) 
 (2.3.3) 
 (2.3.4) 
 (2.3.5) 
 (2.3.6) 
 (2.3.7) 
 (2.3.8) 

Selecting Elements
Use the same selection notation as for sequences and sets: and both represent the th element in the list
Selection operations can be combined to select elements from nested lists.
Selecting partial lists
Select a range of elements from a list by using to specify a range of indices. Leaving off an end point means "from the beginning" or "until the end."

 (2.3.9) 
 (2.3.10) 
 (2.3.11) 
 (2.3.12) 
 (2.3.13) 
 (2.3.14) 
 (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.

 (2.3.16) 
 (2.3.17) 
 (2.3.18) 

The op and nops 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 nops command returns the number of operands.
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.
Adding Elements to a List
The same method is used to add an element to a list.

 (2.3.19) 
 (2.3.20) 
 (2.3.21) 
 (2.3.22) 
 (2.3.23) 
 (2.3.24) 
 (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.

 (2.3.26) 
 (2.3.27) 
 (2.3.28) 
 (2.3.29) 
 (2.3.30) 

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

 (2.3.31) 





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.

 (2.4.1) 
 (2.4.2) 
 (2.4.3) 
 (2.4.4) 
 (2.4.5) 
 (2.4.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 builtin 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.

 (2.4.7) 
 (2.4.8) 
 (2.4.9) 
 (2.4.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 rtablebased data structures when you will need to change the data.
Matrices and vectors are covered in the tutorial 5WorkingWithMatrices. 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.

 (2.5.1) 
 (2.5.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.
For more information on indexing, see rtable_indexing.

 (2.5.3) 
 (2.5.4) 
 (2.5.5) 
 (2.5.6) 
 (2.5.7) 
 (2.5.8) 

Creating an Array using an Initializer Function
You can define an array or matrix by giving a function to be used when filling in the entries.
Example:
Define a 5 x 5 array using the function .
First, use the function template from the Expression palette. Replace each placeholder with the appropriate value, using [Tab] to move between placeholders.
Then, define the array by giving the row range, column range, and initializer function name.

 (2.5.9) 





Tables


Use a table if you want to index your data with something other than a number. For instance, use a table to store a collection of associated pairs.
Steps

Result

Define a table which associates the pairs:
The entries are indexed by a, b, and c.
To see all the entries of the table, use eval.

 (2.6.1) 
 (2.6.2) 
 (2.6.3) 
 (2.6.4) 
 (2.6.5) 
 (2.6.6) 




For more information on these data structures, see the Basic Data Structures chapter of the Maple Programming Guide.
Modules are a programming structure that allow you to associate related data and procedures. Most Maple packages are implemented as modules. For more information on modules, see the Programming with Modules chapter of the Maple Programming Guide.

Go to Maple Portal Previous Tutorial Next Tutorial
