> # SET data type ---- a sequence between braces----No Duplicates------No control on the ordering > > s1:= {0,1,2,3}; s1 := {0, 1, 2, 3} -------------------------------------------------------------------------------- > s2:={8,3,8,9,0}; s2 := {0, 3, 8, 9} -------------------------------------------------------------------------------- > # set operators: union, minus, intersect, member > > s3:= s1 union s2; s3 := {0, 1, 2, 3, 8, 9} -------------------------------------------------------------------------------- > member(2, s3); true -------------------------------------------------------------------------------- > member(2, s3, 'pos'); true -------------------------------------------------------------------------------- > pos; 3 -------------------------------------------------------------------------------- > ?powerset -------------------------------------------------------------------------------- > # load the combinat package > > > with(combinat): -------------------------------------------------------------------------------- > powerset(3); {{}, {1}, {1, 2, 3}, {2, 3}, {3}, {1, 3}, {2}, {1, 2}} -------------------------------------------------------------------------------- > powerset(s1); {{}, {0, 1, 2, 3}, {1}, {1, 2, 3}, {2, 3}, {3}, {1, 3}, {2}, {1, 2}, {0, 2, 3}, {0, 3}, {0, 1, 3}, {0}, {0, 1}, {0, 2}, {0, 1, 2}} -------------------------------------------------------------------------------- > > > # select elements from a set > > select(isprime, s1); {2, 3} -------------------------------------------------------------------------------- > s1; {0, 1, 2, 3} -------------------------------------------------------------------------------- > select(type, s1, 'nonnegint'); {0, 1, 2, 3} -------------------------------------------------------------------------------- > > > > # LIST data type ------seq in brackets---- allows duplicates ----ordering is preserved > > list:= [1, 0, 2, 4, 0]; Error, attempting to assign to `list` which is protected -------------------------------------------------------------------------------- > l1:= [1, 0, 2, 4, 0]; l1 := [1, 0, 2, 4, 0] -------------------------------------------------------------------------------- > l2:=[x$5]; l2 := [x, x, x, x, x] -------------------------------------------------------------------------------- > # member, append, insert, concatenate, replace, sort, rotate, reverse > > member(0, l1, 'pos'); true -------------------------------------------------------------------------------- > l1[3]; 2 -------------------------------------------------------------------------------- > [l2[2..3], l1]; [x, x, [1, 0, 2, 4, 0]] -------------------------------------------------------------------------------- > # op : extract operands from an expression -------------------------------------------------------------------------------- > [l2[2..3], op(l1)]; [x, x, 1, 0, 2, 4, 0] -------------------------------------------------------------------------------- > subs( 0 = alpha, l1); [1, alpha, 2, 4, alpha] -------------------------------------------------------------------------------- > #rotate left one position > [ l1[2..nops(l1)], l1[1]]; [0, 2, 4, 0, 1] -------------------------------------------------------------------------------- > #### ASK them to do it ------- reverse the list > l1; [1, 0, 2, 4, 0] > [seq ( l1[nops(l1) -i +1], i=1..nops(l1) ) ]; [0, 4, 2, 0, 1] -------------------------------------------------------------------------------- > ## define a Maple function > > rotateleft := proc(l:list) > [ l[2..nops(l) ], l[1] ] > end: -------------------------------------------------------------------------------- > rotateleft(l1); [0, 2, 4, 0, 1] -------------------------------------------------------------------------------- > > > > # ARRAYS --- vectors --- matrices > #vector v > v:= array ( [a, b, c]); v := [ a, b, c ] -------------------------------------------------------------------------------- > M:= array( [ [a, b], [c, d] ] ); [ a b ] M := [ ] [ c d ] -------------------------------------------------------------------------------- > # actually v is a vector and M is a matrix > type(v, 'vector'); type(M, 'matrix'); type(v, 'array'); true true true -------------------------------------------------------------------------------- > # NOTE: Maple considers vectors as column vectors even if they are printed horizontally > > # you can use matrix and vector from the linalg package (use even the same format) > > with(linalg): Warning: new definition for fibonacci Warning: new definition for norm Warning: new definition for trace -------------------------------------------------------------------------------- > matrix(3,3, 0); # the zero matrix [ 0 0 0 ] [ ] [ 0 0 0 ] [ ] [ 0 0 0 ] -------------------------------------------------------------------------------- > > > > > # CONVERT > > # convert a seq into a set or list -------> easy > # more sophisticated ----- > convert ( [a, b, c], 'vector'); [ a, b, c ] -------------------------------------------------------------------------------- > l2:= [1, 2, 4, 5, 6, 7]; l2 := [1, 2, 4, 5, 6, 7] -------------------------------------------------------------------------------- > convert(l2, 'set'); {1, 2, 4, 5, 6, 7} -------------------------------------------------------------------------------- > l2:= convert( linalg[matrix] (2, 3, l2), 'listlist'); l2 := [[1, 2, 4], [5, 6, 7]] -------------------------------------------------------------------------------- > l3:=op(l2); l3 := [1, 2, 4], [5, 6, 7] -------------------------------------------------------------------------------- > -------------------------------------------------------------------------------- >