Yesterday, Peter Vafeades asked about a limitation on the number of function
arguments allowed by the min and max functions. James Amundson responded
that Maxima compiled with gcl limits this number, but Maxima compiled with
cmucl or clisp doesn't. I don't know how to eliminate this gcl
restriction, but for the max and min functions, I propose the following
work around. (Off-list, Peter told me that he was computing
apply(max,[x1,...xn]) and not max(x1,x2,..,xn); nevertheless, I suspect
the problem is still with gcl/maxima's limitation on the number of function
arguments.)
---------start of file my_max.lisp ---------------
;; If x = [x1,x2,x3,...,xn], return max(xn, max( .. max(x3, max(x1,x2))..).
;; Signal an error if either x is the empty list or if x isn't a list.
(defun $lmax (x)
(cond (($listp x)
(setq x (rest x))
(cond ((null x)
(merror "Error: argument to lmax must be nonempty."))
(t
(reduce #'$max x))))
(t
(merror "Error: argument to lmax must be a list."))))
;; If x = [x1,x2,x3,...,xn], return min(xn, min( .. min(x3, min(x1,x2))..).
;; Signal an error if either x is the empty list or if x isn't a list.
(defun $lmin (x)
(cond (($listp x)
(setq x (rest x))
(cond ((null x)
(merror "Error: argument to lmin must be nonempty."))
(t
(reduce #'$min x))))
(t
(merror "Error: argument to lmin must be a list."))))
----------end of file----------------------------------------
The following may help someone understand gcl's restriction on
the number of function arguments better.
[barton@localhost maxima]$ maxima
GCL (GNU Common Lisp) Version(2.4.0) Fri Mar 1 22:06:12 CST 2002
Licensed under GNU Library General Public License
Contains Enhancements by W. Schelter
Maxima 5.6 Fri Mar 1 22:05:34 CST 2002 (with enhancements by William Schelter).
Licensed under the GNU Public License (see file COPYING)
Loading maxima-init.lisp
Finished loading maxima-init.lisp
(C1) p : makelist(i,i,1,75)$
(C2) apply(max,p);
Error: MACSYMA-TOP-LEVEL [or a callee] requires less than seventy-five arguments.
Fast links are on: do (si::use-fast-links nil) for debugging
Error signalled by MACSYMA-TOP-LEVEL.
Broken at MACSYMA-TOP-LEVEL. Type :H for Help.
MAXIMA>>:q
/* My experiments show that the upper bound on the number of function
arguments is 64. The restriction disappears when compar.lisp is loaded.
Apparently, the restriction on the number of function arguments is
introduced when the code is compiled.
*/
(C3) load("/home/barton/mac2/maxima-5.6/src/compar.lisp")$
Loading /home/barton/mac2/maxima-5.6/src/compar.lisp
Finished loading /home/barton/mac2/maxima-5.6/src/compar.lisp
(C4) apply(max,p);
(D4) 75
/* The un-compiled code is *very* slow.
*/
(C5) showtime : all;
Evaluation took 0.00 seconds (0.00 elapsed)
(D5) ALL
(C6) p : makelist(i,i,1,1000)$
Evaluation took 0.37 seconds (0.51 elapsed)
(C7) apply(max,p);
Evaluation took 140.49 seconds (429.45 elapsed)
(D7) 1000
/* Here are examples of lmin and lmax. Don't be alarmed by the slowness -- I'm
running this on a circa 1996 computer. Also, starting a new maxima session
gives faster run times.
*/
(C8) load("/home/barton/maxima/my_max.lisp");
Loading /home/barton/maxima/my_max.lisp
Finished loading /home/barton/maxima/my_max.lisp
Evaluation took 0.00 seconds (0.00 elapsed)
(D8) /home/barton/maxima/my_max.lisp
(C9) lmax(p);
Evaluation took 0.85 seconds (2.16 elapsed)
(D9) 1000
(C10) lmin(p);
Evaluation took 0.62 seconds (1.28 elapsed)
(D10) 1
Regards,
Barton