2 * Functions and Variables for Evaluation::
5 @c -----------------------------------------------------------------------------
6 @node Functions and Variables for Evaluation, , Evaluation, Evaluation
7 @section Functions and Variables for Evaluation
8 @c -----------------------------------------------------------------------------
11 @c THIS ITEM IS VERY IMPORTANT !!
13 @c -----------------------------------------------------------------------------
17 @fnindex Quote operator
20 The single quote operator @code{'} prevents evaluation.
22 Applied to a symbol, the single quote prevents evaluation of the symbol.
24 Applied to a function call, the single quote prevents evaluation of the function
25 call, although the arguments of the function are still evaluated (if evaluation
26 is not otherwise prevented). The result is the noun form of the function call.
28 Applied to a parenthesized expression, the single quote prevents evaluation of
29 all symbols and function calls in the expression.
30 @c DUNNO IF THESE EXAMPLES ARE STILL NEEDED -- COVERED BY ITEMS UNDER "Examples"
31 E.g., @code{'(f(x))} means do not evaluate the expression @code{f(x)}.
32 @code{'f(x)} (with the single quote applied to @code{f} instead of @code{f(x)})
33 means return the noun form of @code{f} applied to @code{[x]}.
35 The single quote does not prevent simplification.
37 When the global flag @mref{noundisp} is @code{true}, nouns display with a single
38 quote. This switch is always @code{true} when displaying function definitions.
40 See also the quote-quote operator @mxref{quote-quote, ''} and @mrefdot{nouns}
45 the single quote prevents evaluation of the symbol.
65 Applied to a function call, the single quote prevents evaluation of the function
66 call. The result is the noun form of the function call.
71 @c integrate (x^2, x, x0, x1);
72 @c 'integrate (x^2, x, x0, x1);
80 (%i3) integrate (x^2, x, x0, x1);
84 (%i4) 'integrate (x^2, x, x0, x1);
100 Applied to a parenthesized expression, the single quote prevents evaluation of
101 all symbols and function calls in the expression.
117 (%i4) '(sqrt(aa) + bb);
123 The single quote does not prevent simplification.
126 @c sin (17 * %pi) + cos (17 * %pi);
127 @c '(sin (17 * %pi) + cos (17 * %pi));
130 (%i1) sin (17 * %pi) + cos (17 * %pi);
132 (%i2) '(sin (17 * %pi) + cos (17 * %pi));
136 Maxima considers floating point operations by its in-built mathematical
137 functions to be a simplification.
145 (%o1) .8414709848078965
147 (%o2) .8414709848078965
150 When the global flag @mref{noundisp} is @code{true}, nouns display with a single
162 @c noundisp : not noundisp;
172 (%o2) 3.141592653589793b0
185 (%i9) noundisp : not noundisp;
198 @opencatbox{Categories:}
199 @category{Evaluation}
204 @c -----------------------------------------------------------------------------
208 @fnindex Quote-quote operator
211 The quote-quote operator @code{'@w{}'} (two single quote marks) modifies
212 evaluation in input expressions.
214 Applied to a general expression @var{expr}, quote-quote causes the value of
215 @var{expr} to be substituted for @var{expr} in the input expression.
217 Applied to the operator of an expression, quote-quote changes the operator from
218 a noun to a verb (if it is not already a verb).
220 The quote-quote operator is applied by the input parser; it is not stored as
221 part of a parsed input expression. The quote-quote operator is always applied
222 as soon as it is parsed, and cannot be quoted. Thus quote-quote causes
223 evaluation when evaluation is otherwise suppressed, such as in function
224 definitions, lambda expressions, and expressions quoted by single quote
227 Quote-quote is recognized by @mref{batch} and @mrefdot{load}
229 See also @mrefcomma{ev} the single-quote operator @mxref{quote, '} and @mrefdot{nouns}
233 Applied to a general expression @var{expr}, quote-quote causes the value of
234 @var{expr} to be substituted for @var{expr} in the input expression.
237 @c expand ((a + b)^3);
240 @c [aa : cc, bb : dd, cc : 17, dd : 29];
241 @c foo_1 (x) := aa - bb * x;
245 @c foo_2 (x) := ''aa - ''bb * x;
247 @c [x0 : x1, x1 : x2, x2 : x3];
253 (%i1) expand ((a + b)^3);
255 (%o1) b + 3 a b + 3 a b + a
258 (%o2) [expand((b + a) ), b + 3 a b + 3 a b + a ]
261 (%o3) [expand((b + a) ), b + 3 a b + 3 a b + a ]
262 (%i4) [aa : cc, bb : dd, cc : 17, dd : 29];
263 (%o4) [cc, dd, 17, 29]
264 (%i5) foo_1 (x) := aa - bb * x;
265 (%o5) foo_1(x) := aa - bb x
270 (%i8) ''(foo_1 (10));
272 (%i9) foo_2 (x) := ''aa - ''bb * x;
273 (%o9) foo_2(x) := cc - dd x
276 (%i11) [x0 : x1, x1 : x2, x2 : x3];
286 Applied to the operator of an expression, quote-quote changes the operator from
287 a noun to a verb (if it is not already a verb).
290 @c declare (foo, noun);
291 @c foo (x) := x - 1729;
296 (%i1) declare (foo, noun);
298 (%i2) foo (x) := x - 1729;
299 (%o2) ''foo(x) := x - 1729
306 The quote-quote operator is applied by the input parser; it is not stored as
307 part of a parsed input expression.
310 @c [aa : bb, cc : dd, bb : 1234, dd : 5678];
312 @c display (_, op (_), args (_));
314 @c display (_, op (_), args (_));
317 (%i1) [aa : bb, cc : dd, bb : 1234, dd : 5678];
318 (%o1) [bb, dd, 1234, 5678]
321 (%i3) display (_, op (_), args (_));
326 args(cc + aa) = [cc, aa]
331 (%i5) display (_, op (_), args (_));
336 args(dd + bb) = [dd, bb]
341 Quote-quote causes evaluation when evaluation is otherwise suppressed, such as
342 in function definitions, lambda expressions, and expressions quoted by single
346 @c foo_1a (x) := ''(integrate (log (x), x));
347 @c foo_1b (x) := integrate (log (x), x);
348 @c dispfun (foo_1a, foo_1b);
349 @c integrate (log (x), x);
350 @c foo_2a (x) := ''%;
352 @c dispfun (foo_2a, foo_2b);
353 @c F : lambda ([u], diff (sin (u), u));
354 @c G : lambda ([u], ''(diff (sin (u), u)));
355 @c '(sum (a[k], k, 1, 3) + sum (b[k], k, 1, 3));
356 @c '(''(sum (a[k], k, 1, 3)) + ''(sum (b[k], k, 1, 3)));
359 (%i1) foo_1a (x) := ''(integrate (log (x), x));
360 (%o1) foo_1a(x) := x log(x) - x
361 (%i2) foo_1b (x) := integrate (log (x), x);
362 (%o2) foo_1b(x) := integrate(log(x), x)
363 (%i3) dispfun (foo_1a, foo_1b);
364 (%t3) foo_1a(x) := x log(x) - x
366 (%t4) foo_1b(x) := integrate(log(x), x)
369 (%i5) integrate (log (x), x);
371 (%i6) foo_2a (x) := ''%;
372 (%o6) foo_2a(x) := x log(x) - x
373 (%i7) foo_2b (x) := %;
375 (%i8) dispfun (foo_2a, foo_2b);
376 (%t8) foo_2a(x) := x log(x) - x
381 (%i10) F : lambda ([u], diff (sin (u), u));
382 (%o10) lambda([u], diff(sin(u), u))
383 (%i11) G : lambda ([u], ''(diff (sin (u), u)));
384 (%o11) lambda([u], cos(u))
385 (%i12) '(sum (a[k], k, 1, 3) + sum (b[k], k, 1, 3));
386 (%o12) sum(b , k, 1, 3) + sum(a , k, 1, 3)
388 (%i13) '(''(sum (a[k], k, 1, 3)) + ''(sum (b[k], k, 1, 3)));
389 (%o13) b + a + b + a + b + a
393 @opencatbox{Categories:}
394 @category{Evaluation}
399 @c NEEDS CLARIFICATION
402 @c -----------------------------------------------------------------------------
404 @deffn {Function} ev (@var{expr}, @var{arg_1}, @dots{}, @var{arg_n})
406 Evaluates the expression @var{expr} in the environment specified by the
407 arguments @var{arg_1}, @dots{}, @var{arg_n}. The arguments are switches
408 (Boolean flags), assignments, equations, and functions. @code{ev} returns the
409 result (another expression) of the evaluation.
411 The evaluation is carried out in steps, as follows.
415 First the environment is set up by scanning the arguments which may
416 be any or all of the following.
420 @mref{simp} causes @var{expr} to be simplified regardless of the setting of the
421 switch @code{simp} which inhibits simplification if @code{false}.
423 @mref{noeval} suppresses the evaluation phase of @code{ev} (see step (4) below).
424 This is useful in conjunction with the other switches and in causing
425 @var{expr} to be resimplified without being reevaluated.
427 @mref{nouns} causes the evaluation of noun forms (typically unevaluated
428 functions such as @code{'integrate} or @code{'diff}) in @var{expr}.
430 @mref{expand} causes expansion.
432 @code{expand (@var{m}, @var{n})} causes expansion, setting the values of
433 @mref{maxposex} and @mref{maxnegex} to @var{m} and @var{n} respectively.
435 @mref{detout} causes any matrix inverses computed in @var{expr} to have their
436 determinant kept outside of the inverse rather than dividing through
439 @mref{diff} causes all differentiations indicated in @var{expr} to be performed.
441 @code{derivlist (@var{x}, @var{y}, @var{z}, ...)} causes only differentiations
442 with respect to the indicated variables. See also @mrefdot{derivlist}
444 @code{risch} causes integrals in @var{expr} to be evaluated using the Risch
445 algorithm. See @mrefdot{risch} The standard integration routine is invoked
446 when using the special symbol @mrefdot{nouns}
448 @mref{float} causes non-integral rational numbers to be converted to floating
451 @mref{numer} causes some mathematical functions (including exponentiation)
452 with numerical arguments to be evaluated in floating point. It causes
453 variables in @var{expr} which have been given numervals to be replaced by
454 their values. It also sets the @mref{float} switch on.
456 @mref{pred} causes predicates (expressions which evaluate to @code{true} or
457 @code{false}) to be evaluated.
459 @mref{eval} causes an extra post-evaluation of @var{expr} to occur.
460 (See step (5) below.)
461 @code{eval} may occur multiple times. For each instance of @code{eval}, the
462 expression is evaluated again.
464 @code{A} where @code{A} is an atom declared to be an evaluation flag
465 @mref{evflag} causes @code{A} to be bound to @code{true} during the evaluation
468 @code{V: expression} (or alternately @code{V=expression}) causes @code{V} to be
469 bound to the value of @code{expression} during the evaluation of @var{expr}.
470 Note that if @code{V} is a Maxima option, then @code{expression} is used for
471 its value during the evaluation of @var{expr}. If more than one argument to
472 @code{ev} is of this type then the binding is done in parallel. If @code{V} is
473 a non-atomic expression then a substitution rather than a binding is performed.
475 @code{F} where @code{F}, a function name, has been declared to be an evaluation
476 function @mref{evfun} causes @code{F} to be applied to @var{expr}.
478 Any other function names, e.g. @mrefcomma{sum} cause evaluation of occurrences
479 of those names in @var{expr} as though they were verbs.
481 In addition a function occurring in @var{expr} (say @code{F(x)}) may be defined
482 locally for the purpose of this evaluation of @var{expr} by giving
483 @code{F(x) := expression} as an argument to @code{ev}.
485 If an atom not mentioned above or a subscripted variable or subscripted
486 expression was given as an argument, it is evaluated and if the result is an
487 equation or assignment then the indicated binding or substitution is performed.
488 If the result is a list then the members of the list are treated as if they were
489 additional arguments given to @code{ev}. This permits a list of equations to be
490 given (e.g. @code{[X=1, Y=A**2]}) or a list of names of equations (e.g.,
491 @code{[%t1, %t2]} where @code{%t1} and @code{%t2} are equations) such as that
492 returned by @mrefdot{solve}
495 The arguments of @code{ev} may be given in any order with the exception of
496 substitution equations which are handled in sequence, left to right, and
497 evaluation functions which are composed, e.g., @code{ev (@var{expr}, ratsimp,
498 realpart)} is handled as @code{realpart (ratsimp (@var{expr}))}.
500 The @mrefcomma{simp} @mrefcomma{numer} and @mref{float} switches may also be set
501 locally in a block, or globally in Maxima so that they will remain in effect
504 If @var{expr} is a canonical rational expression (CRE), then the expression
505 returned by @code{ev} is also a CRE, provided the @code{numer} and @code{float}
506 switches are not both @code{true}.
509 During step (1), a list is made of the non-subscripted variables appearing on
510 the left side of equations in the arguments or in the value of some arguments
511 if the value is an equation. The variables (subscripted variables which do not
512 have associated @mref{memoizing functions} as well as non-subscripted variables) in the
513 expression @var{expr} are replaced by their global values, except for those
514 appearing in this list. Usually, @var{expr} is just a label or @code{%} (as in
515 @code{%i2} in the example below), so this step simply retrieves the expression
516 named by the label, so that @code{ev} may work on it.
519 If any substitutions are indicated by the arguments, they are carried out now.
522 The resulting expression is then re-evaluated (unless one of the arguments was
523 @mref{noeval}) and simplified according to the arguments. Note that any
524 function calls in @var{expr} will be carried out after the variables in it are
525 evaluated and that @code{ev(F(x))} thus may behave like @code{F(ev(x))}.
528 For each instance of @mref{eval} in the arguments, steps (3) and (4) are
532 See also @mxrefcomma{quote-quote, ''} @mref{at} and @mrefdot{subst}
537 @c sin(x) + cos(y) + (w+1)^2 + 'diff (sin(w), w);
538 @c ev (%, numer, expand, diff, x=2, y=1);
541 (%i1) sin(x) + cos(y) + (w+1)^2 + 'diff (sin(w), w);
543 (%o1) cos(y) + sin(x) + -- (sin(w)) + (w + 1)
545 (%i2) ev (%, numer, expand, diff, x=2, y=1);
547 (%o2) cos(w) + w + 2 w + 2.449599732693821
550 An alternate top level syntax has been provided for @code{ev}, whereby one
551 may just type in its arguments, without the @code{ev()}. That is, one may
555 @var{expr}, @var{arg_1}, ..., @var{arg_n}
558 This is not permitted as part of another expression, e.g., in functions,
561 Notice the parallel binding process in the following example.
564 (%i3) programmode: false;
566 (%i4) x+y, x: a+y, y: 2;
569 (%i6) -3*x + 2*y = -4$
570 (%i7) solve ([%o5, %o6]);
583 (%i9) x + 1/x > gamma (1/2);
585 (%o9) x + - > sqrt(%pi)
587 (%i10) %, numer, x=1/2;
588 (%o10) 2.5 > 1.772453850905516
593 @opencatbox{Categories:}
594 @category{Evaluation}
598 @c -----------------------------------------------------------------------------
600 @defvr {Special symbol} eval
602 As an argument in a call to @code{ev (@var{expr})}, @code{eval} causes an extra
603 evaluation of @var{expr}. See @mrefdot{ev}
608 @c [a:b,b:c,c:d,d:e];
615 (%i1) [a:b,b:c,c:d,d:e];
627 @opencatbox{Categories:}
628 @category{Evaluation flags}
632 @c -----------------------------------------------------------------------------
634 @defvr {Property} evflag
636 When a symbol @var{x} has the @code{evflag} property, the expressions
637 @code{ev(@var{expr}, @var{x})} and @code{@var{expr}, @var{x}} (at the
638 interactive prompt) are equivalent to @code{ev(@var{expr}, @var{x} = true)}.
639 That is, @var{x} is bound to @code{true} while @var{expr} is evaluated.
641 The expression @code{declare(@var{x}, evflag)} gives the @code{evflag} property
642 to the variable @var{x}.
644 The flags which have the @code{evflag} property by default are the following:
645 @c FOLLOWING LIST CONSTRUCTED FROM LIST UNDER (prog1 '(evflag properties) ...)
646 @c NEAR LINE 2649 OF mlisp.lisp AT PRESENT (2004/11).
649 algebraic cauchysum demoivre
650 dotscrules %emode %enumer
651 exponentialize exptisolate factorflag
652 float halfangles infeval
653 isolate_wrt_times keepfloat letrat
654 listarith logabs logarc
656 m1pbranch numer_pbranch programmode
657 radexpand ratalgdenom ratfac
658 ratmx ratsimpexpons simp
659 simpproduct simpsum sumexpand
668 @c sin (1/2), float=true;
673 @c sum (1/k^2, k, 1, inf);
674 @c sum (1/k^2, k, 1, inf), simpsum;
675 @c declare (aa, evflag);
676 @c if aa = true then YES else NO;
677 @c if aa = true then YES else NO, aa;
684 (%i2) sin (1/2), float;
685 (%o2) 0.479425538604203
686 (%i3) sin (1/2), float=true;
687 (%o3) 0.479425538604203
696 (%i8) sum (1/k^2, k, 1, inf);
704 (%i9) sum (1/k^2, k, 1, inf), simpsum;
709 (%i10) declare (aa, evflag);
711 (%i11) if aa = true then YES else NO;
713 (%i12) if aa = true then YES else NO, aa;
717 @opencatbox{Categories:}
718 @category{Evaluation flags}
719 @category{Simplification flags and variables}
723 @c -----------------------------------------------------------------------------
725 @defvr {Property} evfun
727 When a function @var{F} has the @code{evfun} property, the expressions
728 @code{ev(@var{expr}, @var{F})} and @code{@var{expr}, @var{F}} (at the
729 interactive prompt) are equivalent to @code{@var{F}(ev(@var{expr}))}.
731 If two or more @code{evfun} functions @var{F}, @var{G}, etc., are specified,
732 the functions are applied in the order that they are specified.
734 The expression @code{declare(@var{F}, evfun)} gives the @code{evfun} property
735 to the function @var{F}. The functions which have the @code{evfun} property by
736 default are the following:
737 @c FOLLOWING LIST CONSTRUCTED FROM LIST UNDER (prog1 '(evfun properties) ...)
738 @c NEAR LINE 2643 IN mlisp.lisp AT PRESENT (2004/11).
741 bfloat factor fullratsimp
742 logcontract polarform radcan
743 ratexpand ratsimp rectform
744 rootscontract trigexpand trigreduce
753 @c cos(4 * x) / sin(x)^4;
754 @c cos(4 * x) / sin(x)^4, trigexpand;
755 @c cos(4 * x) / sin(x)^4, trigexpand, ratexpand;
756 @c ratexpand (trigexpand (cos(4 * x) / sin(x)^4));
757 @c declare ([F, G], evfun);
758 @c (aa : bb, bb : cc, cc : dd);
770 (%i2) x^3 - 1, factor;
772 (%o2) (x - 1) (x + x + 1)
773 (%i3) factor (x^3 - 1);
775 (%o3) (x - 1) (x + x + 1)
776 (%i4) cos(4 * x) / sin(x)^4;
783 (%i5) cos(4 * x) / sin(x)^4, trigexpand;
785 sin (x) - 6 cos (x) sin (x) + cos (x)
786 (%o5) -------------------------------------
789 (%i6) cos(4 * x) / sin(x)^4, trigexpand, ratexpand;
792 (%o6) - --------- + ------- + 1
795 (%i7) ratexpand (trigexpand (cos(4 * x) / sin(x)^4));
798 (%o7) - --------- + ------- + 1
801 (%i8) declare ([F, G], evfun);
803 (%i9) (aa : bb, bb : cc, cc : dd);
815 (%i15) G (F (ev (aa)));
819 @opencatbox{Categories:}
820 @category{Evaluation flags}
826 @c -----------------------------------------------------------------------------
828 @defvr {Option variable} infeval
830 Enables "infinite evaluation" mode. @mref{ev} repeatedly evaluates an
831 expression until it stops changing. To prevent a variable, say @code{X}, from
832 being evaluated away in this mode, simply include @code{X='X} as an argument to
833 @code{ev}. Of course expressions such as @code{ev (X, X=X+1, infeval)} will
834 generate an infinite loop.
836 @opencatbox{Categories:}
837 @category{Evaluation flags}
841 @c NEEDS CLARIFICATION, EXAMPLES
842 @c NEED TO MENTION THIS IS AN evflag
844 @c -----------------------------------------------------------------------------
846 @defvr {Special symbol} noeval
848 @code{noeval} suppresses the evaluation phase of @mrefdot{ev} This is useful in
849 conjunction with other switches and in causing expressions
850 to be resimplified without being reevaluated.
852 @opencatbox{Categories:}
853 @category{Evaluation flags}
857 @c NEEDS CLARIFICATION, EXAMPLES
859 @c -----------------------------------------------------------------------------
861 @defvr {Special symbol} nouns
863 @code{nouns} is an @mrefdot{evflag} When used as an option to the @mref{ev}@w{}
864 command, @code{nouns} converts all "noun" forms occurring in the expression
865 being @code{ev}'d to "verbs", i.e., evaluates them. See also
866 @mrefcomma{noun} @mrefcomma{nounify} @code{verb}, and @mrefdot{verbify}
868 @opencatbox{Categories:}
869 @category{Evaluation flags}
870 @category{Nouns and verbs}
874 @c -----------------------------------------------------------------------------
876 @defvr {Special symbol} pred
878 As an argument in a call to @code{ev (@var{expr})}, @code{pred} causes
879 predicates (expressions which evaluate to @code{true} or @code{false}) to be
880 evaluated. See @mrefdot{ev}
895 @opencatbox{Categories:}
896 @category{Evaluation flags}