1 / 68

Programming in Maxima

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

lminarik
Download Presentation

Programming in Maxima

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in Maxima

  2. 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

  3. 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

  4. Review of Arrays in Maxima • Creation • Initialization

  5. Two ways to create matrices in Maxima • Select Enter from the Algebra menu

  6. One way to create them

  7. Other ways to create arrays • array(B,20); • C: make_array( any , 20); • Array(D, 20, 10); • E: make_array( any, 20, 10);

  8. Other ways to create arrays array(B,20); C: make_array( any , 20); array(D, 20, 10); E: make_array( any, 20, 10);

  9. 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.

  10. Example – Create an array (%i1) array(A, 10); (%o1) A (%i2) A(1) : 3;

  11. (%i1) array(A, 10); (%o1) A (%i2) A(1) : 3; Improper value assignment: A(1) -- an error. To debug this try debugmode(true); (%i3)

  12. 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

  13. (%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

  14. 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!

  15. Iteration example (%i1) array(B,10); (%o1) B (%i3) for i: 1 step 1 thru 10 do B[i] : 3 * i ; (%o3) done

  16. 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

  17. 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!

  18. 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

  19. (%i4) B[6]; (%o4) 18 (%i5) B[9]; (%o5) 27

  20. What about the array boundaries? How big can the index be? How small? Example: what is the output of (%i7) B[11];

  21. 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

  22. Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index

  23. Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index Start value

  24. Let’s review the syntax for i: 1 step 1 thru 10 do B[i] : 3 * i $ The index Step value Start value

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. What does this do? for i: 1 step 1 thru 7 do B[i] : 3 * i $

  33. 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]

  34. What does this do? for i: 2 step 1 thru 10 do B[i] : 3 * i $

  35. 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]

  36. What does this do? for i: 2 step 2 thru 10 do B[i] : 3 * i $

  37. 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]

  38. What does this do? for i: 1 step 2 thru 10 do B[i] : 3 * i $

  39. 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]

  40. 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) } $

  41. 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) } $

  42. 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

  43. 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

  44. Want:

  45. (%i8) B[4]; (%o8) 12 (%i9) B[6]; (%o9) 36

  46. 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.

  47. 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

  48. Can we have loops within loops? • Yes! • Just watch indentation • Count parentheses, braces, …

More Related