Part 6: Data Structures - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Getting Started : Tutorials : MaplePortal/Tutorial6

Part 6: Data Structures

Go to Maple Portal     Previous Tutorial     Next Tutorial

 

Introduction

Data Structures in Maple

See Also

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 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:

s1,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.

 

 

s1,2,3,a,b,c

1,2,3,a,b,c

(2.1.1)

s1

1

(2.1.2)

s2

2

(2.1.3)

s4

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.

 

seqi,i=1..10

1,2,3,4,5,6,7,8,9,10

(2.1.5)

seqk2,k=1..5

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 S1a,b,x,y,z,a is evaluated, the duplicate a is removed.

S1a,b,x,y,z,a

a,b,x,y,z

(2.2.1)

S21,2,b,c

1,2,b,c

(2.2.2)

Set Arithmetic

 

To perform set arithmetic, use the symbols from the Common Symbols palette or use the operators union, intersect, and minus.

 

Example:

Find the union, intersection and set difference of the sets S1 and S2.

 

 

 

 

 

Testing Set Membership

Example:

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.

 

 

 

S1S2 = 1,2,a,b,c,x,y,z

S1S2 = b

S1S2 = 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

(2.2.3)

member1,S1

false

(2.2.4)

evalbx∈S1

true

(2.2.5)

evalb1 in S1

false

(2.2.6)

Selecting Elements

Use the same selection notation as for sequences: si and si both represent the ith element in the set s.

 

Example:

Select the first and third elements of S1

 

S11

a

(2.2.7)

S13

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.

whattypeS1

set

(2.2.9)

typeS1,set

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.

L1x,y,z,x

x,y,z,x

(2.3.1)

s

1,2,3,a,b,c

(2.3.2)

L2s

1,2,3,a,b,c

(2.3.3)

L3L1,1,2,3,L2

x,y,z,x,1,2,3,1,2,3,a,b,c

(2.3.4)

 

Ma,b,1,2,4,5,3,7

a,b,1,2,4,5,3,7

(2.3.5)

whattypeM

list

(2.3.6)

typeM,listlist

true

(2.3.7)

typeL3,listlist

false

(2.3.8)

Selecting Elements

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

(2.3.9)

L13

z

(2.3.10)

M1

a,b

(2.3.11)

M12

b

(2.3.12)

 

L12..3

y,z

(2.3.13)

L1..3

x,y,z

(2.3.14)

L13..

z,x

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

 

 

 

 

memberx,L1

true

(2.3.16)

membery,L1,'i'

true

(2.3.17)

i

2

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

 

 

 

L1

x,y,z,x

(2.3.19)

opL1

x,y,z,x

(2.3.20)

nopsL1

4

(2.3.21)

opL1

x,y,z,x

(2.3.22)

opL2

1,2,3,a,b,c

(2.3.23)

opL1,opL2

x,y,z,x,1,2,3,a,b,c

(2.3.24)

X,Y,opL2

X,Y,1,2,3,a,b,c

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

 

L1

x,y,z,x

(2.3.26)

L14α

α

(2.3.27)

L1

x,y,z,α

(2.3.28)

L4a,b,a,c,d,a

a,b,a,c,d,a

(2.3.29)

evalL4,a=A

A,b,A,c,d,A

(2.3.30)

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 

3.7,1,1,2,4.2,5,8

(2.3.31)

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:

 

MyCards  MultiSet Ty Cobb, 3, George Brett, 1, Nolan Ryan, 2 

MyCardsTy Cobb,3,George Brett,1,Nolan Ryan,2

(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:

 

MyCards  MyCards +Ty Cobb, 2, Reggie Jackson,1

MyCardsTy Cobb,5,George Brett,1,Nolan Ryan,2,Reggie Jackson,1

(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  MultiSetgeneralized a=12, b=3.1415, c=2

Ma,12,b,3.1415,c,2

(2.4.3)

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

 

convertx2+13 x22x2+3 x14,MultiSet, generalized

x2,2,x2+1,3,x2+3x1,4

(2.4.4)

For more information about working with MultiSets, see MultiSet.

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.

 

 

 

 

 

S1

a,b,x,y,z

(2.5.1)

mapf,S1

fa,fb,fx,fy,fz

(2.5.2)

mapsin,S1

sina,sinb,sinx,siny,sinz

(2.5.3)

mapxx2,S1

a2,b2,x2,y2,z2

(2.5.4)

A12,6,9:

A22,7,6:

zipx,yxy,A1,A2

4,42,54

(2.5.5)

zipigcd,A1,A2

2,1,3

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

 

S1

a,b,x,y,z

(2.5.7)

for i in S1 do i3end do

z3

(2.5.8)

s

1,2,3,a,b,c

(2.5.9)

for j in s do j2sinjπ3 end do

c2sin13cπ

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

 

 

 

 

A1Array1.1, 1.2, 1.3, 1.4

1.11.21.31.4

(2.6.1)

A2Array1..3,1..3,1,2,3,4,5,6,7,8,9

123456789

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

 

 

 

 

 

For more information on indexing, see rtable_indexing.

A12

1.2

(2.6.3)

A22,1

4

(2.6.4)

A12

1.2

(2.6.5)

A12..3

1.21.3

(2.6.6)

 

A151.5

1.11.21.31.41.5

(2.6.7)

A1

1.11.21.31.41.5

(2.6.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 x,y0.1x+y.

 

First, use the function template f:=a,bz 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.

indexrulex,y0.1x+y:

A3Array1..5,1..5,indexrule

1.12.13.14.15.1