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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 ;;this will make operators which declare the type and result of numerical operations
12 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
14 (defmacro def-op
(name arg-type op
&optional return-type
)
15 `(setf (macro-function ',name
)
16 (make-operation ',arg-type
',op
',return-type
)))
18 ;;make very sure .type .op and .return are not special!!
19 (defun make-operation (.type .op .return
)
20 (or .return
(setf .return .type
))
22 (declare (ignore env
))
23 (loop for v in
(cdr bod
)
24 when
(eq t .type
) collect v into body
26 collect
`(the , .type
,v
) into body
27 finally
(setq body
`(, .op
,@body
))
31 `(the , .return
,body
))))))
33 ;; these allow running of code and they print out where the error occurred
38 (defun chk-type (lis na typ sho
)
39 (unless (every #'(lambda (v) (typep v typ
)) lis
)
40 (format t
"~%Bad call ~a types:~a" (cons na sho
) (mapcar #'type-of lis
))
44 (defmacro def-op
(name arg-type old
)
45 `(defmacro ,name
(&rest l
)
47 (chk-type (list ,@l
) ',',name
',',arg-type
',l
)
53 (def-op f1- fixnum
1-
)
54 (def-op f1
+ fixnum
1+)
55 (def-op quotient t quot
))
57 ;;this is essentially what the quotient is supposed to do.
59 (declaim (inline quot
))
61 (if (and (integerp a
) (integerp b
))
65 (defmacro status
(option &optional item
)
66 (cond ((equal (symbol-name option
) (symbol-name '#:feature
))
67 `(member ,(intern (string item
) (find-package 'keyword
)) *features
*))
68 ((equal option
'gctime
) 0)))
71 (defun string<$
(str1 str2
)
72 "Compare string, but flip the case for maxima variable names to maintain
73 the same order irrespective of the lisp case mode."
74 (declare (string str1 str2
))
75 (cond (#+scl
(eq ext
:*case-mode
* :lower
)
76 #+allegro
(eq excl
:*current-case-mode
* :case-sensitive-lower
)
77 (let ((str1l (length str1
))
78 (str2l (length str2
)))
79 (cond ((and (> str1l
1) (char= (aref str1
0) #\$
)
80 (> str2l
1) (char= (aref str2
0) #\$
))
81 (flet ((case-flip (str)
82 (let ((some-upper nil
)
84 (dotimes (i (length str
))
85 (let ((ch (schar str i
)))
86 (when (lower-case-p ch
)
88 (when (upper-case-p ch
)
89 (setf some-upper t
))))
90 (cond ((and some-upper some-lower
)
96 (let ((flip1 (case-flip str1
))
97 (flip2 (case-flip str2
)))
98 (do ((index 1 (1+ index
)))
99 ((or (>= index str1l
) (>= index str2l
))
100 (if (= index str1l
) index nil
))
101 (let ((ch1 (aref str1 index
))
102 (ch2 (aref str2 index
)))
103 (cond ((and (eq flip1
:downcase
) (both-case-p ch1
))
104 (setf ch1
(char-downcase ch1
)))
105 ((and (eq flip1
:upcase
) (both-case-p ch1
))
106 (setf ch1
(char-upcase ch1
))))
107 (cond ((and (eq flip2
:downcase
) (both-case-p ch2
))
108 (setf ch2
(char-downcase ch2
)))
109 ((and (eq flip2
:upcase
) (both-case-p ch2
))
110 (setf ch2
(char-upcase ch2
))))
111 (unless (char= ch1 ch2
)
112 (return (if (char< ch1 ch2
)
116 (string< str1 str2
)))))
118 (string< str1 str2
))))
121 (defun string<$
(str1 str2
)
124 ;;numbers<strings<symbols<lists<?
125 (defun alphalessp (x y
)
127 (if (numberp y
) (< x y
) t
))
129 (cond ((numberp y
) nil
)
134 (cond ((or (numberp y
) (stringp y
)) nil
)
136 (let ((nx (print-invert-case x
))
137 (ny (print-invert-case y
)))
138 (declare (string nx ny
))
139 (cond ((string<$ nx ny
)
142 (cond ((eq nx ny
) nil
)
143 ((null (symbol-package x
)) nil
)
144 ((null (symbol-package y
)) nil
)
146 (package-name (symbol-package x
))
147 (package-name (symbol-package y
))))))
151 (cond ((or (numberp y
) (stringp y
)(symbolp y
)) nil
)
153 (or (alphalessp (car x
) (car y
))
154 (and (equal (car x
) (car y
))
155 (alphalessp (cdr x
) (cdr y
)))))
157 ((or (numberp y
) (stringp y
) (symbolp y
)(consp y
))
159 (t ;neither is of known type:
160 (alphalessp (format nil
"~s" x
)(format nil
"~s" y
)))))
162 (defmacro symbol-array
(sym)
165 (defun arraydims (ar)
167 (setq ar
(symbol-array ar
)))
168 (cons (array-element-type ar
) (array-dimensions ar
)))
170 (declaim (inline fixnump bignump posint negint memq firstn
))
172 (declare (optimize (speed 3)))
176 (declare (optimize (speed 3)))
180 (declare (optimize (speed 3)))
181 (and (integerp x
) (> x
0)))
184 (declare (optimize (speed 3)))
185 (and (integerp x
) (< x
0)))
187 ;; if x is in the list, return the sublist with element, else nil.
189 ;; At least at the time memq was designed it was (at least in many cases) faster
190 ;; than the lisp's built-in function "member", see:
191 ;; https://people.eecs.berkeley.edu/~fateman/papers/lispoptim.pdf
193 (declare (optimize (speed 3)))
194 (member x lis
:test
#'eq
))
196 (defun firstn (n lis
)
197 (declare (type (integer 0 (#.most-positive-fixnum
)) n
)
198 (optimize (speed 3)))
201 ;;actually this was for lists too.
203 (defun putprop (sym val indic
)
205 (setf (getf (cdr sym
) indic
) val
)
206 (setf (get sym indic
) val
)))
208 (defmacro defprop
(sym val indic
)
210 `(setf (symbol-function ',sym
) #',val
)
211 `(setf (get ',sym
',indic
) ',val
)))
213 ;; Find the N most significant or least significant bits of the
214 ;; absolute value of X. If N is positive, take the most significant;
215 ;; otherwise, the least significant.
219 ;; If the desired number of bits is larger than the actual
220 ;; number, just return the number. (Prevents gratuitously
221 ;; generating a huge bignum if n is very large, as can happen
223 (if (< (integer-length x
) (- n
))
225 (logand x
(1- (ash 1 (- n
)))))
226 (ash x
(min (- n
(integer-length x
)) 0)))))
228 ;; also correct but slower.
229 ;;(defun haipart (integer count)
230 ;; (let ((x (abs integer)))
231 ;; (if (minusp count)
232 ;; (ldb (byte (- count) 0) x)
233 ;; (ldb (byte count (max 0 (- (integer-length x) count))) x))))
235 ;;used in translation
236 (defun fset (sym val
)
237 (setf (symbol-function sym
) val
))
239 (defun zl-get (sym tag
)
240 (cond ((symbolp sym
) (get sym tag
))
241 ((consp sym
) (getf (cdr sym
) tag
))))
243 (defun getl (plist indicator-list
)
244 (cond ((symbolp plist
)
245 (setq plist
(symbol-plist plist
)))
246 ((consp plist
) (setq plist
(cdr plist
)))
247 (t (return-from getl nil
)))
248 (loop for tail on plist by
#'cddr
249 when
(member (car tail
) indicator-list
:test
#'eq
)
252 (declaim (inline safe-get safe-getl
))
253 (defun safe-get (sym prop
)
254 (and (symbolp sym
) (get sym prop
)))
256 (defun safe-getl (sym prop
)
257 (and (symbolp sym
) (getl sym prop
)))
260 `(cons ,x nil
)) ;;can one optimize this??
262 (defvar *acursor
* (make-array 11 :element-type
'fixnum
:initial-element
0))
264 ;; Format of *acursor*.
265 ;; 0 1 2 3 4 5 6 7 8 9 10
266 ;; dim i1 i2 i3 i4 i5 d1 d2 d3 d4 d5
267 ;; array dimension current index maximal index
269 (defun set-up-cursor (ar)
270 (let ((lis (array-dimensions ar
)))
271 (setf (aref *acursor
* 0) (length lis
))
272 (loop for v in lis for i from
6 do
(setf (aref *acursor
* i
) (1- v
)))
273 (loop for i from
1 to
(length lis
) do
(setf (aref *acursor
* i
) 0))))
275 (defun aset-by-cursor (ar val
)
276 (let ((curs *acursor
*))
277 (declare (type (simple-array fixnum
(11)) curs
))
279 (1 (setf (aref ar
(aref curs
1)) val
))
280 (2 (setf (aref ar
(aref curs
1) (aref curs
2)) val
))
281 (3 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)) val
))
282 (4 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
284 (5 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
285 (aref curs
4) (aref curs
5)) val
)))
286 ;; set the index (`cursor') for the next call to ASET-BY-CURSOR
287 (loop for j downfrom
(aref curs
0)
288 do
(cond ((< (aref curs j
) (aref curs
(+ 5 j
)))
289 (setf (aref curs j
) (+ (aref curs j
) 1))
290 (return-from aset-by-cursor t
))
291 (t (setf (aref curs j
) 0)))
292 (cond ((eql j
0) (return-from aset-by-cursor nil
))))))
294 (defun fillarray (ar x
)
296 (setq ar
(get ar
'array
)))
297 (when (/= (array-rank ar
) 1)
298 (setq ar
(make-array (array-total-size ar
) :displaced-to ar
)))
299 (setq x
(cond ((null x
)
300 (ecase (array-element-type ar
)
304 ((arrayp x
)(listarray x
))
307 (when (> (length ar
) 0)
309 (loop while
(aset-by-cursor ar
(car x
))
310 do
(and (cdr x
) (setq x
(cdr x
))))))
314 (setq x
(get x
'array
)))
315 (if (eql (array-rank x
) 1)
317 (coerce (make-array (apply '* (array-dimensions x
)) :displaced-to x
318 :element-type
(array-element-type x
))
321 (defmacro check-arg
(place pred
&rest res
)
323 (setq pred
(list pred place
)))
324 `(assert ,pred
(,place
) ,@res
))
326 (defmacro deff
(fun val
)
327 `(setf (symbol-function ',fun
) ,val
))
329 (defmacro xcons
(x y
)
330 (cond ((atom x
) `(cons ,y
,x
))
331 (t (let ((g (gensym)))
335 (defun make-equal-hash-table (not-dim1)
336 (let ((table (make-hash-table :test
'equal
)))
337 (or not-dim1
(setf (gethash 'dim1 table
) t
))
340 ;;; exp is shadowed to save trouble for other packages--its declared special
345 ;; This used to be enabled, but
346 ;; http://clisp.cons.org/impnotes/num-dict.html seems to indicate
347 ;; that the result of float, coerce, sqrt, etc., on a rational will
348 ;; return a float of the specified type. But ANSI CL says we must
349 ;; return a single-float. I (rtoy) am commenting this out for now.
351 ;; (setq custom:*default-float-format* 'double-float)
353 ;; We currently don't want any warnings about floating-point contagion.
354 (setq custom
::*warn-on-floating-point-contagion
* nil
)
356 ;; We definitely want ANSI-style floating-point contagion.
357 (setq custom
:*floating-point-contagion-ansi
* t
)
359 ;; Set custom:*floating-point-rational-contagion-ansi* so that
360 ;; contagion is done as per the ANSI CL standard. Has an effect only
361 ;; in those few cases when the mathematical result is exact although
362 ;; one of the arguments is a floating-point number, such as (* 0
363 ;; 1.618), (/ 0 1.618), (atan 0 1.0), (expt 2.0 0)
364 (setq custom
:*floating-point-rational-contagion-ansi
* t
)
366 ;; When building maxima using with 'flonum being a 'long-float it may be
367 ;; useful to adjust the number of bits of precision that CLISP uses for
370 (setf (ext:long-float-digits
) 128)
372 ;; We want underflows not to signal errors.
373 (ext:without-package-lock
()
374 (setq sys
::*inhibit-floating-point-underflow
* t
))
379 ;; We want underflows not to signal errors
380 (when (fboundp (find-symbol "FLOAT-UNDERFLOW-MODE" "SYS"))
381 (funcall (find-symbol "FLOAT-UNDERFLOW-MODE" "SYS") nil
))
384 ;; Make the maximum exponent larger for CMUCL. Without this, cmucl
385 ;; will generate a continuable error when raising an integer to a
386 ;; power greater than this.
388 (setf ext
::*intexp-maximum-exponent
* 100000)
389 ;;;; Setup the mapping from the Maxima 'flonum float type to a CL float type.
391 ;;;; Add :flonum-long to *features* if you want flonum to be a
392 ;;;; long-float. Or add :flonum-double-double if you want flonum to
393 ;;;; be a double-double (currently only for CMUCL). Otherwise, you
394 ;;;; get double-float as the flonum type.
396 ;;;; Default double-float flonum.
397 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
398 (setq *read-default-float-format
* 'double-float
))
400 #-
(or flonum-long flonum-double-double
)
402 ;; Tell Lisp the float type for a 'flonum.
404 (deftype flonum
(&optional low high
)
406 `(double-float ,low
,high
))
408 `(double-float ,low
))
412 ;; Some versions of clisp and ABCL appear to be buggy: (coerce 1 'flonum)
413 ;; signals an error. So does (coerce 1 '(double-float 0d0)). But
414 ;; (coerce 1 'double-float) returns 1d0 as expected. So for now, make
415 ;; flonum be exactly the same as double-float, without bounds.
417 (deftype flonum
(&optional low high
)
418 (declare (ignorable low high
))
421 (defconstant most-positive-flonum most-positive-double-float
)
422 (defconstant most-negative-flonum most-negative-double-float
)
423 (defconstant least-positive-flonum least-positive-double-float
)
424 (defconstant least-negative-flonum least-negative-double-float
)
425 (defconstant flonum-epsilon double-float-epsilon
)
426 (defconstant least-positive-normalized-flonum least-positive-normalized-double-float
)
427 (defconstant least-negative-normalized-flonum least-negative-normalized-double-float
)
429 (defconstant flonum-exponent-marker
#\D
)
434 ;;;; The Maxima 'flonum can be a CL 'long-float on the Scieneer CL or CLISP,
435 ;;;; but should be the same as 'double-float on other CL implementations.
437 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
438 (setq *read-default-float-format
* 'long-float
))
440 ;; Tell Lisp the float type for a 'flonum.
441 (deftype flonum
(&optional low high
)
443 `(long-float ,low
,high
))
449 (defconstant most-positive-flonum most-positive-long-float
)
450 (defconstant most-negative-flonum most-negative-long-float
)
451 (defconstant least-positive-flonum least-positive-long-float
)
452 (defconstant least-negative-flonum least-negative-long-float
)
453 (defconstant flonum-epsilon long-float-epsilon
)
454 (defconstant least-positive-normalized-flonum least-positive-normalized-long-float
)
456 (defconstant flonum-exponent-marker
#\L
)
460 #+flonum-double-double
463 ;;;; The Maxima 'flonum can be a 'kernel:double-double-float on the CMU CL.
465 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
466 (setq *read-default-float-format
* 'kernel
:double-double-float
))
468 ;; Tell Lisp the float type for a 'flonum.
469 (deftype flonum
(&optional low high
)
471 `(kernel:double-double-float
,low
,high
))
473 `(kernel:double-double-float
,low
))
475 'kernel
:double-double-float
)))
477 ;; While double-double can represent number as up to
478 ;; most-positive-double-float, it can't really do operations on them
479 ;; due to the way multiplication and division are implemented. (I
480 ;; don't think there's any workaround for that.)
482 ;; So, the largest number that can be used is the float just less than
483 ;; 2^1024/(1+2^27). This is the number given here.
484 (defconstant most-positive-double-double-hi
485 (scale-float (cl:float
(1- 9007199187632128) 1d0
) 944))
487 (defconstant most-positive-flonum
(cl:float most-positive-double-double-hi
1w0
))
488 (defconstant most-negative-flonum
(cl:float
(- most-positive-double-double-hi
1w0
)))
489 (defconstant least-positive-flonum
(cl:float least-positive-double-float
1w0
))
490 (defconstant least-negative-flonum
(cl:float least-negative-double-float
1w0
))
491 ;; This is an approximation to a double-double epsilon. Due to the
492 ;; way double-doubles are represented, epsilon is actually zero
493 ;; because 1+x = 1 only when x is zero. But double-doubles only have
494 ;; 106 bits of precision, so we use that as epsilon.
495 (defconstant flonum-epsilon
(scale-float 1w0 -
106))
496 (defconstant least-positive-normalized-flonum
(cl:float least-positive-normalized-double-float
1w0
))
498 (defconstant flonum-exponent-marker
#\W
)
503 (defmacro float
(x &optional
(y 1e0
))
506 (defmacro with-collector
(collector-sym &body forms
)
507 (let ((acc (gensym)))
509 (flet ((,collector-sym
(x) (push x
,acc
)))
513 ;; DO-MERGE-ASYM moved here from nset.lisp so that it is defined before
514 ;; it is referenced in compar.lisp.
515 (defmacro do-merge-symm
(list1 list2 eqfun lessfun bothfun onefun
)
516 ;; Like do-merge-asym, but calls onefun if an element appears in one but
517 ;; not the other list, regardless of which list it appears in.
518 `(do-merge-asym ,list1
,list2
,eqfun
,lessfun
,bothfun
,onefun
,onefun
))
520 (defmacro do-merge-asym
521 (list1 list2 eqfun lessfun bothfun only1fun only2fun
)
523 ;; The element equality function is eqfun, and they must be sorted by lessfun.
524 ;; Calls bothfun on each element that is shared by the two lists;
525 ;; calls only1fun on each element that appears only in the first list;
526 ;; calls only2fun on each element that appears only in the second list.
527 ;; If both/only1/only2 fun are nil, treat as no-op.
528 (let ((l1var (gensym))
530 `(do ((,l1var
,list1
)
532 ((cond ((null ,l1var
)
535 (funcall ,only2fun
(car ,l2var
))
536 (setq ,l2var
(cdr ,l2var
))))
541 (funcall ,only1fun
(car ,l1var
))
542 (setq ,l1var
(cdr ,l1var
))))
544 ((funcall ,eqfun
(car ,l1var
) (car ,l2var
))
545 (if ,bothfun
(funcall ,bothfun
(car ,l1var
)))
546 (setq ,l1var
(cdr ,l1var
) ,l2var
(cdr ,l2var
))
548 ((funcall ,lessfun
(car ,l1var
) (car ,l2var
))
549 (if ,only1fun
(funcall ,only1fun
(car ,l1var
)))
550 (setq ,l1var
(cdr ,l1var
))
553 (if ,only2fun
(funcall ,only2fun
(car ,l2var
)))
554 (setq ,l2var
(cdr ,l2var
))
558 ; (do-merge-asym '(a a a b c g h k l)
559 ; '(a b b c c h i j k k)
562 ; '(lambda (x) (prin0 'both x))
563 ; '(lambda (x) (prin0 'one1 x))
564 ; '(lambda (x) (prin0 'one2 x)))