1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 (defmacro defprop
(sym val indic
)
205 `(setf (symbol-function ',sym
) #',val
)
206 `(setf (get ',sym
',indic
) ',val
)))
208 ;; Find the N most significant or least significant bits of the
209 ;; absolute value of X. If N is positive, take the most significant;
210 ;; otherwise, the least significant.
214 ;; If the desired number of bits is larger than the actual
215 ;; number, just return the number. (Prevents gratuitously
216 ;; generating a huge bignum if n is very large, as can happen
218 (if (< (integer-length x
) (- n
))
220 (logand x
(1- (ash 1 (- n
)))))
221 (ash x
(min (- n
(integer-length x
)) 0)))))
223 ;; also correct but slower.
224 ;;(defun haipart (integer count)
225 ;; (let ((x (abs integer)))
226 ;; (if (minusp count)
227 ;; (ldb (byte (- count) 0) x)
228 ;; (ldb (byte count (max 0 (- (integer-length x) count))) x))))
230 ;;used in translation
231 (defun fset (sym val
)
232 (setf (symbol-function sym
) val
))
234 (defun zl-get (sym tag
)
235 (cond ((symbolp sym
) (get sym tag
))
236 ((consp sym
) (getf (cdr sym
) tag
))))
238 (defun getl (plist indicator-list
)
239 (cond ((symbolp plist
)
240 (setq plist
(symbol-plist plist
)))
241 ((consp plist
) (setq plist
(cdr plist
)))
242 (t (return-from getl nil
)))
243 (loop for tail on plist by
#'cddr
244 when
(member (car tail
) indicator-list
:test
#'eq
)
247 (declaim (inline safe-get safe-getl
))
248 (defun safe-get (sym prop
)
249 (and (symbolp sym
) (get sym prop
)))
251 (defun safe-getl (sym prop
)
252 (and (symbolp sym
) (getl sym prop
)))
255 `(cons ,x nil
)) ;;can one optimize this??
257 (defvar *acursor
* (make-array 11 :element-type
'fixnum
:initial-element
0))
259 ;; Format of *acursor*.
260 ;; 0 1 2 3 4 5 6 7 8 9 10
261 ;; dim i1 i2 i3 i4 i5 d1 d2 d3 d4 d5
262 ;; array dimension current index maximal index
264 (defun set-up-cursor (ar)
265 (let ((lis (array-dimensions ar
)))
266 (setf (aref *acursor
* 0) (length lis
))
267 (loop for v in lis for i from
6 do
(setf (aref *acursor
* i
) (1- v
)))
268 (loop for i from
1 to
(length lis
) do
(setf (aref *acursor
* i
) 0))))
270 (defun aset-by-cursor (ar val
)
271 (let ((curs *acursor
*))
272 (declare (type (simple-array fixnum
(11)) curs
))
274 (1 (setf (aref ar
(aref curs
1)) val
))
275 (2 (setf (aref ar
(aref curs
1) (aref curs
2)) val
))
276 (3 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)) val
))
277 (4 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
279 (5 (setf (aref ar
(aref curs
1) (aref curs
2) (aref curs
3)
280 (aref curs
4) (aref curs
5)) val
)))
281 ;; set the index (`cursor') for the next call to ASET-BY-CURSOR
282 (loop for j downfrom
(aref curs
0)
283 do
(cond ((< (aref curs j
) (aref curs
(+ 5 j
)))
284 (setf (aref curs j
) (+ (aref curs j
) 1))
285 (return-from aset-by-cursor t
))
286 (t (setf (aref curs j
) 0)))
287 (cond ((eql j
0) (return-from aset-by-cursor nil
))))))
289 (defun fillarray (ar x
)
291 (setq ar
(get ar
'array
)))
292 (when (/= (array-rank ar
) 1)
293 (setq ar
(make-array (array-total-size ar
) :displaced-to ar
)))
294 (setq x
(cond ((null x
)
295 (ecase (array-element-type ar
)
299 ((arrayp x
)(listarray x
))
302 (when (> (length ar
) 0)
304 (loop while
(aset-by-cursor ar
(car x
))
305 do
(and (cdr x
) (setq x
(cdr x
))))))
309 (setq x
(get x
'array
)))
310 (if (eql (array-rank x
) 1)
312 (coerce (make-array (apply '* (array-dimensions x
)) :displaced-to x
313 :element-type
(array-element-type x
))
316 (defmacro check-arg
(place pred
&rest res
)
318 (setq pred
(list pred place
)))
319 `(assert ,pred
(,place
) ,@res
))
321 (defmacro deff
(fun val
)
322 `(setf (symbol-function ',fun
) ,val
))
324 (defmacro xcons
(x y
)
325 (cond ((atom x
) `(cons ,y
,x
))
326 (t (let ((g (gensym)))
330 (defun make-equal-hash-table (not-dim1)
331 (let ((table (make-hash-table :test
'equal
)))
332 (or not-dim1
(setf (gethash 'dim1 table
) t
))
335 ;;; exp is shadowed to save trouble for other packages--its declared special
339 (defmacro float
(x &optional
(y 1e0
))
342 (defmacro with-collector
(collector-sym &body forms
)
343 (let ((acc (gensym)))
345 (flet ((,collector-sym
(x) (push x
,acc
)))
349 ;; DO-MERGE-ASYM moved here from nset.lisp so that it is defined before
350 ;; it is referenced in compar.lisp.
351 (defmacro do-merge-symm
(list1 list2 eqfun lessfun bothfun onefun
)
352 ;; Like do-merge-asym, but calls onefun if an element appears in one but
353 ;; not the other list, regardless of which list it appears in.
354 `(do-merge-asym ,list1
,list2
,eqfun
,lessfun
,bothfun
,onefun
,onefun
))
356 (defmacro do-merge-asym
357 (list1 list2 eqfun lessfun bothfun only1fun only2fun
)
359 ;; The element equality function is eqfun, and they must be sorted by lessfun.
360 ;; Calls bothfun on each element that is shared by the two lists;
361 ;; calls only1fun on each element that appears only in the first list;
362 ;; calls only2fun on each element that appears only in the second list.
363 ;; If both/only1/only2 fun are nil, treat as no-op.
364 (let ((l1var (gensym))
366 `(do ((,l1var
,list1
)
368 ((cond ((null ,l1var
)
371 (funcall ,only2fun
(car ,l2var
))
372 (setq ,l2var
(cdr ,l2var
))))
377 (funcall ,only1fun
(car ,l1var
))
378 (setq ,l1var
(cdr ,l1var
))))
380 ((funcall ,eqfun
(car ,l1var
) (car ,l2var
))
381 (if ,bothfun
(funcall ,bothfun
(car ,l1var
)))
382 (setq ,l1var
(cdr ,l1var
) ,l2var
(cdr ,l2var
))
384 ((funcall ,lessfun
(car ,l1var
) (car ,l2var
))
385 (if ,only1fun
(funcall ,only1fun
(car ,l1var
)))
386 (setq ,l1var
(cdr ,l1var
))
389 (if ,only2fun
(funcall ,only2fun
(car ,l2var
)))
390 (setq ,l2var
(cdr ,l2var
))
394 ; (do-merge-asym '(a a a b c g h k l)
395 ; '(a b b c c h i j k k)
398 ; '(lambda (x) (prin0 'both x))
399 ; '(lambda (x) (prin0 'one1 x))
400 ; '(lambda (x) (prin0 'one2 x)))