One level evaluation local variables of a procedure

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: dentifies them. > expr := a * b * c * a^b; expr := a b c ab > subs( a*b=3, expr ); a b c ab 198 • Chapter 6: Evaluation and Simplification The expr expression is a product of four factors. > op( expr ); a, b, c, ab The product a*b is not a factor in expr. You can make the substitution a*b=3 in three ways: solve the subexpression for one of the variables, > subs( a=3/b, expr ); 3 3 c ( )b b use a side relation to simplify, > simplify( expr, { a*b=3 } ); 3 c ab or use the algsubs command, which performs algebraic substitutions. > algsubs( a*b=3, expr); 3 c ab Note that in the first case all occurrences of a have been replaced by 3/b. Whereas, in the second and third cases both variables a and b remain in the result. You can make several substitutions with one call to subs. > expr := z * sin( x^2 ) + w; expr := z sin(x2 ) + w > subs( x=sqrt(z), w=Pi, expr ); z sin(z ) + π The subs command makes the substitutions from left to right. > subs( z=x, x=sqrt(z), expr ); 6.3 Structural Manipulations • 199 √ z sin(z ) + w If you give a set or list of substitutions, subs makes those substitutions simultaneously. > subs( { x=sqrt(Pi), z=3 }, expr ); 3 sin(π ) + w Note that in general you must explicitly evaluate the result of a call to subs. > eval( % ); w Use the subsop command to substitute for a specific operand of an expression. > expr := 5^x; expr := 5x > op( expr ); 5, x > subsop( 1=t, expr ); tx The zeroth operand of a function is typically the name of the function. > expr := cos(x); expr := cos(x) > subsop( 0=sin, expr ); sin(x) 200 • Chapter 6: Evaluation and Simplification For information about the operands of an expression, see this section, pages 188–193. Changing the Type of an Expression To convert an expression to another type, use the convert command. Consider the Taylor series for sin(x). > f := sin(x); f := sin(x) > t := taylor( f, x=0 ); t := x − 15 13 x+ x + O(x6 ) 6 120 For example, you cannot plot a series, you must use convert(..., polynom) to convert it into a polynomial approximation first. > p := convert( t, polynom ); p := x − 15 13 x+ x 6 120 Similarly, the title of a plot must be a string, not a general expression. You can use convert(..., string) to convert an expression to a string. > p_txt := convert( p, string ); p _txt := “x-1/6*x^3+1/120*x^ 5” > plot( p, x=-4..4, title=p_txt ); x–1/6*x^3+1/120*x^5 1.5 1 0.5 –4 –3 –2 –1 0 –0.5 –1 –1.5 1 2 x 3 4 6.3 Structural Manipulations • 201 The cat command concatenates all its arguments to create a new string. > ttl := cat( convert( f, string ), > " and its Taylor approximation ", > p_txt ); ttl := “sin(x) and its Taylor approximation x-1/6*x^\ 3+1/120*x^5” > plot( [f, p], x=-4..4, title=ttl ); sin(x)anditsTaylorapproximationx–1/6*x^3+1/120*x^5 1.5 1 0.5 –4 –3 –2 –1 0 –0.5 –1 –1.5 1 2 x 3 4 You can also convert a list to a set or a set to a list. > L := [1,2,5,2,1]; L := [1, 2, 5, 2, 1] > S := convert( L, set ); S := {1, 2, 5} &gt...
View Full Document

Ask a homework question - tutors are online