| ||||||||||||||||||||
Assignment Statements
Symbols, such as x or a or eqn2, are more
than just names - they can also have values and can name functions. A symbol is given a value by
following it with a colon and an equals sign and then the expression which is
to be its value. If a symbol does not have a value, it evaluates to itself.
Otherwise, Algebra UltimaCalc will fetch its value and simplify it. See
also eval, which evaluates an expression
to a specified depth of recursion.
Examples:
x := 3 * a
a := b + c
b := 7
After entering the three lines above, if you now type x and hit 'Enter', the
value of x, which is 3*a will be simplified to 3*(b+c) and the value
of b will be used to simplify the result to 21 + 3*c.
While looking up values of values, the simplifier checks to see whether the assignments refer to each other. So, if you enter the following:
p := q
q := p
and then try to find the value of p or q, you will be told that there is a problem.
As well as assigning a value to a symbol, you can also set the value of an item in an expression or an element of a matrix element.
Examples:
x:=a*b*c sets the value of the symbol x to a*b*c. The function operand(x, 2) extracts the second operand of x, which in this case is b.
If we now do operand(x, 2) := 1+b, this second operand is changed, giving the
result a * (1 + b) * c.
m:=matrix([2, 3], [4, 5]); element(m, 2, 1) := 10; m defines a matrix with elements 2, 3, 4, 5 and makes this matrix the value of the symbol m so we can refer to it later. The next instruction changes one of the elements of the matrix, and the matrix stored in m ends up having the elements 2, 3, 10, 5.
See also 'Assignment Operators', below.
unassign(...)
This allows you to undo the assignment of a value. After entering the two example lines above and verifying that
there is indeed a problem, enter the line:
unassign(q)
If you now check the values of p and q you will find that p still has
the value q but q no longer has a value, as it evaluates to itself.
The unassign command can be given two or more arguments, which should all be symbols. All these symbols will have their values removed. Using unassign with no arguments will cause all symbols to lose their values, if this action is confirmed.
Assignment to Functions
You can define your own functions as in the following example:
twice(x) := 2 * x
This will define twice as a function that returns the result of doubling its single argument.
The function unassign will not remove the function definition, though you can always redefine
the function if necessary. A symbol can have both a value and a function definition assigned to it, though
this may not be good practice.
As well as this direct entry method of defining functions, there is a function editor which provides additional functionality.
Assignment Operators
To add something to the value of a symbol, you could do this kind of thing:
a := one gives the value one to the symbol a.
a := a + two changes the value stored in a to one + two.
More simply, you could do the following:
a += three to add three to the value of a.
The other assignment operators are -=, *=, and /= which act similarly. Continuing the example:
a -= two; a /= z change the value of a to (one + three) / z.
The expression to the left of an assignment operator need not only be a symbol - it can be anything (apart from a function) that is acceptable to the operator +=. (See above)
