1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (member x lis
:test
#'eq
))
14 ;;this will make operators which declare the type and result of numerical operations
15 (eval-when (:compile-toplevel
:load-toplevel
)
17 (defmacro def-op
(name arg-type op
&optional return-type
)
18 `(setf (macro-function ',name
)
19 (make-operation ',arg-type
',op
',return-type
)))
21 ;;make very sure .type .op and .return are not special!!
22 (defun make-operation (.type .op .return
)
23 (or .return
(setf .return .type
))
25 (declare (ignore env
))
26 (loop for v in
(cdr bod
)
27 when
(eq t .type
) collect v into body
29 collect
`(the , .type
,v
) into body
30 finally
(setq body
`(, .op
,@body
))
34 `(the , .return
,body
))))))
36 ;; these allow running of code and they print out where the error occurred
41 (defun chk-type (lis na typ sho
)
42 (unless (every #'(lambda (v) (typep v typ
)) lis
)
43 (format t
"~%Bad call ~a types:~a" (cons na sho
) (mapcar #'type-of lis
))
47 (defmacro def-op
(name arg-type old
)
48 `(defmacro ,name
(&rest l
)
50 (chk-type (list ,@l
) ',',name
',',arg-type
',l
)
56 (def-op f1- fixnum
1-
)
57 (def-op f1
+ fixnum
1+)
58 (def-op quotient t quot
))
60 ;;this is essentially what the quotient is supposed to do.
62 (declaim (inline quot
))
64 (if (and (integerp a
) (integerp b
))
68 (defmacro status
(option &optional item
)
69 (cond ((equal (symbol-name option
) (symbol-name '#:feature
))
70 `(member ,(intern (string item
) (find-package 'keyword
)) *features
*))
71 ((equal option
'gctime
) 0)))
74 (defun string<$
(str1 str2
)
75 "Compare string, but flip the case for maxima variable names to maintain
76 the same order irrespective of the lisp case mode."
77 (declare (string str1 str2
))
78 (cond (#+scl
(eq ext
:*case-mode
* :lower
)
79 #+allegro
(eq excl
:*current-case-mode
* :case-sensitive-lower
)
80 (let ((str1l (length str1
))
81 (str2l (length str2
)))
82 (cond ((and (> str1l
1) (char= (aref str1
0) #\$
)
83 (> str2l
1) (char= (aref str2
0) #\$
))
84 (flet ((case-flip (str)
85 (let ((some-upper nil
)
87 (dotimes (i (length str
))
88 (let ((ch (schar str i
)))
89 (when (lower-case-p ch
)
91 (when (upper-case-p ch
)
92 (setf some-upper t
))))
93 (cond ((and some-upper some-lower
)
99 (let ((flip1 (case-flip str1
))
100 (flip2 (case-flip str2
)))
101 (do ((index 1 (1+ index
)))
102 ((or (>= index str1l
) (>= index str2l
))
103 (if (= index str1l
) index nil
))
104 (let ((ch1 (aref str1 index
))
105 (ch2 (aref str2 index
)))
106 (cond ((and (eq flip1
:downcase
) (both-case-p ch1
))
107 (setf ch1
(char-downcase ch1
)))
108 ((and (eq flip1
:upcase
) (both-case-p ch1
))
109 (setf ch1
(char-upcase ch1
))))
110 (cond ((and (eq flip2
:downcase
) (both-case-p ch2
))
111 (setf ch2
(char-downcase ch2
)))
112 ((and (eq flip2
:upcase
) (both-case-p ch2
))
113 (setf ch2
(char-upcase ch2
))))
114 (unless (char= ch1 ch2
)
115 (return (if (char< ch1 ch2
)
119 (string< str1 str2
)))))
121 (string< str1 str2
))))
124 (defun string<$
(str1 str2
)
127 ;;numbers<strings<symbols<lists<?
128 (defun alphalessp (x y
)
130 (if (numberp y
) (< x y
) t
))
132 (cond ((numberp y
) nil
)
137 (cond ((or (numberp y
) (stringp y
)) nil
)
139 (let ((nx (symbol-name x
))
140 (ny (symbol-name y
)))
141 (declare (string nx ny
))
142 (cond ((string<$ nx ny
)
145 (cond ((eq nx ny
) nil
)
146 ((null (symbol-package x
)) nil
)
147 ((null (symbol-package y
)) nil
)
149 (package-name (symbol-package x
))
150 (package-name (symbol-package y
))))))
154 (cond ((or (numberp y
) (stringp y
)(symbolp y
)) nil
)
156 (or (alphalessp (car x
) (car y
))
157 (and (equal (car x
) (car y
))
158 (alphalessp (cdr x
) (cdr y
)))))
160 ((or (numberp y
) (stringp y
) (symbolp y
)(consp y
))
162 (t ;neither is of known type:
163 (alphalessp (format nil
"~s" x
)(format nil
"~s" y
)))))
165 (defmacro symbol-array
(sym)
168 (defun arraydims (ar)
170 (setq ar
(symbol-array ar
)))
171 (cons (array-element-type ar
) (array-dimensions ar
)))
173 (defun firstn (n lis
)
176 (declaim (inline fixnump
))
183 ;;actually this was for lists too.
185 (defun putprop (sym val indic
)
187 (setf (getf (cdr sym
) indic
) val
)
188 (setf (get sym indic
) val
)))
190 (defmacro defprop
(sym val indic
)
192 `(setf (symbol-function ',sym
) #',val
)
193 `(setf (get ',sym
',indic
) ',val
)))
195 ;; Find the N most significant or least significant bits of the
196 ;; absolute value of X. If N is positive, take the most significant;
197 ;; otherwise, the least significant.
201 ;; If the desired number of bits is larger than the actual
202 ;; number, just return the number. (Prevents gratuitously
203 ;; generating a huge bignum if n is very large, as can happen
205 (if (< (integer-length x
) (- n
))
207 (logand x
(1- (ash 1 (- n
)))))
208 (ash x
(min (- n
(integer-length x
)) 0)))))
210 ;; also correct but slower.
211 ;;(defun haipart (integer count)
212 ;; (let ((x (abs integer)))
213 ;; (if (minusp count)
214 ;; (ldb (byte (- count) 0) x)
215 ;; (ldb (byte count (max 0 (- (integer-length x) count))) x))))
217 ;;used in translation
218 (defun fset (sym val
)
219 (setf (symbol-function sym
) val
))
221 (defun oldget (plist indic
)
222 (cond ((symbolp plist
)
223 (setq plist
(symbol-plist plist
)))
224 ((consp plist
) (setq plist
(cdr plist
)))
225 (t (return-from oldget nil
)))
226 (loop for tail on plist by
#'cddr
227 when
(eq (car tail
) indic
)
228 do
(return (second tail
))))
230 (defun safe-get (sym prop
)
231 (and (symbolp sym
) (get sym prop
)))
233 (defmacro safe-getl
(sym prop
)
234 `(and (symbolp ,sym
) (getl ,sym
,prop
)))
236 (defun getl (plist indicator-list
)
237 (cond ((symbolp plist
)
238 (setq plist
(symbol-plist plist
)))
239 ((consp plist
) (setq plist
(cdr plist
)))
240 (t (return-from getl nil
)))
241 (loop for tail on plist by
#'cddr
242 when
(member (car tail
) indicator-list
:test
#'eq
)
246 `(cons ,x nil
)) ;;can one optimize this??
248 (defvar *acursor
* (make-array 11 :element-type
'fixnum
:initial-element
0))
250 ;; Format of *acursor*.
251 ;; 0 1 2 3 4 5 6 7 8 9 10
252 ;; dim i1 i2 i3 i4 i5 d1 d2 d3 d4 d5
253 ;; array dimension current index maximal index
255 (defun set-up-cursor (ar)
256 (let ((lis (array-dimensions ar
)))
257 (setf (aref *acursor
* 0) (length lis
))
258 (loop for v in lis for i from
6 do
(setf (aref *acursor
* i
) (1- v
)))
259 (loop for i from
1 to
(length lis
) do
(setf (aref *acursor
* i
) 0))))
261 (defun aset-by-cursor (ar val
)
262 (let ((curs *acursor
*))
263 (declare (type (simple-array fixnum
(11)) curs
))
265 (1 (setf (aref ar
(aref curs
1)) val
))
266 (2 (setf (aref ar
(aref curs
1) (aref curs
2)) val
))
267 (3 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)) val
))
268 (4 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
270 (5 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
271 (aref curs
4) (aref curs
5)) val
)))
272 ;; set the index (`cursor') for the next call to ASET-BY-CURSOR
273 (loop for j downfrom
(aref curs
0)
274 do
(cond ((< (aref curs j
) (aref curs
(+ 5 j
)))
275 (setf (aref curs j
) (+ (aref curs j
) 1))
276 (return-from aset-by-cursor t
))
277 (t (setf (aref curs j
) 0)))
278 (cond ((eql j
0) (return-from aset-by-cursor nil
))))))
280 (defun fillarray (ar x
)
282 (setq ar
(get ar
'array
)))
283 (when (/= (array-rank ar
) 1)
284 (setq ar
(make-array (array-total-size ar
) :displaced-to ar
)))
285 (setq x
(cond ((null x
)
286 (ecase (array-element-type ar
)
290 ((arrayp x
)(listarray x
))
293 (when (> (length ar
) 0)
295 (loop while
(aset-by-cursor ar
(car x
))
296 do
(and (cdr x
) (setq x
(cdr x
))))))
300 (setq x
(get x
'array
)))
301 (if (eql (array-rank x
) 1)
303 (coerce (make-array (apply '* (array-dimensions x
)) :displaced-to x
304 :element-type
(array-element-type x
))
307 (defmacro check-arg
(place pred
&rest res
)
309 (setq pred
(list pred place
)))
310 `(assert ,pred
(,place
) ,@res
))
312 (defmacro deff
(fun val
)
313 `(setf (symbol-function ',fun
) ,val
))
315 (defmacro xcons
(x y
)
316 (cond ((atom x
) `(cons ,y
,x
))
317 (t (let ((g (gensym)))
321 (defun make-equal-hash-table (not-dim1)
322 (let ((table (make-hash-table :test
'equal
)))
323 (or not-dim1
(setf (gethash 'dim1 table
) t
))
326 ;;; Range of atan should be [0,2*pi]
328 (let ((tem (cl:atan y x
)))
333 ;;; Range of atan2 should be (-pi,pi]
334 ;;; CL manual says that's what lisp::atan is supposed to have.
335 (deff atan2
#'cl
:atan
)
337 ;;; exp is shadowed to save trouble for other packages--its declared special
342 ;; This used to be enabled, but
343 ;; http://clisp.cons.org/impnotes/num-dict.html seems to indicate
344 ;; that the result of float, coerce, sqrt, etc., on a rational will
345 ;; return a float of the specified type. But ANSI CL says we must
346 ;; return a single-float. I (rtoy) am commenting this out for now.
348 ;; (setq custom:*default-float-format* 'double-float)
350 ;; We currently don't want any warnings about floating-point contagion.
351 (setq custom
::*warn-on-floating-point-contagion
* nil
)
353 ;; We definitely want ANSI-style floating-point contagion.
354 (setq custom
:*floating-point-contagion-ansi
* t
)
356 ;; Set custom:*floating-point-rational-contagion-ansi* so that
357 ;; contagion is done as per the ANSI CL standard. Has an effect only
358 ;; in those few cases when the mathematical result is exact although
359 ;; one of the arguments is a floating-point number, such as (* 0
360 ;; 1.618), (/ 0 1.618), (atan 0 1.0), (expt 2.0 0)
361 (setq custom
:*floating-point-rational-contagion-ansi
* t
)
363 ;; When building maxima using with 'flonum being a 'long-float it may be
364 ;; useful to adjust the number of bits of precision that CLISP uses for
367 (setf (ext:long-float-digits
) 128)
369 ;; We want underflows not to signal errors.
370 (ext:without-package-lock
()
371 (setq sys
::*inhibit-floating-point-underflow
* t
))
376 ;; We want underflows not to signal errors
377 (when (fboundp (find-symbol "FLOAT-UNDERFLOW-MODE" "SYS"))
378 (funcall (find-symbol "FLOAT-UNDERFLOW-MODE" "SYS") nil
))
381 ;; Make the maximum exponent larger for CMUCL. Without this, cmucl
382 ;; will generate a continuable error when raising an integer to a
383 ;; power greater than this.
385 (setf ext
::*intexp-maximum-exponent
* 100000)
386 ;;;; Setup the mapping from the Maxima 'flonum float type to a CL float type.
388 ;;;; Add :flonum-long to *features* if you want flonum to be a
389 ;;;; long-float. Or add :flonum-double-double if you want flonum to
390 ;;;; be a double-double (currently only for CMUCL). Otherwise, you
391 ;;;; get double-float as the flonum type.
393 ;;;; Default double-float flonum.
394 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
395 (setq *read-default-float-format
* 'double-float
))
397 #-
(or flonum-long flonum-double-double
)
399 ;; Tell Lisp the float type for a 'flonum.
401 (deftype flonum
(&optional low high
)
403 `(double-float ,low
,high
))
405 `(double-float ,low
))
409 ;; Some versions of clisp appear to be buggy: (coerce 1 'flonum)
410 ;; signals an error. So does (coerce 1 '(double-float 0d0)). But
411 ;; (coerce 1 'double-float) returns 1d0 as expected. So for now, make
412 ;; flonum be exactly the same as double-float, without bounds.
414 (deftype flonum
(&optional low high
)
415 (declare (ignorable low high
))
418 (defconstant most-positive-flonum most-positive-double-float
)
419 (defconstant most-negative-flonum most-negative-double-float
)
420 (defconstant least-positive-flonum least-positive-double-float
)
421 (defconstant least-negative-flonum least-negative-double-float
)
422 (defconstant flonum-epsilon double-float-epsilon
)
423 (defconstant least-positive-normalized-flonum least-positive-normalized-double-float
)
425 (defconstant flonum-exponent-marker
#\D
)
430 ;;;; The Maxima 'flonum can be a CL 'long-float on the Scieneer CL or CLISP,
431 ;;;; but should be the same as 'double-float on other CL implementations.
433 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
434 (setq *read-default-float-format
* 'long-float
))
436 ;; Tell Lisp the float type for a 'flonum.
437 (deftype flonum
(&optional low high
)
439 `(long-float ,low
,high
))
445 (defconstant most-positive-flonum most-positive-long-float
)
446 (defconstant most-negative-flonum most-negative-long-float
)
447 (defconstant least-positive-flonum least-positive-long-float
)
448 (defconstant least-negative-flonum least-negative-long-float
)
449 (defconstant flonum-epsilon long-float-epsilon
)
450 (defconstant least-positive-normalized-flonum least-positive-normalized-long-float
)
452 (defconstant flonum-exponent-marker
#\L
)
456 #+flonum-double-double
459 ;;;; The Maxima 'flonum can be a 'kernel:double-double-float on the CMU CL.
461 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
462 (setq *read-default-float-format
* 'kernel
:double-double-float
))
464 ;; Tell Lisp the float type for a 'flonum.
465 (deftype flonum
(&optional low high
)
467 `(kernel:double-double-float
,low
,high
))
469 `(kernel:double-double-float
,low
))
471 'kernel
:double-double-float
)))
473 ;; While double-double can represent number as up to
474 ;; most-positive-double-float, it can't really do operations on them
475 ;; due to the way multiplication and division are implemented. (I
476 ;; don't think there's any workaround for that.)
478 ;; So, the largest number that can be used is the float just less than
479 ;; 2^1024/(1+2^27). This is the number given here.
480 (defconstant most-positive-double-double-hi
481 (scale-float (cl:float
(1- 9007199187632128) 1d0
) 944))
483 (defconstant most-positive-flonum
(cl:float most-positive-double-double-hi
1w0
))
484 (defconstant most-negative-flonum
(cl:float
(- most-positive-double-double-hi
1w0
)))
485 (defconstant least-positive-flonum
(cl:float least-positive-double-float
1w0
))
486 (defconstant least-negative-flonum
(cl:float least-negative-double-float
1w0
))
487 ;; This is an approximation to a double-double epsilon. Due to the
488 ;; way double-doubles are represented, epsilon is actually zero
489 ;; because 1+x = 1 only when x is zero. But double-doubles only have
490 ;; 106 bits of precision, so we use that as epsilon.
491 (defconstant flonum-epsilon
(scale-float 1w0 -
106))
492 (defconstant least-positive-normalized-flonum
(cl:float least-positive-normalized-double-float
1w0
))
494 (defconstant flonum-exponent-marker
#\W
)
499 (defmacro float
(x &optional
(y 1e0
))
502 (defmacro with-collector
(collector-sym &body forms
)
503 (let ((acc (gensym)))
505 (flet ((,collector-sym
(x) (push x
,acc
)))
509 ;; DO-MERGE-ASYM moved here from nset.lisp so that it is defined before
510 ;; it is referenced in compar.lisp.
511 (defmacro do-merge-symm
(list1 list2 eqfun lessfun bothfun onefun
)
512 ;; Like do-merge-asym, but calls onefun if an element appears in one but
513 ;; not the other list, regardless of which list it appears in.
514 `(do-merge-asym ,list1
,list2
,eqfun
,lessfun
,bothfun
,onefun
,onefun
))
516 (defmacro do-merge-asym
517 (list1 list2 eqfun lessfun bothfun only1fun only2fun
)
519 ;; The element equality function is eqfun, and they must be sorted by lessfun.
520 ;; Calls bothfun on each element that is shared by the two lists;
521 ;; calls only1fun on each element that appears only in the first list;
522 ;; calls only2fun on each element that appears only in the second list.
523 ;; If both/only1/only2 fun are nil, treat as no-op.
524 (let ((l1var (gensym))
526 `(do ((,l1var
,list1
)
528 ((cond ((null ,l1var
)
531 (funcall ,only2fun
(car ,l2var
))
532 (setq ,l2var
(cdr ,l2var
))))
537 (funcall ,only1fun
(car ,l1var
))
538 (setq ,l1var
(cdr ,l1var
))))
540 ((funcall ,eqfun
(car ,l1var
) (car ,l2var
))
541 (if ,bothfun
(funcall ,bothfun
(car ,l1var
)))
542 (setq ,l1var
(cdr ,l1var
) ,l2var
(cdr ,l2var
))
544 ((funcall ,lessfun
(car ,l1var
) (car ,l2var
))
545 (if ,only1fun
(funcall ,only1fun
(car ,l1var
)))
546 (setq ,l1var
(cdr ,l1var
))
549 (if ,only2fun
(funcall ,only2fun
(car ,l2var
)))
550 (setq ,l2var
(cdr ,l2var
))
554 ; (do-merge-asym '(a a a b c g h k l)
555 ; '(a b b c c h i j k k)
558 ; '(lambda (x) (prin0 'both x))
559 ; '(lambda (x) (prin0 'one1 x))
560 ; '(lambda (x) (prin0 'one2 x)))
577 ;; Defines a function named NAME that checks that the number of
578 ;; arguments is correct. If the number of actual arguments is
579 ;; incorrect, a maxima error is signaled.
581 ;; The required arguments is given by REQUIRED-ARG-LIST. Allowed
582 ;; (maxima) keyword arguments is given by KEYWORD-ARG-LIST.
584 ;; The body of the function can refer to KEYLIST which is the list of
585 ;; maxima keyword arguments converted to lisp keyword arguments.
587 (defmacro defun-checked
(name ((&rest required-arg-list
)
588 &rest keyword-arg-list
)
590 (let ((number-of-required-args (length required-arg-list
))
591 (number-of-keyword-args (length keyword-arg-list
))
592 (arg-list (gensym "ARG-LIST-"))
593 (helper-fun (gensym "REAL-FUN-"))
594 (options (gensym "OPTIONS-ARG-")))
595 `(defun ,name
(&rest
,arg-list
)
596 ;; Check that the required number of arguments is given and
597 ;; that we don't supply too many arguments.
599 ;; NOTE: The check when keyword args are given is a little too
600 ;; tight. It's valid to have duplicate keyword args, but we
601 ;; disallow that if the number of arguments exceed the limit.
602 (when (or (> (length ,arg-list
) ,(+ number-of-required-args number-of-keyword-args
))
603 (< (length ,arg-list
) ,number-of-required-args
))
604 (merror (intl:gettext
"~M arguments supplied to ~M: found ~M")
605 (if (< (length ,arg-list
) ,number-of-required-args
)
606 (intl:gettext
"Too few")
607 (if (> (length ,arg-list
) ,(+ number-of-required-args
608 number-of-keyword-args
))
609 (intl:gettext
"Too many")
610 (intl:gettext
"Incorrect number of")))
611 ',(if keyword-arg-list
612 `((,name
) ,@required-arg-list
((mlist simp
) ,@keyword-arg-list
))
613 `((,name
) ,@required-arg-list
))
614 (cons '(mlist) ,arg-list
)))
615 (flet ((,helper-fun
(,@required-arg-list
616 ,@(when keyword-arg-list
618 (let ,(when keyword-arg-list
619 `((keylist (lispify-maxima-keyword-options ,options
620 ',keyword-arg-list
))))
622 (apply #',helper-fun
,arg-list
)))))