eval - Maple Help

eval

evaluation

Calling Sequence

 eval(e, x=a) $\genfrac{}{}{0}{}{e}{\phantom{x=a}}|\genfrac{}{}{0}{}{\phantom{e}}{x=a}$ eval(e, eqns) eval['recurse'](e, eqns) eval(e) eval(x, n)

Parameters

 e - expression x - usually a name but may be a general expression a - expression eqns - list or set of equations n - positive integer

Description

 • The most common use of the eval function is to evaluate an expression e at a given point x=a.  For example, eval(x^2+3*x+2,x=1) evaluates the polynomial x^2+3*x+2 at the point $x=1$ obtaining 6.
 • You can enter the command eval using either the 1-D or 2-D calling sequence. For example, eval(x^3 + 3*x + 2, x=1) is equivalent to $\genfrac{}{}{0}{}{\left({x}^{3}+3x+2\right)}{\phantom{x=1}}|\genfrac{}{}{0}{}{\phantom{\left({x}^{3}+3x+2\right)}}{x=1}$.
 • In the second and third calling sequences, where eqns is a set or list of equations, the expression e is evaluated at all the given points (or expressions) simultaneously.  If the index 'recurse' is given (the third calling sequence above), the evaluation is repeated until either the result does not change or an infinite loop is detected in the evaluation.
 • Usually, the left-hand sides of the equations will be variable names being evaluated.  However, if the left-hand side x of an equation is not a simple name, a syntactic substitution may be attempted which has the same limitations as with subs.
 • Since eval does pointwise evaluation, eval cannot be used to evaluate an expression at a singularity. Use limit instead.
 • The eval command knows how to correctly evaluate derivatives, integrals, piecewise function definitions, etc. whereas the subs command in comparison makes substitutions which may not make sense mathematically. If there are symbolic dependencies between the expression and the point at which it should be evaluated that are considered "unsafe", eval will return unevaluated. This knowledge is coded in the Maple library in the Maple procedures eval/diff, eval/int, eval/piecewise, etc. The user may teach Maple how to evaluate a function as follows. Given a function foo(y), the call eval(foo(y),x=a) will result in eval/foo(foo(y),x=a) being called if the user has defined eval/foo to be a Maple procedure.
 • The two remaining calling sequences of the eval function are used to perform explicit evaluation of a variable or expression. The meaning of evaluation in these cases is different from the previous calling sequences. The default evaluation rules in Maple are full evaluation for global variables, and one-level evaluation for local variables and parameters.  Sometimes the user requires full evaluation or one-level evaluation explicitly. For example, if  $x≔y$ and $y≔1$ in a Maple session, what is the value of $x$ ? In an interactive session where x and y are global variables, x would evaluate to 1 and we would say that x is "fully evaluated". For one-level evaluation, we would use the command eval(x, 1) which would in this case yield y. However, inside a Maple procedure, if x is a local variable or a parameter, then x evaluates to y and we would say x evaluated "one level". For parameters, this is only relevant when the variable being passed as a parameter is not a global variable, since it would then be fully evaluated before being passed to the procedure. For local variables and parameters, full evaluation is obtained with eval(x), yielding 1 in this example.
 • The call eval(e) means the expression e is fully evaluated up to two delayed evaluations (see uneval). This means that each name in e will be evaluated to its value, and the value will be evaluated recursively, and each function call in e will be executed with its parameters evaluated, but not beyond two levels of delayed evaluations.
 • The call eval(x, 1) means evaluate the name x one level. That is, the value assigned to the name x is returned without further evaluation.  More generally, eval(x, n) yields n-levels of evaluation of names.
 • Evaluation to n levels (n>1) is most useful for expressions that are still just names after n-1 levels of evaluation.  The eval(x, n) call returns the result of evaluating the name found at level n-1 to one level.
 • While evaluation to n levels can be applied directly to expressions other than names, the outcome can vary greatly depending on how the actual structure of the expression interacts with various automatic simplifications.
 • Applying eval to mutable container objects like tables, rtables, and modules does not map over the elements.  Use map(eval,M) to evaluate elements in an object, M.
 • When called from within a procedure being executed in the evalhf environment, eval forces an evaluation to take place outside of evalhf. Everything within the argument sequence to eval is evaluated in symbolic mode (that is, using symbols, integers, and software floating-point values). The result of the evaluation is converted back to a hardware float or array thereof. If this is not possible, an exception is raised.

 • The eval command is thread safe as of Maple 15, provided that evaluating the given expression is itself thread safe.

Examples

Examples of using eval to evaluate at a 'point':

 > $\mathrm{poly}≔{x}^{3}+3x+2$
 ${\mathrm{poly}}{≔}{{x}}^{{3}}{+}{3}{}{x}{+}{2}$ (1)
 > $\mathrm{eval}\left(\mathrm{poly},x=1\right)$
 ${6}$ (2)
 > $\mathrm{poly}≔xy+3z+2$
 ${\mathrm{poly}}{≔}{x}{}{y}{+}{3}{}{z}{+}{2}$ (3)
 > $\mathrm{eval}\left(\mathrm{poly},\left[x=2,y=3,z=t\right]\right)$
 ${3}{}{t}{+}{8}$ (4)
 > $\mathrm{eval}\left(\mathrm{int}\left(\mathrm{sin}\left(x\right),x\right),x=1\right)$
 ${-}{\mathrm{cos}}{}\left({1}\right)$ (5)

