Documentation |
MuPAD^{®} automatically evaluates results returned by most of the system functions. However, a few functions can return unevaluated results. For example, the text2expr function does not evaluate the returned results:
text2expr("2 + 2")
The last function and its shortcut %, which return the previously computed object, also do not evaluate the results:
%
For such cases, MuPAD provides the eval function. This function enables you to enforce evaluation of an expression. For example, enforce evaluation of the previously returned expression:
eval(%);
Another example of the function that does not automatically evaluate returned results is the subs function. This function can simplify expressions that contain only purely arithmetical operations:
subs(x^2 + 1, x = 0)
However, the subs function does not evaluate expressions. For example, substitute the variable x with the value 0 in the following expression that contains the sine function:
subs(sin(x^2) + 1, x = 0)
You can use the eval function to enforce evaluation of the results returned by subs. In this case, MuPAD evaluates the whole expression:
eval(%)
Alternatively, the subs function provides a more efficient method to evaluate its results. The EvalChanges option enforces evaluation of the modified parts of the expression, leaving the unchanged parts out of the evaluation process:
subs(sin(x^2) + 1, x = 0, EvalChanges)
Most efficiently, evaluate an expression at a particular value of a variable by using the evalAt function. See Evaluation at a Point.
Also, MuPAD does not evaluate arrays, tables, and polynomials. For example, the system does not evaluate the identifiers a and b of the following array A:
A := array(1..2, [a, b]): b := 2*a: a := 1: A
When you access the entries of the array A by using the op function, the system does not evaluate the entries of A. When you use the indexed access, the system evaluates the entries of arrays, matrices and tables:
op(A, 1), op(A, 2); A[1], A[2]
To evaluate all entries of an array, a table, or a polynomial apply the eval function to that array, table, or polynomial. Use the map function to apply eval to an array or a table:
map(A, eval)
For polynomials, use the mapcoeffs function:
p := poly(c*x, [x]): c := 10: mapcoeffs(p, eval)
delete a, b, c: