<<

WEEK 10: FAREWELL TO PYTHON... WELCOME TO MAPLE!

1. A review of PYTHON’s features During the past few weeks, we have explored the following aspects of PYTHON: • Built-in types: integer, long integer, float, complex, boolean, list, se- quence, string etc. • Operations on built-in types: +, −, ∗, abs, len, append, etc. • Loops: N = 1000 i=1 q = [] while i <= N q.append(i*i) i += 1 • Conditional statements: if x < y: min = x else: min = y • Functions: def fac(n): if n==0 then: return 1 else: return n*fac(n-1) • Modules: math, turtle, etc. • Classes: Rational, Vector, , Polygon, etc. With these features, PYTHON is a powerful tool for doing mathematics on a computer. In particular, the use of modules makes it straightforward to add greater functionality, e.g. GL (3D graphics), NumPy (numerical algorithms).

2. What about MAPLE? MAPLE is really designed to be an interactive tool. Nevertheless, it can also be used as a . It has some of the basic facilities available in PYTHON: Loops; conditional statements; functions (in the form of “procedures”); good graphics and some very useful additional modules called “packages”: • Built-in types: long integer, float (arbitrary precision), complex, rational, boolean, array, sequence, string etc. • Operations on built-in types: +, −, ∗, mathematical functions, etc. • Loops: 1 2 WEEK 10: FAREWELL TO PYTHON... WELCOME TO MAPLE!

Table 1. Some of the most useful MAPLE packages

Name Description Example DEtools Tools for differential equations exactsol(ode,y) linalg Linear Algebra gausselim(A) plots Graphics package polygonplot(p,axes=none) stats package random[uniform](10)

N := 1000 : q := array(1..N) : for i from 1 to N do q[i] := i*i : od : • Conditional statements: if x < y then min := x else min := y fi: • Functions: fac := proc(n) if n=0 then return 1 else return n*fac(n-1) fi : end proc : • Packages: See Table 1. • Classes: None! The most obvious difference between PYTHON and MAPLE is that the latter does not support classes. From our point of view, this is not a problem: MAPLE compensates for the lack of classes by providing so many specialised mathematical features.

3. An example To illustrate some of the differences between MAPLE and PYTHON, consider the problem of converting a sequence of temperatures from Fahrenheit to Celsius. The PYTHON version looks like this: # # output a table of Farenheit and equivalent Celsius # temperatures from low to high in steps of step # low = 0.0 high = 100.0 step = 10.0 WEEK 10: FAREWELL TO PYTHON... WELCOME TO MAPLE! 3 farenheit = low while farenheit <= high: celsius = 5.0*(farenheit-32.0)/9.0 print farenheit,celsius farenheit += step The MAPLE program would look like this: # # output a table of Farenheit and equivalent Celsius # temperatures from low to high in steps of step # low := 0.0; high := 100.0; step := 10.0; farenheit := low; # Farenheit temperature. while (farenheit <= high) do celsius := 5.0*(farenheit-32.0)/9.0 : print(farenheit,celsius); farenheit := farenheit + step : od : The most obvious syntactical differences are In MAPLE, the assignment operator is :=

In MAPLE, every statement must end either with ; or :

4. Arrays

Consider the problem of writing a program that, given a set (xn,yn), 1 ≤ n ≤ N, of scattered data points, finds a straight line that best fits it. See Figure 1.

y = ax +

(xn,yn)

Figure 1. Fitting a straight line to a set of scattered points. 4 WEEK 10: FAREWELL TO PYTHON... WELCOME TO MAPLE!

The Intelligent will approach this task in three stages: (1) Gain a clear understanding of the problem (analysis). (2) Identify the key concepts involved in a solution (design). (3) Implement the solution in a program (programming).” Analysis. The equation of a straight line is y = ax + b . So we seek two numbers a and b such that N 1 2 (4.1) f(a,b)= [yn − axn − b] X 2 n=1 is minimised. Design. Necessary conditions for a and b to minimise f in Equation (4.1) are: N ∂f (4.2) 0= (a,b)= − [yn − axn − b] xn X ∂a n=1 N ∂f (4.3) 0= (a,b)= − [yn − axn − b] X ∂b n=1 These conditions give two equations for the two unknowns a and b. We put them in form:

N 2 N N n=1 xn n=1 xn a n=1 xn yn (4.4) P P    = P  . N N  n=1 xn N  b  n=1 yn  P P We can then solve this system for a and b. Programming. The implementation of this solution requires • some means of entering the data, • some container to hold the data. • some algorithm for solving the linear system (it’s in fact easy to do this “by hand”. Here, we illustrate a feature of MAPLE). The following MAPLE implementation reads the data from a file, stores the data in arrays, and uses the linalg package to solve the linear system (4.4). # read scattered data points and compute the best fit

read ‘d:/yves/compMath/2003/lectures/code/data.m‘ : # read the data print(x); print(y); # look at the data print(N);

M := array(1..2,1..2) : # use arrays for clarity rightHandSide := array(1..2) : solution := array(1..2) :

M[1,1] := add(x[n]*x[n],n=1..N) : # assign M[1,2] := add(x[n],n=1..N) : M[2,1] := M[1,2] : WEEK 10: FAREWELL TO PYTHON... WELCOME TO MAPLE! 5

M[2,2] := N : rightHandSide[1] := add(x[n]*y[n],n=1..N) : rightHandSide[2] := add(x[n],n=1..N) : with(linalg): # "import" the linalg package solution := linsolve(M,rightHandSide) : # solve the system a := solution[1] ; b := solution[2] ;