690 likes | 706 Views
Programming in Maxima. Using lists for our polynomial algebra program:. Maxima code was somewhat difficult to write No easy general way to get to the next element first second third … last. Need a more systematic way. Use arrays Access array elements by an index
E N D
Using lists for our polynomial algebra program: • Maxima code was somewhat difficult to write • No easy general way to get to the next element • first • second • third • … • last
Need a more systematic way • Use arrays • Access array elements by an index • Code to access array element #1 is similar to code to access array element # 2 • Code to access array element #2 is similar to code to access array element # 3 • Can build on this idea to create code to iterate
Review of Arrays in Maxima • Creation • Initialization
Two ways to create matrices in Maxima • Select Enter from the Algebra menu
Other ways to create arrays • array(B,20); • C: make_array( any , 20); • Array(D, 20, 10); • E: make_array( any, 20, 10);
Other ways to create arrays array(B,20); C: make_array( any , 20); array(D, 20, 10); E: make_array( any, 20, 10);
Array entries • Array entries can be accessed by their indicies. • Warning: make sure each index stays within the appropriate range limit. • Often this will require programming.
Example – Create an array (%i1) array(A, 10); (%o1) A (%i2) A(1) : 3;
(%i1) array(A, 10); (%o1) A (%i2) A(1) : 3; Improper value assignment: A(1) -- an error. To debug this try debugmode(true); (%i3)
Proper syntax (%i3) A[1]: 3; (%o3) 3 (%i4) A[2]: 6; A[3]: 9; A[4]: 12; A[5]: 15; (%o4) 6 (%o5) 9 (%o6) 12 (%o7) 15
(%i8) A[6] : 24; A[7]: 21; A[8] : 24; A[9]: 27; A[10]: 30; (%o8) 24 (%o9) 21 (%o10) 24 (%o11) 27 (%o12) 30
This took too much effort • Just wanted to initialize the elements of the array. • Want to avoid all this error-prone typing • Really didn’t want to do this for 100 elements! • Iteration is the key!
Iteration example (%i1) array(B,10); (%o1) B (%i3) for i: 1 step 1 thru 10 do B[i] : 3 * i ; (%o3) done
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
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!
Iteration example (%i1) array(B,10); (%o1) B (%i3) for i: 1 step 1 thru 10 do B[i] : 3 * i $ (%i4) B[6]; (%o4) 18
(%i4) B[6]; (%o4) 18 (%i5) B[9]; (%o5) 27
What about the array boundaries? How big can the index be? How small? Example: what is the output of (%i7) B[11];
What about the array limits? How big can the index be? How small? (%i7) B[11]; Array B has dimensions [10], but was called with [11] -- an error. To debug this try debugmode(true); Maxima checks array limits – not all languages do this check
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index Start value
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index Step value Start value
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index Step value End value Start value
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ Start of what gets iterated The index Step value End value Start value
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ Start of what gets iterated The index Step value End value Start value
What ends the body that’s iterated? • Either $ or ; • $ does the computation, but does not display the result as output • ; does the computation, and does display the result as output
Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ Start of what gets iterated The index Step value End value Ends the statements to be iterated Start value
Recall that ; and $ end sentences for i: 1 step 1 thru 10 do B[i] : 3 * i ; Start of what gets iterated The index Step value End value Ends the statements to be iterated Start value
Note the form of the “do-statement” for i: 1 step 1 thru 10 do B[i] : 3 * I $ Any of the do-statement parameters can be changed
What does this do? for i: 1 step 1 thru 7 do B[i] : 3 * i $
What does this do? for i: 1 step 1 thru 7 do B[i] : 3 * i $ Assigns values to B[1] … B[7] Nothing is done to B[8], B[9], B[10]
What does this do? for i: 2 step 1 thru 10 do B[i] : 3 * i $
What does this do? for i: 2 step 1 thru 10 do B[i] : 3 * i $ Assigns values to B[2] … B[10] Nothing is done to B[1]
What does this do? for i: 2 step 2 thru 10 do B[i] : 3 * i $
What does this do? for i: 2 step 2 thru 10 do B[i] : 3 * i $ Assigns values to B[2], B[4], B[6], B[8],B[10] Nothing is done to B[1], B[3], B[5], B[7], B[9]
What does this do? for i: 1 step 2 thru 10 do B[i] : 3 * i $
What does this do? for i: 1 step 2 thru 10 do B[i] : 3 * i $ Assigns values to B[1], B[3], B[5], B[7], B[9] Nothing is done to B[2], B[4], B[6], B[8],B[10]
Can have multiple statements in a loop body array(B, 10); for i : 1 step 1 thru 5 do {B[i]:3*i, B[i+5]: (i + 5) *(i + 5) } $
Can have multiple statements in a loop body for i : 1 step 1 thru 5 do {B[i]:3*i, B[i+5]: (i + 5) *(i + 5) } $
Can have multiple statements in a loop body for i : 1 step 1 thru 5 do {B[i]:3*i, B[i+5]: (i + 5) *(i + 5) } $ Insert { and } as delimiters of the loop body
Can have multiple statements in a loop body for i : 1 step 1 thru 5 do {B[i]:3*i, B[i+5]: (i + 5) *(i + 5) } $ Insert { and } as delimiters of the loop body Insert , to separate statements
(%i8) B[4]; (%o8) 12 (%i9) B[6]; (%o9) 36
Style concerns • Long lines that wrap around are hard to read. • Use carriage returns and tabs to make readable. • Carriage returns and tabs are ignored by the Maxima interpreter. • Maxima programs, especially long ones, are often stored as notebooks in separate files.
Example of formatting do-statements for i : 1 step 1 thru 5 do { B[i] : 3 * i, B[i + 5] : (i +5) * (i + 5) } $ Use returns and tabs for clarity
Can we have loops within loops? • Yes! • Just watch indentation • Count parentheses, braces, …