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{x_1}, @dots{}, @var{x_n}])
647 Constructs and evaluates an expression @code{@var{F}(@var{arg_1}, ...,
650 @code{apply} does not attempt to distinguish a @mref{memoizing function} from an ordinary
651 function; when @var{F} is the name of a memoizing function, @code{apply} evaluates
652 @code{@var{F}(...)} (that is, a function call with parentheses instead of square
653 brackets). @code{arrayapply} evaluates a function call with square brackets in
656 See also @mref{funmake} and @mrefdot{args}
660 @code{apply} evaluates its arguments.
661 In this example, @code{min} is applied to the value of @code{L}.
664 @c L : [1, 5, -10.2, 4, 3];
669 (%i1) L : [1, 5, -10.2, 4, 3];
670 (%o1) [1, 5, - 10.2, 4, 3]
673 (%i2) apply (min, L);
678 @code{apply} evaluates arguments, even if the function @var{F} quotes them.
681 @c F (x) := x / 1729;
685 @c apply (dispfun, [fname]);
689 (%i1) F (x) := x / 1729;
707 (%i4) dispfun (fname);
708 fundef: no such function: fname
709 -- an error. To debug this try: debugmode(true);
712 (%i5) apply (dispfun, [fname]);
721 @code{apply} evaluates the function name @var{F}.
722 Single quote @code{'} defeats evaluation.
723 @code{demoivre} is the name of a global variable and also a function.
727 @c demoivre (exp (%i * x));
728 @c apply (demoivre, [exp (%i * x)]);
729 @c apply ('demoivre, [exp (%i * x)]);
737 (%i2) demoivre (exp (%i * x));
738 (%o2) %i sin(x) + cos(x)
741 (%i3) apply (demoivre, [exp (%i * x)]);
742 apply: found false where a function was expected.
743 -- an error. To debug this try: debugmode(true);
746 (%i4) apply ('demoivre, [exp (%i * x)]);
747 (%o4) %i sin(x) + cos(x)
751 How to convert a nested list into a matrix:
759 (%i1) a:[[1,2],[3,4]];
760 (%o1) [[1, 2], [3, 4]]
763 (%i2) apply(matrix,a);
771 @opencatbox{Categories:}
772 @category{Function application}
776 @c -----------------------------------------------------------------------------
778 @deffn {Function} block @
779 @fname{block} ([@var{v_1}, @dots{}, @var{v_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
780 @fname{block} (@var{expr_1}, @dots{}, @var{expr_n})
782 The function @code{block} allows to make the variables @var{v_1}, @dots{},
783 @var{v_m} to be local for a sequence of commands. If these variables
784 are already bound @code{block} saves the current values of the
785 variables @var{v_1}, @dots{}, @var{v_m} (if any) upon entry to the
786 block, then unbinds the variables so that they evaluate to themselves;
787 The local variables may be bound to arbitrary values within the block
788 but when the block is exited the saved values are restored, and the
789 values assigned within the block are lost.
791 If there is no need to define local variables then the list at the
792 beginning of the @code{block} command may be omitted.
793 In this case if neither @mref{return} nor @mref{go} are used
794 @code{block} behaves similar to the following construct:
797 ( expr_1, expr_2,... , expr_n );
800 @var{expr_1}, @dots{}, @var{expr_n} will be evaluated in sequence and
801 the value of the last expression will be returned. The sequence can be
802 modified by the @code{go}, @code{throw}, and @code{return} functions. The last
803 expression is @var{expr_n} unless @code{return} or an expression containing
804 @code{throw} is evaluated.
806 The declaration @code{local(@var{v_1}, ..., @var{v_m})} within @code{block}
807 saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_m},
808 removes any properties before evaluating other expressions, and restores any
809 saved properties on exit from the block. Some declarations are implemented as
810 properties of a symbol, including @code{:=}, @code{array}, @code{dependencies},
811 @code{atvalue}, @code{matchdeclare}, @code{atomgrad}, @code{constant},
812 @code{nonscalar}, @code{assume}, and some others. The effect of @code{local}
813 is to make such declarations effective only within the block; otherwise
814 declarations within a block are actually global declarations.
816 @code{block} may appear within another @code{block}.
817 Local variables are established each time a new @code{block} is evaluated.
818 Local variables appear to be global to any enclosed blocks.
819 If a variable is non-local in a block,
820 its value is the value most recently assigned by an enclosing block, if any,
821 otherwise, it is the value of the variable in the global environment.
822 This policy may coincide with the usual understanding of "dynamic scope".
824 The value of the block is the value of the last statement or the
825 value of the argument to the function @code{return} which may be used to exit
826 explicitly from the block. The function @code{go} may be used to transfer
827 control to the statement of the block that is tagged with the argument
828 to @code{go}. To tag a statement, precede it by an atomic argument as
829 another statement in the block. For example:
830 @code{block ([x], x:1, loop, x: x+1, ..., go(loop), ...)}. The argument to
831 @code{go} must be the name of a tag appearing within the block. One cannot use
832 @code{go} to transfer to a tag in a block other than the one containing the
835 Blocks typically appear on the right side of a function definition
836 but can be used in other places as well.
838 See also @mref{return} and @mrefdot{go}
840 @c Needs some examples.
842 @opencatbox{Categories:}
843 @category{Expressions}
844 @category{Programming}
848 @c REPHRASE, NEEDS EXAMPLE
850 @c -----------------------------------------------------------------------------
852 @deffn {Function} break (@var{expr_1}, @dots{}, @var{expr_n})
854 Evaluates and prints @var{expr_1}, @dots{}, @var{expr_n} and then
855 causes a Maxima break at which point the user can examine and change
856 his environment. Upon typing @code{exit;} the computation resumes.
858 @opencatbox{Categories:}
863 @c FOR SOME REASON throw IS IN SOME OTHER FILE. MOVE throw INTO THIS FILE.
864 @c NEEDS CLARIFICATION
866 @c -----------------------------------------------------------------------------
868 @deffn {Function} catch (@var{expr_1}, @dots{}, @var{expr_n})
870 Evaluates @var{expr_1}, @dots{}, @var{expr_n} one by one; if any
871 leads to the evaluation of an expression of the
872 form @code{throw (arg)}, then the value of the @code{catch} is the value of
873 @code{throw (arg)}, and no further expressions are evaluated.
874 This "non-local return" thus goes through any depth of
875 nesting to the nearest enclosing @code{catch}. If there is no @code{catch}
876 enclosing a @code{throw}, an error message is printed.
878 If the evaluation of the arguments does not lead to the evaluation of any
879 @code{throw} then the value of @code{catch} is the value of @var{expr_n}.
882 @c lambda ([x], if x < 0 then throw(x) else f(x))$
883 @c g(l) := catch (map (''%, l))$
885 @c g ([1, 2, -3, 7]);
888 (%i1) lambda ([x], if x < 0 then throw(x) else f(x))$
889 (%i2) g(l) := catch (map (''%, l))$
890 (%i3) g ([1, 2, 3, 7]);
891 (%o3) [f(1), f(2), f(3), f(7)]
892 (%i4) g ([1, 2, -3, 7]);
897 The function @code{g} returns a list of @code{f} of each element of @code{l} if
898 @code{l} consists only of non-negative numbers; otherwise, @code{g} "catches"
899 the first negative element of @code{l} and "throws" it up.
901 @opencatbox{Categories:}
902 @category{Programming}
906 @c -----------------------------------------------------------------------------
908 @deffn {Function} compfile @
909 @fname{compfile} (@var{filename}, @var{f_1}, @dots{}, @var{f_n}) @
910 @fname{compfile} (@var{filename}, functions) @
911 @fname{compfile} (@var{filename}, all)
913 Translates Maxima functions into Lisp and writes the translated code into the
916 @code{compfile(@var{filename}, @var{f_1}, ..., @var{f_n})} translates the
917 specified functions. @code{compfile (@var{filename}, functions)} and
918 @code{compfile (@var{filename}, all)} translate all user-defined functions.
920 The Lisp translations are not evaluated, nor is the output file processed by
922 @c SO LET'S CONSIDER GIVING THIS FUNCTION A MORE ACCURATE NAME.
923 @code{translate} creates and evaluates Lisp translations. @code{compile_file}
924 translates Maxima into Lisp, and then executes the Lisp compiler.
926 See also @mrefcomma{translate} @mrefcomma{translate_file} and @mrefdot{compile_file}
928 @opencatbox{Categories:}
929 @category{Translation and compilation}
933 @c THIS VARIABLE IS OBSOLETE: ASSIGNING compgrind: true CAUSES compfile
934 @c TO EVENTUALLY CALL AN OBSOLETE FUNCTION SPRIN1.
935 @c RECOMMENDATION IS TO CUT THIS ITEM, AND CUT $compgrind FROM src/transs.lisp
937 @c Default value: @code{false}
939 @c When @code{compgrind} is @code{true}, function definitions printed by
940 @c @code{compfile} are pretty-printed.
944 @c -----------------------------------------------------------------------------
946 @deffn {Function} compile @
947 @fname{compile} (@var{f_1}, @dots{}, @var{f_n}) @
948 @fname{compile} (functions) @
949 @fname{compile} (all)
951 Translates Maxima functions @var{f_1}, @dots{}, @var{f_n} into Lisp, evaluates
952 the Lisp translations, and calls the Lisp function @code{COMPILE} on each
953 translated function. @code{compile} returns a list of the names of the
956 @code{compile (all)} or @code{compile (functions)} compiles all user-defined
959 @code{compile} quotes its arguments;
960 the quote-quote operator @code{'@w{}'} defeats quotation.
962 Compiling a function to native code can mean a big increase in speed and might
963 cause the memory footprint to reduce drastically.
964 Code tends to be especially effective when the flexibility it needs to provide
965 is limited. If compilation doesn't provide the speed that is needed a few ways
966 to limit the code's functionality are the following:
968 @item If the function accesses global variables the complexity of the function
969 can be drastically be reduced by limiting these variables to one data type,
970 for example using @mref{mode_declare} or a statement like the following one:
971 @code{put(x_1, bigfloat, numerical_type)}
972 @item The compiler might warn about undeclared variables if text could either be
973 a named option to a command or (if they are assigned a value to) the name
974 of a variable. Prepending the option with a single quote @code{'}
975 tells the compiler that the text is meant as an option.
978 @opencatbox{Categories:}
979 @category{Translation and compilation}
983 @c -----------------------------------------------------------------------------
985 @deffn {Function} define @
986 @fname{define} (@var{f}(@var{x_1}, @dots{}, @var{x_n}), @var{expr}) @
987 @fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}], @var{expr}) @
988 @fname{define} (@var{f}[@var{x_1}, @dots{}, @var{x_n}](@var{y_1}, @dots{}, @var{y_m}), @var{expr}) @
989 @fname{define} (funmake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
990 @fname{define} (arraymake (@var{f}, [@var{x_1}, @dots{}, @var{x_n}]), @var{expr}) @
991 @fname{define} (ev (@var{expr_1}), @var{expr_2})
993 Defines a function named @var{f} with arguments @var{x_1}, @dots{}, @var{x_n}
994 and function body @var{expr}. @code{define} always evaluates its second
995 argument (unless explicitly quoted). The function so defined may be an ordinary
996 Maxima function (with arguments enclosed in parentheses) or a @mref{memoizing function}
997 (with arguments enclosed in square brackets).
999 When the last or only function argument @var{x_n} is a list of one element,
1000 the function defined by @code{define} accepts a variable number of arguments.
1001 Actual arguments are assigned one-to-one to formal arguments @var{x_1}, @dots{},
1002 @var{x_(n - 1)}, and any further actual arguments, if present, are assigned to
1003 @var{x_n} as a list.
1005 When the first argument of @code{define} is an expression of the form
1006 @code{@var{f}(@var{x_1}, ..., @var{x_n})} or @code{@var{f}[@var{x_1}, ...,
1007 @var{x_n}]}, the function arguments are evaluated but @var{f} is not evaluated,
1008 even if there is already a function or variable by that name.
1010 When the first argument is an expression with operator @code{funmake},
1011 @code{arraymake}, or @code{ev}, the first argument is evaluated;
1012 this allows for the function name to be computed, as well as the body.
1014 All function definitions appear in the same namespace; defining a function
1015 @code{f} within another function @code{g} does not automatically limit the scope
1016 of @code{f} to @code{g}. However, @code{local(f)} makes the definition of
1017 function @code{f} effective only within the block or other compound expression
1018 in which @code{local} appears.
1020 If some formal argument @var{x_k} is a quoted symbol (after evaluation), the
1021 function defined by @code{define} does not evaluate the corresponding actual
1022 argument. Otherwise all actual arguments are evaluated.
1024 See also @mref{:=} and @mrefdot{::=}
1028 @code{define} always evaluates its second argument (unless explicitly quoted).
1031 @c expr : cos(y) - sin(x);
1032 @c define (F1 (x, y), expr);
1034 @c F2 (x, y) := expr;
1039 (%i1) expr : cos(y) - sin(x);
1040 (%o1) cos(y) - sin(x)
1043 (%i2) define (F1 (x, y), expr);
1044 (%o2) F1(x, y) := cos(y) - sin(x)
1048 (%o3) cos(b) - sin(a)
1051 (%i4) F2 (x, y) := expr;
1052 (%o4) F2(x, y) := expr
1056 (%o5) cos(y) - sin(x)
1060 The function defined by @code{define} may be an ordinary Maxima function or a
1061 @mref{memoizing function}.
1064 @c define (G1 (x, y), x.y - y.x);
1065 @c define (G2 [x, y], x.y - y.x);
1069 (%i1) define (G1 (x, y), x.y - y.x);
1070 (%o1) G1(x, y) := x . y - y . x
1073 (%i2) define (G2 [x, y], x.y - y.x);
1074 (%o2) G2 := x . y - y . x
1079 When the last or only function argument @var{x_n} is a list of one element,
1080 the function defined by @code{define} accepts a variable number of arguments.
1083 @c define (H ([L]), '(apply ("+", L)));
1088 (%i1) define (H ([L]), '(apply ("+", L)));
1089 (%o1) H([L]) := apply("+", L)
1097 When the first argument is an expression with operator @code{funmake},
1098 @code{arraymake}, or @code{ev}, the first argument is evaluated.
1102 @c funmake (F, [u]);
1103 @c define (funmake (F, [u]), cos(u) + 1);
1104 @c define (arraymake (F, [u]), cos(u) + 1);
1105 @c define (foo (x, y), bar (y, x));
1106 @c define (ev (foo (x, y)), sin(x) - cos(y));
1110 (%i1) [F : I, u : x];
1114 (%i2) funmake (F, [u]);
1118 (%i3) define (funmake (F, [u]), cos(u) + 1);
1119 (%o3) I(x) := cos(x) + 1
1122 (%i4) define (arraymake (F, [u]), cos(u) + 1);
1123 (%o4) I := cos(x) + 1
1127 (%i5) define (foo (x, y), bar (y, x));
1128 (%o5) foo(x, y) := bar(y, x)
1131 (%i6) define (ev (foo (x, y)), sin(x) - cos(y));
1132 (%o6) bar(y, x) := sin(x) - cos(y)
1136 @opencatbox{Categories:}
1137 @category{Function definition}
1141 @c SEE NOTE BELOW ABOUT THE DOCUMENTATION STRING
1142 @c @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode}, @var{documentation})
1144 @c -----------------------------------------------------------------------------
1145 @anchor{define_variable}
1146 @deffn {Function} define_variable (@var{name}, @var{default_value}, @var{mode})
1148 Introduces a global variable into the Maxima environment.
1149 @code{define_variable} is useful in user-written packages, which are often
1150 translated or compiled as it gives the compiler hints of the type (``mode'')
1151 of a variable and therefore avoids requiring it to generate generic code that
1152 can deal with every variable being an integer, float, maxima object, array etc.
1154 @code{define_variable} carries out the following steps:
1158 @code{mode_declare (@var{name}, @var{mode})} declares the mode (``type'') of
1159 @var{name} to the translator which can considerably speed up compiled code as
1160 it allows having to create generic code. See @mref{mode_declare} for a list of
1164 If the variable is unbound, @var{default_value} is assigned to @var{name}.
1167 Associates @var{name} with a test function
1168 to ensure that @var{name} is only assigned values of the declared mode.
1172 @c FOLLOWING STATEMENT APPEARS TO BE OUT OF DATE.
1173 @c EXAMINING DEFMSPEC $DEFINE_VARIABLE AND DEF%TR $DEFINE_VARIABLE IN src/trmode.lisp,
1174 @c IT APPEARS THAT THE 4TH ARGUMENT IS NEVER REFERRED TO.
1175 @c EXECUTING translate_file ON A MAXIMA BATCH FILE WHICH CONTAINS
1176 @c define_variable (foo, 2222, integer, "THIS IS FOO");
1177 @c DOES NOT PUT "THIS IS FOO" INTO THE LISP FILE NOR THE UNLISP FILE.
1178 @c The optional 4th argument is a documentation string. When
1179 @c @code{translate_file} is used on a package which includes documentation
1180 @c strings, a second file is output in addition to the Lisp file which
1181 @c will contain the documentation strings, formatted suitably for use in
1182 @c manuals, usage files, or (for instance) @code{describe}.
1184 The @code{value_check} property can be assigned to any variable which has been
1185 defined via @code{define_variable} with a mode other than @code{any}.
1186 The @code{value_check} property is a lambda expression or the name of a function
1187 of one variable, which is called when an attempt is made to assign a value to
1188 the variable. The argument of the @code{value_check} function is the would-be
1191 @code{define_variable} evaluates @code{default_value}, and quotes @code{name}
1192 and @code{mode}. @code{define_variable} returns the current value of
1193 @code{name}, which is @code{default_value} if @code{name} was unbound before,
1194 and otherwise it is the previous value of @code{name}.
1198 @code{foo} is a Boolean variable, with the initial value @code{true}.
1201 @c define_variable (foo, true, boolean);
1209 (%i1) define_variable (foo, true, boolean);
1222 translator: foo was declared with mode boolean
1223 , but it has value: %pi
1224 -- an error. To debug this try: debugmode(true);
1232 @code{bar} is an integer variable, which must be prime.
1235 @c define_variable (bar, 2, integer);
1236 @c qput (bar, prime_test, value_check);
1237 @c prime_test (y) := if not primep(y) then
1238 @c error (y, "is not prime.");
1245 (%i1) define_variable (bar, 2, integer);
1249 (%i2) qput (bar, prime_test, value_check);
1253 (%i3) prime_test (y) := if not primep(y) then
1254 error (y, "is not prime.");
1255 (%o3) prime_test(y) := if not primep(y)
1256 then error(y, "is not prime.")
1265 #0: prime_test(y=1440)
1266 -- an error. To debug this try: debugmode(true);
1274 @code{baz_quux} is a variable which cannot be assigned a value.
1275 The mode @code{any_check} is like @code{any}, but @code{any_check} enables the
1276 @code{value_check} mechanism, and @code{any} does not.
1279 @c define_variable (baz_quux, 'baz_quux, any_check);
1280 @c F: lambda ([y], if y # 'baz_quux then
1281 @c error ("Cannot assign to `baz_quux'."));
1282 @c qput (baz_quux, ''F, value_check);
1283 @c baz_quux: 'baz_quux;
1284 @c baz_quux: sqrt(2);
1289 (%i1) define_variable (baz_quux, 'baz_quux, any_check);
1293 (%i2) F: lambda ([y], if y # 'baz_quux then
1294 error ("Cannot assign to `baz_quux'."));
1295 (%o2) lambda([y], if y # 'baz_quux
1296 then error(Cannot assign to `baz_quux'.))
1299 (%i3) qput (baz_quux, ''F, value_check);
1300 (%o3) lambda([y], if y # 'baz_quux
1301 then error(Cannot assign to `baz_quux'.))
1304 (%i4) baz_quux: 'baz_quux;
1308 (%i5) baz_quux: sqrt(2);
1309 Cannot assign to `baz_quux'.
1310 #0: lambda([y],if y # 'baz_quux then
1311 error("Cannot assign to `baz_quux'."))(y=sqrt(2))
1312 -- an error. To debug this try: debugmode(true);
1320 @opencatbox{Categories:}
1321 @category{Translation and compilation}
1325 @c -----------------------------------------------------------------------------
1327 @deffn {Function} dispfun @
1328 @fname{dispfun} (@var{f_1}, @dots{}, @var{f_n}) @
1329 @fname{dispfun} (all)
1331 Displays the definition of the user-defined functions @var{f_1}, @dots{},
1332 @var{f_n}. Each argument may be the name of a macro (defined with @code{::=}),
1333 an ordinary function (defined with @code{:=} or @code{define}), an array
1334 function (defined with @code{:=} or @code{define}, but enclosing arguments in
1335 square brackets @code{[ ]}), a subscripted function (defined with @code{:=} or
1336 @code{define}, but enclosing some arguments in square brackets and others in
1337 parentheses @code{( )}), one of a family of subscripted functions selected by a
1338 particular subscript value, or a subscripted function defined with a constant
1341 @code{dispfun (all)} displays all user-defined functions as
1342 given by the @code{functions}, @code{arrays}, and @code{macros} lists,
1343 omitting subscripted functions defined with constant subscripts.
1345 @code{dispfun} creates an intermediate expression label
1346 (@code{%t1}, @code{%t2}, etc.)
1347 for each displayed function, and assigns the function definition to the label.
1348 In contrast, @code{fundef} returns the function definition.
1350 @code{dispfun} quotes its arguments; the quote-quote operator @code{'@w{}'}
1351 defeats quotation. @code{dispfun} returns the list of intermediate expression
1352 labels corresponding to the displayed functions.
1357 @c m(x, y) ::= x^(-y);
1358 @c f(x, y) := x^(-y);
1359 @c g[x, y] := x^(-y);
1360 @c h[x](y) := x^(-y);
1361 @c i[8](y) := 8^(-y);
1362 @c dispfun (m, f, g, h, h[5], h[10], i[8]);
1367 (%i1) m(x, y) ::= x^(-y);
1372 (%i2) f(x, y) := x^(-y);
1377 (%i3) g[x, y] := x^(-y);
1383 (%i4) h[x](y) := x^(-y);
1389 (%i5) i[8](y) := 8^(-y);
1395 (%i6) dispfun (m, f, g, h, h[5], h[10], i[8]);
1424 (%o12) [%t6, %t7, %t8, %t9, %t10, %t11, %t12]
1429 (%o13) [m(x, y) ::= x , f(x, y) := x , g := x ,
1432 h (y) := x , h (y) := --, h (y) := ---, i (y) := 8 ]
1438 @opencatbox{Categories:}
1439 @category{Function definition}
1440 @category{Display functions}
1444 @c -----------------------------------------------------------------------------
1446 @deffn {Function} fullmap (@var{f}, @var{expr_1}, @dots{})
1448 Similar to @code{map}, but @code{fullmap} keeps mapping down all subexpressions
1449 until the main operators are no longer the same.
1451 @code{fullmap} is used by the Maxima simplifier for certain matrix
1452 manipulations; thus, Maxima sometimes generates an error message concerning
1453 @code{fullmap} even though @code{fullmap} was not explicitly called by the user.
1468 (%i2) fullmap (g, %);
1469 (%o2) g(b) g(c) + g(a)
1472 (%i3) map (g, %th(2));
1477 @opencatbox{Categories:}
1478 @category{Function application}
1479 @category{Expressions}
1483 @c -----------------------------------------------------------------------------
1485 @deffn {Function} fullmapl (@var{f}, @var{list_1}, @dots{})
1487 Similar to @code{fullmap}, but @code{fullmapl} only maps onto lists and
1493 @c fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
1497 (%i1) fullmapl ("+", [3, [4, 5]], [[a, 1], [0, -1.5]]);
1498 (%o1) [[a + 3, 4], [4, 3.5]]
1502 @opencatbox{Categories:}
1503 @category{Function application}
1504 @category{Expressions}
1508 @c -----------------------------------------------------------------------------
1510 @defvr {System variable} functions
1511 Default value: @code{[]}
1513 @code{functions} is the list of ordinary Maxima functions
1514 in the current session.
1515 An ordinary function is a function constructed by
1516 @code{define} or @code{:=} and called with parentheses @code{()}.
1517 A function may be defined at the Maxima prompt
1518 or in a Maxima file loaded by @code{load} or @code{batch}.
1520 @mref{Memoizing functions} (called with square brackets, e.g., @code{F[x]}) and subscripted
1521 functions (called with square brackets and parentheses, e.g., @code{F[x](y)})
1522 are listed by the global variable @code{arrays}, and not by @code{functions}.
1524 Lisp functions are not kept on any list.
1529 @c F_1 (x) := x - 100;
1530 @c F_2 (x, y) := x / y;
1531 @c define (F_3 (x), sqrt (x));
1532 @c G_1 [x] := x - 100;
1533 @c G_2 [x, y] := x / y;
1534 @c define (G_3 [x], sqrt (x));
1535 @c H_1 [x] (y) := x^y;
1541 (%i1) F_1 (x) := x - 100;
1542 (%o1) F_1(x) := x - 100
1545 (%i2) F_2 (x, y) := x / y;
1547 (%o2) F_2(x, y) := -
1551 (%i3) define (F_3 (x), sqrt (x));
1552 (%o3) F_3(x) := sqrt(x)
1555 (%i4) G_1 [x] := x - 100;
1556 (%o4) G_1 := x - 100
1560 (%i5) G_2 [x, y] := x / y;
1566 (%i6) define (G_3 [x], sqrt (x));
1567 (%o6) G_3 := sqrt(x)
1571 (%i7) H_1 [x] (y) := x^y;
1578 (%o8) [F_1(x), F_2(x, y), F_3(x)]
1582 (%o9) [G_1, G_2, G_3, H_1]
1586 @opencatbox{Categories:}
1587 @category{Function definition}
1588 @category{Global variables}
1592 @c -----------------------------------------------------------------------------
1594 @deffn {Function} fundef (@var{f})
1596 Returns the definition of the function @var{f}.
1600 @item the name of a macro (defined with @code{::=}),
1601 @item an ordinary function (defined with @code{:=} or @code{define}),
1602 @item a @mref{memoizing function} (defined with @code{:=} or @code{define}, but enclosing arguments in square brackets @code{[ ]}),
1603 @item a subscripted function (defined with @code{:=} or @code{define},
1604 but enclosing some arguments in square brackets and others in parentheses
1606 @item one of a family of subscripted functions selected by a particular
1608 @item or a subscripted function defined with a constant subscript.
1611 @code{fundef} quotes its argument;
1612 the quote-quote operator @code{'@w{}'} defeats quotation.
1614 @code{fundef (@var{f})} returns the definition of @var{f}.
1615 In contrast, @code{dispfun (@var{f})} creates an intermediate expression label
1616 and assigns the definition to the label.
1618 @c PROBABLY NEED SOME EXAMPLES HERE
1619 @opencatbox{Categories:}
1620 @category{Function definition}
1624 @c -----------------------------------------------------------------------------
1626 @deffn {Function} funmake (@var{F}, [@var{arg_1}, @dots{}, @var{arg_n}])
1628 Returns an expression @code{@var{F}(@var{arg_1}, ..., @var{arg_n})}.
1629 The return value is simplified, but not evaluated,
1630 so the function @var{F} is not called, even if it exists.
1632 @code{funmake} does not attempt to distinguish @mref{memoizing functions} from ordinary
1633 functions; when @var{F} is the name of a memoizing function,
1634 @code{funmake} returns @code{@var{F}(...)}
1635 (that is, a function call with parentheses instead of square brackets).
1636 @code{arraymake} returns a function call with square brackets in this case.
1638 @code{funmake} evaluates its arguments.
1640 See also @mref{apply} and @mrefdot{args}
1644 @code{funmake} applied to an ordinary Maxima function.
1647 @c F (x, y) := y^2 - x^2;
1648 @c funmake (F, [a + 1, b + 1]);
1653 (%i1) F (x, y) := y^2 - x^2;
1655 (%o1) F(x, y) := y - x
1658 (%i2) funmake (F, [a + 1, b + 1]);
1659 (%o2) F(a + 1, b + 1)
1664 (%o3) (b + 1) - (a + 1)
1668 @code{funmake} applied to a macro.
1671 @c G (x) ::= (x - 1)/2;
1672 @c funmake (G, [u]);
1677 (%i1) G (x) ::= (x - 1)/2;
1679 (%o1) G(x) ::= -----
1683 (%i2) funmake (G, [u]);
1694 @code{funmake} applied to a subscripted function.
1697 @c H [a] (x) := (x - 1)^a;
1698 @c funmake (H [n], [%e]);
1700 @c funmake ('(H [n]), [%e]);
1705 (%i1) H [a] (x) := (x - 1)^a;
1707 (%o1) H (x) := (x - 1)
1711 (%i2) funmake (H [n], [%e]);
1713 (%o2) lambda([x], (x - 1) )(%e)
1721 (%i4) funmake ('(H [n]), [%e]);
1732 @code{funmake} applied to a symbol which is not a defined function of any kind.
1735 @c funmake (A, [u]);
1740 (%i1) funmake (A, [u]);
1749 @code{funmake} evaluates its arguments, but not the return value.
1752 @c det(a,b,c) := b^2 -4*a*c;
1753 @c (x : 8, y : 10, z : 12);
1755 @c funmake (f, [x, y, z]);
1760 (%i1) det(a,b,c) := b^2 -4*a*c;
1762 (%o1) det(a, b, c) := b - 4 a c
1765 (%i2) (x : 8, y : 10, z : 12);
1773 (%i4) funmake (f, [x, y, z]);
1774 (%o4) det(8, 10, 12)
1782 Maxima simplifies @code{funmake}'s return value.
1785 @c funmake (sin, [%pi / 2]);
1789 (%i1) funmake (sin, [%pi / 2]);
1794 @opencatbox{Categories:}
1795 @category{Function application}
1796 @category{Expressions}
1800 @c -----------------------------------------------------------------------------
1802 @deffn {Function} lambda @
1803 @fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}], @var{expr_1}, @dots{}, @var{expr_n}) @
1804 @fname{lambda} ([[@var{L}]], @var{expr_1}, @dots{}, @var{expr_n}) @
1805 @fname{lambda} ([@var{x_1}, @dots{}, @var{x_m}, [@var{L}]], @var{expr_1}, @dots{}, @var{expr_n})
1807 Defines and returns a lambda expression (that is, an anonymous function).
1808 The function may have required arguments @var{x_1}, @dots{}, @var{x_m} and/or
1809 optional arguments @var{L}, which appear within the function body as a list.
1810 The return value of the function is @var{expr_n}. A lambda expression can be
1811 assigned to a variable and evaluated like an ordinary function. A lambda
1812 expression may appear in some contexts in which a function name is expected.
1814 When the function is evaluated, unbound local variables @var{x_1}, @dots{},
1815 @var{x_m} are created. @code{lambda} may appear within @code{block} or another
1816 @code{lambda}; local variables are established each time another @code{block} or
1817 @code{lambda} is evaluated. Local variables appear to be global to any enclosed
1818 @code{block} or @code{lambda}. If a variable is not local, its value is the
1819 value most recently assigned in an enclosing @code{block} or @code{lambda}, if
1820 any, otherwise, it is the value of the variable in the global environment.
1821 This policy may coincide with the usual understanding of "dynamic scope".
1823 After local variables are established, @var{expr_1} through @var{expr_n} are
1824 evaluated in turn. The special variable @code{%%}, representing the value of
1825 the preceding expression, is recognized. @code{throw} and @code{catch} may also
1826 appear in the list of expressions.
1828 @code{return} cannot appear in a lambda expression unless enclosed by
1829 @code{block}, in which case @code{return} defines the return value of the block
1830 and not of the lambda expression, unless the block happens to be @var{expr_n}.
1831 Likewise, @code{go} cannot appear in a lambda expression unless enclosed by
1834 @code{lambda} quotes its arguments;
1835 the quote-quote operator @code{'@w{}'} defeats quotation.
1841 A lambda expression can be assigned to a variable and evaluated like an ordinary
1846 @c f: lambda ([x], x^2);
1851 (%i1) f: lambda ([x], x^2);
1853 (%o1) lambda([x], x )
1864 A lambda expression may appear in contexts in which a function evaluation is expected.
1868 @c lambda ([x], x^2) (a);
1869 @c apply (lambda ([x], x^2), [a]);
1870 @c map (lambda ([x], x^2), [a, b, c, d, e]);
1874 (%i1) lambda ([x], x^2) (a);
1879 (%i2) apply (lambda ([x], x^2), [a]);
1884 (%i3) map (lambda ([x], x^2), [a, b, c, d, e]);
1886 (%o3) [a , b , c , d , e ]
1892 Argument variables are local variables.
1893 Other variables appear to be global variables.
1894 Global variables are evaluated at the time the lambda expression is evaluated,
1895 unless some special evaluation is forced by some means, such as @code{'@w{}'}.
1901 @c g: lambda ([a], a*b);
1904 @c g2: lambda ([a], a*''b);
1912 (%i3) g: lambda ([a], a*b);
1913 (%o3) lambda([a], a b)
1923 (%i6) g2: lambda ([a], a*''b);
1924 (%o6) lambda([a], a %gamma)
1937 Lambda expressions may be nested. Local variables within the outer lambda
1938 expression appear to be global to the inner expression unless masked by local
1939 variables of the same names.
1943 @c h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1948 (%i1) h: lambda ([a, b], h2: lambda ([a], a*b), h2(1/2));
1950 (%o1) lambda([a, b], h2 : lambda([a], a b), h2(-))
1954 (%i2) h(%pi, %gamma);
1963 Since @code{lambda} quotes its arguments, lambda expression @code{i} below does
1964 not define a "multiply by @code{a}" function. Such a function can be defined
1965 via @code{buildq}, as in lambda expression @code{i2} below.
1969 @c i: lambda ([a], lambda ([x], a*x));
1971 @c i2: lambda([a], buildq([a: a], lambda([x], a*x)));
1977 (%i1) i: lambda ([a], lambda ([x], a*x));
1978 (%o1) lambda([a], lambda([x], a x))
1982 (%o2) lambda([x], a x)
1985 (%i3) i2: lambda([a], buildq([a: a], lambda([x], a*x)));
1986 (%o3) lambda([a], buildq([a : a], lambda([x], a x)))
1991 (%o4) lambda([x], (-) x)
2004 A lambda expression may take a variable number of arguments,
2005 which are indicated by @code{[@var{L}]} as the sole or final argument.
2006 The arguments appear within the function body as a list.
2010 @c f : lambda ([aa, bb, [cc]], aa * cc + bb);
2011 @c f (foo, %i, 17, 29, 256);
2012 @c g : lambda ([[aa]], apply ("+", aa));
2013 @c g (17, 29, x, y, z, %e);
2017 (%i1) f : lambda ([aa, bb, [cc]], aa * cc + bb);
2018 (%o1) lambda([aa, bb, [cc]], aa cc + bb)
2021 (%i2) f (foo, %i, 17, 29, 256);
2022 (%o2) [17 foo + %i, 29 foo + %i, 256 foo + %i]
2025 (%i3) g : lambda ([[aa]], apply ("+", aa));
2026 (%o3) lambda([[aa]], apply(+, aa))
2029 (%i4) g (17, 29, x, y, z, %e);
2030 (%o4) z + y + x + %e + 46
2034 @opencatbox{Categories:}
2035 @category{Function definition}
2039 @c NEEDS CLARIFICATION AND EXAMPLES
2041 @c -----------------------------------------------------------------------------
2043 @deffn {Function} local (@var{v_1}, @dots{}, @var{v_n})
2045 Saves the properties associated with the symbols @var{v_1}, @dots{}, @var{v_n},
2046 removes any properties before evaluating other expressions,
2047 and restores any saved properties on exit
2048 from the block or other compound expression in which @code{local} appears.
2050 Some declarations are implemented as properties of a symbol, including
2051 @code{:=}, @code{array}, @code{dependencies}, @code{atvalue},
2052 @code{matchdeclare}, @code{atomgrad}, @code{constant}, @code{nonscalar},
2053 @code{assume}, and some others. The effect of @code{local} is to make such
2054 declarations effective only within the block or other compound expression in
2055 which @code{local} appears; otherwise such declarations are global declarations.
2057 @code{local} can only appear in @code{block}
2058 or in the body of a function definition or @code{lambda} expression,
2059 and only one occurrence is permitted in each.
2061 @code{local} quotes its arguments.
2062 @code{local} returns @code{done}.
2066 A local function definition.
2069 @c foo (x) := 1 - x;
2071 @c block (local (foo), foo (x) := 2 * x, foo (100));
2076 (%i1) foo (x) := 1 - x;
2077 (%o1) foo(x) := 1 - x
2084 (%i3) block (local (foo), foo (x) := 2 * x, foo (100));
2093 @opencatbox{Categories:}
2094 @category{Function definition}
2095 @category{Programming}
2099 @c -----------------------------------------------------------------------------
2100 @anchor{macroexpansion}
2101 @defvr {Option variable} macroexpansion
2102 Default value: @code{false}
2104 @code{macroexpansion} controls whether the expansion (that is, the return value)
2105 of a macro function is substituted for the macro function call.
2106 A substitution may speed up subsequent expression evaluations,
2107 at the cost of storing the expansion.
2111 The expansion of a macro function is not substituted for the macro function call.
2113 The first time a macro function call is evaluated,
2114 the expansion is stored.
2115 The expansion is not recomputed on subsequent calls;
2116 any side effects (such as @code{print} or assignment to global variables) happen
2117 only when the macro function call is first evaluated.
2118 Expansion in an expression does not affect other expressions
2119 which have the same macro function call.
2121 The first time a macro function call is evaluated,
2122 the expansion is substituted for the call,
2123 thus modifying the expression from which the macro function was called.
2124 The expansion is not recomputed on subsequent calls;
2125 any side effects happen only when the macro function call is first evaluated.
2126 Expansion in an expression does not affect other expressions
2127 which have the same macro function call.
2132 When @code{macroexpansion} is @code{false},
2133 a macro function is called every time the calling expression is evaluated,
2134 and the calling expression is not modified.
2137 @c f (x) := h (x) / g (x);
2138 @c g (x) ::= block (print ("x + 99 is equal to", x),
2139 @c return (x + 99));
2140 @c h (x) ::= block (print ("x - 99 is equal to", x),
2141 @c return (x - 99));
2142 @c macroexpansion: false;
2149 (%i1) f (x) := h (x) / g (x);
2155 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2157 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2161 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2163 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2167 (%i4) macroexpansion: false;
2172 x - 99 is equal to x
2173 x + 99 is equal to x
2188 x - 99 is equal to x
2189 x + 99 is equal to x
2196 When @code{macroexpansion} is @code{expand},
2197 a macro function is called once,
2198 and the calling expression is not modified.
2201 @c f (x) := h (x) / g (x);
2202 @c g (x) ::= block (print ("x + 99 is equal to", x),
2203 @c return (x + 99));
2204 @c h (x) ::= block (print ("x - 99 is equal to", x),
2205 @c return (x - 99));
2206 @c macroexpansion: expand;
2213 (%i1) f (x) := h (x) / g (x);
2219 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2221 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2225 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2227 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2231 (%i4) macroexpansion: expand;
2236 x - 99 is equal to x
2237 x + 99 is equal to x
2244 mmacroexpanded(x - 99, h(x))
2245 (%t6) f(x) := ----------------------------
2246 mmacroexpanded(x + 99, g(x))
2258 When @code{macroexpansion} is @code{displace},
2259 a macro function is called once,
2260 and the calling expression is modified.
2263 @c f (x) := h (x) / g (x);
2264 @c g (x) ::= block (print ("x + 99 is equal to", x),
2265 @c return (x + 99));
2266 @c h (x) ::= block (print ("x - 99 is equal to", x),
2267 @c return (x - 99));
2268 @c macroexpansion: displace;
2275 (%i1) f (x) := h (x) / g (x);
2281 (%i2) g (x) ::= block (print ("x + 99 is equal to", x),
2283 (%o2) g(x) ::= block(print("x + 99 is equal to", x),
2287 (%i3) h (x) ::= block (print ("x - 99 is equal to", x),
2289 (%o3) h(x) ::= block(print("x - 99 is equal to", x),
2293 (%i4) macroexpansion: displace;
2298 x - 99 is equal to x
2299 x + 99 is equal to x
2307 (%t6) f(x) := ------
2320 @opencatbox{Categories:}
2321 @category{Function application}
2322 @category{Global flags}
2326 @c -----------------------------------------------------------------------------
2327 @anchor{mode_declare}
2328 @anchor{modedeclare}
2329 @deffn {Function} mode_declare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
2330 @deffnx {Function} modedeclare (@var{y_1}, @var{mode_1}, @dots{}, @var{y_n}, @var{mode_n})
2332 A @code{mode_declare} informs the compiler which type (lisp programmers name the type:
2333 ``mode'') a function parameter or its return value will be of. This can greatly
2334 boost the efficiency of the code the compiler generates: Without knowing the type of
2335 all variables and knowing the return value of all functions a function uses
2336 in advance very generic (and thus potentially slow) code needs to be generated.
2338 The arguments of @code{mode_declare} are pairs consisting of a variable (or a list
2339 of variables all having the same mode) and a mode. Available modes (``types'') are:
2341 array an declared array (see the detailed description below)
2342 boolean true or false
2343 integer integers (including arbitrary-size integers)
2344 fixnum integers (excluding arbitrary-size integers)
2345 float machine-size floating-point numbers
2346 real machine-size floating-point or integer
2348 any any kind of object (useful for arrays of any)
2351 A function parameter named @code{a} can be declared as an array filled with elements
2352 of the type @code{t} the following way:
2354 mode_declare (a, array(t, dim1, dim2, ...))
2356 If none of the elements of the array @code{a} needs to be checked if it still doesn't
2357 contain a value additional code can be omitted by declaring this fact, too:
2359 mode_declare (a, array (t, complete, dim1, dim2, ...))
2361 The @code{complete} has no effect if all array elements are of the type
2362 @code{fixnum} or @code{float}: Machine-sized numbers inevitably contain a value
2363 (and will automatically be initialized to 0 in most lisp implementations).
2365 Another way to tell that all entries of the array @code{a} are of the type
2366 (``mode'') @code{m} and have been assigned a value to would be:
2368 mode_declare (completearray (a), m))
2371 Numeric code using arrays might run faster still if the size of the array is
2372 known at compile time, as well, as in:
2374 mode_declare (completearray (a [10, 10]), float)
2376 for a floating point number array named @code{a} which is 10 x 10.
2378 @code{mode_declare} also can be used in order to declare the type of the result
2379 of a function. In this case the function compilation needs to be preceded by
2380 another @code{mode_declare} statement. For example the expression,
2382 mode_declare ([function (f_1, f_2, ...)], fixnum)
2384 declares that the values returned by @code{f_1}, @code{f_2}, @dots{} are
2385 single-word integers.
2387 @code{modedeclare} is a synonym for @code{mode_declare}.
2389 If the type of function parameters and results doesn't match the declaration by
2390 @code{mode_declare} the function may misbehave or a warning or an error might
2391 occur, see @mrefcomma{mode_checkp} @mref{mode_check_errorp} and
2392 @mrefdot{mode_check_warnp}
2394 See @mref{mode_identity} for declaring the type of lists and @mref{define_variable} for
2395 declaring the type of all global variables compiled code uses, as well.
2399 @c square_float(f):=(
2400 @c mode_declare(f,float),
2403 @c mode_declare([function(f)],float);
2404 @c compile(square_float);
2405 @c square_float(100.0);
2409 (%i1) square_float(f):=(
2410 mode_declare(f,float),
2413 (%o1) square_float(f) := (mode_declare(f, float), f f)
2416 (%i2) mode_declare([function(f)],float);
2417 (%o2) [[function(f)]]
2420 (%i3) compile(square_float);
2421 (%o3) [square_float]
2424 (%i4) square_float(100.0);
2430 @opencatbox{Categories:}
2431 @category{Translation and compilation}
2435 @c NEEDS MORE EXAMPLES?
2437 @c -----------------------------------------------------------------------------
2438 @anchor{mode_checkp}
2439 @defvr {Option variable} mode_checkp
2440 Default value: @code{true}
2442 When @code{mode_checkp} is @code{true}, @mref{mode_declare} does not only define
2443 which type a variable will be of so the compiler can generate more efficient code,
2444 but will also create a runtime warning if the variable isn't of the variable type
2445 the code was compiled to deal with.
2448 @c mode_checkp:true;
2450 @c mode_declare(f,float),
2458 (%i1) mode_checkp:true;
2463 mode_declare(f,float),
2466 (%o2) square(f) := (mode_declare(f, float), f )
2469 (%i3) compile(square);
2474 (%o4) 5.289999999999999
2478 Maxima encountered a Lisp error:
2486 Automatically continuing.
2487 To enable the Lisp debugger set *debugger-hook* to nil.
2491 @opencatbox{Categories:}
2492 @category{Translation flags and variables}
2496 @c -----------------------------------------------------------------------------
2497 @anchor{mode_check_errorp}
2498 @defvr {Option variable} mode_check_errorp
2499 Default value: @code{false}
2501 When @code{mode_check_errorp} is @code{true}, @code{mode_declare} calls
2503 @c NEED SOME EXAMPLES HERE.
2505 @opencatbox{Categories:}
2506 @category{Translation flags and variables}
2510 @c -----------------------------------------------------------------------------
2511 @anchor{mode_check_warnp}
2512 @defvr {Option variable} mode_check_warnp
2513 Default value: @code{true}
2515 @c WHAT DOES THIS MEAN ??
2516 When @code{mode_check_warnp} is @code{true}, mode errors are
2518 @c NEED SOME EXAMPLES HERE.
2520 @opencatbox{Categories:}
2521 @category{Translation flags and variables}
2525 @c NEEDS AN EXAMPLE FOR DECLARING THE RETURN TYPE
2527 @c -----------------------------------------------------------------------------
2528 @anchor{mode_identity}
2529 @deffn {Function} mode_identity (@var{arg_1}, @var{arg_2})
2531 @code{mode_identity} works similar to @mref{mode_declare}, but is used for
2532 informing the compiler that a thing like a @code{macro} or a list operation
2533 will only return a specific type of object. The purpose of doing so is that
2534 maxima supports many objects: Machine integers, arbitrary length integers,
2535 equations, machine floats, big floats, which means that for everything that
2536 deals with return values of operations that can result in any object the
2537 compiler needs to output generic (and therefore potentially slow) code.
2539 The first argument to @code{mode_identity} is the type of return value
2540 something will return (for possible types see @mref{mode_declare}).
2541 (i.e., one of @code{float}, @code{fixnum}, @code{number},
2542 The second argument is the expression that will return an object of this
2545 If the the return value of this expression is of a type the code was not
2546 compiled for error or warning is signalled.
2547 @c ARE THE MODE_DECLARE VARIABLES FOR TURNING OFF THIS ERROR OR WARNING
2548 @c EFFECTIVE HERE, TOO?
2550 If you knew that @code{first (l)} returned a number then you could write
2553 @code{mode_identity (number, first (l))}.
2555 However, if you need this construct more often it would be more efficient
2556 to define a function that returns a number fist:
2558 firstnumb (x) ::= buildq ([x], mode_identity (number, first(x)));
2561 @code{firstnumb} now can be used every time you need the first element
2562 of a list that is guaranteed to be filled with numbers.
2563 @opencatbox{Categories:}
2564 @category{Translation and compilation}
2568 @c -----------------------------------------------------------------------------
2569 @anchor{remfunction}
2570 @deffn {Function} remfunction @
2571 @fname{remfunction} (@var{f_1}, @dots{}, @var{f_n}) @
2572 @fname{remfunction} (all)
2574 Unbinds the function definitions of the symbols @var{f_1}, @dots{}, @var{f_n}.
2575 The arguments may be the names of ordinary functions (created by @mref{:=} or
2576 @mref{define}) or macro functions (created by @mref{::=}).
2578 @code{remfunction (all)} unbinds all function definitions.
2580 @code{remfunction} quotes its arguments.
2582 @code{remfunction} returns a list of the symbols for which the function
2583 definition was unbound. @code{false} is returned in place of any symbol for
2584 which there is no function definition.
2586 @code{remfunction} does not apply to @mref{memoizing functions} or subscripted functions.
2587 @mref{remarray} applies to those types of functions.
2589 @opencatbox{Categories:}
2590 @category{Function definition}
2594 @c NEEDS MORE WORK !!!
2596 @c -----------------------------------------------------------------------------
2598 @defvr {Option variable} savedef
2599 Default value: @code{true}
2601 When @code{savedef} is @code{true}, the Maxima version of a user function is
2602 preserved when the function is translated. This permits the definition to be
2603 displayed by @code{dispfun} and allows the function to be edited.
2605 When @code{savedef} is @code{false}, the names of translated functions are
2606 removed from the @code{functions} list.
2608 @opencatbox{Categories:}
2609 @category{Translation flags and variables}
2613 @c -----------------------------------------------------------------------------
2615 @deffn {Function} translate @
2616 @fname{translate} (@var{f_1}, @dots{}, @var{f_n}) @
2617 @fname{translate} (functions) @
2618 @fname{translate} (all)
2620 Translates the user-defined functions @var{f_1}, @dots{}, @var{f_n} from the
2621 Maxima language into Lisp and evaluates the Lisp translations.
2622 Typically the translated functions run faster than the originals.
2624 @code{translate (all)} or @code{translate (functions)} translates all
2625 user-defined functions.
2627 Functions to be translated should include a call to @code{mode_declare} at the
2628 beginning when possible in order to produce more efficient code. For example:
2631 f (x_1, x_2, ...) := block ([v_1, v_2, ...],
2632 mode_declare (v_1, mode_1, v_2, mode_2, ...), ...)
2636 where the @var{x_1}, @var{x_2}, @dots{} are the parameters to the function and
2637 the @var{v_1}, @var{v_2}, @dots{} are the local variables.
2639 The names of translated functions are removed from the @code{functions} list
2640 if @code{savedef} is @code{false} (see below) and are added to the @code{props}
2643 Functions should not be translated unless they are fully debugged.
2645 Expressions are assumed simplified; if they are not, correct but non-optimal
2646 code gets generated. Thus, the user should not set the @code{simp} switch to
2647 @code{false} which inhibits simplification of the expressions to be translated.
2649 The switch @code{translate}, if @code{true}, causes automatic
2650 translation of a user's function to Lisp.
2652 Note that translated
2653 functions may not run identically to the way they did before
2654 translation as certain incompatibilities may exist between the Lisp
2655 and Maxima versions. Principally, the @code{rat} function with more than
2656 one argument and the @code{ratvars} function should not be used if any
2657 variables are @code{mode_declare}'d canonical rational expressions (CRE).
2658 Also the @code{prederror: false} setting
2660 @c WHAT ABOUT % AND %% ???
2662 @code{savedef} - if @code{true} will cause the Maxima version of a user
2663 function to remain when the function is @code{translate}'d. This permits the
2664 definition to be displayed by @code{dispfun} and allows the function to be
2667 @code{transrun} - if @code{false} will cause the interpreted version of all
2668 functions to be run (provided they are still around) rather than the
2671 The result returned by @code{translate} is a list of the names of the
2672 functions translated.
2674 @opencatbox{Categories:}
2675 @category{Translation and compilation}
2679 @c -----------------------------------------------------------------------------
2680 @anchor{translate_file}
2681 @deffn {Function} translate_file @
2682 @fname{translate_file} (@var{maxima_filename}) @
2683 @fname{translate_file} (@var{maxima_filename}, @var{lisp_filename})
2685 Translates a file of Maxima code into a file of Lisp code.
2686 @code{translate_file} returns a list of three filenames:
2687 the name of the Maxima file, the name of the Lisp file, and the name of file
2688 containing additional information about the translation.
2689 @code{translate_file} evaluates its arguments.
2691 @code{translate_file ("foo.mac"); load("foo.LISP")} is the same as the command
2692 @code{batch ("foo.mac")} except for certain restrictions, the use of
2693 @code{'@w{}'} and @code{%}, for example.
2694 @c FIGURE OUT WHAT THE RESTRICTIONS ARE AND STATE THEM
2696 @code{translate_file (@var{maxima_filename})} translates a Maxima file
2697 @var{maxima_filename} into a similarly-named Lisp file.
2698 For example, @code{foo.mac} is translated into @code{foo.LISP}.
2699 The Maxima filename may include a directory name or names,
2700 in which case the Lisp output file is written
2701 to the same directory from which the Maxima input comes.
2703 @code{translate_file (@var{maxima_filename}, @var{lisp_filename})} translates
2704 a Maxima file @var{maxima_filename} into a Lisp file @var{lisp_filename}.
2705 @code{translate_file} ignores the filename extension, if any, of
2706 @code{lisp_filename}; the filename extension of the Lisp output file is always
2707 @code{LISP}. The Lisp filename may include a directory name or names,
2708 in which case the Lisp output file is written to the specified directory.
2710 @code{translate_file} also writes a file of translator warning
2711 messages of various degrees of severity.
2712 The filename extension of this file is @code{UNLISP}.
2713 This file may contain valuable information, though possibly obscure,
2714 for tracking down bugs in translated code.
2715 The @code{UNLISP} file is always written
2716 to the same directory from which the Maxima input comes.
2718 @code{translate_file} emits Lisp code which causes
2719 some declarations and definitions to take effect as soon
2720 as the Lisp code is compiled.
2721 See @code{compile_file} for more on this topic.
2723 @c CHECK ALL THESE AND SEE WHICH ONES ARE OBSOLETE
2726 @code{tr_array_as_ref}
2727 @c tr_bind_mode_hook EXISTS BUT IT APPEARS TO BE A GROTESQUE UNDOCUMENTED HACK
2728 @c WE DON'T WANT TO MENTION IT
2729 @c @code{tr_bind_mode_hook},
2730 @mrefcomma{tr_bound_function_applyp}
2731 @c tr_exponent EXISTS AND WORKS AS ADVERTISED IN src/troper.lisp
2732 @c NOT OTHERWISE DOCUMENTED; ITS EFFECT SEEMS TOO WEAK TO MENTION
2734 @mrefcomma{tr_file_tty_messagesp}
2735 @mrefcomma{tr_float_can_branch_complex}
2736 @mrefcomma{tr_function_call_default}
2737 @mrefcomma{tr_numer}
2738 @mrefcomma{tr_optimize_max_loop}
2739 @mrefcomma{tr_state_vars}
2740 @mrefcomma{tr_warnings_get}
2742 @code{tr_warn_bad_function_calls}
2743 @mrefcomma{tr_warn_fexpr}
2744 @mrefcomma{tr_warn_meval}
2745 @mrefcomma{tr_warn_mode}
2746 @mrefcomma{tr_warn_undeclared}
2747 and @mrefdot{tr_warn_undefined_variable}
2750 @opencatbox{Categories:}
2751 @category{Translation and compilation}
2755 @c -----------------------------------------------------------------------------
2757 @defvr {Option variable} transrun
2758 Default value: @code{true}
2760 When @code{transrun} is @code{false} will cause the interpreted
2761 version of all functions to be run (provided they are still around)
2762 rather than the translated version.
2764 @opencatbox{Categories:}
2765 @category{Translation flags and variables}
2769 @c IN WHAT CONTEXT IS tr_array_as_ref: false APPROPRIATE ??? NOT SEEING THE USEFULNESS HERE.
2770 @c ALSO, I GUESS WE SHOULD HAVE AN ITEM FOR translate_fast_arrays, ANOTHER CONFUSING FLAG ...
2772 @c -----------------------------------------------------------------------------
2773 @anchor{tr_array_as_ref}
2774 @defvr {Option variable} tr_array_as_ref
2775 Default value: @code{true}
2777 If @code{translate_fast_arrays} is @code{false}, array references in Lisp code
2778 emitted by @code{translate_file} are affected by @code{tr_array_as_ref}.
2779 When @code{tr_array_as_ref} is @code{true},
2780 array names are evaluated,
2781 otherwise array names appear as literal symbols in translated code.
2783 @code{tr_array_as_ref} has no effect if @code{translate_fast_arrays} is
2786 @opencatbox{Categories:}
2787 @category{Translation flags and variables}
2791 @c WHY IS THIS FLAG NEEDED ??? UNDER WHAT CIRCUMSTANCES CAN TRANSLATION
2792 @c OF A BOUND VARIABLE USED AS A FUNCTION GO WRONG ???
2794 @c -----------------------------------------------------------------------------
2795 @anchor{tr_bound_function_applyp}
2796 @defvr {Option variable} tr_bound_function_applyp
2797 Default value: @code{true}
2799 When @code{tr_bound_function_applyp} is @code{true} and @code{tr_function_call_default}
2800 is @code{general}, if a bound variable (such as a function argument) is found being
2801 used as a function then Maxima will rewrite that function call using @code{apply} and
2802 print a warning message.
2804 For example, if @code{g} is defined by @code{g(f,x) := f(x+1)} then translating
2805 @code{g} will cause Maxima to print a warning and rewrite @code{f(x+1)} as
2806 @code{apply(f,[x+1])}.
2811 @c tr_bound_function_applyp : true$
2813 @c g (lambda ([x], x));
2814 @c tr_bound_function_applyp : false$
2816 @c g (lambda ([x], x));
2820 (%i2) g (f) := f (3)$
2821 (%i3) tr_bound_function_applyp : true$
2823 (%i4) translate (g)$
2824 warning: f is a bound variable in f(3), but it is used as a function.
2825 note: instead I'll translate it as: apply(f,[3])
2828 (%i5) g (lambda ([x], x));
2831 (%i6) tr_bound_function_applyp : false$
2832 (%i7) translate (g)$
2834 (%i8) g (lambda ([x], x));
2839 @opencatbox{Categories:}
2840 @category{Translation flags and variables}
2844 @c -----------------------------------------------------------------------------
2845 @anchor{tr_file_tty_messagesp}
2846 @defvr {Option variable} tr_file_tty_messagesp
2847 Default value: @code{false}
2849 When @code{tr_file_tty_messagesp} is @code{true}, messages generated by
2850 @code{translate_file} during translation of a file are displayed on the console
2851 and inserted into the UNLISP file. When @code{false}, messages about
2852 translation of the file are only inserted into the UNLISP file.
2854 @opencatbox{Categories:}
2855 @category{Translation flags and variables}
2859 @c -----------------------------------------------------------------------------
2860 @anchor{tr_float_can_branch_complex}
2861 @defvr {Option variable} tr_float_can_branch_complex
2862 Default value: @code{true}
2864 Tells the Maxima-to-Lisp translator to assume that the functions
2865 @code{acos}, @code{asin}, @code{asec}, @code{acsc}, @code{acosh},
2866 @code{asech}, @code{atanh}, @code{acoth}, @code{log} and @code{sqrt}
2867 can return complex results.
2869 When it is @code{true} then @code{acos(x)} is of mode @code{any}
2870 even if @code{x} is of mode @code{float} (as set by @code{mode_declare}).
2871 When @code{false} then @code{acos(x)} is of mode
2872 @code{float} if and only if @code{x} is of mode @code{float}.
2874 @opencatbox{Categories:}
2875 @category{Translation flags and variables}
2879 @c -----------------------------------------------------------------------------
2880 @anchor{tr_function_call_default}
2881 @defvr {Option variable} tr_function_call_default
2882 Default value: @code{general}
2884 @code{false} means give up and call @code{meval}, @code{expr} means assume Lisp
2885 fixed arg function. @code{general}, the default gives code good for
2886 @code{mexprs} and @code{mlexprs} but not @code{macros}. @code{general} assures
2887 variable bindings are correct in compiled code. In @code{general} mode, when
2888 translating F(X), if F is a bound variable, then it assumes that
2889 @code{apply (f, [x])} is meant, and translates a such, with appropriate warning.
2890 There is no need to turn this off. With the default settings, no warning
2891 messages implies full compatibility of translated and compiled code with the
2894 @opencatbox{Categories:}
2895 @category{Translation flags and variables}
2899 @c -----------------------------------------------------------------------------
2901 @defvr {Option variable} tr_numer
2902 Default value: @code{false}
2904 When @code{tr_numer} is @code{true}, @code{numer} properties are used for
2905 atoms which have them, e.g. @code{%pi}.
2907 @opencatbox{Categories:}
2908 @category{Translation flags and variables}
2912 @c -----------------------------------------------------------------------------
2913 @anchor{tr_optimize_max_loop}
2914 @defvr {Option variable} tr_optimize_max_loop
2917 @code{tr_optimize_max_loop} is the maximum number of times the
2918 macro-expansion and optimization pass of the translator will loop in
2919 considering a form. This is to catch macro expansion errors, and
2920 non-terminating optimization properties.
2922 @opencatbox{Categories:}
2923 @category{Translation flags and variables}
2927 @c ARE ANY OF THESE OBSOLETE ??
2929 @c -----------------------------------------------------------------------------
2930 @anchor{tr_state_vars}
2931 @defvr {System variable} tr_state_vars
2934 [translate_fast_arrays, tr_function_call_default, tr_bound_function_applyp,
2935 tr_array_as_ref, tr_numer, tr_float_can_branch_complex, define_variable]
2938 The list of the switches that affect the form of the
2940 @c DOES THE GENERAL USER REALLY CARE ABOUT DEBUGGING THE TRANSLATOR ???
2942 This information is useful to system people when
2943 trying to debug the translator. By comparing the translated product
2944 to what should have been produced for a given state, it is possible to
2947 @opencatbox{Categories:}
2948 @category{Translation flags and variables}
2952 @c tr_warnings_get EXISTS AND FUNCTIONS AS ADVERTISED (SORT OF) -- RETURNS *tr-runtime-warned*
2953 @c WHICH HAS ONLY A FEW KINDS OF WARNINGS PUSHED ONTO IT; IT'S CERTAINLY NOT COMPREHENSIVE
2954 @c DO WE REALLY NEED THIS SLIGHTLY WORKING FUNCTION ??
2956 @c -----------------------------------------------------------------------------
2957 @anchor{tr_warnings_get}
2958 @deffn {Function} tr_warnings_get ()
2960 Prints a list of warnings which have been given by
2961 the translator during the current translation.
2963 @opencatbox{Categories:}
2964 @category{Translation and compilation}
2968 @c -----------------------------------------------------------------------------
2969 @defvr {Option variable} tr_warn_bad_function_calls
2970 Default value: @code{true}
2972 - Gives a warning when
2973 when function calls are being made which may not be correct due to
2974 improper declarations that were made at translate time.
2976 @opencatbox{Categories:}
2977 @category{Translation flags and variables}
2981 @c -----------------------------------------------------------------------------
2982 @anchor{tr_warn_fexpr}
2983 @defvr {Option variable} tr_warn_fexpr
2984 Default value: @code{compfile}
2986 - Gives a warning if any FEXPRs are
2987 encountered. FEXPRs should not normally be output in translated code,
2988 all legitimate special program forms are translated.
2990 @opencatbox{Categories:}
2991 @category{Translation flags and variables}
2995 @c -----------------------------------------------------------------------------
2996 @anchor{tr_warn_meval}
2997 @defvr {Option variable} tr_warn_meval
2998 Default value: @code{compfile}
3000 - Gives a warning if the function @code{meval} gets called. If @code{meval} is
3001 called that indicates problems in the translation.
3003 @opencatbox{Categories:}
3004 @category{Translation flags and variables}
3008 @c -----------------------------------------------------------------------------
3009 @anchor{tr_warn_mode}
3010 @defvr {Option variable} tr_warn_mode
3011 Default value: @code{all}
3013 - Gives a warning when variables are
3014 assigned values inappropriate for their mode.
3016 @opencatbox{Categories:}
3017 @category{Translation flags and variables}
3021 @c -----------------------------------------------------------------------------
3022 @anchor{tr_warn_undeclared}
3023 @defvr {Option variable} tr_warn_undeclared
3024 Default value: @code{compile}
3026 - Determines when to send
3027 warnings about undeclared variables to the TTY.
3029 @opencatbox{Categories:}
3030 @category{Translation flags and variables}
3034 @c -----------------------------------------------------------------------------
3035 @anchor{tr_warn_undefined_variable}
3036 @defvr {Option variable} tr_warn_undefined_variable
3037 Default value: @code{all}
3039 - Gives a warning when
3040 undefined global variables are seen.
3042 @opencatbox{Categories:}
3043 @category{Translation flags and variables}
3047 @c -----------------------------------------------------------------------------
3048 @anchor{compile_file}
3049 @deffn {Function} compile_file @
3050 @fname{compile_file} (@var{filename}) @
3051 @fname{compile_file} (@var{filename}, @var{compiled_filename}) @
3052 @fname{compile_file} (@var{filename}, @var{compiled_filename}, @var{lisp_filename})
3054 Translates the Maxima file @var{filename} into Lisp, and executes the Lisp compiler.
3055 The compiled code is not loaded into Maxima.
3057 @code{compile_file} returns a list of the names of four files: the original
3058 Maxima file, the Lisp translation, notes on translation, and the compiled code.
3059 If the compilation fails, the fourth item is @code{false}.
3061 Some declarations and definitions take effect as soon
3062 as the Lisp code is compiled (without loading the compiled code).
3063 These include functions defined with the @code{:=} operator,
3064 macros define with the @code{::=} operator,
3065 @c HEDGE -- DON'T KNOW IF THERE IS ANOTHER WAY
3066 @code{alias}, @code{declare},
3067 @code{define_variable}, @code{mode_declare},
3069 @code{infix}, @code{matchfix},
3070 @code{nofix}, @code{postfix}, @code{prefix},
3071 and @code{compfile}.
3073 Assignments and function calls are not evaluated until the compiled code is
3074 loaded. In particular, within the Maxima file, assignments to the translation
3075 flags (@code{tr_numer}, etc.) have no effect on the translation.
3077 @c @code{compile_file} may mistake warnings for errors and
3078 @c return @code{false} as the name of the compiled code when, in fact,
3079 @c the compilation succeeded. This is a bug.
3080 @c REPORTED AS SOURCEFORGE BUG # 1103722.
3082 @var{filename} may not contain @code{:lisp} statements.
3084 @code{compile_file} evaluates its arguments.
3086 @opencatbox{Categories:}
3087 @category{Translation and compilation}
3091 @c NEEDS CLARIFICATION
3093 @c -----------------------------------------------------------------------------
3094 @anchor{declare_translated}
3095 @deffn {Function} declare_translated (@var{f_1}, @var{f_2}, @dots{})
3097 When translating a file of Maxima code
3098 to Lisp, it is important for the translator to know which functions it
3099 sees in the file are to be called as translated or compiled functions,
3100 and which ones are just Maxima functions or undefined. Putting this
3101 declaration at the top of the file, lets it know that although a symbol
3102 does which does not yet have a Lisp function value, will have one at
3103 call time. @code{(MFUNCTION-CALL fn arg1 arg2 ...)} is generated when
3104 the translator does not know @code{fn} is going to be a Lisp function.
3106 @opencatbox{Categories:}
3107 @category{Translation and compilation}