12 010 Computational Methods of Scientific Programming Lecture

  • Slides: 10
Download presentation
12. 010 Computational Methods of Scientific Programming Lecture 13 Today’s lecture • More Mathematica

12. 010 Computational Methods of Scientific Programming Lecture 13 Today’s lecture • More Mathematica functionality • Mathematica Version of poly_area. f Web page http: //www-gpsg. mit. edu/~tah/12. 010 2002/10/22 12. 010 Lec 13

Review of Lecture 12 • Started looking at Mathematica and went over the basic

Review of Lecture 12 • Started looking at Mathematica and went over the basic features: – Front End / Kernel – Notebooks – Arbitrary Numerical Precision – Programming Control Features 2002/10/22 12. 010 Lec 13 2

Resources • Mathematica Book • Third Party Books – Programming in Mathematica – Beginner’s

Resources • Mathematica Book • Third Party Books – Programming in Mathematica – Beginner’s Guide to Mathematica • Electronic Book as well!!* • Web Sites* – http: //www. wolfram. com – http: //www. mathsource. com • Internal* – ? and ? ? – Help 2002/10/22 12. 010 Lec 13 3

Conversion of Units* • • Add the units package: <<Miscellaneaous`Units Convert[quantity with units, new

Conversion of Units* • • Add the units package: <<Miscellaneaous`Units Convert[quantity with units, new units] Convert. Temperature[number, old scale, new scale] SI[ ], MKS[ ], CGS[ ] 2002/10/22 12. 010 Lec 13 4

Variables and Scope • • Variable Types (optional, but can be helpful) Contexts (Global`

Variables and Scope • • Variable Types (optional, but can be helpful) Contexts (Global` and others) Local variables in Module Mathematica iterator in Do, Table, and others* – {max}, {symbol, min, max} – {symbol, min, max, increment} 2002/10/22 12. 010 Lec 13 5

Assignment and Delayed Assignment • Assignment and delayed assignment – = is immediate –

Assignment and Delayed Assignment • Assignment and delayed assignment – = is immediate – : = is delayed • Functions that remember their values* – f[x_] : = f[x] = function of x – Mathematica looks for specific definitions first, so it checks first for previously calculated values are checked – ? yields the usage, if defined: f: : usage = "string"; – ? ? yields the definition and all the values 2002/10/22 12. 010 Lec 13 6

String Operations* • To. String[ ] and To. Expression[ ] • String Manupulation –

String Operations* • To. String[ ] and To. Expression[ ] • String Manupulation – String. Take – String. Drop – String. Join • Sorting strings • Characters from strings 2002/10/22 12. 010 Lec 13 7

Importing and Exporting* • • • Directories and $Path Importing Data Images Saving Variables

Importing and Exporting* • • • Directories and $Path Importing Data Images Saving Variables (Save[ ]) Saving Kernel States (Dump. Save[ ]) 2002/10/22 12. 010 Lec 13 8

Mathematica Programming • Evolution of code – Set of statements – Group into functions

Mathematica Programming • Evolution of code – Set of statements – Group into functions – Package of functions • Use of Module – no compiling • Mathematica Compile[ ] – logical, integer, real, or complex arguments – single value returned (not a list) – speeds up numerical calculations 2002/10/22 12. 010 Lec 13 9

Translating poly_area. f • Expressions go from Mathematica to FORTRAN with Fortran. Form[ ]*

Translating poly_area. f • Expressions go from Mathematica to FORTRAN with Fortran. Form[ ]* • Basic Translation from FORTRAN to Mathematica – Possible, but not Mathematica savvy • Mathematica style – Descriptive variable names (begin with lower case and use embedded capitals) – Short functions that use the power of Mathematica – Use lists instead of arrays – "map" over lists rather than loop • Example of poly_area. f -> poly. Area* 2002/10/22 12. 010 Lec 13 10