LearningGuide

# This type matches the function name with arguments of

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

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. &gt; shorter := (x,y) -&gt; evalb( length(x) &lt; length(y) ); shorter := (x, y ) → evalb(length(x) &lt; length(y )) &gt; sort( [&quot;Mary&quot;, &quot;has&quot;, &quot;a&quot;, &quot;little&quot;, &quot;lamb&quot;], 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. &gt; big_list := [1,&quot;d&quot;,3,5,2,&quot;a&quot;,&quot;c&quot;,&quot;b&quot;,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. &gt; list1 := select( type, big_list, string ); list1 := [“d”, “a”, “c”, “b”] &gt; list2 := select( type, big_list, numeric ); list2 := [1, 3, 5, 2, 9] Then sort the two lists independently. &gt; list1 := sort(list1); 188 • Chapter 6: Evaluation and Simpliﬁcation list1 := [“a”, “b”, “c”, “d”] &gt; list2 := sort(list2); list2 := [1, 2, 3, 5, 9] Finally, stack the two lists together. &gt; 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. &gt; eq := a^2 + b^ 2 = c^2; eq := a2 + b2 = c2 &gt; lhs( eq ); a2 + b2 The rhs command similarly selects the right-hand side. &gt; rhs( eq ); c2 The lhs and rhs commands also work on ranges. &gt; lhs( 2..5 ); 6.3 Structural Manipulations • 189 2 &gt; rhs( 2..5 ); 5 &gt; eq := x = -2..infinity; eq := x = −2..∞ &gt; lhs( eq ); x &gt; rhs( eq ); −2..∞ &gt; lhs( rhs(eq) ); −2 &gt; rhs( rhs(eq) ); ∞ Using the numer and demom Commands The numer and denom commands extract the numerator and denominator, respectively, from a fraction. &gt; numer( 2/3 ); 2 &gt; denom( 2/3 ); 3 &gt; fract := ( 1+sin(x)^3-y/x) / ( y^2 - 1 + x ); 190 • Chapter 6: Evaluation and Simpliﬁcation 1 + sin(x)3 − fract := y2 − 1 + x y x &gt; numer( fract ); x + sin(x)3 x − y &gt; denom( fract ); x (y 2 − 1 + x) Using the whattype, op, and nops Commands Consider the expression &gt; expr := 3 + sin(x) + 2*cos(x)^2*sin(x); expr := 3 + sin(x) + 2 cos(x)2 sin(x) The whattype command identiﬁes expr as a sum. &gt; whattype( expr ); + Use the op command to list the terms of a sum or, in general, the operands of an expression. &gt; 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