This preview shows page 1. Sign up to view the full content.
Unformatted text preview: guments, it passes the extra argument(s) to the function. 6.3 Structural Manipulations • 181 > map( f, [a, b, c], p, q ); [f(a, p, q ), f(b, p, q ), f(c, p, q )]
> map( diff, [ (x+1)*(x+2), x*(x+2) ], x ); [2 x + 3, 2 x + 2] Using the map2 Command The map2 command is closely related to map. Whereas map sequentially replaces the ﬁrst argument of a function, the map2 command replaces the second argument to a function.
> map2( f, p, [a,b,c], q, r ); [f(p, a, q, r), f(p, b, q, r), f(p, c, q, r)] You can use map2 to list all the partial derivatives of an expression.
> map2( diff, x^y/z, [x,y,z] ); [ xy xy y xy ln(x) , , − 2] xz z z You can use map2 in conjunction with the map command. For an example, see the ?map help page. Using the seq Command You can use the seq command to generate sequences resembling the output from map. In this example, seq generates a sequence by applying the function f to the elements of a set and a list.
> seq( f(i), i={a,b,c} ); f(a), f(b), f(c)
> seq( f(p, i, q, r), i=[a,b,c] ); f(p, a, q, r), f(p, b, q, r), f(p, c, q, r) Another example is Pascal’s Triangle.
> L := [ seq( i, i=0..5 ) ]; 182 • Chapter 6: Evaluation and Simpliﬁcation L := [0, 1, 2, 3, 4, 5]
> [ seq( [ seq( binomial(n,m), m=L ) ], n=L ) ]; [[1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0], [1, 2, 1, 0, 0, 0], [1, 3, 3, 1, 0, 0], [1, 4, 6, 4, 1, 0], [1, 5, 10, 10, 5, 1]]
> map( print, % ); [1, 0, 0, 0, 0, 0] [1, 1, 0, 0, 0, 0] [1, 2, 1, 0, 0, 0] [1, 3, 3, 1, 0, 0] [1, 4, 6, 4, 1, 0] [1, 5, 10, 10, 5, 1] Using the add and mul Commands The add and mul commands work like seq except that they generate sums and products, respectively, instead of sequences.
> add( i^2, i=[5, y, sin(x), 5] ); 50 + y 2 + sin(x)2 Note: The map, map2, seq, add, and mul commands can also act on general expressions. Choosing Elements from a List or Set
You can select certain elements from a list or a set, if you have a booleanvalued function that determines which elements to select. The following booleanvalued function returns true if its argument is larger than three.
> large := x > is(x > 3); large := x → is(3 < x) 6.3 Structural Manipulations • 183 Use the select command to choose the elements in a list or set that satisfy large.
> L := [ 8, 2.95, Pi, sin(9) ]; L := [8, 2.95, π, sin(9)]
> select( large, L ); [8, π ] Using the remove Command The remove command removes the elements from L that satisfy large and displays as output the remaining elements.
> remove( large, L ); [2.95, sin(9)] Using the selectremove Command To perform operations of both the select and remove commands simultaneously, use the selectremove command.
> selectremove( large, L); [8, π ], [2.95, sin(9)] Using the type Command Use the type command to determine the type of an expression.
> type( 3, numeric ); true
> type( cos(1), numeric ); false The syntax of select here passes the third argument, numeric, to the type command. 184 • Chapter 6: Evaluation and Simpliﬁca...
View Full
Document
 Spring '12
 NIL
 Math, Division

Click to edit the document details