lec6.mws

L-6 MCS 320 Friday 2 September 2005

> restart;

1. Evaluation

Last time we saw how to make links between the variables.

> a := b; b := c; c := 3;

a := b

b := c

c := 3

> a;b;c;

3

3

3

These three variables all have the value 3.

> c := 4;

c := 4

> a;b;c;

4

4

4

We can trace the links with the eval command:

> eval(a,1);

b

> eval(a,2);

c

> eval(a,3);

4

The links between a and b, between b and c where made because at the time of the assignment, c did not yet have a value.  Suppose we assign to d, a new variable, the variable a:

> d := a;

d := 4

> eval(d,1);

4

At the righthand side of an assignment, Maple evaluates the variables in full.

To create the link to a variable which has already a value, and thus to prevent the evaluation, we must use right quotes:

> d := 'a';

d := a

> eval(d,1);

a

> eval(d,2);

b

> d; # complete evaluation

4

> eval(d);

4

Another use of the right quotes

To assign the result of a procedure to a variable, we must use right quotes, similar as passing the address to a C function.  In C we have call by value and call by name.

> p := x^2 - x + 3; q := x - 1;

p := x^2-x+3

q := x-1

Suppose we want to compute the remainder of the division of p by q:

> quotient := 0;  # suppose the quotient is already in use...

quotient := 0

We want to assign the result of the remainder to "rest" and the quotient to "quotient".

> rest := rem(p,q,x,quotient);

Error, (in rem) illegal use of a formal parameter

> quotient;

0

> rest := rem(p,q,x,'quotient');

rest := 3

> quotient;

x

The first use of the command rem went wrong because Maple could not assign to the value currently taken by "quotient".  The second time, we called the rem with the name (or the address) of the variable.

This is the 3rd use of right quotes, remember the unassign as first use.

2. Names of Variables

Left quotes are used to give special names to variables.

> `the day of today` := "Friday";

the day of today :=

To have names of variables with spaces, we can use the left quotes.

The empty symbol is just two left quotes after each other.  Concatenating to the empty symbol is useful for making sequences of variables.

> x || (1..9);

x1, x2, x3, x4, x5, x6, x7, x8, x9

We just created a sequence of variables, concatenating the numbers 1 to 9 to the symbol x.

To have a two dimensional table of variables, we should concatenate to the empty symbol:

> ``||(a,b,c) || (1..4);

41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44

What happened here was that Maple evaluated to the current values taken by a, b, c:

> ``||('a','b','c')||(1..4);

a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4

The right quotes prevented the evaluation and yielded the correct sequence of variables.

We use the name of a without having to unassign a.  A local use of the name a is in summations.

> sum(a,a=1..20); # sum the first 20 numbers

Error, (in sum) summation variable previously assigned, second argument evaluates to 4 = (1 .. 20)

> sum('a','a'=1..20);

210

In this summation, the a is treated as a local variable and the value assigned to a is ignored.

Some of the variables are protected, for example Pi.  Another one is gamma:

> gamma := 3;

Error, attempting to assign to `gamma` which is protected

> unprotect(gamma); # we have no use for "the" gamma

> gamma := 3;

gamma := 3

The "gamma" is now just another variable.

> gamma := 'gamma';

gamma := gamma

> evalf(gamma);

.5772156649

>

With the assignment we see we have not lost the value of the constant gamma.