Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prénom Nom Document Analysis: Exploration of Mathematica 6 Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008.

Similar presentations


Presentation on theme: "Prénom Nom Document Analysis: Exploration of Mathematica 6 Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008."— Presentation transcript:

1 Prénom Nom Document Analysis: Exploration of Mathematica 6 Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008

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

3 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold 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  f is the head  a,b are 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 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold List manipulation  In Mathematica lists 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 © Prof. Rolf Ingold 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 from 1 to 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 © Prof. Rolf Ingold Use of variables  In Mathematica any expression may be assigned to variable  an assignment has the form var=expr  then, in subsequent expressions each occurence of var will be replaced by expr x=a+b+c; 2x  2 (a + b + c)‏  a variable is unset (removed) by var=., Unset[var], or Clear[var]

11 © Prof. Rolf Ingold Definition of functions  In Mathematica new functions may be defined  a function definition has the form f[x_]:=expr  in subsequent expressions f[a] will result in the evaluation of expr by substituting x by a x = a + b + c; f[x_] := x^2; f[{12, a, x}]  {144, a^2, (a + b + c)^2}

12 © Prof. Rolf Ingold Immediate and delayed assignments  There are two different ways to make assignments in Mathematica  lhs=rhs is said immediate assignment because rhs is evaluated when the assignment is made  lhs:=rhs is said delayed assignment because rhs is evaluated each time the value of rhs is 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 © Prof. Rolf Ingold Transformation rules  An interesting programming paradigm of Matematica is the concept of transformation rules  a transformation rule is written lhs->rhs  transformations are obtained by applying rules to expressions using expr/.rule or expr/.{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 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold 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 © Prof. Rolf Ingold 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 with f applied n times to expr  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 © Prof. Rolf Ingold 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


Download ppt "Prénom Nom Document Analysis: Exploration of Mathematica 6 Prof. Rolf Ingold, University of Fribourg Master course, spring semester 2008."

Similar presentations


Ads by Google