This preview shows page 1. Sign up to view the full content.
Unformatted text preview: tion > select( type, L, numeric ); [8, 2.95] For more information on types and using select and remove on a general expression, see this section, pages 190–196. Merging Two Lists
Sometimes you need to merge two lists. Here is a list of xvalues and a list of y values.
> X := [ seq( ithprime(i), i=1..6 ) ]; X := [2, 3, 5, 7, 11, 13]
> Y := [ seq( binomial(6, i), i=1..6 ) ]; Y := [6, 15, 20, 15, 6, 1] To plot the y values against the xvalues, construct a list of lists: [ [x1,y1 ], [x2,y2 ], ... ]. That is, for each pair of values, construct a twoelement list.
> pair := (x,y) > [x, y]; pair := (x, y ) → [x, y ] Using the zip Command The zip command can merge the lists X and Y according to the binary function pair.
> P := zip( pair, X, Y ); P := [[2, 6], [3, 15], [5, 20], [7, 15], [11, 6], [13, 1]] > plot( P ); 6.3 Structural Manipulations • 185 20 18 16 14 12 10 8 6 4 2 2 4 6 8 10 12 If the two lists have diﬀerent length, then zip returns a list the size of the shorter one.
> zip( (x,y) > x.y, [a,b,c,d,e,f], [1,2,3] ); [a, 2 b, 3 c] You can specify a fourth argument to zip. Then zip returns a list the size of the longer input list, using the fourth argument for the missing values.
> zip( (x,y) > x.y, [a,b,c,d,e,f], [1,2,3], 99 ); [a, 2 b, 3 c, 99 d, 99 e, 99 f ]
> zip( igcd, [7657,342,876], [34,756,213,346,123], 6! ); [1, 18, 3, 2, 3] The zip command can also merge vectors. For more information, refer to the ?zip help page. Sorting Lists
A list is a fundamental orderpreserving data structure in Maple. The elements in a list remain in the order used in creating the list. You can create a copy of a list sorted in another order by using the sort command. The sort command sorts lists, among other things, in ascending order. It sorts a list of numbers in numerical order.
> sort( [1,3,2,4,5,3,6,3,6] ); 186 • Chapter 6: Evaluation and Simpliﬁcation [1, 2, 3, 3, 3, 4, 5, 6, 6] The sort command also sorts a list of strings in lexicographic order.
> sort( ["Mary", "had", "a", "little", "lamb"] ); [“Mary”, “a”, “had”, “lamb”, “little”] Session Dependent Machine Addresses If a list contains both numbers and strings, or expressions diﬀerent from numbers and strings, sort uses the machine addresses, which are session dependent.
> sort( [x, 1, "apple"] ); [1, x, “apple”]
> sort( [5, 10, sin(34)] ); [10, sin(34), −5] Note that to Maple, π is not numeric.
> sort( [4.3, Pi, 2/3] ); [π, 2 , 4.3] 3 You can specify a boolean function to deﬁne an ordering for a list. The boolean function must take two arguments and return true if the ﬁrst argument precedes the second. You can use this to sort a list of numbers in descending order.
> sort( [3.12, 1, 1/2], (x,y) > evalb( x>y ) ); [3.12, 1, 1 ] 2 Using the is Command The is command can compare constants like π and sin(5) with pure numbers.
> bf := (x,y) > is( x < y ); 6.3 Structural Manipulations • 187 bf := (x, y ) → is(x < y )
&g...
View Full
Document
 Spring '12
 NIL
 Math, Division

Click to edit the document details