840 likes | 856 Views
Maxima program control. Topics. Branching Iteration. Branching. Branching controls the execution of a program. Statements in a program are executed only if a condition holds. An informal example, not quite Maxima: if (gender is “male”) then use men’s room else use women’s room.
E N D
Topics • Branching • Iteration
Branching controls the execution of a program • Statements in a program are executed only if a condition holds. • An informal example, not quite Maxima: if (gender is “male”) then use men’s room else use women’s room
Branching controls the execution of a program • Statements in a program are executed only if a condition holds. • Informal example, not quite Maxima: if ( gender is male ) then use men’s room else use women’s room Condition
A formal Maxima example if (gender = male) then print ("use men’s room") else print ("use women’s room"); • Statement also uses the Maxima print function
Question – how does Maxima know the gender of the programmer?
Question – how does Maxima know the gender of the programmer? • The value had to have been initialized first. gender: male; • We now think of sequences of Maxima instructions forming Maxima programs.
formal example has 3 words reserved in Maxima if (gender is male) then print ("use men’s room") else print ("use women’s room");
Another example if (flag = 1) then plot2d(f(x), [x, 1, 100]); else plot2d(g(x), [x, 1, 1000]);
Functions and if statements • They can be combined: f(x):= if (x < 0) then 0 else if (0 <= x) and (x < 1) then x else 1
Functions and if statements • They can be combined: f(x):= if (x < 0) then 0 else if (0 <= x) and (x < 1) then x else 1 Argument less than 0 Argument between 0 and 1 Any other value of the argument
What do we have to know about branches? • What condition determines the choice of branch we want our program to execute? • What happens in each branch? • What is the value of the logical condition that determines the branch we take • Must know this before we start coding • The syntax of an if-else statement
Formal syntax • if cond_1 then expr_1 else expr_0 • The value of this expression: • evaluates to expr_1 if cond_1 evaluates to true • otherwise the expression evaluates to expr_0.
The condition • Must be able to be evaluated to either true or false. • Uses operators for comparing things • In nearly all situations, operators are one or more of • Relational operators • Logical operators
Some uses are obvious if (1 < 2) then print (1) else print("I love this course"); Output is 1 = 1
Some uses are obvious if (1 >= 2) then print (1) else print("I love this course"); Output is I love this course
if ((1 < 2) and (2 < 3)) then print(1) else print("I love this course"); Output is
Less obvious if ((1 < 2) and (2 < 3)) then print(1) else print("I love this course"); Output is 1 = 1 Note the use of parentheses
if ((1 < 2) and (2 > 3)) then print(1) else print("I love this course"); Output is
if ((1 < 2) and (2 > 3)) then print(1) else print("I love this course"); Output is I love this course
Let’s go back to the equality relational operators • =, # • equal, notequal • = and # are by far the most common • Even they do not quite behave the way you think • We will avoid equal and notequal as much as we can
Examples of the use of = if (1 = 1) then print("use men’s room") else print("use women’s room"); Output: use men’s room
if (1 = 2-1) then print ("use men’s room") else ("use women’s room"); Output: use men’s room
if (1 = (2 + 4 + 6)/(6 + 4 + 2)*(2-1)) then ("use men’s room") else ("use women’s room"); Output: use men’s room
Unfortunately, Maxima doesn’t really understand = for everything if (1 = sin(x)^2 + cos(x)^2) then print("use men’s room") else print("use women’s room"); Output: use women’s room
More complex if-else statements if cond_1 then expr_1 elseif cond_2 then expr_2 elseif ... else expr_0 evaluates to expr_k if cond_k is true and all preceding conditions are false. • If none of the conditions are true, the expression evaluates to expr_0.
if (avg < 60) then print("F") elseif ((60 <= avg) and (avg < 70)) then print("D") elseif ((70 <= avg) and (avg < 80)) then print("C") elseif ((80 <= avg) and (avg < 90)) then print("B") else print(A);
Thing to note • All cases are covered • Parentheses were used to delimit every input to a relational operation such as and • Code was indented consistently to make it more readable
There are shortcuts – you can omit the ending else if cond_1 then expr_1 is equivalent to if cond_1 then expr_1 else false
The same holds for elseif statements • Try this one yourself
What happens if we omit the else? if (1 = (2 + 4 + 6)/(6 + 4 + 2)*(2-1)) then print("use men’s room"); Output is use men’s room Note that the condition in the parentheses evaluates totrue.
if (1 = (2 + 4 + 6)/(6 + 4 + 2) * 2) then print("use men’s room"); Output is false Note that the condition in the parentheses evaluates tofalse. The phrase “use men’s room” never gets printed.
We have already used one form of iteration: for i: 0 step 1 thru 9 do B[i] : 3 * i ;
Iteration in Maxima The idea: • Identify statements you want to repeat • Start the iteration, using an iterator • Choose when to start • Choose where to end • Choose how large a step you make It’s just like walking!
This can be simplified for i: 0 thru 9 do B[i] : 3 * i ; • The step can be omitted – the step value defaults to 1 if omitted. • Don’t omit the step if any other increment is intended.
So far, all iterations just used loop control variables (usually indicies) • We can do more – accumulate information • Example: Find the sum of the integers from 1 to 100.
Approach (by hand) • Add the second to the first, • then the third to that sum • Then the fourth to that sum, • …
Approach on a calculator • Clear the memory • Add the first • Add the second • Add the third • …
The Maxima approach follows the calculator model sum:0; for i: 1 thru 100 do sum : sum + i ; print(sum);
Comments show how the program works sum:0; /* initialize */ for i: 1 thru 100 do sum : sum + i ; /* accumulate */ print(sum); /* output */ 5051
Another example sum_of_squares:0; /* initialize */ for i: 1 thru 100 do sum_of_squares : sum_of_squares + i * i; /* accumulate */ print(sum_of_squares); /* output */ 338350
Probably better to use shorter name sum:0; /* initialize */ for i: 1 thru 100 do sum : sum + i *i ; /* accumulate */ print(sum); /* output */
Can find averages of arrays array(B, 20); for i: 0 thru 99 do B[i] : sin(i * %pi/4); /* init */ B[17]; 1 ------- sqrt(2) avg: 0; sum :0; for i :0 step 1 thru 99 do sum : sum + B[i]; print(sum, avg);
There are 3 forms of iteration • A do-loop which follows the format we have seen so far • A do-loop where the ending condition depends on some condition continues to hold. • A do-loop where the loop continues to execute unless a condition is true.
There are 3 forms of iteration • for variable: initial_value step increment thru limit do body (we have done this one many times already)