CS99 Lecture5: Expressions and Statements - week of 9/11 Advice about Studying & Labs Lecture from lecture4 continued: Worksheet Management - need to know rules to handle assignments - restart: clear all assignments - kernel modes - shared (default) -- all worksheet know all values - parallel -- each worksheet runs independently - anames() -- show list of assigned names Evaluating Expressions - must understand how Maple's "brain" works - automatic simplification and full evaluation Automatic Simplification - reduce "clearly" identical expressions - > x-x; x+x; x/x; - sometimes hard to predict Full evaluation - how Maple calculates (evaluates) results of an expression - process: - convert expression into tree - replace all assigned names with expressions, which are also converted to trees - perform automatic simplification - perform mathematical operations and functions for subexpressions until reaching the final answer - works from bottom to top Assignments Revisted - how does Maple treat assignments during full evaluation? > expr; fully evaluate expr > name:=expr fully evaluate expr and store result in name - what if expr contains a name n? if n is assigned, the assigned expression replaces n if n unassigned, n remains symbolic But Maple does not change any assignment INSIDE expr! example) > restart: x:=y+z: y:=1: x; output: 1+z explanation) clear assignments x gets y + z y gets 1 (the expression for x is still y+z: the assignment y:=1 only changed y) evaluate x (Maple replaces y with 1 inside the expression for x DURING full evaluation. So, x fully evaluates to 1+z, but x is NOT assigned to 1+z) - Example 1: see figure 3-6 - Example 2: see figure 3-7 Unevaluation - single quotes '' delay evaluation - 'expr' tells Maple to first strip 'expr' of the quotes - reason why unassignment works > a := 'a': Maple strips left side of quotes, then Maple sees a:=a internally. So, the name a gets assigned to name a. Chapter 4 - Types definition: classification of expression checking: use type function expression tree: surface, structured, nested finding types: whattype - Real Numbers integers = whole numbers fractions = ratio of two integers rationals numbers = integers + fractions floats, decimals arithmetic: + rational until forced into a float + evalf rounding: + Digits + Rounding evalf(0.05+0.4,1) -> 0.4 evalf(0.05+0.5,1) -> 0.6 irrational numbers + sqrt(2) + Pi - Complex Numbers sqrt(-1) -> i - Extended Numerics infinity: n + inf undefined: 0/0 - Boolean Expressions Logic: + operators: and, or, not + values: true, false ex) T and F -> F, T or F -> T Relations: + for comparisons + <,>,<>,= + expr rel expr ex) solve(x^2+4=0,x) Boolean + logical or relational expr + evalb(expr) -> evaluate truth of expr - Multiple expressions + ranges ex) 1..2 + sequences ex) 1,2,3 + lists ex) [1,2,3] + sets ex) {1,2,3} + selection ex) L:=[a,b,c]; L[1]; L[2..3]; - Names Indexing: resembles selection, no assignment in name Symbolic Constants: constants yields false, true, infinity,... Protected Names: predefined names Greek Letters: not really a type, but something built-in - Miscellaneous Strings Functions