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 -----------------------------------------------------------------------------
15 @fnindex Quote operator
18 The single quote operator @code{'} prevents evaluation.
20 Applied to a symbol, the single quote prevents evaluation of the symbol.
22 Applied to a function call, the single quote prevents evaluation of the function
23 call, although the arguments of the function are still evaluated (if evaluation
24 is not otherwise prevented). The result is the noun form of the function call.
26 Applied to a parenthesized expression, the single quote prevents evaluation of
27 all symbols and function calls in the expression.
28 @c DUNNO IF THESE EXAMPLES ARE STILL NEEDED -- COVERED BY ITEMS UNDER "Examples"
29 E.g., @code{'(f(x))} means do not evaluate the expression @code{f(x)}.
30 @code{'f(x)} (with the single quote applied to @code{f} instead of @code{f(x)})
31 means return the noun form of @code{f} applied to @code{[x]}.
33 The single quote does not prevent simplification.
35 When the global flag @mref{noundisp} is @code{true}, nouns display with a single
36 quote. This switch is always @code{true} when displaying function definitions.
38 See also the quote-quote operator @mxref{quote-quote, ''} and @mrefdot{nouns}
43 the single quote prevents evaluation of the symbol.
63 Applied to a function call, the single quote prevents evaluation of the function
64 call. The result is the noun form of the function call.
69 @c integrate (x^2, x, x0, x1);
70 @c 'integrate (x^2, x, x0, x1);
78 (%i3) integrate (x^2, x, x0, x1);
82 (%i4) 'integrate (x^2, x, x0, x1);
98 Applied to a parenthesized expression, the single quote prevents evaluation of
99 all symbols and function calls in the expression.
115 (%i4) '(sqrt(aa) + bb);
121 The single quote does not prevent simplification.
124 @c sin (17 * %pi) + cos (17 * %pi);
125 @c '(sin (17 * %pi) + cos (17 * %pi));
128 (%i1) sin (17 * %pi) + cos (17 * %pi);
130 (%i2) '(sin (17 * %pi) + cos (17 * %pi));
134 Maxima considers floating point operations by its in-built mathematical
135 functions to be a simplification.
143 (%o1) .8414709848078965
145 (%o2) .8414709848078965
148 When the global flag @mref{noundisp} is @code{true}, nouns display with a single
160 @c noundisp : not noundisp;
170 (%o2) 3.141592653589793b0
183 (%i9) noundisp : not noundisp;
196 @opencatbox{Categories:}
197 @category{Evaluation}
202 @c -----------------------------------------------------------------------------
204 @fnindex Quote-quote operator
207 The quote-quote operator @code{'@w{}'} (two single quote marks) modifies
208 evaluation in input expressions.
210 Applied to a general expression @var{expr}, quote-quote causes the value of
211 @var{expr} to be substituted for @var{expr} in the input expression.
213 Applied to the operator of an expression, quote-quote changes the operator from
214 a noun to a verb (if it is not already a verb).
216 The quote-quote operator is applied by the input parser; it is not stored as
217 part of a parsed input expression. The quote-quote operator is always applied
218 as soon as it is parsed, and cannot be quoted. Thus quote-quote causes
219 evaluation when evaluation is otherwise suppressed, such as in function
220 definitions, lambda expressions, and expressions quoted by single quote
223 Quote-quote is recognized by @mref{batch} and @mrefdot{load}
225 See also @mrefcomma{ev} the single-quote operator @mxref{quote, '} and @mrefdot{nouns}
229 Applied to a general expression @var{expr}, quote-quote causes the value of
230 @var{expr} to be substituted for @var{expr} in the input expression.
233 @c expand ((a + b)^3);
236 @c [aa : cc, bb : dd, cc : 17, dd : 29];
237 @c foo_1 (x) := aa - bb * x;
241 @c foo_2 (x) := ''aa - ''bb * x;
243 @c [x0 : x1, x1 : x2, x2 : x3];
249 (%i1) expand ((a + b)^3);
251 (%o1) b + 3 a b + 3 a b + a
254 (%o2) [expand((b + a) ), b + 3 a b + 3 a b + a ]
257 (%o3) [expand((b + a) ), b + 3 a b + 3 a b + a ]
258 (%i4) [aa : cc, bb : dd, cc : 17, dd : 29];
259 (%o4) [cc, dd, 17, 29]
260 (%i5) foo_1 (x) := aa - bb * x;
261 (%o5) foo_1(x) := aa - bb x
266 (%i8) ''(foo_1 (10));
268 (%i9) foo_2 (x) := ''aa - ''bb * x;
269 (%o9) foo_2(x) := cc - dd x
272 (%i11) [x0 : x1, x1 : x2, x2 : x3];
282 Applied to the operator of an expression, quote-quote changes the operator from
283 a noun to a verb (if it is not already a verb).
286 @c declare (foo, noun);
287 @c foo (x) := x - 1729;
292 (%i1) declare (foo, noun);
294 (%i2) foo (x) := x - 1729;
295 (%o2) ''foo(x) := x - 1729
302 The quote-quote operator is applied by the input parser; it is not stored as
303 part of a parsed input expression.
306 @c [aa : bb, cc : dd, bb : 1234, dd : 5678];
308 @c display (_, op (_), args (_));
310 @c display (_, op (_), args (_));
313 (%i1) [aa : bb, cc : dd, bb : 1234, dd : 5678];
314 (%o1) [bb, dd, 1234, 5678]
317 (%i3) display (_, op (_), args (_));
322 args(cc + aa) = [cc, aa]
327 (%i5) display (_, op (_), args (_));
332 args(dd + bb) = [dd, bb]
337 Quote-quote causes evaluation when evaluation is otherwise suppressed, such as
338 in function definitions, lambda expressions, and expressions quoted by single
342 @c foo_1a (x) := ''(integrate (log (x), x));
343 @c foo_1b (x) := integrate (log (x), x);
344 @c dispfun (foo_1a, foo_1b);
345 @c integrate (log (x), x);
346 @c foo_2a (x) := ''%;
348 @c dispfun (foo_2a, foo_2b);
349 @c F : lambda ([u], diff (sin (u), u));
350 @c G : lambda ([u], ''(diff (sin (u), u)));
351 @c '(sum (a[k], k, 1, 3) + sum (b[k], k, 1, 3));
352 @c '(''(sum (a[k], k, 1, 3)) + ''(sum (b[k], k, 1, 3)));
355 (%i1) foo_1a (x) := ''(integrate (log (x), x));
356 (%o1) foo_1a(x) := x log(x) - x
357 (%i2) foo_1b (x) := integrate (log (x), x);
358 (%o2) foo_1b(x) := integrate(log(x), x)
359 (%i3) dispfun (foo_1a, foo_1b);
360 (%t3) foo_1a(x) := x log(x) - x
362 (%t4) foo_1b(x) := integrate(log(x), x)
365 (%i5) integrate (log (x), x);
367 (%i6) foo_2a (x) := ''%;
368 (%o6) foo_2a(x) := x log(x) - x
369 (%i7) foo_2b (x) := %;
371 (%i8) dispfun (foo_2a, foo_2b);
372 (%t8) foo_2a(x) := x log(x) - x
377 (%i10) F : lambda ([u], diff (sin (u), u));
378 (%o10) lambda([u], diff(sin(u), u))
379 (%i11) G : lambda ([u], ''(diff (sin (u), u)));
380 (%o11) lambda([u], cos(u))
381 (%i12) '(sum (a[k], k, 1, 3) + sum (b[k], k, 1, 3));
382 (%o12) sum(b , k, 1, 3) + sum(a , k, 1, 3)
384 (%i13) '(''(sum (a[k], k, 1, 3)) + ''(sum (b[k], k, 1, 3)));
385 (%o13) b + a + b + a + b + a
389 @opencatbox{Categories:}
390 @category{Evaluation}
395 @c NEEDS CLARIFICATION
398 @c -----------------------------------------------------------------------------
400 @deffn {Function} ev (@var{expr}, @var{arg_1}, @dots{}, @var{arg_n})
402 Evaluates the expression @var{expr} in the environment specified by the
403 arguments @var{arg_1}, @dots{}, @var{arg_n}. The arguments are switches
404 (Boolean flags), assignments, equations, and functions. @code{ev} returns the
405 result (another expression) of the evaluation.
407 The evaluation is carried out in steps, as follows.
411 First the environment is set up by scanning the arguments which may
412 be any or all of the following.
416 @mref{simp} causes @var{expr} to be simplified regardless of the setting of the
417 switch @code{simp} which inhibits simplification if @code{false}.
419 @mref{noeval} suppresses the evaluation phase of @code{ev} (see step (4) below).
420 This is useful in conjunction with the other switches and in causing
421 @var{expr} to be resimplified without being reevaluated.
423 @mref{nouns} causes the evaluation of noun forms (typically unevaluated
424 functions such as @code{'integrate} or @code{'diff}) in @var{expr}.
426 @mref{expand} causes expansion.
428 @code{expand (@var{m}, @var{n})} causes expansion, setting the values of
429 @mref{maxposex} and @mref{maxnegex} to @var{m} and @var{n} respectively.
431 @mref{detout} causes any matrix inverses computed in @var{expr} to have their
432 determinant kept outside of the inverse rather than dividing through
435 @mref{diff} causes all differentiations indicated in @var{expr} to be performed.
437 @code{derivlist (@var{x}, @var{y}, @var{z}, ...)} causes only differentiations
438 with respect to the indicated variables. See also @mrefdot{derivlist}
440 @code{risch} causes integrals in @var{expr} to be evaluated using the Risch
441 algorithm. See @mrefdot{risch} The standard integration routine is invoked
442 when using the special symbol @mrefdot{nouns}
444 @mref{float} causes non-integral rational numbers to be converted to floating
447 @mref{numer} causes some mathematical functions (including exponentiation)
448 with numerical arguments to be evaluated in floating point. It causes
449 variables in @var{expr} which have been given numervals to be replaced by
450 their values. It also sets the @mref{float} switch on.
452 @mref{pred} causes predicates (expressions which evaluate to @code{true} or
453 @code{false}) to be evaluated.
455 @mref{eval} causes an extra post-evaluation of @var{expr} to occur.
456 (See step (5) below.)
457 @code{eval} may occur multiple times. For each instance of @code{eval}, the
458 expression is evaluated again.
460 @code{A} where @code{A} is an atom declared to be an evaluation flag
461 @mref{evflag} causes @code{A} to be bound to @code{true} during the evaluation
464 @code{V: expression} (or alternately @code{V=expression}) causes @code{V} to be
465 bound to the value of @code{expression} during the evaluation of @var{expr}.
466 Note that if @code{V} is a Maxima option, then @code{expression} is used for
467 its value during the evaluation of @var{expr}. If more than one argument to
468 @code{ev} is of this type then the binding is done in parallel. If @code{V} is
469 a non-atomic expression then a substitution rather than a binding is performed.
471 @code{F} where @code{F}, a function name, has been declared to be an evaluation
472 function @mref{evfun} causes @code{F} to be applied to @var{expr}.
474 Any other function names, e.g. @mrefcomma{sum} cause evaluation of occurrences
475 of those names in @var{expr} as though they were verbs.
477 In addition a function occurring in @var{expr} (say @code{F(x)}) may be defined
478 locally for the purpose of this evaluation of @var{expr} by giving
479 @code{F(x) := expression} as an argument to @code{ev}.
481 If an atom not mentioned above or a subscripted variable or subscripted
482 expression was given as an argument, it is evaluated and if the result is an
483 equation or assignment then the indicated binding or substitution is performed.
484 If the result is a list then the members of the list are treated as if they were
485 additional arguments given to @code{ev}. This permits a list of equations to be
486 given (e.g. @code{[X=1, Y=A**2]}) or a list of names of equations (e.g.,
487 @code{[%t1, %t2]} where @code{%t1} and @code{%t2} are equations) such as that
488 returned by @mrefdot{solve}
491 The arguments of @code{ev} may be given in any order with the exception of
492 substitution equations which are handled in sequence, left to right, and
493 evaluation functions which are composed, e.g., @code{ev (@var{expr}, ratsimp,
494 realpart)} is handled as @code{realpart (ratsimp (@var{expr}))}.
496 The @mrefcomma{simp} @mrefcomma{numer} and @mref{float} switches may also be set
497 locally in a block, or globally in Maxima so that they will remain in effect
500 If @var{expr} is a canonical rational expression (CRE), then the expression
501 returned by @code{ev} is also a CRE, provided the @code{numer} and @code{float}
502 switches are not both @code{true}.
505 During step (1), a list is made of the non-subscripted variables appearing on
506 the left side of equations in the arguments or in the value of some arguments
507 if the value is an equation. The variables (subscripted variables which do not
508 have associated @mref{memoizing functions} as well as non-subscripted variables) in the
509 expression @var{expr} are replaced by their global values, except for those
510 appearing in this list. Usually, @var{expr} is just a label or @code{%} (as in
511 @code{%i2} in the example below), so this step simply retrieves the expression
512 named by the label, so that @code{ev} may work on it.
515 If any substitutions are indicated by the arguments, they are carried out now.
518 The resulting expression is then re-evaluated (unless one of the arguments was
519 @mref{noeval}) and simplified according to the arguments. Note that any
520 function calls in @var{expr} will be carried out after the variables in it are
521 evaluated and that @code{ev(F(x))} thus may behave like @code{F(ev(x))}.
524 For each instance of @mref{eval} in the arguments, steps (3) and (4) are
528 See also @mxrefcomma{quote-quote, ''} @mref{at} and @mrefdot{subst}
533 @c sin(x) + cos(y) + (w+1)^2 + 'diff (sin(w), w);
534 @c ev (%, numer, expand, diff, x=2, y=1);
537 (%i1) sin(x) + cos(y) + (w+1)^2 + 'diff (sin(w), w);
539 (%o1) cos(y) + sin(x) + -- (sin(w)) + (w + 1)
541 (%i2) ev (%, numer, expand, diff, x=2, y=1);
543 (%o2) cos(w) + w + 2 w + 2.449599732693821
546 An alternate top level syntax has been provided for @code{ev}, whereby one
547 may just type in its arguments, without the @code{ev()}. That is, one may
551 @var{expr}, @var{arg_1}, ..., @var{arg_n}
554 This is not permitted as part of another expression, e.g., in functions,
557 Notice the parallel binding process in the following example.
560 (%i3) programmode: false;
562 (%i4) x+y, x: a+y, y: 2;
565 (%i6) -3*x + 2*y = -4$
566 (%i7) solve ([%o5, %o6]);
579 (%i9) x + 1/x > gamma (1/2);
581 (%o9) x + - > sqrt(%pi)
583 (%i10) %, numer, x=1/2;
584 (%o10) 2.5 > 1.772453850905516
589 @opencatbox{Categories:}
590 @category{Evaluation}
594 @c -----------------------------------------------------------------------------
596 @defvr {Special symbol} eval
598 As an argument in a call to @code{ev (@var{expr})}, @code{eval} causes an extra
599 evaluation of @var{expr}. See @mrefdot{ev}
604 @c [a:b,b:c,c:d,d:e];
611 (%i1) [a:b,b:c,c:d,d:e];
623 @opencatbox{Categories:}
624 @category{Evaluation flags}
628 @c -----------------------------------------------------------------------------
630 @defvr {Property} evflag
632 When a symbol @var{x} has the @code{evflag} property, the expressions
633 @code{ev(@var{expr}, @var{x})} and @code{@var{expr}, @var{x}} (at the
634 interactive prompt) are equivalent to @code{ev(@var{expr}, @var{x} = true)}.
635 That is, @var{x} is bound to @code{true} while @var{expr} is evaluated.
637 The expression @code{declare(@var{x}, evflag)} gives the @code{evflag} property
638 to the variable @var{x}.
640 The flags which have the @code{evflag} property by default are the following:
641 @c FOLLOWING LIST CONSTRUCTED FROM LIST UNDER (prog1 '(evflag properties) ...)
642 @c NEAR LINE 2649 OF mlisp.lisp AT PRESENT (2004/11).
645 algebraic cauchysum demoivre
646 dotscrules %emode %enumer
647 exponentialize exptisolate factorflag
648 float halfangles infeval
649 isolate_wrt_times keepfloat letrat
650 listarith logabs logarc
652 m1pbranch numer_pbranch programmode
653 radexpand ratalgdenom ratfac
654 ratmx ratsimpexpons simp
655 simpproduct simpsum sumexpand
664 @c sin (1/2), float=true;
669 @c sum (1/k^2, k, 1, inf);
670 @c sum (1/k^2, k, 1, inf), simpsum;
671 @c declare (aa, evflag);
672 @c if aa = true then YES else NO;
673 @c if aa = true then YES else NO, aa;
680 (%i2) sin (1/2), float;
681 (%o2) 0.479425538604203
682 (%i3) sin (1/2), float=true;
683 (%o3) 0.479425538604203
692 (%i8) sum (1/k^2, k, 1, inf);
700 (%i9) sum (1/k^2, k, 1, inf), simpsum;
705 (%i10) declare (aa, evflag);
707 (%i11) if aa = true then YES else NO;
709 (%i12) if aa = true then YES else NO, aa;
713 @opencatbox{Categories:}
714 @category{Evaluation flags}
715 @category{Simplification flags and variables}
719 @c -----------------------------------------------------------------------------
721 @defvr {Property} evfun
723 When a function @var{F} has the @code{evfun} property, the expressions
724 @code{ev(@var{expr}, @var{F})} and @code{@var{expr}, @var{F}} (at the
725 interactive prompt) are equivalent to @code{@var{F}(ev(@var{expr}))}.
727 If two or more @code{evfun} functions @var{F}, @var{G}, etc., are specified,
728 the functions are applied in the order that they are specified.
730 The expression @code{declare(@var{F}, evfun)} gives the @code{evfun} property
731 to the function @var{F}. The functions which have the @code{evfun} property by
732 default are the following:
733 @c FOLLOWING LIST CONSTRUCTED FROM LIST UNDER (prog1 '(evfun properties) ...)
734 @c NEAR LINE 2643 IN mlisp.lisp AT PRESENT (2004/11).
737 bfloat factor fullratsimp
738 logcontract polarform radcan
739 ratexpand ratsimp rectform
740 rootscontract trigexpand trigreduce
749 @c cos(4 * x) / sin(x)^4;
750 @c cos(4 * x) / sin(x)^4, trigexpand;
751 @c cos(4 * x) / sin(x)^4, trigexpand, ratexpand;
752 @c ratexpand (trigexpand (cos(4 * x) / sin(x)^4));
753 @c declare ([F, G], evfun);
754 @c (aa : bb, bb : cc, cc : dd);
766 (%i2) x^3 - 1, factor;
768 (%o2) (x - 1) (x + x + 1)
769 (%i3) factor (x^3 - 1);
771 (%o3) (x - 1) (x + x + 1)
772 (%i4) cos(4 * x) / sin(x)^4;
779 (%i5) cos(4 * x) / sin(x)^4, trigexpand;
781 sin (x) - 6 cos (x) sin (x) + cos (x)
782 (%o5) -------------------------------------
785 (%i6) cos(4 * x) / sin(x)^4, trigexpand, ratexpand;
788 (%o6) - --------- + ------- + 1
791 (%i7) ratexpand (trigexpand (cos(4 * x) / sin(x)^4));
794 (%o7) - --------- + ------- + 1
797 (%i8) declare ([F, G], evfun);
799 (%i9) (aa : bb, bb : cc, cc : dd);
811 (%i15) G (F (ev (aa)));
815 @opencatbox{Categories:}
816 @category{Evaluation flags}
822 @c -----------------------------------------------------------------------------
824 @defvr {Option variable} infeval
826 Enables "infinite evaluation" mode. @mref{ev} repeatedly evaluates an
827 expression until it stops changing. To prevent a variable, say @code{X}, from
828 being evaluated away in this mode, simply include @code{X='X} as an argument to
829 @code{ev}. Of course expressions such as @code{ev (X, X=X+1, infeval)} will
830 generate an infinite loop.
832 @opencatbox{Categories:}
833 @category{Evaluation flags}
837 @c NEEDS CLARIFICATION, EXAMPLES
838 @c NEED TO MENTION THIS IS AN evflag
840 @c -----------------------------------------------------------------------------
842 @defvr {Special symbol} noeval
844 @code{noeval} suppresses the evaluation phase of @mrefdot{ev} This is useful in
845 conjunction with other switches and in causing expressions
846 to be resimplified without being reevaluated.
848 @opencatbox{Categories:}
849 @category{Evaluation flags}
853 @c NEEDS CLARIFICATION, EXAMPLES
855 @c -----------------------------------------------------------------------------
857 @defvr {Special symbol} nouns
859 @code{nouns} is an @mrefdot{evflag} When used as an option to the @mref{ev}@w{}
860 command, @code{nouns} converts all "noun" forms occurring in the expression
861 being @code{ev}'d to "verbs", i.e., evaluates them. See also
862 @mrefcomma{noun} @mrefcomma{nounify} @code{verb}, and @mrefdot{verbify}
864 @opencatbox{Categories:}
865 @category{Evaluation flags}
866 @category{Nouns and verbs}
870 @c -----------------------------------------------------------------------------
872 @defvr {Special symbol} pred
874 As an argument in a call to @code{ev (@var{expr})}, @code{pred} causes
875 predicates (expressions which evaluate to @code{true} or @code{false}) to be
876 evaluated. See @mrefdot{ev}
891 @opencatbox{Categories:}
892 @category{Evaluation flags}