L-5 MCS 320 Wednesday 31 August 2005
| > | restart; |
The restart clears all assigned values from the variables. This is often too drastic. In this lecture we will see how to unassign a particular variable.
1. Verifying Symbolic Formulas
Let us start with an application. Computer algebra can produce huge formulas, but how to verify those?
| > | p := x^3 + a*x^2 + b*x + c; |
| > | sols := solve(p,x): # Maple knows the Cardano formulas |
We will verify the formulas for a random instance.
| > | x1 := rand(): x2 := rand(): x3 := rand(): |
| > | q := (x-x1)*(x-x2)*(x-x3); # a polynomial with known roots |
| > | eq := expand(q); |
| > | a1 := coeff(eq,x,2); b1 := coeff(eq,x,1); c1 := coeff(eq,x,0); |
To verify the formulas for the solutions, we assign to the general coefficients a, b, c, the values a1, b1, and c1:
| > | a,b,c := a1,b1,c1; |
| > | a; b; c; |
The side effect of this assignment is that the solutions assigned to "sols" have changed as well:
| > | sols; |
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
To verify the formulas of Cardano, we do not really see that much from comparing the values in sols with the random roots x1, x2, and x3:
| > | x3 = simplify(sols[1]); |

![]()
![]()
The simplification works well for the real root but not for the "complex roots".
| > | simplify(sols[2]); |

![]()
![]()
![]()
| > | evalf(sols[2],50); |
![]()
| > | x1; |
We see that the x1 agrees (modulo rounding) to the symbolic solution in sols.
| > | evalf(sols[3],50); |
![]()
| > | x2; |
The imaginary part goes to zero, mysteriously not to something close to 10^(-50)...
We have lost our original polynomial p:
| > | p; |
This loss may be unfortunate if p was obtained after very long calculations.
| > | a := 'a'; b := 'b'; c := 'c'; |
With this unassignment, we have recovered our original polynomial p
| > | p; |
and the symbolic solutions as well:
| > | sols; |
![]()

![]()



![]()


| > |
2. Sharing Values and Side Effects of Assignments
We can deliberately create links between variables.
| > | x := 10; y := x; |
What is important is that x and y are independent from each other:
| > | x := 11; y; |
We see that x := 11; changed x, but left y to the value of 10.
Suppose we want to link another variable z to x, then we can use the right quotes:
| > | z := 'x'; # we assign to z the name of x |
| > | x := 12; y; z; |
The variables z and x are linked, when x changes, also the value of z changes.
To verify the links between z and x, we can use the addressing operator, similar to &x
| > | a := addressof(x); |
| > | addressof(z); |
x and z have the same address. Similar to the dereferencing * operator in C, we can ask for *a, when a is an address:
| > | pointto(a); |
We see that *a is 12, as x is assigned to the value 12.
3. The commands assign, unassign, evaln
With right quotes we can unassign a variable. There are two alternatives: "unassign" and "evaln".
First let us look to an application of assign:
| > | formula := balance = deposit*(1+interest_rate); |
| > | deposit := 1024; interest_rate := 0.04; |
| > | formula; |
The formula has changed by the assignments, but what we really would like to change is the variable balance. We would like to assign to balance the value computed by the formula.
The assign commands turns an equation "=" into ":=", for example:
| > | assign(formula); |
| > | balance; |
| > | unassign('balance'); # notice the right quotes ! |
| > | balance; |
| > | formula; |
Typically, many solvers in Maple return equations:
| > | eqs := { y1 + y2 = p1, y1 + p1*y2 = y2 }; |
| > | sol := solve(eqs,{y1,y2}); |
| > | assign(sol); # turns the equations in sol to assignments |
| > | y1; y2; |
An alternative to unassign is "evaln" (evaln = evaluate to the name):
| > | y1 := evaln(y1); y2 := evaln(y2); |
| > | sol; |
| > | y1; y2; |
Try exercise 4, this is a situation where evaln is really needed.
Finally, it could be that we forgot which variables are in use, or that we want a program to pick up the names of the variables that are in use:
| > | anames(user); # list the names of variables assigned by the user |
| > | anames(integer); |
![]()
![]()
| > | unames(); # unassigned names are the names without a value |
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
| > |
| > |