1 / 17

Document Analysis: Exploration of Mathematica 6

Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008. Document Analysis: Exploration of Mathematica 6. Outline. Basics of Mathematica Numerical and symbolic computation List manipulation Variable and functions Transformation rules and patterns Graphics

dixon
Download Presentation

Document Analysis: Exploration of Mathematica 6

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. Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008 Document Analysis:Exploration of Mathematica 6

  2. Outline Basics of Mathematica Numerical and symbolic computation List manipulation Variable and functions Transformation rules and patterns Graphics Functional and imperative programming

  3. Mathematica Basics Mathematica runs two process a front end displaying editable notebooks a back end running the kernel for computation Notebooks and the kernel communicate by exchanging expressions input expressions are written in the Mathematica language output expressions (returned by the kernel) are displayed in an appropriate form

  4. Mathematica expressions In Mathematica everything, i.e. data, programs, formulas, graphics, documents are represented as symbolic expressions All expressions have a standard form f[a,b,…] reflecting the internal structure, where fis the head a,bare the arguments The Mathematica language allows richer expressions FullForm[1+x^2+(y+z)^2] Plus[1,Power[x,2],Power[Plus[y,z],2]]

  5. Predefined functions Mathematica offers more than 2'200 predefined functions in Numerical computation evaluation on integer, rational, real, complex numbers, vectors and matrices equation solving numerical calculus Symbolic computation calculus (integral and differential)‏ polynomial and other algebras discrete mathematics Data manipulation (list manipulation, combinatory, statistics, ...)‏ Visualization and graphics (with interactive control)‏ Programming (functional and imperative style)‏ Access to external resources (files, databases, libraries)‏ Various specialized add-ons are available

  6. Numerical computation Mathematica provides exact calculation whenever possible (typically on integer and rational numbers)‏ 2^100 1267650600228229401496703205376 12/234+56/789 418/3419 Mathematica allows the evaluation of numerical expressions with arbitrary precision N[Sqrt[2],30] 1.41421356237309504880168872421 N[12/234+56/789,40] 0.1222579701667154138637028370868675051185 Additionally, Mathematica is able to control accuracy

  7. Symbolic computation Mathematica offers many functions to perform symbolic computation formula manipulation Simplify[(x^2-1)/(x-1)] 1 + x equation solving Solve[x^2-(1+a)*x+a==0,x] {{x -> 1}, {x -> a}} calculus D[Sin[x]^2,x] 2 Cos[x] Sin[x] Integrate[6*x^2,x] 2 x^3

  8. List manipulation In Mathematicalists are used to represent collections, arrays, sets, vectors and matrices, etc. lists are represented by {a,b,…} or {{a,b},{c,d},…} elements are extracted using the Part operator :lst[[i]] Many functions are "listable", that is are applicable on each element 2*{3,5,x} {6, 10, 2 x} {2,4,x}*{3,5,x} {6, 20, x^2} There are hundreds of other functions generating lists: Range, Array, Table, Join, Take, Drop, Select, Map,etc.

  9. Iterative function The function Table[expr,iter1,…] generates a list by applying one or several iterators iter1, … over the expression expr Table[i^2,{i,10}] {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} Iterators have one of the following forms {max} iterates max times {var,max} iterates with var running from1to max {var,min,max} with var running from min to max {var,min,max,step} with var running from min to max by step

  10. Use of variables In Mathematica any expression may be assigned to variable an assignment has the formvar=expr then, in subsequent expressions each occurence ofvarwill be replaced byexpr x=a+b+c;2x 2 (a + b + c)‏ a variable is unset (removed) byvar=. , Unset[var], or Clear[var]

  11. Definition of functions In Mathematica new functions may be defined a function definition has the formf[x_]:=expr in subsequent expressionsf[a] will result in the evaluation ofexprby substitutingxbya x = a + b + c;f[x_] := x^2;f[{12, a, x}] {144, a^2, (a + b + c)^2}

  12. Immediate and delayed assignments There are two different ways to make assignments in Mathematica lhs=rhsis said immediate assignment becauserhsis evaluated when the assignment is made lhs:=rhsis said delayed assignment becauserhsis evaluated each time the value ofrhsis requested By default, assignments are defining global variables name conflicts are often sources of errors ! By convention user defined variables and functions should start with a lowercase letter

  13. Transformation rules An interesting programming paradigm of Matematica is the concept of transformation rules a transformation rule is writtenlhs->rhs transformations are obtained by applying rules to expressions usingexpr/.ruleorexpr/.{rule,…} {x,x^2,y}/.x->a {a, a^2, y} x+y/.{{x->1,y->2},{x->4,y->2}} {3, 6} f[1]+f[2]+f[4]/.{f[x_]->x^2} {a, a^2, y} Rules are used to define options

  14. Patterns Mathematica defines a pattern language to describe classes of expressions Patterns are used to restrict the functions and rules _ represents any expression x_ (or x:_)represents any expression and is referred to as x x_h(or x:_h)represents an expression (referred to as x) with head h Patterns can be composed recursively and include constraints _[_:List] represents any function having a list as argument _[x_,x_] represents any function having twice the same argument Additionally patterns can be restricted by tests (pat?test) or by conditions (pat/;cond)‏ x_?NumberQ x_/;x<0

  15. Graphics In Mathematica graphics are generated by expressions ListPlot[ Table[ {k,PDF[BinomialDistribution[50,p],k]}, {p,{0.3,0.5,0.8}}, {k,0,50}], Filling->Axis]

  16. Functional programming Several predefined functions are useful for functional programming Apply[f,expr] replaces the head of expr by f and evaluates it Map[f,list]applies the function f to each element of list Nest[f,expr,n]gives an expression withfappliedntimes toexpr NestList[f,expr,n]does the same but returns the list of all intermediate results Mathematica can handle pure functions, without explicit names they are written body&in which#1,#2, ...represent the arguments (or#for a single argument)‏ Map[(2^#)&,Range[10]] {2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}

  17. Imperative programming Mathematica provides classical control structures expressed with a functional syntax If[condition,ifTrue] If[condition,ifTrue,ifFalse] Which[test1,value1,test2,value2,…] Switch[expr,form1,value1,form2,value2,…] While[test,body] Do[expr,iter1,…] For[start,test,incr,body] Mathematica provides also block structures to control the scope of symbols Module[{x,y,…},expr] with lexical scoping Block[{x,y,…},expr] with dynamic scoping

More Related