Example where eval will return a (correct) error instead of an incorrect answer:

 > $\mathrm{expr}≔\frac{\mathrm{sin}\left(x\right)}{x}$
 ${\mathrm{expr}}{≔}\frac{{\mathrm{sin}}{}\left({x}\right)}{{x}}$ (6)
 > $\mathrm{eval}\left(\mathrm{expr},x=0\right)$
 > $\mathrm{limit}\left(\mathrm{expr},x=0\right)$
 ${1}$ (7)

Using eval for inert integrals.

 > $\mathrm{integral}≔\mathrm{Int}\left(f\left(t,a\right),t=a..x\right)$
 ${\mathrm{integral}}{≔}{{\int }}_{{a}}^{{x}}{f}{}\left({t}{,}{a}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{t}$ (8)
 > $\mathrm{eval}\left(\mathrm{integral},\left\{a=1,t=0\right\}\right)$
 ${{\int }}_{{1}}^{{x}}{f}{}\left({t}{,}{1}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{t}$ (9)

Using eval for unevaluated derivatives.

 > $\mathrm{der}≔\mathrm{diff}\left(f\left(x\right),x\right)+f\left(x\right)$
 ${\mathrm{der}}{≔}\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right){+}{f}{}\left({x}\right)$ (10)
 > $\mathrm{eval}\left(\mathrm{der},x=0\right)$
 $\genfrac{}{}{0}{}{\left(\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)\right)}{\phantom{\left\{{x}{=}{0}\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)\right)}}{\left\{{x}{=}{0}\right\}}{+}{f}{}\left({0}\right)$ (11)

Evaluate the variable a to a number of levels:

 > $a≔b$
 ${a}{≔}{b}$ (12)
 > $b≔c$
 ${b}{≔}{c}$ (13)
 > $c≔x+1$
 ${c}{≔}{x}{+}{1}$ (14)

Default full evaluation.

 > $a$
 ${x}{+}{1}$ (15)

Force full evaluation.

 > $\mathrm{eval}\left(a\right)$
 ${x}{+}{1}$ (16)
 > $\mathrm{eval}\left(a,1\right)$
 ${b}$ (17)
 > $\mathrm{eval}\left(a,2\right)$
 ${c}$ (18)
 > $\mathrm{eval}\left(a,3\right)$
 ${x}{+}{1}$ (19)
 > $\mathrm{eval}\left(a,4\right)$
 ${x}{+}{1}$ (20)

Recursive evaluation.

 > $\mathrm{eval}\left(u+v+w,\left[u=v,v=w,w=1\right]\right)$
 ${v}{+}{w}{+}{1}$ (21)
 > $\mathrm{eval}\left['\mathrm{recurse}'\right]\left(u+v+w,\left[u=v,v=w,w=1\right]\right)$
 ${3}$ (22)

Comparing eval to subs

 The eval command is different from the subs command.
 > expr := sin(x)/cos(x);
 ${\mathrm{expr}}{≔}\frac{{\mathrm{sin}}{}\left({x}\right)}{{\mathrm{cos}}{}\left({x}\right)}$ (23)
 > subs(x = 0, expr);
 $\frac{{\mathrm{sin}}{}\left({0}\right)}{{\mathrm{cos}}{}\left({0}\right)}$ (24)
 > eval(expr, x = 0);
 ${0}$ (25)
 The eval and subs commands treat the evaluation of inert integrals differently: the subs command only inserts the given value(s) into the expression, but does not perform any evaluation in the case of inert expressions.
 Using examples from the previous section:
 > a := 'a':
 > subs({a = 1, t = 0}, integral);
 ${\mathrm{Int}}{}\left({f}{}\left({0}{,}{1}\right){,}{0}{=}{1}{..}{x}\right)$ (26)
 > eval(integral, {t=0, a=1});
 ${{\int }}_{{1}}^{{x}}{f}{}\left({t}{,}{1}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{t}$ (27)
 > subs(x=0, der);
 ${\mathrm{diff}}{}\left({f}{}\left({0}\right){,}{0}\right){+}{f}{}\left({0}\right)$ (28)
 > eval(der, x=0);
 $\genfrac{}{}{0}{}{\left(\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)\right)}{\phantom{\left\{{x}{=}{0}\right\}}}{|}\genfrac{}{}{0}{}{\phantom{\left(\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{f}{}\left({x}\right)\right)}}{\left\{{x}{=}{0}\right\}}{+}{f}{}\left({0}\right)$ (29)

Compatibility

 • The eval command was updated in Maple 2016; see Advanced Math.
 • The recurse option was introduced in Maple 18.