More documentation and comment out debugging print.
[maxima.git] / src / clmacs.lisp
blob9cb125e962a4615d34cf9f58034ea3225fa55a54
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9 (in-package :maxima)
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))
21 #'(lambda (bod env)
22 (declare (ignore env))
23 (loop for v in (cdr bod)
24 when (eq t .type) collect v into body
25 else
26 collect `(the , .type ,v) into body
27 finally (setq body `(, .op ,@body))
28 (return
29 (if (eq t .return)
30 body
31 `(the , .return ,body))))))
33 ;; these allow running of code and they print out where the error occurred
34 #+fix-debug
35 (progn
36 (defvar *dbreak* t)
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))
41 (when *dbreak*
42 (break "hi"))))
44 (defmacro def-op (name arg-type old)
45 `(defmacro ,name (&rest l)
46 `(progn
47 (chk-type (list ,@l) ',',name ',',arg-type ',l)
48 (,',old ,@l)))))
50 (def-op f+ fixnum +)
51 (def-op f* fixnum *)
52 (def-op f- fixnum -)
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))
60 (defun quot (a b)
61 (if (and (integerp a) (integerp b))
62 (truncate a b)
63 (/ a 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)))
70 #+(or scl allegro)
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)
83 (some-lower nil))
84 (dotimes (i (length str))
85 (let ((ch (schar str i)))
86 (when (lower-case-p ch)
87 (setf some-lower t))
88 (when (upper-case-p ch)
89 (setf some-upper t))))
90 (cond ((and some-upper some-lower)
91 nil)
92 (some-upper
93 :downcase)
94 (some-lower
95 :upcase)))))
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)
113 index
114 nil))))))))
116 (string< str1 str2)))))
118 (string< str1 str2))))
120 #-(or scl allegro)
121 (defun string<$ (str1 str2)
122 (string< str1 str2))
124 ;;numbers<strings<symbols<lists<?
125 (defun alphalessp (x y)
126 (cond ((numberp x)
127 (if (numberp y) (< x y) t))
128 ((stringp x)
129 (cond ((numberp y) nil)
130 ((stringp y)
131 (string< x y))
132 (t t)))
133 ((symbolp x)
134 (cond ((or (numberp y) (stringp y)) nil)
135 ((symbolp y)
136 (let ((nx (print-invert-case x))
137 (ny (print-invert-case y)))
138 (declare (string nx ny))
139 (cond ((string<$ nx ny)
141 ((string= nx ny)
142 (cond ((eq nx ny) nil)
143 ((null (symbol-package x)) nil)
144 ((null (symbol-package y)) nil)
145 (t (string<
146 (package-name (symbol-package x))
147 (package-name (symbol-package y))))))
148 (t nil))))
149 ((consp y) t)))
150 ((listp x)
151 (cond ((or (numberp y) (stringp y)(symbolp y )) nil)
152 ((listp y)
153 (or (alphalessp (car x) (car y))
154 (and (equal (car x) (car y))
155 (alphalessp (cdr x) (cdr y)))))
156 (t nil)))
157 ((or (numberp y) (stringp y) (symbolp y)(consp y))
158 nil)
159 (t ;neither is of known type:
160 (alphalessp (format nil "~s" x)(format nil "~s" y)))))
162 (defmacro symbol-array (sym)
163 `(get ,sym 'array))
165 (defun arraydims (ar)
166 (when (symbolp 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))
171 (defun fixnump (n)
172 (declare (optimize (speed 3)))
173 (typep n 'fixnum))
175 (defun bignump (x)
176 (declare (optimize (speed 3)))
177 (typep x 'bignum))
179 (defun posint (x)
180 (declare (optimize (speed 3)))
181 (and (integerp x) (> x 0)))
183 (defun negint (x)
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
192 (defun memq (x lis)
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)))
199 (subseq lis 0 n))
201 ;;actually this was for lists too.
203 (defmacro defprop (sym val indic)
204 (if (eq indic 'expr)
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.
211 (defun haipart (x n)
212 (let ((x (abs x)))
213 (if (< n 0)
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
217 ;; with bigfloats.)
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)
245 do (return tail)))
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)))
254 (defmacro ncons (x)
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))
273 (ecase (aref curs 0)
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)
278 (aref curs 4)) val))
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)
290 (when (symbolp ar)
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)
296 (fixnum '(0))
297 (float '(0.0))
298 ((t) '(nil))))
299 ((arrayp x)(listarray x))
300 ((atom x) (list x))
301 (t x)))
302 (when (> (length ar) 0)
303 (set-up-cursor ar)
304 (loop while (aset-by-cursor ar (car x))
305 do (and (cdr x) (setq x (cdr x))))))
307 (defun listarray (x)
308 (when (symbolp x)
309 (setq x (get x 'array)))
310 (if (eql (array-rank x) 1)
311 (coerce x 'list)
312 (coerce (make-array (apply '* (array-dimensions x)) :displaced-to x
313 :element-type (array-element-type x))
314 'list)))
316 (defmacro check-arg (place pred &rest res)
317 (when (atom pred)
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)))
327 `(let ((,g ,x))
328 (cons ,y ,g))))))
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))
333 table))
335 ;;; exp is shadowed to save trouble for other packages--its declared special
336 (deff exp #'cl:exp)
338 ;;;;
339 (defmacro float (x &optional (y 1e0))
340 `(cl:float ,x ,y))
342 (defmacro with-collector (collector-sym &body forms)
343 (let ((acc (gensym)))
344 `(let ((,acc))
345 (flet ((,collector-sym (x) (push x ,acc)))
346 ,@forms
347 (nreverse ,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)
358 ;; Takes two lists.
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))
365 (l2var (gensym)))
366 `(do ((,l1var ,list1)
367 (,l2var ,list2))
368 ((cond ((null ,l1var)
369 (if ,only2fun
370 (while ,l2var
371 (funcall ,only2fun (car ,l2var))
372 (setq ,l2var (cdr ,l2var))))
374 ((null ,l2var)
375 (if ,only1fun
376 (while ,l1var
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))
383 nil)
384 ((funcall ,lessfun (car ,l1var) (car ,l2var))
385 (if ,only1fun (funcall ,only1fun (car ,l1var)))
386 (setq ,l1var (cdr ,l1var))
387 nil)
389 (if ,only2fun (funcall ,only2fun (car ,l2var)))
390 (setq ,l2var (cdr ,l2var))
391 nil))))))
393 ;;; Test
394 ; (do-merge-asym '(a a a b c g h k l)
395 ; '(a b b c c h i j k k)
396 ; 'eq
397 ; 'string<
398 ; '(lambda (x) (prin0 'both x))
399 ; '(lambda (x) (prin0 'one1 x))
400 ; '(lambda (x) (prin0 'one2 x)))
401 ; both a
402 ; one1 a
403 ; one1 a
404 ; both b
405 ; one2 b
406 ; both c
407 ; one2 c
408 ; one1 g
409 ; both h
410 ; one2 i
411 ; one2 j
412 ; both k
413 ; one2 k
414 ; one1 l
415 ; nil