# Taken from Andrew Hick's page: # http://www.math.drexel.edu/~ahicks/mathematics/maple-data-structures.txt # # THIS IS VERY ACCESSIBLE ACCOUNT OF # Sequences, Lists, Sets, Arrays # Functions, Procedures # and some exercises. # Assign to students to read. # # A Short Introduction to Maple Data Structures # R. Andrew Hicks # 1/24/03 # # I. Sequences, Lists, Sets and Arrays # # The basic data structure in Maple is the sequence, which just is a # bunch of objects separated by commas: # > 1,2,3; 1, 2, 3 # # Like everything in Maple, we can give a sequence a name: # > s := 1, 3, 2; s := 1, 3, 2 # # You can then access individual sequence elements with the [ ] # operator: # > s[2]; 3 # # A nice thing about sequence is that it is easy to perform the append # operation: # > t := s, 100; t := 1, 3, 2, 100 # # Most sequences are not typed in by hand of course. There is a sequence # operator: # > u := seq(i^2, i=1..10); u := 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 # Maple is pretty smart about comparing two sequences: # > v := seq(j^2, j=1..10); v := 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 > if(u=v) then 1 else 0; fi; 1 # # Sequences can have mixed type by the way: # > w := "hello, world", 3, x^2; 2 w := "hello, world", 3, x # # Sequences are not arrays! You can't change elements using the [ ] # operator: # > w[1] := 0; Error, cannot assign to an expression sequence # # A set in Maple is created by putting braces { } around a sequence: # > S := {s}; S := {1, 2, 3} # # Maple doesn't care about the order in a set of course. # > x := 3,2,1; x := 3, 2, 1 > s; 1, 3, 2 > if({x}={s}) then 1 else 0; fi; 1 # # Many of the usual set operations are available. For example, union: # > {1,2} union {4,1}; {1, 2, 4} > {1,2} minus {4,1}; {2} # # Set equality checking is available: # > if({1,3,4}={3,4,1}) then 1 else 0; fi; 1 # # The [ ] operator is also available, but be careful, it may not give # the same answer all the time since sets have no order. # > S[1]; 1 # A close relative of the set is the list, which of course does keep the # elements ordered. > l := [s]; l := [1, 3, 2] > if([1,2,3]=[1,3,2]) then 1 else 0; fi; 0 # # Maple has many nice built in list and set functions. Try to use the # built in ones as opposed to writing them yourself - it will make your # code faster. # > sort(l); [1, 2, 3] # # In order to make a set or list into a sequence, use the op() operator. # The op operator works on all Maple expressions. # > S; {1, 2, 3} > op(S); 1, 2, 3 > l; [1, 3, 2] # # You can convert a list to a set in the following way: # > [op(S)]; [1, 2, 3] # # But there is a built in convert function, that works for many, many # different types: # > convert(S, list); [1, 2, 3] > convert(l, set); {1, 2, 3} # # The number of elements of a list or set can be obtained using the # nops() operator: # > nops(l); 3 # # I don't know any way of finding the number of elements of a sequence # other than doing the following: # > nops([s]); 3 # # Arrays are pretty much what one might expect: # > X := array(1..10); X := array(1 .. 10, []) > print(X); [X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], X[10]] # # Unlike lists, you can change a given entry: # > X[1] := 2; X[1] := 2 > print(X); [2, X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], X[10]] # # Multi-dimensional arrays: # > Y := array(1..4, 1..3); Y := array(1 .. 4, 1 .. 3, []) > print(Y); [Y[1, 1] Y[1, 2] Y[1, 3]] [ ] [Y[2, 1] Y[2, 2] Y[2, 3]] [ ] [Y[3, 1] Y[3, 2] Y[3, 3]] [ ] [Y[4, 1] Y[4, 2] Y[4, 3]] # Functions and Expressions # # Functions in Maple can be defined with an arrow notation: > x -> x^2; 2 x -> x # You can give a function a name if you like, but you don't have to. > (x -> x^2)(3); 9 > f := x -> x^2; 2 f := x -> x > f(3); 9 # Functions can be applied to entire sets and lists with the map() # operator: # > map(f,l); [1, 9, 4] > map(f, S); {1, 4, 9} # # Be careful applying it to sequences! for some reason I don't # undertand, when a sequence is passed to a function in Maple, only the # first element of the sequence is really passed. # > s; 1, 3, 2 > map(f, s); 1 # # Functions are really just procedures in disguise. If you definition is # complicated you might want to use a procedure. (The output of a # procedure is the last Maple statement before the end proc line.) # > f := proc(x) > x^2; > end proc; f := proc(x) x^2 end proc > f(3); 9 # # If you are want something like a function to append an item to a list, # you have to do it this way. For example: # > append := (x, L) -> [op(L), x]; append := (x, L) -> [op(L), x] > append(666, l); [1, 3, 2, 666] # # But there is an alternative way to calculate function values in many # cases, using the subs command, which substitutes a given value into an # expression. # > f := z^2; 2 f := z # # Notice that no arrow is used in the above definition of f ! # > subs(z=3, f); 9 # # Why did I use z, rather than the variable x ? Let's try using x. # > f := x^2; 2 f := (3, 2, 1) # # OK, that's clearly not what we want. The problem is that we already # defined x above: # > x; 3, 2, 1 # # But why didn't this cause a problem in this - # > append := (x, L) -> [op(L),x]; append := (x, L) -> [op(L), x] # # This has to do with what are called the scoping rules. In this case, # the x on the right hand side of the assignment operator is a local # variable. So the subs approach suggest above is a little more # dangerous, since global variables are being used. # # # Exercises # # 1. Write a function that reverses a list. Do this both recursively and # non-recusively. Which is faster ? # # 2. Write a function that takes a list of objects, and returns a list # of the same objects, but with no duplicates. (Order doesn't matter.) # # 3. Write a function that prepends an element to a list (i.e. puts in # on the front of the list) . # # 4. Write a function that takes a set of sets of numbers, and a # function on the integers, and applies the function to all of the # integers in the "inner" sets, returning another set of sets of # integers. For example if f was x->x^2, and the set was {{1,2}, {3}} # then the result would be {{1,4},{9}}. # # 5. Implement mergesort in Maple. Compare its runtime to the built in # sort function. # >