2 * Introduction to Function Definition::
5 * Functions and Variables for Function Definition::
8 @c -----------------------------------------------------------------------------
9 @node Introduction to Function Definition, Function, Function Definition, Function Definition
10 @section Introduction to Function Definition
11 @c -----------------------------------------------------------------------------
13 @c -----------------------------------------------------------------------------
14 @node Function, Macros, Introduction to Function Definition, Function Definition
15 @c NEEDS WORK, THIS TOPIC IS IMPORTANT
16 @c MENTION DYNAMIC SCOPE (VS LEXICAL SCOPE)
18 @c -----------------------------------------------------------------------------
20 @opencatbox{Categories:}
21 @category{Function definition}
22 @category{Programming}
25 @c -----------------------------------------------------------------------------
26 @subsection Ordinary functions
27 @c -----------------------------------------------------------------------------
29 To define a function in Maxima you use the @code{:=} operator.
37 defines a function @code{f}.
38 Anonymous functions may also be created using @code{lambda}.
46 can be used instead of @code{f}
50 f(i,j) := block ([], ...);
51 map (lambda ([i], i+1), l)
55 would return a list with 1 added to each term.
57 You may also define a function with a variable number of arguments,
58 by having a final argument which is assigned to a list of the extra
64 @c f (a, b, [u]) := [a, b, u];
65 @c f (1, 2, 3, 4, 5, 6);
72 (%i3) f (a, b, [u]) := [a, b, u];
73 (%o3) f(a, b, [u]) := [a, b, u]
74 (%i4) f (1, 2, 3, 4, 5, 6);
75 (%o4) [1, 2, [3, 4, 5, 6]]
78 The right hand side of a function is an expression. Thus
79 if you want a sequence of expressions, you do
82 f(x) := (expr1, expr2, ...., exprn);
85 and the value of @var{exprn} is what is returned by the function.
87 If you wish to make a @code{return} from some expression inside the
88 function then you must use @code{block} and @code{return}.
91 block ([], expr1, ..., if (a > 10) then return(a), ..., exprn)
94 is itself an expression, and so could take the place of the
95 right hand side of a function definition. Here it may happen
96 that the return happens earlier than the last expression.
98 @c COPY THIS STUFF TO @defun block AS NEEDED
99 @c ESPECIALLY STUFF ABOUT LOCAL VARIABLES
100 The first @code{[]} in the block, may contain a list of variables and
101 variable assignments, such as @code{[a: 3, b, c: []]}, which would cause the
102 three variables @code{a},@code{b},and @code{c} to not refer to their
103 global values, but rather have these special values for as long as the
104 code executes inside the @code{block}, or inside functions called from
105 inside the @code{block}. This is called @i{dynamic} binding, since the
106 variables last from the start of the block to the time it exits. Once
107 you return from the @code{block}, or throw out of it, the old values (if
108 any) of the variables will be restored. It is certainly a good idea
109 to protect your variables in this way. Note that the assignments
110 in the block variables, are done in parallel. This means, that if
111 you had used @code{c: a} in the above, the value of @code{c} would
112 have been the value of @code{a} at the time you just entered the block,
113 but before @code{a} was bound. Thus doing something like
116 block ([a: a], expr1, ... a: a+3, ..., exprn)
119 will protect the external value of @code{a} from being altered, but
120 would let you access what that value was. Thus the right hand
121 side of the assignments, is evaluated in the entering context, before
123 Using just @code{block ([x], ...)} would cause the @code{x} to have itself
124 as value, just as if it would have if you entered a fresh Maxima
127 The actual arguments to a function are treated in exactly same way as
128 the variables in a block. Thus in
131 f(x) := (expr1, ..., exprn);
140 we would have a similar context for evaluation of the expressions
144 block ([x: 1], expr1, ..., exprn)
147 Inside functions, when the right hand side of a definition,
148 may be computed at runtime, it is useful to use @code{define} and
149 possibly @code{buildq}.
151 @anchor{memoizing function}
152 @anchor{memoizing functions}
153 @anchor{Memoizing function}
154 @anchor{Memoizing functions}
155 @c -----------------------------------------------------------------------------
156 @subsection Memoizing Functions
157 @c -----------------------------------------------------------------------------
159 A @i{memoizing function} caches the result the first time it is called with a
160 given argument, and returns the stored value, without recomputing it, when that
161 same argument is given. Memoizing functions are often called
162 @i{array function} and are in fact handled like arrays in many ways:
164 The names of memoizing functions are appended to the global list @code{arrays}
165 (not the global list @code{functions}). @code{arrayinfo} returns the list of
166 arguments for which there are stored values, and @code{listarray} returns the
167 stored values. @code{dispfun} and @code{fundef} return the array function
170 @code{arraymake} constructs an array function call,
171 analogous to @code{funmake} for ordinary functions.
172 @code{arrayapply} applies an array function to its arguments,
173 analogous to @code{apply} for ordinary functions.
174 There is nothing exactly analogous to @code{map} for array functions,
175 although @code{map(lambda([@var{x}], @var{a}[@var{x}]), @var{L})} or
176 @code{makelist(@var{a}[@var{x}], @var{x}, @var{L})}, where @var{L} is a list,
177 are not too far off the mark.
179 @code{remarray} removes an array function definition (including any stored
180 function values), analogous to @code{remfunction} for ordinary functions.
182 @code{kill(@var{a}[@var{x}])} removes the value of the array function @var{a}
183 stored for the argument @var{x};
184 the next time @var{a} is called with argument @var{x},
185 the function value is recomputed.
186 However, there is no way to remove all of the stored values at once,
187 except for @code{kill(@var{a})} or @code{remarray(@var{a})},
188 which also remove the function definition.
193 If evaluating the function needs much time and only a limited number of points
194 is ever evaluated (which means not much time is spent looking up results in a
195 long list of cached results) Memoizing functions can speed up calculations
199 @c a[x]:=float(sum(sin(x*t),t,1,10000));
206 Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
209 (%i2) a[x]:=float(sum(sin(x*t),t,1,10000));
210 Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
211 (%o2) a := float(sum(sin(x t), t, 1, 10000))
216 Evaluation took 5.1250 seconds (5.1260 elapsed) using 775.250 MB.
217 (%o3) 1.633891021792447
221 Evaluation took 0.0000 seconds (0.0000 elapsed) using 0 bytes.
222 (%o4) 1.633891021792447
226 As the memoizing function is only evaluated once for each input value
227 changes in variables the memoizing function uses are not considered
228 for values that are already cached:
265 @c -----------------------------------------------------------------------------
266 @node Macros, Functions and Variables for Function Definition, Function, Function Definition
268 @c -----------------------------------------------------------------------------
270 @c -----------------------------------------------------------------------------
272 @deffn {Function} buildq (@var{L}, @var{expr})
274 Substitutes variables named by the list @var{L} into the expression @var{expr},
275 in parallel, without evaluating @var{expr}. The resulting expression is
276 simplified, but not evaluated, after @code{buildq} carries out the substitution.
278 The elements of @var{L} are symbols or assignment expressions
279 @code{@var{symbol}: @var{value}}, evaluated in parallel. That is, the binding
280 of a variable on the right-hand side of an assignment is the binding of that
281 variable in the context from which @code{buildq} was called, not the binding of
282 that variable in the variable list @var{L}. If some variable in @var{L} is not
283 given an explicit assignment, its binding in @code{buildq} is the same as in
284 the context from which @code{buildq} was called.
286 Then the variables named by @var{L} are substituted into @var{expr} in parallel.
287 That is, the substitution for every variable is determined before any
288 substitution is made, so the substitution for one variable has no effect on any
291 If any variable @var{x} appears as @code{splice (@var{x})} in @var{expr},
292 then @var{x} must be bound to a list,
293 and the list is spliced (interpolated) into @var{expr} instead of substituted.
295 Any variables in @var{expr} not appearing in @var{L} are carried into the result
296 verbatim, even if they have bindings in the context from which @code{buildq}
301 @code{a} is explicitly bound to @code{x}, while @code{b} has the same binding
302 (namely 29) as in the calling context, and @code{c} is carried through verbatim.
303 The resulting expression is not evaluated until the explicit evaluation
307 @c (a: 17, b: 29, c: 1729)$
308 @c buildq ([a: x, b], a + b + c);
312 (%i1) (a: 17, b: 29, c: 1729)$
314 (%i2) buildq ([a: x, b], a + b + c);
323 @code{e} is bound to a list, which appears as such in the arguments of
324 @code{foo}, and interpolated into the arguments of @code{bar}.
327 @c buildq ([e: [a, b, c]], foo (x, e, y));
328 @c buildq ([e: [a, b, c]], bar (x, splice (e), y));
332 (%i1) buildq ([e: [a, b, c]], foo (x, e, y));
333 (%o1) foo(x, [a, b, c], y)
336 (%i2) buildq ([e: [a, b, c]], bar (x, splice (e), y));
337 (%o2) bar(x, a, b, c, y)
341 The result is simplified after substitution. If simplification were applied
342 before substitution, these two results would be the same.
345 @c buildq ([e: [a, b, c]], splice (e) + splice (e));
346 @c buildq ([e: [a, b, c]], 2 * splice (e));
350 (%i1) buildq ([e: [a, b, c]], splice (e) + splice (e));
351 (%o1) 2 c + 2 b + 2 a
354 (%i2) buildq ([e: [a, b, c]], 2 * splice (e));
359 The variables in @var{L} are bound in parallel; if bound sequentially,
360 the first result would be @code{foo (b, b)}.
361 Substitutions are carried out in parallel;
362 compare the second result with the result of @code{subst},
363 which carries out substitutions sequentially.
366 @c buildq ([a: b, b: a], foo (a, b));
367 @c buildq ([u: v, v: w, w: x, x: y, y: z, z: u],
368 @c bar (u, v, w, x, y, z));
369 @c subst ([u=v, v=w, w=x, x=y, y=z, z=u],
370 @c bar (u, v, w, x, y, z));
374 (%i1) buildq ([a: b, b: a], foo (a, b));
378 (%i2) buildq ([u: v, v: w, w: x, x: y, y: z, z: u],
379 bar (u, v, w, x, y, z));
380 (%o2) bar(v, w, x, y, z, u)
383 (%i3) subst ([u=v, v=w, w=x, x=y, y=z, z=u],
384 bar (u, v, w, x, y, z));
385 (%o3) bar(u, u, u, u, u, u)
389 Construct a list of equations with some variables or expressions on the
390 left-hand side and their values on the right-hand side. @code{macroexpand}
391 shows the expression returned by @code{show_values}.
394 @c show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
395 @c (a: 17, b: 29, c: 1729)$
396 @c show_values (a, b, c - a - b);
397 @c macroexpand (show_values (a, b, c - a - b));
401 (%i1) show_values ([L]) ::= buildq ([L], map ("=", 'L, L));
402 (%o1) show_values([L]) ::= buildq([L], map("=", 'L, L))
404 (%i2) (a: 17, b: 29, c: 1729)$
406 (%i3) show_values (a, b, c - a - b);
407 (%o3) [a = 17, b = 29, c - b - a = 1683]
410 (%i4) macroexpand (show_values (a, b, c - a - b));
411 (%o4) map(=, '([a, b, c - b - a]), [a, b, c - b - a])
415 Given a function of several arguments,
416 create another function for which some of the arguments are fixed.
420 @c buildq ([f, a], lambda ([[x]], apply (f, append (a, x))))$
421 @c by3 : curry ("*", 3);
426 (%i1) curry (f, [a]) :=
427 buildq ([f, a], lambda ([[x]], apply (f, append (a, x))))$
430 (%i2) by3 : curry ("*", 3);
431 (%o2) lambda([[x]], apply(*, append([3], x)))
439 @opencatbox{Categories:}
440 @category{Function definition}
444 @c -----------------------------------------------------------------------------
446 @deffn {Function} macroexpand (@var{expr})
448 Returns the macro expansion of @var{expr} without evaluating it,
449 when @code{expr} is a macro function call.
450 Otherwise, @code{macroexpand} returns @var{expr}.
452 If the expansion of @var{expr} yields another macro function call,
453 that macro function call is also expanded.
455 @code{macroexpand} quotes its argument.
456 However, if the expansion of a macro function call has side effects,
457 those side effects are executed.
459 See also @mrefcomma{::=} @mrefcomma{macros} and @mrefdot{macroexpand1}.
465 @c h (x) ::= buildq ([x], g (x - a));
467 @c macroexpand (h (y));
472 (%i1) g (x) ::= x / 99;
478 (%i2) h (x) ::= buildq ([x], g (x - a));
479 (%o2) h(x) ::= buildq([x], g(x - a))
486 (%i4) macroexpand (h (y));
499 @opencatbox{Categories:}
500 @category{Function application}
504 @c -----------------------------------------------------------------------------
505 @anchor{macroexpand1}
506 @deffn {Function} macroexpand1 (@var{expr})
508 Returns the macro expansion of @var{expr} without evaluating it,
509 when @code{expr} is a macro function call.
510 Otherwise, @code{macroexpand1} returns @var{expr}.
512 @code{macroexpand1} quotes its argument.
513 However, if the expansion of a macro function call has side effects,
514 those side effects are executed.
516 If the expansion of @var{expr} yields another macro function call,
517 that macro function call is not expanded.
519 See also @mrefcomma{::=} @mrefcomma{macros} and @mrefdot{macroexpand}
525 @c h (x) ::= buildq ([x], g (x - a));
527 @c macroexpand1 (h (y));
532 (%i1) g (x) ::= x / 99;
538 (%i2) h (x) ::= buildq ([x], g (x - a));
539 (%o2) h(x) ::= buildq([x], g(x - a))
546 (%i4) macroexpand1 (h (y));
557 @opencatbox{Categories:}
558 @category{Function application}
562 @c -----------------------------------------------------------------------------
564 @defvr {Global variable} macros
565 Default value: @code{[]}
567 @code{macros} is the list of user-defined macro functions.
568 The macro function definition operator @code{::=} puts a new macro function
569 onto this list, and @code{kill}, @code{remove}, and @code{remfunction} remove
570 macro functions from the list.
572 See also @mrefdot{infolists}
574 @opencatbox{Categories:}
575 @category{Function definition}
576 @category{Global variables}
580 @c -----------------------------------------------------------------------------
582 @deffn {Function} splice (@var{a})
584 Splices (interpolates) the list named by the atom @var{a} into an expression,
585 but only if @code{splice} appears within @code{buildq};
586 otherwise, @code{splice} is treated as an undefined function.
587 If appearing within @code{buildq} as @var{a} alone (without @code{splice}),
588 @var{a} is substituted (not interpolated) as a list into the result.
589 The argument of @code{splice} can only be an atom;
590 it cannot be a literal list or an expression which yields a list.
592 Typically @code{splice} supplies the arguments for a function or operator.
593 For a function @code{f}, the expression @code{f (splice (@var{a}))} within
594 @code{buildq} expands to @code{f (@var{a}[1], @var{a}[2], @var{a}[3], ...)}.
595 For an operator @code{o}, the expression @code{"o" (splice (@var{a}))} within
596 @code{buildq} expands to @code{"o" (@var{a}[1], @var{a}[2], @var{a}[3], ...)},
597 where @code{o} may be any type of operator (typically one which takes multiple
598 arguments). Note that the operator must be enclosed in double quotes @code{"}.
603 @c buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
604 @c buildq ([x: [1, %pi]], "/" (splice (x)));
605 @c matchfix ("<>", "<>");
606 @c buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
610 (%i1) buildq ([x: [1, %pi, z - y]], foo (splice (x)) / length (x));
612 (%o1) -----------------------
613 length([1, %pi, z - y])
616 (%i2) buildq ([x: [1, %pi]], "/" (splice (x)));
622 (%i3) matchfix ("<>", "<>");
626 (%i4) buildq ([x: [1, %pi, z - y]], "<>" (splice (x)));
627 (%o4) <>1, %pi, z - y<>
631 @opencatbox{Categories:}
632 @category{Function definition}
636 @c end concepts Function Definition
638 @c -----------------------------------------------------------------------------
639 @node Functions and Variables for Function Definition, , Macros, Function Definition
640 @section Functions and Variables for Function Definition
641 @c -----------------------------------------------------------------------------
643 @c -----------------------------------------------------------------------------
645 @deffn {Function} apply (@var{F}, [@var{arg_1}, @dots{}, @var{arg_n}])
647 Constructs and evaluates an expression @code{@var{F}(@var{arg_1}, ..., @var{arg_n})}.
648 The function arguments @code{[@var{arg_1}, @dots{}, @var{arg_n}]} may
649 be of any length and comprise any expressions.
650 @code{apply} evaluates all of its arguments, @var{F} and @var{arg_1}, @dots{}, @var{arg_n} alike,
651 unless evaluation is prevented by quotation.
653 @code{apply} does not attempt to distinguish a @mref{memoizing function} from an ordinary
654 function; when @var{F} is the name of a memoizing function, @code{apply} evaluates
655 @code{@var{F}(...)} (that is, a function call with parentheses instead of square
656 brackets). @code{arrayapply} evaluates a function call with square brackets in
659 See also @mref{funmake} and @mrefdot{args}
663 The function arguments @code{[@var{arg_1}, @dots{}, @var{arg_n}]} may be of any length.
664 Here @code{min} and @code{"+"} are applied to a list @code{L}.
667 @c L : [1, 5, -10.2, 4, 3];
673 (%i1) L : [1, 5, -10.2, 4, 3];
674 (%o1) [1, 5, - 10.2, 4, 3]
677 (%i2) apply (min, L);
681 (%i3) apply ("+", L);
686 @code{apply} evaluates all of its arguments, unless evaluation is prevented by quotation.
687 First example: @code{dispfun} ordinarily does not evaluate its argument,
688 but we can ensure the evaluation of the argument via @code{apply}.
691 @c F (x) := x / 1729;
695 @c apply (dispfun, [fname]);
699 (%i1) F (x) := x / 1729;
717 (%i4) dispfun (fname);
718 fundef: no such function: fname
719 -- an error. To debug this try: debugmode(true);
722 (%i5) apply (dispfun, [fname]);
731 @code{apply} evaluates all of its arguments, unless evaluation is prevented by quotation.
732 Second example: create a function that declares all of its arguments to be complex.
735 @c g([u]):=apply('declare,[u,complex])$
741 (%i1) g([u]) := apply('declare,[u,complex])$
744 (%o3) [kind(a, complex), kind(b, complex), kind(c, complex)]
748 @code{apply} evaluates all of its arguments, unless evaluation is prevented by quotation.
749 Third example: @code{apply} ordinarily evaluates its first argument,
750 but single quote @code{'} prevents evaluation.
751 Note that @code{demoivre} is the name of a global variable and also a function.
755 @c demoivre (exp (%i * x));
756 @c apply (demoivre, [exp (%i * x)]);
757 @c apply ('demoivre, [exp (%i * x)]);
765 (%i2) demoivre (exp (%i * x));
766 (%o2) %i sin(x) + cos(x)
769 (%i3) apply (demoivre, [exp (%i * x)]);
770 apply: found false where a function was expected.
771 -- an error. To debug this try: debugmode(true);
774 (%i4) apply ('demoivre, [exp (%i * x)]);
775 (%o4) %i sin(x) + cos(x)
779 The function arguments @code{[@var{arg_1}, @dots{}, @var{arg_n}]} may
780 be of any length and comprise any expressions.
781 Convert a nested list into a matrix by calling @code{apply}.
789 (%i1) a:[[1,2],[3,4]];
790 (%o1) [[1, 2], [3, 4]]
793 (%i2) apply(matrix,a);
800 @opencatbox{Categories:}
801 @category{Function application}
805 @c -----------------------------------------------------------------------------
807 @deffn {Function} block @
808 @fname{block} ([@var{v_1}, @dots{}, @var{v_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
809 @fname{block} (@var{expr_1}, @dots{}, @var{expr_n})
811 The function @code{block} allows to make the variables @var{v_1}, @dots{},
812 @var{v_m} to be local for a sequence of commands. If these variables
813 are already bound @code{block} saves the current values of the
814 variables @var{v_1}, @dots{}, @var{v_m} (if any) upon entry to the
815 block, then unbinds the variables so that they evaluate to themselves;
816 The local variables may be bound to arbitrary values within the block
817 but when the block is exited the saved values are restored, and the
818 values assigned within the block are lost.
820 If there is no need to define local variables then the list at the
821 beginning of the @code{block} command may be omitted.
822 In this case if neither @mref{return} nor @mref{go} are used
823 @code{block} behaves similar to the following construct:
826 ( expr_1, expr_2,... , expr_n );
829 @var{expr_1}, @dots{}, @var{expr_n} will be evaluated in sequence and
830 the value of the last expression will be returned. The sequence can be
831 modified by the @code{go}, @code{throw}, and @code{return} functions. The last
832 expression is @var{expr_n} unless @code{return} or an expression containing
833 @code{throw} is evaluated.
835 The declaration @code{local(@var{v_1}, ..., @var{v_m})} within @code{block}
836 saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_m},
837 removes any properties before evaluating other expressions, and restores any
838 saved properties on exit from the block. Some declarations are implemented as
839 properties of a symbol, including @code{:=}, @code{array}, @code{dependencies},
840 @code{atvalue}, @code{matchdeclare}, @code{atomgrad}, @code{constant},
841 @code{nonscalar}, @code{assume}, and some others. The effect of @code{local}
842 is to make such declarations effective only within the block; otherwise
843 declarations within a block are actually global declarations.
845 @code{block} may appear within another @code{block}.
846 Local variables are established each time a new @code{block} is evaluated.
847 Local variables appear to be global to any enclosed blocks.
848 If a variable is non-local in a block,
849 its value is the value most recently assigned by an enclosing block, if any,
850 otherwise, it is the value of the variable in the global environment.
851 This policy may coincide with the usual understanding of "dynamic scope".
853 The value of the block is the value of the last statement or the
854 value of the argument to the function @code{return} which may be used to exit
855 explicitly from the block. The function @code{go} may be used to transfer
856 control to the statement of the block that is tagged with the argument
857 to @code{go}. To tag a statement, precede it by an atomic argument as
858 another statement in the block. For example:
859 @code{block ([x], x:1, loop, x: x+1, ..., go(loop), ...)}. The argument to
860 @code{go} must be the name of a tag appearing within the block. One cannot use
861 @code{go} to transfer to a tag in a block other than the one containing the
864 Blocks typically appear on the right side of a function definition
865 but can be used in other places as well.
867 See also @mref{return} and @mrefdot{go}
869 @c Needs some examples.
871 @opencatbox{Categories:}
872 @category{Expressions}
873 @category{Programming}
877 @c REPHRASE, NEEDS EXAMPLE
879 @c -----------------------------------------------------------------------------
881 @deffn {Function} break (@var{expr_1}, @dots{}, @var{expr_n})
883 Evaluates and prints @var{expr_1}, @dots{}, @var{expr_n} and then
884 causes a Maxima break at which point the user can examine and change
885 his environment. Upon typing @code{exit;} the computation resumes.
887 @opencatbox{Categories:}
892 @c FOR SOME REASON throw IS IN SOME OTHER FILE. MOVE throw INTO THIS FILE.
893 @c NEEDS CLARIFICATION
895 @c -----------------------------------------------------------------------------
897 @deffn {Function} catch (@var{expr_1}, @dots{}, @var{expr_n})
899 Evaluates @var{expr_1}, @dots{}, @var{expr_n} one by one; if any
900 leads to the evaluation of an expression of the
901 form @code{throw (arg)}, then the value of the @code{catch} is the value of
902 @code{throw (arg)}, and no further expressions are evaluated.
903 This "non-local return" thus goes through any depth of
904 nesting to the nearest enclosing @code{catch}. If there is no @code{catch}
905 enclosing a @code{throw}, an error message is printed.
907 If the evaluation of the arguments does not lead to the evaluation of any
908 @code{throw} then the value of @code{catch} is the value of @var{expr_n}.
911 @c lambda ([x], if x < 0 then throw(x) else f(x))$
912 @c g(l) := catch (map (''%, l))$
914 @c g ([1, 2, -3, 7]);
917 (%i1) lambda ([x], if x < 0 then throw(x) else f(x))$
918 (%i2) g(l) := catch (map (''%, l))$
919 (%i3) g ([1, 2, 3, 7]);
920 (%o3) [f(1), f(2), f(3), f(7)]
921 (%i4) g ([1, 2, -3, 7]);
926 The function @code{g} returns a list of @code{f} of each element of @code{l} if
927 @code{l} consists only of non-negative numbers; otherwise, @code{g} "catches"
928 the first negative element of @code{l} and "throws" it up.
930 @opencatbox{Categories:}
931 @category{Programming}
935 @c -----------------------------------------------------------------------------
937 @deffn {Function} compfile @
938 @fname{compfile} (@var{filename}, @var{f_1}, @dots{}, @var{f_n}) @
939 @fname{compfile} (@var{filename}, functions) @
940 @fname{compfile} (@var{filename}, all)
942 Translates Maxima functions into Lisp and writes the translated code into the
945 @code{compfile(@var{filename}, @var{f_1}, ..., @var{f_n})} translates the
946 specified functions. @code{compfile (@var{filename}, functions)} and
947 @code{compfile (@var{filename}, all)} translate all user-defined functions.
949 The Lisp translations are not evaluated, nor is the output file processed by
951 @c SO LET'S CONSIDER GIVING THIS FUNCTION A MORE ACCURATE NAME.
952 @code{translate} creates and evaluates Lisp translations. @code{compile_file}
953 translates Maxima into Lisp, and then executes the Lisp compiler.
955 See also @mrefcomma{translate} @mrefcomma{translate_file} and @mrefdot{compile_file}
957 @opencatbox{Categories:}
958 @category{Translation and compilation}
962 @c THIS VARIABLE IS OBSOLETE: ASSIGNING compgrind: true CAUSES compfile
963 @c TO EVENTUALLY CALL AN OBSOLETE FUNCTION SPRIN1.
964 @c RECOMMENDATION IS TO CUT THIS ITEM, AND CUT $compgrind FROM src/transs.lisp
966 @c Default value: @code{false}
968 @c When @code{compgrind} is @code{true}, function definitions printed by
969 @c @code{compfile} are pretty-printed.
973 @c -----------------------------------------------------------------------------
975 @deffn {Function} compile @
976 @fname{compile} (@var{f_1}, @dots{}, @var{f_n}) @
977 @fname{compile} (functions) @
978 @fname{compile} (all)
980 Translates Maxima functions @var{f_1}, @dots{}, @var{f_n} into Lisp, evaluates
981 the Lisp translations, and calls the Lisp function @code{COMPILE} on each
982 translated function. @code{compile} returns a list of the names of the
985 @code{compile (all)} or @code{compile (functions)} compiles all user-defined
988 @code{compile} quotes its arguments;
989 the quote-quote operator @code{'@w{}'} defeats quotation.
991 Compiling a function to native code can mean a big increase in speed and might
992 cause the memory footprint to reduce drastically.
993 Code tends to be especially effective when the flexibility it needs to provide
994 is limited. If compilation doesn't provide the speed that is needed a few ways
995 to limit the code's functionality are the following:
997 @item If the function accesses global variables the complexity of the function
998 can be drastically be reduced by limiting these variables to one data type,
999 for example using @mref{mode_declare} or a statement like the following one:
1000 @code{put(x_1, bigfloat, numerical_type)}
1001 @item The compiler might warn about undeclared variables if text could either be
1002 a named option to a command or (if they are assigned a value to) the name
1003 of a variable. Prepending the option with a single quote @code{'}
1004 tells the compiler that the text is meant as an option.
1007 @opencatbox{Categories:}
1008 @category{Translation and compilation}
1012 @c -----------------------------------------------------------------------------
1014 @deffn {Function} define @
1015 @fname{define} (@var{f}(@var{x_1}, @dots{}, @var{x_n}), @var{expr}) @
1016 @fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}], @var{expr}) @
1017 @fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}](@var{y_1}, @dots{}, @var{y_m}), @var{expr}) @
1018 @fname{define} (funmake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
1019 @fname{define} (arraymake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
1020 @fname{define} (ev (@var{expr_1}), @var{expr_2})
1022 Defines a function named @var{f} with arguments @var{x_1}, @dots{}, @var{x_n}
1023 and function body @var{expr}. @code{define} always evaluates its second
1024 argument (unless explicitly quoted). The function so defined may be an ordinary
1025 Maxima function (with arguments enclosed in parentheses) or a @mref{memoizing function}
1026 (with arguments enclosed in square brackets).
1028 When the last or only function argument @var{x_n} is a list of one element,
1029 the function defined by @code{define} accepts a variable number of arguments.
1030 Actual arguments are assigned one-to-one to formal arguments @var{x_1}, @dots{},
1031 @var{x_(n - 1)}, and any further actual arguments, if present, are assigned to
1032 @var{x_n} as a list.
1034 When the first argument of @code{define} is an expression of the form
1035 @code{@var{f}(@var{x_1}, ..., @var{x_n})} or @code{@var{f}[@var{x_1}, ...,
1036 @var{x_n}]}, the function arguments are evaluated but @var{f} is not evaluated,
1037 even if there is already a function or variable by that name.
1039 When the first argument is an expression with operator @code{funmake},
1040 @code{arraymake}, or @code{ev}, the first argument is evaluated;
1041 this allows for the function name to be computed, as well as the body.
1043 All function definitions appear in the same namespace; defining a function
1044 @code{f} within another function @code{g} does not automatically limit the scope
1045 of @code{f} to @code{g}. However, @code{local(f)} makes the definition of
1046 function @code{f} effective only within the block or other compound expression
1047 in which @code{local} appears.
1049 If some formal argument @var{x_k} is a quoted symbol (after evaluation), the
1050 function defined by @code{define} does not evaluate the corresponding actual
1051 argument. Otherwise all actual arguments are evaluated.
1053 See also @mref{:=} and @mrefdot{::=}
1057 @code{define} always evaluates its second argument (unless explicitly quoted).
1060 @c expr : cos(y) - sin(x);
1061 @c define (F1 (x, y), expr);
1063 @c F2 (x, y) := expr;
1068 (%i1) expr : cos(y) - sin(x);
1069 (%o1) cos(y) - sin(x)
1072 (%i2) define (F1 (x, y), expr);
1073 (%o2) F1(x, y) := cos(y) - sin(x)
1077 (%o3) cos(b) - sin(a)
1080 (%i4) F2 (x, y) := expr;
1081 (%o4) F2(x, y) := expr
1085 (%o5) cos(y) - sin(x)
1089 The function defined by @code{define} may be an ordinary Maxima function or a
1090 @mref{memoizing function}.
1093 @c define (G1 (x, y), x.y - y.x);
1094 @c define (G2 [x, y], x.y - y.x);
1098 (%i1) define (G1 (x, y), x.y - y.x);
1099 (%o1) G1(x, y) := x . y - y . x
1102 (%i2) define (G2 [x, y], x.y - y.x);
1103 (%o2) G2 := x . y - y . x
1108 When the last or only function argument @var{x_n} is a list of one element,
1109 the function defined by @code{define} accepts a variable number of arguments.
1112 @c define (H ([L]), '(apply ("+", L)));
1117 (%i1) define (H ([L]), '(apply ("+", L)));
1118 (%o1) H([L]) := apply("+", L)
1126 When the first argument is an expression with operator @code{funmake},
1127 @code{arraymake}, or @code{ev}, the first argument is evaluated.
1131 @c funmake (F, [u]);
1132 @c define (funmake (F, [u]), cos(u) + 1);
1133 @c define (arraymake (F, [u]), cos(u) + 1);
1134 @c define (foo (x, y), bar (y, x));
1135 @c define (ev (foo (x, y)), sin(x) - cos(y));
1139 (%i1) [F : I, u : x];
1143 (%i2) funmake (F, [u]);
1147 (%i3) define (funmake (F, [u]), cos(u) + 1);
1148 (%o3) I(x) := cos(x) + 1
1151 (%i4) define (arraymake (F, [u]), cos(u) + 1);
1152 (%o4) I := cos(x) + 1
1156 (%i5) define (foo (x, y), bar (y, x));
1157 (%o5) foo(x, y) := bar(y, x)
1160 (%i6) define (ev (foo (x, y)), sin(x) - cos(y));
1161 (%o6) bar(y, x) := sin(x) - cos(y)
1165 @opencatbox{Categories:}
1166 @category{Function definition}
1170 @c SEE NOTE BELOW ABOUT THE DOCUMENTATION STRING
1171 @c @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode}, @var{documentation})
1173 @c -----------------------------------------------------------------------------
1174 @anchor{define_variable}
1175 @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode})
1177 Introduces a global variable into the Maxima environment.
1178 @code{define_variable} is useful in user-written packages, which are often
1179 translated or compiled as it gives the compiler hints of the type (``mode'')
1180 of a variable and therefore avoids requiring it to generate generic code that
1181 can deal with every variable being an integer, float, maxima object, array etc.
1183 @code{define_variable} carries out the following steps:
1187 @code{mode_declare (@var{name}, @var{mode})} declares the mode (``type'') of
1188 @var{name} to the translator which can considerably speed up compiled code as
1189 it allows having to create generic code. See @mref{mode_declare} for a list of
1193 If the variable is unbound, @var{default_value} is assigned to @var{name}.
1196 Associates @var{name} with a test function
1197 to ensure that @var{name} is only assigned values of the declared mode.
1201 @c FOLLOWING STATEMENT APPEARS TO BE OUT OF DATE.
1202 @c EXAMINING DEFMSPEC $DEFINE_VARIABLE AND DEF%TR $DEFINE_VARIABLE IN src/trmode.lisp,
1203 @c IT APPEARS THAT THE 4TH ARGUMENT IS NEVER REFERRED TO.
1204 @c EXECUTING translate_file ON A MAXIMA BATCH FILE WHICH CONTAINS
1205 @c define_variable (foo, 2222, integer, "THIS IS FOO");
1206 @c DOES NOT PUT "THIS IS FOO" INTO THE LISP FILE NOR THE UNLISP FILE.
1207 @c The optional 4th argument is a documentation string. When
1208 @c @code{translate_file} is used on a package which includes documentation
1209 @c strings, a second file is output in addition to the Lisp file which
1210 @c will contain the documentation strings, formatted suitably for use in
1211 @c manuals, usage files, or (for instance) @code{describe}.
1213 The @code{value_check} property can be assigned to any variable which has been
1214 defined via @code{define_variable} with a mode other than @code{any}.
1215 The @code{value_check} property is a lambda expression or the name of a function
1216 of one variable, which is called when an attempt is made to assign a value to
1217 the variable. The argument of the @code{value_check} function is the would-be
1220 @code{define_variable} evaluates @code{default_value}, and quotes @code{name}
1221 and @code{mode}. @code{define_variable} returns the current value of
1222 @code{name}, which is @code{default_value} if @code{name} was unbound before,
1223 and otherwise it is the previous value of @code{name}.
1227 @code{foo} is a Boolean variable, with the initial value @code{true}.
1230 @c define_variable (foo, true, boolean);
1238 (%i1) define_variable (foo, true, boolean);
1251 translator: foo was declared with mode boolean
1252 , but it has value: %pi
1253 -- an error. To debug this try: debugmode(true);
1261 @code{bar} is an integer variable, which must be prime.
1264 @c define_variable (bar, 2, integer);
1265 @c qput (bar, prime_test, value_check);
1266 @c prime_test (y) := if not primep(y) then
1267 @c error (y, "is not prime.");
1274 (%i1) define_variable (bar, 2, integer);
1278 (%i2) qput (bar, prime_test, value_check);
1282 (%i3) prime_test (y) := if not primep(y) then
1283 error (y, "is not prime.");
1284 (%o3) prime_test(y) := if not primep(y)
1285 then error(y, "is not prime.")
1294 #0: prime_test(y=1440)
1295 -- an error. To debug this try: debugmode(true);
1303 @code{baz_quux} is a variable which cannot be assigned a value.
1304 The mode @code{any_check} is like @code{any}, but @code{any_check} enables the
1305 @code{value_check} mechanism, and @code{any} does not.
1308 @c define_variable (baz_quux, 'baz_quux, any_check);
1309 @c F: lambda ([y], if y # 'baz_quux then
1310 @c error ("Cannot assign to `baz_quux'."));
1311 @c qput (baz_quux, ''F, value_check);
1312 @c baz_quux: 'baz_quux;
1313 @c baz_quux: sqrt(2);
1318 (%i1) define_variable (baz_quux, 'baz_quux, any_check);
1322 (%i2) F: lambda ([y], if y # 'baz_quux then
1323 error ("Cannot assign to `baz_quux'."));
1324 (%o2) lambda([y], if y # 'baz_quux
1325 then error(Cannot assign to `baz_quux'.))
1328 (%i3) qput (baz_quux, ''F, value_check);
1329 (%o3) lambda([y], if y # 'baz_quux
1330 then error(Cannot assign to `baz_quux'.))
1333 (%i4) baz_quux: 'baz_quux;
1337 (%i5) baz_quux: sqrt(2);
1338 Cannot assign to `baz_quux'.
1339 #0: lambda([y],if y # 'baz_quux then
1340 error("Cannot assign to `baz_quux'."))(y=sqrt(2))
1341 -- an error. To debug this try: debugmode(true);
1349 @opencatbox{Categories:}
1350 @category{Translation and compilation}
1354 @c -----------------------------------------------------------------------------
1356 @deffn {Function} dispfun @
1357 @fname{dispfun} (@var{f_1}, @dots{}, @var{f_n}) @
1358 @fname{dispfun} (all)
1360 Displays the definition of the user-defined functions @var{f_1}, @dots{},
1361 @var{f_n}. Each argument may be the name of a macro (defined with @code{::=}),
1362 an ordinary function (defined with @code{:=} or @code{define}), an array
1363 function (defined with @code{:=} or @code{define}, but enclosing arguments in
1364 square brackets @code{[ ]}), a subscripted function (defined with @code{:=} or
1365 @code{define}, but enclosing some arguments in square brackets and others in
1366 parentheses @code{( )}), one of a family of subscripted functions selected by a
1367 particular subscript value, or a subscripted function defined with a constant
1370 @code{dispfun (all)} displays all user-defined functions as
1371 given by the @code{functions}, @code{arrays}, and @code{macros} lists,
1372 omitting subscripted functions defined with constant subscripts.
1374 @code{dispfun} creates an intermediate expression label
1375 (@code{%t1}, @code{%t2}, etc.)
1376 for each displayed function, and assigns the function definition to the label.
1377 In contrast, @code{fundef} returns the function definition.
1379 @code{dispfun} quotes its arguments; the quote-quote operator @code{'@w{}'}
1380 defeats quotation. @code{dispfun} returns the list of intermediate expression
1381 labels corresponding to the displayed functions.
1386 @c m(x, y) ::= x^(-y);
1387 @c f(x, y) := x^(-y);
1388 @c g[x, y] := x^(-y);
1389 @c h[x](y) := x^(-y);
1390 @c i[8](y) := 8^(-y);
1391 @c dispfun (m, f, g, h, h[5], h[10], i[8]);
1396 (%i1) m(x, y) ::= x^(-y);
1401 (%i2) f(x, y) := x^(-y);
1406 (%i3) g[x, y] := x^(-y);
1412 (%i4) h[x](y) := x^(-y);
1418 (%i5) i[8](y) := 8^(-y);
1424 (%i6) dispfun (m, f, g, h, h[5], h[10], i[8]);
1453 (%o12) [%t6, %t7, %t8, %t9, %t10, %t11, %t12]
1458 (%o13) [m(x, y) ::= x , f(x, y) := x , g := x ,
1461 h (y) := x , h (y) := --, h (y) := ---, i (y) := 8 ]
1467 @opencatbox{Categories:}
1468 @category{Function definition}
1469 @category{Display functions}
1473 @c -----------------------------------------------------------------------------
1475 @deffn {Function} fullmap (@var{f}, @var{expr_1}, @dots{})
1477 Similar to @code{map}, but @code{fullmap} keeps mapping down all subexpressions
1478 until the main operators are no longer the same.
1480 @code{fullmap} is used by the Maxima simplifier for certain matrix
1481 manipulations; thus, Maxima sometimes generates an error message concerning
1482 @code{fullmap} even though @code{fullmap} was not explicitly called by the user.
1497 (%i2) fullmap (g, %);
1498 (%o2) g(b) g(c) + g(a)
1501 (%i3) map (g, %th(2));
1506 @opencatbox{Categories:}
1507 @category{Function application}
1508 @category{Expressions}
1512 @c -----------------------------------------------------------------------------
1514 @deffn {Function} fullmapl (@var{f}, @var{list_1}, @dots{})
1516 Similar to @code{fullmap}, but @code{fullmapl} only maps onto lists and
1522 @c fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
1526 (%i1) fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
1527 (%o1) [[a + 3, 4], [4, 3.5]]
1531 @opencatbox{Categories:}
1532 @category{Function application}
1533 @category{Expressions}
1537 @c -----------------------------------------------------------------------------
1539 @defvr {System variable} functions
1540 Default value: @code{[]}
1542 @code{functions} is the list of ordinary Maxima functions
1543 in the current session.
1544 An ordinary function is a function constructed by
1545 @code{define} or @code{:=} and called with parentheses @code{()}.
1546 A function may be defined at the Maxima prompt
1547 or in a Maxima file loaded by @code{load} or @code{batch}.
1549 @mref{Memoizing functions} (called with square brackets, e.g., @code{F[x]}) and subscripted
1550 functions (called with square brackets and parentheses, e.g., @code{F[x](y)})
1551 are listed by the global variable @code{arrays}, and not by @code{functions}.
1553 Lisp functions are not kept on any list.
1558 @c F_1 (x) := x - 100;
1559 @c F_2 (x, y) := x / y;
1560 @c define (F_3 (x), sqrt (x));
1561 @c G_1 [x] := x - 100;
1562 @c G_2 [x, y] := x / y;
1563 @c define (G_3 [x], sqrt (x));
1564 @c H_1 [x] (y) := x^y;
1570 (%i1) F_1 (x) := x - 100;
1571 (%o1) F_1(x) := x - 100
1574 (%i2) F_2 (x, y) := x / y;
1576 (%o2) F_2(x, y) := -
1580 (%i3) define (F_3 (x), sqrt (x));
1581 (%o3) F_3(x) := sqrt(x)
1584 (%i4) G_1 [x] := x - 100;
1585 (%o4) G_1 := x - 100
1589 (%i5) G_2 [x, y] := x / y;
1595 (%i6) define (G_3 [x], sqrt (x));
1596 (%o6) G_3 := sqrt(x)
1600 (%i7) H_1 [x] (y) := x^y;
1607 (%o8) [F_1(x), F_2(x, y), F_3(x)]
1611 (%o9) [G_1, G_2, G_3, H_1]
1615 @opencatbox{Categories:}
1616 @category{Function definition}
1617 @category{Global variables}
1621 @c -----------------------------------------------------------------------------
1623 @deffn {Function} fundef (@var{f})
1625 Returns the definition of the function @var{f}.
1629 @item the name of a macro (defined with @code{::=}),
1630 @item an ordinary function (defined with @code{:=} or @code{define}),
1631 @item a @mref{memoizing function} (defined with @code{:=} or @code{define}, but enclosing arguments in square brackets @code{[ ]}),
1632 @item a subscripted function (defined with @code{:=} or @code{define},
1633 but enclosing some arguments in square brackets and others in parentheses
1635 @item one of a family of subscripted functions selected by a particular
1637 @item or a subscripted function defined with a constant subscript.
1640 @code{fundef} quotes its argument;
1641 the quote-quote operator @code{'@w{}'} defeats quotation.
1643 @code{fundef (@var{f})} returns the definition of @var{f}.
1644 In contrast, @code{dispfun (@var{f})} creates an intermediate expression label
1645 and assigns the definition to the label.
1647 @c PROBABLY NEED SOME EXAMPLES HERE
1648 @opencatbox{Categories:}
1649 @category{Function definition}
1653 @c -----------------------------------------------------------------------------
1655 @deffn {Function} funmake (@var{F}, [@var{arg_1}, @dots{}, @var{arg_n}])
1657 Returns an expression @code{@var{F}(@var{arg_1}, ..., @var{arg_n})}.
1658 The return value is simplified, but not evaluated,
1659 so the function @var{F} is not called, even if it exists.
1661 @code{funmake} does not attempt to distinguish @mref{memoizing functions} from ordinary
1662 functions; when @var{F} is the name of a memoizing function,
1663 @code{funmake} returns @code{@var{F}(...)}
1664 (that is, a function call with parentheses instead of square brackets).
1665 @code{arraymake} returns a function call with square brackets in this case.
1667 @code{funmake} evaluates its arguments.
1669 See also @mref{apply} and @mrefdot{args}
1673 @code{funmake} applied to an ordinary Maxima function.
1676 @c F (x, y) := y^2 - x^2;
1677 @c funmake (F, [a + 1, b + 1]);
1682 (%i1) F (x, y) := y^2 - x^2;
1684 (%o1) F(x, y) := y - x
1687 (%i2) funmake (F, [a + 1, b + 1]);
1688 (%o2) F(a + 1, b + 1)
1693 (%o3) (b + 1) - (a + 1)
1697 @code{funmake} applied to a macro.
1700 @c G (x) ::= (x - 1)/2;
1701 @c funmake (G, [u]);
1706 (%i1) G (x) ::= (x - 1)/2;
1708 (%o1) G(x) ::= -----
1712 (%i2) funmake (G, [u]);
1723 @code{funmake} applied to a subscripted function.
1726 @c H [a] (x) := (x - 1)^a;
1727 @c funmake (H [n], [%e]);
1729 @c funmake ('(H [n]), [%e]);
1734 (%i1) H [a] (x) := (x - 1)^a;
1736 (%o1) H (x) := (x - 1)
1740 (%i2) funmake (H [n], [%e]);
1742 (%o2) lambda([x], (x - 1) )(%e)
1750 (%i4) funmake ('(H [n]), [%e]);
1761 @code{funmake} applied to a symbol which is not a defined function of any kind.
1764 @c funmake (A, [u]);
1769 (%i1) funmake (A, [u]);
1778 @code{funmake} evaluates its arguments, but not the return value.
1781 @c det(a,b,c) := b^2 -4*a*c;
1782 @c (x : 8, y : 10, z : 12);
1784 @c funmake (f, [x, y, z]);
1789 (%i1) det(a,b,c) := b^2 -4*a*c;
1791 (%o1) det(a, b, c) := b - 4 a c
1794 (%i2) (x : 8, y : 10, z : 12);
1802 (%i4) funmake (f, [x, y, z]);
1803 (%o4) det(8, 10, 12)
1811 Maxima simplifies @code{funmake}'s return value.
1814 @c funmake (sin, [%pi / 2]);
1818 (%i1) funmake (sin, [%pi / 2]);
1823 @opencatbox{Categories:}
1824 @category{Function application}
1825 @category{Expressions}
1829 @c -----------------------------------------------------------------------------
1831 @deffn {Function} lambda @
1832 @fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
1833 @fname{lambda} ([[@var{L}]], @var{expr_1}, @dots{}, @var{expr_n}) @
1834 @fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}, [@var{L}]], @var{expr_1}, @dots{}, @var{expr_n})
1836 Defines and returns a lambda expression (that is, an anonymous function).
1837 The function may have required arguments @var{x_1}, @dots{}, @var{x_m} and/or
1838 optional arguments @var{L}, which appear within the function body as a list.
1839 The return value of the function is @var{expr_n}. A lambda expression can be
1840 assigned to a variable and evaluated like an ordinary function. A lambda
1841 expression may appear in some contexts in which a function name is expected.
1843 When the function is evaluated, unbound local variables @var{x_1}, @dots{},
1844 @var{x_m} are created. @code{lambda} may appear within @code{block} or another
1845 @code{lambda}; local variables are established each time another @code{block} or
1846 @code{lambda} is evaluated. Local variables appear to be global to any enclosed
1847 @code{block} or @code{lambda}. If a variable is not local, its value is the
1848 value most recently assigned in an enclosing @code{block} or @code{lambda}, if
1849 any, otherwise, it is the value of the variable in the global environment.
1850 This policy may coincide with the usual understanding of "dynamic scope".
1852 After local variables are established, @var{expr_1} through @var{expr_n} are
1853 evaluated in turn. The special variable @code{%%}, representing the value of
1854 the preceding expression, is recognized. @code{throw} and @code{catch} may also
1855 appear in the list of expressions.
1857 @code{return} cannot appear in a lambda expression unless enclosed by
1858 @code{block}, in which case @code{return} defines the return value of the block
1859 and not of the lambda expression, unless the block happens to be @var{expr_n}.
1860 Likewise, @code{go} cannot appear in a lambda expression unless enclosed by
1863 @code{lambda} quotes its arguments;
1864 the quote-quote operator @code{'@w{}'} defeats quotation.
1870 A lambda expression can be assigned to a variable and evaluated like an ordinary
1875 @c f: lambda ([x], x^2);
1880 (%i1) f: lambda ([x], x^2);
1882 (%o1) lambda([x], x )
1893 A lambda expression may appear in contexts in which a function evaluation is expected.
1897 @c lambda ([x], x^2) (a);
1898 @c apply (lambda ([x], x^2), [a]);
1899 @c map (lambda ([x], x^2), [a, b, c, d, e]);
1903 (%i1) lambda ([x], x^2) (a);
1908 (%i2) apply (lambda ([x], x^2), [a]);
1913 (%i3) map (lambda ([x], x^2), [a, b, c, d, e]);
1915 (%o3) [a , b , c , d , e ]
1921 Argument variables are local variables.
1922 Other variables appear to be global variables.
1923 Global variables are evaluated at the time the lambda expression is evaluated,
1924 unless some special evaluation is forced by some means, such as @code{'@w{}'}.
1930 @c g: lambda ([a], a*b);
1933 @c g2: lambda ([a], a*''b);
1941 (%i3) g: lambda ([a], a*b);
1942 (%o3) lambda([a], a b)
1952 (%i6) g2: lambda ([a], a*''b);
1953 (%o6) lambda([a], a %gamma)
1966 Lambda expressions may be nested. Local variables within the outer lambda
1967 expression appear to be global to the inner expression unless masked by local
1968 variables of the same names.
1972 @c h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1977 (%i1) h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1979 (%o1) lambda([a, b], h2 : lambda([a], a b), h2(-))
1983 (%i2) h(%pi, %gamma);
1992 Since @code{lambda} quotes its arguments, lambda expression @code{i} below does
1993 not define a "multiply by @code{a}" function. Such a function can be defined
1994 via @code{buildq}, as in lambda expression @code{i2} below.
1998 @c i: lambda ([a], lambda ([x], a*x));
2000 @c i2: lambda([a], buildq([a: a], lambda([x], a*x)));
2006 (%i1) i: lambda ([a], lambda ([x], a*x));
2007 (%o1) lambda([a], lambda([x], a x))
2011 (%o2) lambda([x], a x)
2014 (%i3) i2: lambda([a], buildq([a: a], lambda([x], a*x)));
2015 (%o3) lambda([a], buildq([a : a], lambda([x], a x)))
2020 (%o4) lambda([x], (-) x)
2033 A lambda expression may take a variable number of arguments,
2034 which are indicated by @code{[@var{L}]} as the sole or final argument.
2035 The arguments appear within the function body as a list.
2039 @c f : lambda ([aa, bb, [cc]], aa * cc + bb);
2040 @c f (foo, %i, 17, 29, 256);
2041 @c g : lambda ([[aa]], apply ("+", aa));
2042 @c g (17, 29, x, y, z, %e);
2046 (%i1) f : lambda ([aa, bb, [cc]], aa * cc + bb);
2047 (%o1) lambda([aa, bb, [cc]], aa cc + bb)
2050 (%i2) f (foo, %i, 17, 29, 256);
2051 (%o2) [17 foo + %i, 29 foo + %i, 256 foo + %i]
2054 (%i3) g : lambda ([[aa]], apply ("+", aa));
2055 (%o3) lambda([[aa]], apply(+, aa))
2058 (%i4) g (17, 29, x, y, z, %e);
2059 (%o4) z + y + x + %e + 46
2063 @opencatbox{Categories:}
2064 @category{Function definition}
2068 @c NEEDS CLARIFICATION AND EXAMPLES
2070 @c -----------------------------------------------------------------------------
2072 @deffn {Function} local (@var{v_1}, @dots{}, @var{v_n})
2074 Saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_n},
2075 removes any properties before evaluating other expressions,
2076 and restores any saved properties on exit
2077 from the block or other compound expression in which @code{local} appears.
2079 Some declarations are implemented as properties of a symbol, including
2080 @code{:=}, @code{array}, @code{dependencies}, @code{atvalue},
2081 @code{matchdeclare}, @code{atomgrad}, @code{constant}, @code{nonscalar},
2082 @code{assume}, and some others. The effect of @code{local} is to make such
2083 declarations effective only within the block or other compound expression in
2084 which @code{local} appears; otherwise such declarations are global declarations.
2086 @code{local} can only appear in @code{block}
2087 or in the body of a function definition or @code{lambda} expression,
2088 and only one occurrence is permitted in each.
2090 @code{local} quotes its arguments.
2091 @code{local} returns @code{done}.
2095 A local function definition.
2098 @c foo (x) := 1 - x;
2100 @c block (local (foo), foo (x) := 2 * x, foo (100));
2105 (%i1) foo (x) := 1 - x;
2106 (%o1) foo(x) := 1 - x
2113 (%i3) block (local (foo), foo (x) := 2 * x, foo (100));
2122 @opencatbox{Categories:}
2123 @category{Function definition}
2124 @category{Programming}
2128 @c -----------------------------------------------------------------------------
2129 @anchor{macroexpansion}
2130 @defvr {Option variable} macroexpansion
2131 Default value: @code{false}
2133 @code{macroexpansion} controls whether the expansion (that is, the return value)
2134 of a macro function is substituted for the macro function call.
2135 A substitution may speed up subsequent expression evaluations,
2136 at the cost of storing the expansion.
2140 The expansion of a macro function is not substituted for the macro function call.
2142 The first time a macro function call is evaluated,
2143 the expansion is stored.
2144 The expansion is not recomputed on subsequent calls;
2145 any side effects (such as @code{print} or assignment to global variables) happen
2146 only when the macro function call is first evaluated.
2147 Expansion in an expression does not affect other expressions
2148 which have the same macro function call.
2150 The first time a macro function call is evaluated,
2151 the expansion is substituted for the call,
2152 thus modifying the expression from which the macro function was called.
2153 The expansion is not recomputed on subsequent calls;
2154 any side effects happen only when the macro function call is first evaluated.
2155 Expansion in an expression does not affect other expressions
2156 which have the same macro function call.
2161 When @code{macroexpansion} is @code{false},
2162 a macro function is called every time the calling expression is evaluated,
2163 and the calling expression is not modified.
2166 @c f (x) := h (x) / g (x);
2167 @c g (x) ::= block (print ("x + 99 is equal to", x),
2168 @c return (x + 99));
2169 @c h (x) ::= block (print ("x - 99 is equal to", x),
2170 @c return (x - 99));
2171 @c macroexpansion: false;
2178 (%i1) f (x) := h (x) / g (x);
2184 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2186 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2190 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2192 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2196 (%i4) macroexpansion: false;
2201 x - 99 is equal to x
2202 x + 99 is equal to x
2217 x - 99 is equal to x
2218 x + 99 is equal to x
2225 When @code{macroexpansion} is @code{expand},
2226 a macro function is called once,
2227 and the calling expression is not modified.
2230 @c f (x) := h (x) / g (x);
2231 @c g (x) ::= block (print ("x + 99 is equal to", x),
2232 @c return (x + 99));
2233 @c h (x) ::= block (print ("x - 99 is equal to", x),
2234 @c return (x - 99));
2235 @c macroexpansion: expand;
2242 (%i1) f (x) := h (x) / g (x);
2248 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2250 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2254 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2256 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2260 (%i4) macroexpansion: expand;
2265 x - 99 is equal to x
2266 x + 99 is equal to x
2273 mmacroexpanded(x - 99, h(x))
2274 (%t6) f(x) := ----------------------------
2275 mmacroexpanded(x + 99, g(x))
2287 When @code{macroexpansion} is @code{displace},
2288 a macro function is called once,
2289 and the calling expression is modified.
2292 @c f (x) := h (x) / g (x);
2293 @c g (x) ::= block (print ("x + 99 is equal to", x),
2294 @c return (x + 99));
2295 @c h (x) ::= block (print ("x - 99 is equal to", x),
2296 @c return (x - 99));
2297 @c macroexpansion: displace;
2304 (%i1) f (x) := h (x) / g (x);
2310 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2312 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2316 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2318 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2322 (%i4) macroexpansion: displace;
2327 x - 99 is equal to x
2328 x + 99 is equal to x
2336 (%t6) f(x) := ------
2349 @opencatbox{Categories:}
2350 @category{Function application}
2351 @category{Global flags}
2355 @c -----------------------------------------------------------------------------
2356 @anchor{mode_declare}
2357 @anchor{modedeclare}
2358 @deffn {Function} mode_declare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
2359 @deffnx {Function} modedeclare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
2361 A @code{mode_declare} informs the compiler which type (lisp programmers name the type:
2362 ``mode'') a function parameter or its return value will be of. This can greatly
2363 boost the efficiency of the code the compiler generates: Without knowing the type of
2364 all variables and knowing the return value of all functions a function uses
2365 in advance very generic (and thus potentially slow) code needs to be generated.
2367 The arguments of @code{mode_declare} are pairs consisting of a variable (or a list
2368 of variables all having the same mode) and a mode. Available modes (``types'') are:
2370 array an declared array (see the detailed description below)
2371 boolean true or false
2372 integer integers (including arbitrary-size integers)
2373 fixnum integers (excluding arbitrary-size integers)
2374 float machine-size floating-point numbers
2375 real machine-size floating-point or integer
2377 any any kind of object (useful for arrays of any)
2380 A function parameter named @code{a} can be declared as an array filled with elements
2381 of the type @code{t} the following way:
2383 mode_declare (a, array(t, dim1, dim2, ...))
2385 If none of the elements of the array @code{a} needs to be checked if it still doesn't
2386 contain a value additional code can be omitted by declaring this fact, too:
2388 mode_declare (a, array (t, complete, dim1, dim2, ...))
2390 The @code{complete} has no effect if all array elements are of the type
2391 @code{fixnum} or @code{float}: Machine-sized numbers inevitably contain a value
2392 (and will automatically be initialized to 0 in most lisp implementations).
2394 Another way to tell that all entries of the array @code{a} are of the type
2395 (``mode'') @code{m} and have been assigned a value to would be:
2397 mode_declare (completearray (a), m))
2400 Numeric code using arrays might run faster still if the size of the array is
2401 known at compile time, as well, as in:
2403 mode_declare (completearray (a [10, 10]), float)
2405 for a floating point number array named @code{a} which is 10 x 10.
2407 @code{mode_declare} also can be used in order to declare the type of the result
2408 of a function. In this case the function compilation needs to be preceded by
2409 another @code{mode_declare} statement. For example the expression,
2411 mode_declare ([function (f_1, f_2, ...)], fixnum)
2413 declares that the values returned by @code{f_1}, @code{f_2}, @dots{} are
2414 single-word integers.
2416 @code{modedeclare} is a synonym for @code{mode_declare}.
2418 If the type of function parameters and results doesn't match the declaration by
2419 @code{mode_declare} the function may misbehave or a warning or an error might
2420 occur, see @mrefcomma{mode_checkp} @mref{mode_check_errorp} and
2421 @mrefdot{mode_check_warnp}
2423 See @mref{mode_identity} for declaring the type of lists and @mref{define_variable} for
2424 declaring the type of all global variables compiled code uses, as well.
2428 @c square_float(f):=(
2429 @c mode_declare(f,float),
2432 @c mode_declare([function(f)],float);
2433 @c compile(square_float);
2434 @c square_float(100.0);
2438 (%i1) square_float(f):=(
2439 mode_declare(f,float),
2442 (%o1) square_float(f) := (mode_declare(f, float), f f)
2445 (%i2) mode_declare([function(f)],float);
2446 (%o2) [[function(f)]]
2449 (%i3) compile(square_float);
2450 (%o3) [square_float]
2453 (%i4) square_float(100.0);
2459 @opencatbox{Categories:}
2460 @category{Translation and compilation}
2464 @c NEEDS MORE EXAMPLES?
2466 @c -----------------------------------------------------------------------------
2467 @anchor{mode_checkp}
2468 @defvr {Option variable} mode_checkp
2469 Default value: @code{true}
2471 When @code{mode_checkp} is @code{true}, @mref{mode_declare} does not only define
2472 which type a variable will be of so the compiler can generate more efficient code,
2473 but will also create a runtime warning if the variable isn't of the variable type
2474 the code was compiled to deal with.
2477 @c mode_checkp:true;
2479 @c mode_declare(f,float),
2487 (%i1) mode_checkp:true;
2492 mode_declare(f,float),
2495 (%o2) square(f) := (mode_declare(f, float), f )
2498 (%i3) compile(square);
2503 (%o4) 5.289999999999999
2507 Maxima encountered a Lisp error:
2515 Automatically continuing.
2516 To enable the Lisp debugger set *debugger-hook* to nil.
2520 @opencatbox{Categories:}
2521 @category{Translation flags and variables}
2525 @c -----------------------------------------------------------------------------
2526 @anchor{mode_check_errorp}
2527 @defvr {Option variable} mode_check_errorp
2528 Default value: @code{false}
2530 When @code{mode_check_errorp} is @code{true}, @code{mode_declare} calls
2532 @c NEED SOME EXAMPLES HERE.
2534 @opencatbox{Categories:}
2535 @category{Translation flags and variables}
2539 @c -----------------------------------------------------------------------------
2540 @anchor{mode_check_warnp}
2541 @defvr {Option variable} mode_check_warnp
2542 Default value: @code{true}
2544 @c WHAT DOES THIS MEAN ??
2545 When @code{mode_check_warnp} is @code{true}, mode errors are
2547 @c NEED SOME EXAMPLES HERE.
2549 @opencatbox{Categories:}
2550 @category{Translation flags and variables}
2554 @c NEEDS AN EXAMPLE FOR DECLARING THE RETURN TYPE
2556 @c -----------------------------------------------------------------------------
2557 @anchor{mode_identity}
2558 @deffn {Function} mode_identity (@var{arg_1}, @var{arg_2})
2560 @code{mode_identity} works similar to @mref{mode_declare}, but is used for
2561 informing the compiler that a thing like a @code{macro} or a list operation
2562 will only return a specific type of object. The purpose of doing so is that
2563 maxima supports many objects: Machine integers, arbitrary length integers,
2564 equations, machine floats, big floats, which means that for everything that
2565 deals with return values of operations that can result in any object the
2566 compiler needs to output generic (and therefore potentially slow) code.
2568 The first argument to @code{mode_identity} is the type of return value
2569 something will return (for possible types see @mref{mode_declare}).
2570 (i.e., one of @code{float}, @code{fixnum}, @code{number},
2571 The second argument is the expression that will return an object of this
2574 If the the return value of this expression is of a type the code was not
2575 compiled for error or warning is signalled.
2576 @c ARE THE MODE_DECLARE VARIABLES FOR TURNING OFF THIS ERROR OR WARNING
2577 @c EFFECTIVE HERE, TOO?
2579 If you knew that @code{first (l)} returned a number then you could write
2582 @code{mode_identity (number, first (l))}.
2584 However, if you need this construct more often it would be more efficient
2585 to define a function that returns a number fist:
2587 firstnumb (x) ::= buildq ([x], mode_identity (number, first(x)));
2590 @code{firstnumb} now can be used every time you need the first element
2591 of a list that is guaranteed to be filled with numbers.
2592 @opencatbox{Categories:}
2593 @category{Translation and compilation}
2597 @c -----------------------------------------------------------------------------
2598 @anchor{remfunction}
2599 @deffn {Function} remfunction @
2600 @fname{remfunction} (@var{f_1}, @dots{}, @var{f_n}) @
2601 @fname{remfunction} (all)
2603 Unbinds the function definitions of the symbols @var{f_1}, @dots{}, @var{f_n}.
2604 The arguments may be the names of ordinary functions (created by @mref{:=} or
2605 @mref{define}) or macro functions (created by @mref{::=}).
2607 @code{remfunction (all)} unbinds all function definitions.
2609 @code{remfunction} quotes its arguments.
2611 @code{remfunction} returns a list of the symbols for which the function
2612 definition was unbound. @code{false} is returned in place of any symbol for
2613 which there is no function definition.
2615 @code{remfunction} does not apply to @mref{memoizing functions} or subscripted functions.
2616 @mref{remarray} applies to those types of functions.
2618 @opencatbox{Categories:}
2619 @category{Function definition}
2623 @c NEEDS MORE WORK !!!
2625 @c -----------------------------------------------------------------------------
2627 @defvr {Option variable} savedef
2628 Default value: @code{true}
2630 When @code{savedef} is @code{true}, the Maxima version of a user function is
2631 preserved when the function is translated. This permits the definition to be
2632 displayed by @code{dispfun} and allows the function to be edited.
2634 When @code{savedef} is @code{false}, the names of translated functions are
2635 removed from the @code{functions} list.
2637 @opencatbox{Categories:}
2638 @category{Translation flags and variables}
2642 @c -----------------------------------------------------------------------------
2644 @deffn {Function} translate @
2645 @fname{translate} (@var{f_1}, @dots{}, @var{f_n}) @
2646 @fname{translate} (functions) @
2647 @fname{translate} (all)
2649 Translates the user-defined functions @var{f_1}, @dots{}, @var{f_n} from the
2650 Maxima language into Lisp and evaluates the Lisp translations.
2651 Typically the translated functions run faster than the originals.
2653 @code{translate (all)} or @code{translate (functions)} translates all
2654 user-defined functions.
2656 Functions to be translated should include a call to @code{mode_declare} at the
2657 beginning when possible in order to produce more efficient code. For example:
2660 f (x_1, x_2, ...) := block ([v_1, v_2, ...],
2661 mode_declare (v_1, mode_1, v_2, mode_2, ...), ...)
2665 where the @var{x_1}, @var{x_2}, @dots{} are the parameters to the function and
2666 the @var{v_1}, @var{v_2}, @dots{} are the local variables.
2668 The names of translated functions are removed from the @code{functions} list
2669 if @code{savedef} is @code{false} (see below) and are added to the @code{props}
2672 Functions should not be translated unless they are fully debugged.
2674 Expressions are assumed simplified; if they are not, correct but non-optimal
2675 code gets generated. Thus, the user should not set the @code{simp} switch to
2676 @code{false} which inhibits simplification of the expressions to be translated.
2678 The switch @code{translate}, if @code{true}, causes automatic
2679 translation of a user's function to Lisp.
2681 Note that translated
2682 functions may not run identically to the way they did before
2683 translation as certain incompatibilities may exist between the Lisp
2684 and Maxima versions. Principally, the @code{rat} function with more than
2685 one argument and the @code{ratvars} function should not be used if any
2686 variables are @code{mode_declare}'d canonical rational expressions (CRE).
2687 Also the @code{prederror: false} setting
2689 @c WHAT ABOUT % AND %% ???
2691 @code{savedef} - if @code{true} will cause the Maxima version of a user
2692 function to remain when the function is @code{translate}'d. This permits the
2693 definition to be displayed by @code{dispfun} and allows the function to be
2696 @code{transrun} - if @code{false} will cause the interpreted version of all
2697 functions to be run (provided they are still around) rather than the
2700 The result returned by @code{translate} is a list of the names of the
2701 functions translated.
2703 @opencatbox{Categories:}
2704 @category{Translation and compilation}
2708 @c -----------------------------------------------------------------------------
2709 @anchor{translate_file}
2710 @deffn {Function} translate_file @
2711 @fname{translate_file} (@var{maxima_filename}) @
2712 @fname{translate_file} (@var{maxima_filename}, @var{lisp_filename})
2714 Translates a file of Maxima code into a file of Lisp code.
2715 @code{translate_file} returns a list of three filenames:
2716 the name of the Maxima file, the name of the Lisp file, and the name of file
2717 containing additional information about the translation.
2718 @code{translate_file} evaluates its arguments.
2720 @code{translate_file ("foo.mac"); load("foo.LISP")} is the same as the command
2721 @code{batch ("foo.mac")} except for certain restrictions, the use of
2722 @code{'@w{}'} and @code{%}, for example.
2723 @c FIGURE OUT WHAT THE RESTRICTIONS ARE AND STATE THEM
2725 @code{translate_file (@var{maxima_filename})} translates a Maxima file
2726 @var{maxima_filename} into a similarly-named Lisp file.
2727 For example, @code{foo.mac} is translated into @code{foo.LISP}.
2728 The Maxima filename may include a directory name or names,
2729 in which case the Lisp output file is written
2730 to the same directory from which the Maxima input comes.
2732 @code{translate_file (@var{maxima_filename}, @var{lisp_filename})} translates
2733 a Maxima file @var{maxima_filename} into a Lisp file @var{lisp_filename}.
2734 @code{translate_file} ignores the filename extension, if any, of
2735 @code{lisp_filename}; the filename extension of the Lisp output file is always
2736 @code{LISP}. The Lisp filename may include a directory name or names,
2737 in which case the Lisp output file is written to the specified directory.
2739 @code{translate_file} also writes a file of translator warning
2740 messages of various degrees of severity.
2741 The filename extension of this file is @code{UNLISP}.
2742 This file may contain valuable information, though possibly obscure,
2743 for tracking down bugs in translated code.
2744 The @code{UNLISP} file is always written
2745 to the same directory from which the Maxima input comes.
2747 @code{translate_file} emits Lisp code which causes
2748 some declarations and definitions to take effect as soon
2749 as the Lisp code is compiled.
2750 See @code{compile_file} for more on this topic.
2752 @c CHECK ALL THESE AND SEE WHICH ONES ARE OBSOLETE
2755 @code{tr_array_as_ref}
2756 @c tr_bind_mode_hook EXISTS BUT IT APPEARS TO BE A GROTESQUE UNDOCUMENTED HACK
2757 @c WE DON'T WANT TO MENTION IT
2758 @c @code{tr_bind_mode_hook},
2759 @mrefcomma{tr_bound_function_applyp}
2760 @c tr_exponent EXISTS AND WORKS AS ADVERTISED IN src/troper.lisp
2761 @c NOT OTHERWISE DOCUMENTED; ITS EFFECT SEEMS TOO WEAK TO MENTION
2763 @mrefcomma{tr_file_tty_messagesp}
2764 @mrefcomma{tr_float_can_branch_complex}
2765 @mrefcomma{tr_function_call_default}
2766 @mrefcomma{tr_numer}
2767 @mrefcomma{tr_optimize_max_loop}
2768 @mrefcomma{tr_state_vars}
2769 @mrefcomma{tr_warnings_get}
2771 @code{tr_warn_bad_function_calls}
2772 @mrefcomma{tr_warn_fexpr}
2773 @mrefcomma{tr_warn_meval}
2774 @mrefcomma{tr_warn_mode}
2775 @mrefcomma{tr_warn_undeclared}
2776 and @mrefdot{tr_warn_undefined_variable}
2779 @opencatbox{Categories:}
2780 @category{Translation and compilation}
2784 @c -----------------------------------------------------------------------------
2786 @defvr {Option variable} transrun
2787 Default value: @code{true}
2789 When @code{transrun} is @code{false} will cause the interpreted
2790 version of all functions to be run (provided they are still around)
2791 rather than the translated version.
2793 @opencatbox{Categories:}
2794 @category{Translation flags and variables}
2798 @c IN WHAT CONTEXT IS tr_array_as_ref: false APPROPRIATE ??? NOT SEEING THE USEFULNESS HERE.
2799 @c ALSO, I GUESS WE SHOULD HAVE AN ITEM FOR translate_fast_arrays, ANOTHER CONFUSING FLAG ...
2801 @c -----------------------------------------------------------------------------
2802 @anchor{tr_array_as_ref}
2803 @defvr {Option variable} tr_array_as_ref
2804 Default value: @code{true}
2806 If @code{translate_fast_arrays} is @code{false}, array references in Lisp code
2807 emitted by @code{translate_file} are affected by @code{tr_array_as_ref}.
2808 When @code{tr_array_as_ref} is @code{true},
2809 array names are evaluated,
2810 otherwise array names appear as literal symbols in translated code.
2812 @code{tr_array_as_ref} has no effect if @code{translate_fast_arrays} is
2815 @opencatbox{Categories:}
2816 @category{Translation flags and variables}
2820 @c WHY IS THIS FLAG NEEDED ??? UNDER WHAT CIRCUMSTANCES CAN TRANSLATION
2821 @c OF A BOUND VARIABLE USED AS A FUNCTION GO WRONG ???
2823 @c -----------------------------------------------------------------------------
2824 @anchor{tr_bound_function_applyp}
2825 @defvr {Option variable} tr_bound_function_applyp
2826 Default value: @code{true}
2828 When @code{tr_bound_function_applyp} is @code{true} and @code{tr_function_call_default}
2829 is @code{general}, if a bound variable (such as a function argument) is found being
2830 used as a function then Maxima will rewrite that function call using @code{apply} and
2831 print a warning message.
2833 For example, if @code{g} is defined by @code{g(f,x) := f(x+1)} then translating
2834 @code{g} will cause Maxima to print a warning and rewrite @code{f(x+1)} as
2835 @code{apply(f,[x+1])}.
2840 @c tr_bound_function_applyp : true$
2842 @c g (lambda ([x], x));
2843 @c tr_bound_function_applyp : false$
2845 @c g (lambda ([x], x));
2849 (%i2) g (f) := f (3)$
2850 (%i3) tr_bound_function_applyp : true$
2852 (%i4) translate (g)$
2853 warning: f is a bound variable in f(3), but it is used as a function.
2854 note: instead I'll translate it as: apply(f,[3])
2857 (%i5) g (lambda ([x], x));
2860 (%i6) tr_bound_function_applyp : false$
2861 (%i7) translate (g)$
2863 (%i8) g (lambda ([x], x));
2868 @opencatbox{Categories:}
2869 @category{Translation flags and variables}
2873 @c -----------------------------------------------------------------------------
2874 @anchor{tr_file_tty_messagesp}
2875 @defvr {Option variable} tr_file_tty_messagesp
2876 Default value: @code{false}
2878 When @code{tr_file_tty_messagesp} is @code{true}, messages generated by
2879 @code{translate_file} during translation of a file are displayed on the console
2880 and inserted into the UNLISP file. When @code{false}, messages about
2881 translation of the file are only inserted into the UNLISP file.
2883 @opencatbox{Categories:}
2884 @category{Translation flags and variables}
2888 @c -----------------------------------------------------------------------------
2889 @anchor{tr_float_can_branch_complex}
2890 @defvr {Option variable} tr_float_can_branch_complex
2891 Default value: @code{true}
2893 Tells the Maxima-to-Lisp translator to assume that the functions
2894 @code{acos}, @code{asin}, @code{asec}, @code{acsc}, @code{acosh},
2895 @code{asech}, @code{atanh}, @code{acoth}, @code{log} and @code{sqrt}
2896 can return complex results.
2898 When it is @code{true} then @code{acos(x)} is of mode @code{any}
2899 even if @code{x} is of mode @code{float} (as set by @code{mode_declare}).
2900 When @code{false} then @code{acos(x)} is of mode
2901 @code{float} if and only if @code{x} is of mode @code{float}.
2903 @opencatbox{Categories:}
2904 @category{Translation flags and variables}
2908 @c -----------------------------------------------------------------------------
2909 @anchor{tr_function_call_default}
2910 @defvr {Option variable} tr_function_call_default
2911 Default value: @code{general}
2913 @code{false} means give up and call @code{meval}, @code{expr} means assume Lisp
2914 fixed arg function. @code{general}, the default gives code good for
2915 @code{mexprs} and @code{mlexprs} but not @code{macros}. @code{general} assures
2916 variable bindings are correct in compiled code. In @code{general} mode, when
2917 translating F(X), if F is a bound variable, then it assumes that
2918 @code{apply (f, [x])} is meant, and translates a such, with appropriate warning.
2919 There is no need to turn this off. With the default settings, no warning
2920 messages implies full compatibility of translated and compiled code with the
2923 @opencatbox{Categories:}
2924 @category{Translation flags and variables}
2928 @c -----------------------------------------------------------------------------
2930 @defvr {Option variable} tr_numer
2931 Default value: @code{false}
2933 When @code{tr_numer} is @code{true}, @code{numer} properties are used for
2934 atoms which have them, e.g. @code{%pi}.
2936 @opencatbox{Categories:}
2937 @category{Translation flags and variables}
2941 @c -----------------------------------------------------------------------------
2942 @anchor{tr_optimize_max_loop}
2943 @defvr {Option variable} tr_optimize_max_loop
2946 @code{tr_optimize_max_loop} is the maximum number of times the
2947 macro-expansion and optimization pass of the translator will loop in
2948 considering a form. This is to catch macro expansion errors, and
2949 non-terminating optimization properties.
2951 @opencatbox{Categories:}
2952 @category{Translation flags and variables}
2956 @c ARE ANY OF THESE OBSOLETE ??
2958 @c -----------------------------------------------------------------------------
2959 @anchor{tr_state_vars}
2960 @defvr {System variable} tr_state_vars
2963 [translate_fast_arrays, tr_function_call_default, tr_bound_function_applyp,
2964 tr_array_as_ref, tr_numer, tr_float_can_branch_complex, define_variable]
2967 The list of the switches that affect the form of the
2969 @c DOES THE GENERAL USER REALLY CARE ABOUT DEBUGGING THE TRANSLATOR ???
2971 This information is useful to system people when
2972 trying to debug the translator. By comparing the translated product
2973 to what should have been produced for a given state, it is possible to
2976 @opencatbox{Categories:}
2977 @category{Translation flags and variables}
2981 @c tr_warnings_get EXISTS AND FUNCTIONS AS ADVERTISED (SORT OF) -- RETURNS *tr-runtime-warned*
2982 @c WHICH HAS ONLY A FEW KINDS OF WARNINGS PUSHED ONTO IT; IT'S CERTAINLY NOT COMPREHENSIVE
2983 @c DO WE REALLY NEED THIS SLIGHTLY WORKING FUNCTION ??
2985 @c -----------------------------------------------------------------------------
2986 @anchor{tr_warnings_get}
2987 @deffn {Function} tr_warnings_get ()
2989 Prints a list of warnings which have been given by
2990 the translator during the current translation.
2992 @opencatbox{Categories:}
2993 @category{Translation and compilation}
2997 @c -----------------------------------------------------------------------------
2998 @defvr {Option variable} tr_warn_bad_function_calls
2999 Default value: @code{true}
3001 - Gives a warning when
3002 when function calls are being made which may not be correct due to
3003 improper declarations that were made at translate time.
3005 @opencatbox{Categories:}
3006 @category{Translation flags and variables}
3010 @c -----------------------------------------------------------------------------
3011 @anchor{tr_warn_fexpr}
3012 @defvr {Option variable} tr_warn_fexpr
3013 Default value: @code{compfile}
3015 - Gives a warning if any FEXPRs are
3016 encountered. FEXPRs should not normally be output in translated code,
3017 all legitimate special program forms are translated.
3019 @opencatbox{Categories:}
3020 @category{Translation flags and variables}
3024 @c -----------------------------------------------------------------------------
3025 @anchor{tr_warn_meval}
3026 @defvr {Option variable} tr_warn_meval
3027 Default value: @code{compfile}
3029 - Gives a warning if the function @code{meval} gets called. If @code{meval} is
3030 called that indicates problems in the translation.
3032 @opencatbox{Categories:}
3033 @category{Translation flags and variables}
3037 @c -----------------------------------------------------------------------------
3038 @anchor{tr_warn_mode}
3039 @defvr {Option variable} tr_warn_mode
3040 Default value: @code{all}
3042 - Gives a warning when variables are
3043 assigned values inappropriate for their mode.
3045 @opencatbox{Categories:}
3046 @category{Translation flags and variables}
3050 @c -----------------------------------------------------------------------------
3051 @anchor{tr_warn_undeclared}
3052 @defvr {Option variable} tr_warn_undeclared
3053 Default value: @code{compile}
3055 - Determines when to send
3056 warnings about undeclared variables to the TTY.
3058 @opencatbox{Categories:}
3059 @category{Translation flags and variables}
3063 @c -----------------------------------------------------------------------------
3064 @anchor{tr_warn_undefined_variable}
3065 @defvr {Option variable} tr_warn_undefined_variable
3066 Default value: @code{all}
3068 - Gives a warning when
3069 undefined global variables are seen.
3071 @opencatbox{Categories:}
3072 @category{Translation flags and variables}
3076 @c -----------------------------------------------------------------------------
3077 @anchor{compile_file}
3078 @deffn {Function} compile_file @
3079 @fname{compile_file} (@var{filename}) @
3080 @fname{compile_file} (@var{filename}, @var{compiled_filename}) @
3081 @fname{compile_file} (@var{filename}, @var{compiled_filename}, @var{lisp_filename})
3083 Translates the Maxima file @var{filename} into Lisp, and executes the Lisp compiler.
3084 The compiled code is not loaded into Maxima.
3086 @code{compile_file} returns a list of the names of four files: the original
3087 Maxima file, the Lisp translation, notes on translation, and the compiled code.
3088 If the compilation fails, the fourth item is @code{false}.
3090 Some declarations and definitions take effect as soon
3091 as the Lisp code is compiled (without loading the compiled code).
3092 These include functions defined with the @code{:=} operator,
3093 macros define with the @code{::=} operator,
3094 @c HEDGE -- DON'T KNOW IF THERE IS ANOTHER WAY
3095 @code{alias}, @code{declare},
3096 @code{define_variable}, @code{mode_declare},
3098 @code{infix}, @code{matchfix},
3099 @code{nofix}, @code{postfix}, @code{prefix},
3100 and @code{compfile}.
3102 Assignments and function calls are not evaluated until the compiled code is
3103 loaded. In particular, within the Maxima file, assignments to the translation
3104 flags (@code{tr_numer}, etc.) have no effect on the translation.
3106 @c @code{compile_file} may mistake warnings for errors and
3107 @c return @code{false} as the name of the compiled code when, in fact,
3108 @c the compilation succeeded. This is a bug.
3109 @c REPORTED AS SOURCEFORGE BUG # 1103722.
3111 @var{filename} may not contain @code{:lisp} statements.
3113 @code{compile_file} evaluates its arguments.
3115 @opencatbox{Categories:}
3116 @category{Translation and compilation}
3120 @c NEEDS CLARIFICATION
3122 @c -----------------------------------------------------------------------------
3123 @anchor{declare_translated}
3124 @deffn {Function} declare_translated (@var{f_1}, @var{f_2}, @dots{})
3126 When translating a file of Maxima code
3127 to Lisp, it is important for the translator to know which functions it
3128 sees in the file are to be called as translated or compiled functions,
3129 and which ones are just Maxima functions or undefined. Putting this
3130 declaration at the top of the file, lets it know that although a symbol
3131 does which does not yet have a Lisp function value, will have one at
3132 call time. @code{(MFUNCTION-CALL fn arg1 arg2 ...)} is generated when
3133 the translator does not know @code{fn} is going to be a Lisp function.
3135 @opencatbox{Categories:}
3136 @category{Translation and compilation}