Part 6: Data Structures
Go to Maple Portal Previous Tutorial Next Tutorial
Introduction
Data Structures in Maple
See Also
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.
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: si refers to the ith element of the sequence s.
You can also use subscript notation, si, to refer to the ith 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
s1
1
s2
2
s4
a
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.
seqi,i=1..10
1,2,3,4,5,6,7,8,9,10
seqk2,k=1..5
1,4,9,16,25
Sets
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 S1≔a,b,x,y,z,a is evaluated, the duplicate a is removed.
S1≔a,b,x,y,z,a
S1≔a,b,x,y,z
S2≔1,2,b,c
S2≔1,2,b,c
Set Arithmetic
To perform set arithmetic, use the symbols from the Common Symbols palette or use the operators union, intersect, and minus.
Find the union, intersection and set difference of the sets S1 and S2.
Testing Set Membership
To test for set membership, use the command member.
Alternatively, use the set notation to form the statement x ∈S1 then use evalb (evaluate boolean) to evaluate the expression to get true or false. The keyword in can be used instead of the ∈ symbol.
S1⋃S2 = 1,2,a,b,c,x,y,z
S1⋂S2 = b
S1∖S2 = a,x,y,z
S1 union S2 = 1,2,a,b,c,x,y,z
S1 intersect S2 = b
S1 minus S2 = a,x,y,z
memberx,S1
true
member1,S1
false
evalb⁡x∈S1
evalb1 in S1
Selecting Elements
Use the same selection notation as for sequences: si and si both represent the ith element in the set s.
Select the first and third elements of S1
S11
S13
x
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.
whattypeS1
set
typeS1,set
Lists
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.
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.
L1≔x,y,z,x
L1≔x,y,z,x
s
1,2,3,a,b,c
L2≔s
L2≔1,2,3,a,b,c
L3≔L1,1,2,3,L2
L3≔x,y,z,x,1,2,3,1,2,3,a,b,c
M≔a,b,1,2,4,5,3,7
M≔a,b,1,2,4,5,3,7
whattypeM
list
typeM,listlist
typeL3,listlist
Use the same selection notation as for sequences and sets: si and si both represent the ith 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."
L1
x,y,z,x
L13
z
M1
a,b
M12
b
L12..3
y,z
L1..3
x,y,z
L13..
z,x
Testing List Membership
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.
memberx,L1
membery,L1,'i'
i
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.
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.
opL1
numelemsL1
4
opL2
opL1,opL2
x,y,z,x,1,2,3,a,b,c
X,Y,opL2
X,Y,1,2,3,a,b,c
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.
Replace "a" by "A" everywhere it appears in the list L4.
L14≔α
L1≔x,y,z,α
x,y,z,α
L4≔a,b,a,c,d,a
L4≔a,b,a,c,d,a
evalL4,a=A
A,b,A,c,d,A
Sorting a List
The sort command sorts the elements of a numerical list into ascending order.
sort1,4.2,2,1,−3.7,8,5
0≤r≤R,0≤θ≤2 π
For more commands for with lists, see ListTools.
MultiSets
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.
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:
MyCards ≔ MultiSet Ty Cobb, 3, George Brett, 1, Nolan Ryan, 2
MyCards≔Ty Cobb,3,George Brett,1,Nolan Ryan,2
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:
MyCards ≔ MyCards +Ty Cobb, 2, Reggie Jackson,1
MyCards≔Ty Cobb,5,George Brett,1,Nolan Ryan,2,Reggie Jackson,1
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 ≔ MultiSetgeneralized a=12, b=−3.1415, c=2
M≔a,12,b,−3.1415,c,2
This kind of MultiSet can be produced by the convert command when applied to a rational function:
convertx2+13 x−22x2+3 x−14,MultiSet, generalized
x−2,2,x2+1,3,x2+3⁢x−1,−4
For more information about working with MultiSets, see MultiSet.
Applying a Function to the Elements of a Set or List
To apply a function or procedure to the elements of a set or list, use the element-wise operator ~, map, or zip.
Examples using element-wise operators:
To multiple all elements in S1 by 2, you can use element-wise multiplication. The syntax is to use a tilde (~) after the multiplication operator. This distributes the multiplication operator over all the elements of the set. For more about element-wise operators, see operators/elementwise.
Apply sin to all the elements in S1.
Example using map:
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).
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.
S1
a,b,x,y,z
2⋅~S1
2⁢a,2⁢b,2⁢x,2⁢y,2⁢z
sin~S1
sin⁡a,sin⁡b,sin⁡x,sin⁡y,sin⁡z
mapx→x2,S1
a2,b2,x2,y2,z2
A1≔2,6,9:
A2≔2,7,6:
zipx,y→x⋅y,A1,A2
4,42,54
zipigcd,A1,A2
2,1,3
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 (as well as the ~ operator) 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.
for i in S1 do i3end do
z3
for j in s do j2⋅sinj⋅π3 end do
c2⁢sin⁡c⁢π3
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.
Define an array.
Define an array with a list of entries.
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.
A1≔Array1.1, 1.2, 1.3, 1.4
A1≔1.11.21.31.4
A2≔Array1..3,1..3,1,2,3,4,5,6,7,8,9
A2≔123456789
Indexing and Dynamically Growing Arrays
Extract entries
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
Grow an array with () brackets.
For more information on indexing, see rtable indexing.
A12
1.2
A22,1
A12..3
1.21.3
A15≔1.5
A1≔1.11.21.31.41.5
A1
1.11.21.31.41.5
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.
Define a 5 x 5 array using the function x,y→0.1⋅x+y.
First, use the function template f:=a,b→z 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.
indexrule≔x,y→0.1⋅x+y:
A3≔Array1..5,1..5,indexrule
A3≔1.12.13.14.15.11.22.23.24.25.21.32.33.34.35.31.42.43.44.45.41.52.53.54.55.5
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.
Define a table which associates the pairs:
a,α
b, β
c, γ
The entries are indexed by a, b, and c.
To see all the entries of the table, use entries.
Use the 'pairs' option to see the index=entry pairs.
K≔tablea=α,b=β, c=γ
K≔table⁡b=β,c=γ,a=α
Ka
α
Kb
β
Kc
γ
K
entriesK
β,γ,α
entriesK,'pairs'
b=β,c=γ,a=α
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.
Array, list, map, seq
Download Help Document