This type matches the function name with arguments of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t; sort( [4.3, Pi, 2/3, sin(5)], bf ); [sin(5), 2 , π, 4.3] 3 You can also sort strings by length. > shorter := (x,y) -> evalb( length(x) < length(y) ); shorter := (x, y ) → evalb(length(x) < length(y )) > sort( ["Mary", "has", "a", "little", "lamb"], shorter ); [“a”, “has”, “lamb”, “Mary”, “little”] Sorting Mixed List of Strings and Numbers Maple does not have a built-in method for sorting lists of mixed strings and numbers, other than by machine address. To sort a mixed list of strings and numbers, you can do the following. > big_list := [1,"d",3,5,2,"a","c","b",9]; big _list := [1, “d”, 3, 5, 2, “a”, “c”, “b”, 9] Make two lists from the original, one consisting of numbers and one consisting of strings. > list1 := select( type, big_list, string ); list1 := [“d”, “a”, “c”, “b”] > list2 := select( type, big_list, numeric ); list2 := [1, 3, 5, 2, 9] Then sort the two lists independently. > list1 := sort(list1); 188 • Chapter 6: Evaluation and Simplification list1 := [“a”, “b”, “c”, “d”] > list2 := sort(list2); list2 := [1, 2, 3, 5, 9] Finally, stack the two lists together. > sorted_list := [ op(list1), op(list2) ]; sorted _list := [“a”, “b”, “c”, “d”, 1, 2, 3, 5, 9] Note: The sort command can also sort algebraic expressions. See 6.1 Mathematical Manipulations. The Parts of an Expression To manipulate the details of an expression, you must select the individual parts. Three easy cases for doing this involve equations, ranges, and fractions. Using the lhs and rhs Commands The lhs command selects the lefthand side of an equation. > eq := a^2 + b^ 2 = c^2; eq := a2 + b2 = c2 > lhs( eq ); a2 + b2 The rhs command similarly selects the right-hand side. > rhs( eq ); c2 The lhs and rhs commands also work on ranges. > lhs( 2..5 ); 6.3 Structural Manipulations • 189 2 > rhs( 2..5 ); 5 > eq := x = -2..infinity; eq := x = −2..∞ > lhs( eq ); x > rhs( eq ); −2..∞ > lhs( rhs(eq) ); −2 > rhs( rhs(eq) ); ∞ Using the numer and demom Commands The numer and denom commands extract the numerator and denominator, respectively, from a fraction. > numer( 2/3 ); 2 > denom( 2/3 ); 3 > fract := ( 1+sin(x)^3-y/x) / ( y^2 - 1 + x ); 190 • Chapter 6: Evaluation and Simplification 1 + sin(x)3 − fract := y2 − 1 + x y x > numer( fract ); x + sin(x)3 x − y > denom( fract ); x (y 2 − 1 + x) Using the whattype, op, and nops Commands Consider the expression > expr := 3 + sin(x) + 2*cos(x)^2*sin(x); expr := 3 + sin(x) + 2 cos(x)2 sin(x) The whattype command identifies expr as a sum. > whattype( expr ); + Use the op command to list the terms of a sum or, in general, the operands of an expression. > op( expr ); 3, sin(x), 2 cos(x)2 sin(x) The expression expr consists of three terms. Use the nops command to count the number of operands...
View Full Document

This note was uploaded on 08/27/2012 for the course MATH 1100 taught by Professor Nil during the Spring '12 term at National University of Singapore.

Ask a homework question - tutors are online