home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / src / clmacs.lisp < prev    next >
Encoding:
Text File  |  2003-02-09  |  14.0 KB  |  523 lines

  1. ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;     The data in this file contains enhancments.                    ;;;;;
  4. ;;;                                                                    ;;;;;
  5. ;;;  Copyright (c) 1984,1987 by William Schelter,University of Texas   ;;;;;
  6. ;;;     All rights reserved                                            ;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8.  
  9. (in-package "MAXIMA")
  10.  
  11. (eval-when (compile load eval)
  12. (defmacro if (test &rest args)
  13.   (cond ((> (length args) 2)
  14.        ;(format t
  15.        ;"~%Warning: Too many args for if:~% ~a"
  16.        ;(cons 'if (cons test args)))
  17.      `(lisp::if ,test ,(car args) (progn ,@ (cdr args))))
  18.     (t `(lisp:if ,test ,@ args))))
  19.  
  20.  
  21.  
  22. ;;this will make operators which 
  23. ;;declare the type and result of numerical operations
  24.  
  25.  
  26. (defmacro def-op (name type op &optional return-type)
  27.         `(setf (macro-function ',name) (make-operation ',type ',op
  28.                                ',return-type)))
  29. ;;make very sure .type .op and .return are not special!!
  30. (defun make-operation (.type .op .return)
  31.   (or .return (setf .return .type))
  32.   #'(lambda (bod env) env
  33.       (sloop for v in (cdr bod)
  34.          when (eq t .type) collect v into body
  35.          else
  36.          collect `(the , .type ,v) into body
  37.          finally (setq body `(, .op ,@ body))
  38.          (return
  39.          (if (eq t .return) body
  40.            `(the , .return ,body))))))
  41.  
  42. #+fix-debug
  43. (progn  ;; these allow running of code and they print out where the error
  44.         ;occurred
  45. (defvar *dbreak* t)
  46. (defun chk-type (lis na typ sho)
  47.   (or (sloop for v in lis
  48.          always (typep v typ))
  49.       (format t "~%Bad call ~a types:~a" (cons na sho)
  50.           (sloop for v in lis collect (type-of v)))
  51.       (and *dbreak* (break "hi"))))
  52.      
  53. (defmacro def-op (name type old)
  54.   `(defmacro ,name (&rest l)
  55.      `(progn (chk-type (list ,@ l) ',',name ',',type ',l )
  56.          (,',old ,@ l))))
  57. )
  58. ;;note 1+ and 1- in the main macsyma code were for fixnum 1+, 
  59. ;;so we should replace them by f1+ and f1- and then add the appropriate
  60. ;;definitions here.
  61.  
  62. (def-op f+ fixnum +)
  63. (def-op f* fixnum *)
  64. (def-op f- fixnum -)
  65. (def-op +$ double-float +)
  66. (def-op *$ double-float *)
  67. (def-op -$ double-float -)
  68. (def-op 1-$ double-float 1-)
  69. (def-op 1+$ double-float 1+)
  70. (def-op f1- fixnum 1-)
  71. (def-op f1+ fixnum 1+)
  72. (def-op sub1 t 1-)
  73. (def-op add1 t 1+)
  74. (def-op plus t +)
  75. (def-op times t *)
  76. (def-op difference t -)
  77. (def-op quotient t quot)
  78. (def-op // t quot) ;(def-op // fixnum quot) ??
  79. (def-op //$ double-float quot)
  80. (def-op ^ fixnum expt)
  81. (def-op ^$ double-float expt)
  82. (def-op greaterp t > )
  83. (def-op f> fixnum > t)
  84. (def-op f< fixnum <  t)
  85. (def-op f= fixnum = t)
  86. (def-op lessp t < t)
  87. (def-op remainder t rem)
  88. (def-op lsh fixnum ash)
  89. (def-op fixnum-remainder fixnum rem)
  90. (def-op minus t -)
  91. ;(def-op \\ fixnum rem) ;no calls any more
  92.  
  93. ;exp is shadowed to save trouble for other packages--its declared special
  94. (setf (symbol-function 'exp) (symbol-function 'lisp::exp))
  95.  
  96. )  ;;end eval-when (symbolics needed this).
  97.  
  98. ;;this is essentially what the quotient is supposed to do.
  99.  
  100. (defun quot (a &rest b)
  101.   (cond ((null b)
  102.      (quot 1 a))
  103.     
  104.     ((null (cdr b))
  105.      (setq b (car b))
  106.      (cond ((and (integerp a) (integerp b))
  107.         (values (truncate a b)))
  108.            (t 
  109.          ( / a b))))
  110.     (t (apply 'quot (quot a (car b)) (cdr b)))))
  111.  
  112.  
  113. (defmacro status (option &optional item)
  114.   (let ((it (intern (string item) (find-package 'keyword))))
  115.     (cond ((equal (symbol-name option) "FEATURE")
  116.        `(member ,it *features*))
  117.       ((equal option 'GCTIME) 0))))
  118.  
  119. (defmacro sstatus (option item )
  120.   (let ((it (intern (string item) (find-package 'keyword))))
  121.     (cond ((equal (symbol-name option) "FEATURE")
  122.        `(pushnew ,it *features*))
  123.       (t (error "unknown sstatus ~a" option)))))
  124.  
  125. (defun setplist (sym val)
  126.   (setf (symbol-plist sym) val))
  127.  
  128. (defun sortcar (lis &optional (test 'alphalessp))
  129.   (sort lis test :key 'car))
  130.  
  131. ;numbers<strings<symbols<lists<?
  132. (defun alphalessp (x y)
  133.   (cond ((numberp x)
  134.      (if (numberp y) (< x y)
  135.        t))
  136.     ((stringp x)
  137.      (cond ((numberp y) nil)
  138.            ((stringp y)
  139.         (string-lessp x y))
  140.            (t t)))
  141.     ((symbolp x)
  142.      (cond ((or (numberp y) (stringp y)) nil)
  143.            ((symbolp y)
  144.         (let ((nx (symbol-name x))
  145.               (ny (symbol-name y)))
  146.           (declare (string nx ny))
  147.           (cond ((string-lessp nx ny)
  148.              t)
  149.             ((string-equal nx ny)
  150.              (cond ((eq nx ny) nil)
  151.                    ((null (symbol-package x)) nil)
  152.                    ((null (symbol-package y)) nil)
  153.                    (t (string-lessp
  154.                    (package-name (symbol-package x))
  155.                    (package-name (symbol-package y))))))
  156.             (t nil))))
  157.            ((consp y) t)))
  158.     ((listp x)
  159.      (cond ((or (numberp y) (stringp y)(symbolp y )) nil)
  160.            ((listp y)
  161.         (or (alphalessp (car x) (car y))
  162.             (and (equal (car x) (car y))
  163.             (alphalessp (cdr x) (cdr y)))))
  164.            (t nil)))
  165.     ((or (numberp y) (stringp y) (symbolp y)(consp y))
  166.      nil)
  167.     (t ;neither is of known type:
  168.       (alphalessp (format nil "~s" x)(format nil "~s" y)))))
  169.  
  170.  
  171.            
  172. (defmacro array-active-length (ar)
  173.   `(length (the vector ,ar)))
  174.  
  175. (defmacro symbol-array (sym) `(get ,sym 'array))
  176.  
  177. (defmacro afuncall (sym &rest ind)
  178.   `(aref (symbol-array ,sym) ,@ ind))
  179.  
  180. (defun arraydims (ar)
  181.   (cond ((symbolp ar) (setq ar (symbol-array ar))))
  182.    (cons (array-type ar) (array-dimensions ar)))
  183.  
  184. (defun array-dimension-n (n ar)
  185.   (declare (fixnum n))
  186.   (array-dimension ar (the fixnum (- n 1))))
  187.  
  188. (defun array-type (ar) (array-element-type ar))
  189.  
  190.  
  191. (defun firstn (n lis)
  192.   (declare (fixnum n))
  193.   (sloop for v in lis for i below n collect v))
  194.  
  195. (defun fixnump (n)
  196.   (typep n 'fixnum))
  197.  
  198. (defun fix (n)
  199.   (cond ((integerp n) n)
  200.     ((< n 0) (- (truncate n) 1))
  201.     (t (values (truncate n)))))
  202.  
  203.  
  204. ;;did result of fix have to  be fixnum in maclisp??
  205. ;;so could this be more efficient??
  206. (setf (symbol-function 'fixr) #'round) 
  207.  
  208. (defun mapatoms (func &optional (pack *package*))
  209.   (do-symbols (x pack)
  210.           (funcall func x)))
  211.  
  212. ;;actually this was for lists too.   
  213.  
  214.  
  215.  
  216. (defun putprop (sym val  indic)
  217.   (cond ((consp sym)
  218.      (setf (getf (cdr sym) indic) val))
  219.     (t (setf (get sym indic) val))))
  220.  
  221.  
  222. (defmacro defprop (sym val indic)
  223.   (cond ((eq indic 'expr)
  224.      `(setf (symbol-function ',sym) #',val))
  225.     (t  `(setf (get ',sym ',indic) ',val))))
  226.  
  227.  
  228.  
  229. (defun sassq (item alist fun)
  230.   (or (sloop for v in alist
  231.          when (eq (car v) item)
  232.          do (loop-return v))
  233.       (funcall fun)))
  234.  
  235. (defun memq (x lis)
  236.   (sloop for v on lis
  237.      when (eq x (car v)) do (return v)))
  238.  
  239. (defun assq (x alist)
  240.   (sloop for v in alist
  241.          when (eq (car v) x)
  242.          do (loop-return v)))
  243.  
  244. (defun delq (x lis &optional (count most-positive-fixnum))
  245.   (declare (fixnum count))
  246.   #+lucid (setq count 16777214) ;;yukkk.
  247.   #+cmu (setq count (min count (1- most-positive-fixnum)))
  248.   (delete x lis :test 'eq :count count))
  249.  
  250. (setf (symbol-function 'lsh) #'ash)
  251.  
  252. (defun haulong (x)
  253.   (integer-length x))
  254.  
  255. (defun bigp (x)
  256.   (typep x 'bignum))
  257.  
  258. (defun  bignump (x)
  259.   (typep x 'bignum))
  260.  
  261. (defun haipart (x n)
  262.   (setq x (abs x))
  263.   (cond ((< x 0)
  264.      (logand x (1- (ash 1 (- n)))))
  265.     (t (ash x (min (- n (integer-length x))
  266.                0)))))
  267.  
  268. (defun haipart (x n)
  269.   (setq x (abs x))
  270.   (cond ((< n 0)
  271.      (logand x (1- (ash 1 (- n)))))
  272.     (t (ash x (min (- n (integer-length x))
  273.                0)))))
  274.  
  275. ; also correct but slower.
  276. ;(defun haipart (integer count)
  277. ;  (let ((x (abs integer)))
  278. ;    (if (minusp count)
  279. ;      (ldb (byte (- count) 0) x)
  280. ;      (ldb (byte count (max 0 (- (integer-length x) count))) x))))
  281.  
  282. (defmacro aset (val ar &rest inds)
  283.   `(setf (aref ,ar ,@ inds) ,val))
  284.  
  285.  
  286. ;;used in translation
  287. (defun fset (sym val)
  288.   (setf (symbol-function sym) val))
  289.  
  290. (defun oldget (plist indic)
  291.   (declare (object plist))
  292.   (cond ((symbolp plist)
  293.      (setq plist (symbol-plist plist)))
  294.     ((consp plist) (setq plist (cdr plist)))
  295.     (t (return-from oldget nil)))
  296.   (sloop for tail on plist by 'cddr
  297.      when (eq (car tail) indic)
  298.      do (loop-return (second tail))))
  299.  
  300. (defun safe-get ( sym prop) (and (symbolp sym) (get sym prop)))
  301. (defmacro safe-getl (sym prop) `(and (symbolp ,sym) (getl ,sym ,prop)))
  302.  
  303. (defun getl (plist indicator-list )
  304.   (declare (object plist))
  305.   (cond ((symbolp plist)
  306.      (setq plist (symbol-plist plist)))
  307.     ((consp plist) (setq plist (cdr plist)))
  308.     (t (return-from getl nil)))
  309.   (sloop for tail on plist by 'cddr
  310.      when (memq (car tail) indicator-list)
  311.      do (loop-return tail)))
  312.  
  313. ;;this is the get of maclisp
  314. ;; works on symbols and plists
  315. (defun maclisp-get (sym-or-plist prop)
  316.   (cond ((symbolp sym-or-plist)
  317.      (get sym-or-plist prop))
  318.     ((consp sym-or-plist)
  319.      (getf (cdr sym-or-plist) prop))
  320.     (t nil)))
  321.  
  322. (defun string-search (stringa stringb &optional from to)
  323.   (or from (setf from 0))
  324.   (if to (search stringa stringb :start2 from  :test #'char-equal :end2 to)
  325.     (search stringa stringb :start2 from :test #'char-equal )))
  326.           
  327. (defmacro ncons (x) `(cons ,x nil))  ;;can one optimize this??
  328.  
  329. (defun zl-assoc (x lis)
  330.   (sloop for v in lis when (equal (car v) x) do (loop-return v)))
  331.  
  332. ;;lucid had troubles
  333. ;(defun zl-delete (x lis &optional (count most-positive-fixnum))
  334. ;  (delete x lis :test 'equal :count count))
  335.  
  336. (defun zl-delete (x lis &optional (count most-positive-fixnum))
  337.   (declare (fixnum count))
  338.   (sloop do (cond ((or (null lis)(<= count 0)) (return-from zl-delete lis))
  339.           ((equal (car lis) x)
  340.            (setq lis (cdr lis) count (f1- count)))
  341.           (t (loop-return nil))))
  342.   (sloop with v = lis 
  343.      while (> count 0)
  344.      while (cdr v)
  345.      when (equal (cadr v) x)
  346.      do (setf count (f1- count))(setf (cdr v) (cddr v))
  347.      else do (setq v (cdr v))
  348.      )
  349.   
  350.         lis)
  351.            
  352.            
  353.         
  354.            
  355.             
  356.  
  357. (defun zl-member (x lis)
  358.   (declare (object x lis))
  359.   (sloop for v on lis
  360.      when (equal (car v) x)
  361.      do (return v)))
  362.      
  363. (defun zl-remove (item list &optional (n most-positive-fixnum))
  364.   #+lucid (setq n 16777214) ;;yukkk.
  365.   #+cmu (setq n (min n (1- most-positive-fixnum))) ; yukkk
  366.   (remove item list :count n :test 'equal))
  367.  
  368. (defvar *acursor* nil)
  369.  
  370. ;; Format of *acursor*.
  371. ;; 0                 1  2  3  4  5    6  7  8  9  10
  372. ;; dim               i1 i2 i3 i4 i5   d1 d2 d3 d4 d5
  373. ;; array dimension   current index    maximal index
  374.  
  375. (defun set-up-cursor (ar)
  376.   (or *acursor* (setf *acursor* (make-array 11 :element-type 'fixnum
  377.                         :initial-element 0)))
  378.   (let ((lis (array-dimensions ar)))
  379.     (setf (aref *acursor* 0) (length lis))
  380.     (sloop for v in lis for i from 6 do (setf (aref *acursor* i) (f- v 1)))
  381.     (sloop for i from 1 to (length lis) do (setf (aref *acursor* i) 0))))
  382.  
  383. (defun aset-by-cursor (ar  val)
  384.   (let ((curs  *acursor*))
  385.     (declare (type (lisp::array fixnum)  curs))
  386.     (ecase (aref curs 0)
  387.       (1 (setf (aref ar (aref curs 1)) val))
  388.       (2 (setf (aref ar (aref curs 1) (aref curs 2)) val))
  389.       (3 (setf (aref ar (aref curs 1) (aref curs 2) (aref curs 3)) val))
  390.       (4 (setf (aref ar (aref curs 1) (aref curs 2) (aref curs 3)
  391.              (aref curs 4)) val))
  392.       (5 (setf (aref ar (aref curs 1) (aref curs 2) (aref curs 3)
  393.              (aref curs 4) (aref curs 5)) val)))
  394.     ;; set the index (`cursor') for the next call to ASET-BY-CURSOR
  395.     (sloop for j downfrom (aref curs 0)
  396.        do (cond ((< (aref curs j) (aref curs (f+ 5 j)))
  397.          (setf (aref curs j) (f+  (aref curs j) 1))
  398.          (return-from aset-by-cursor t))
  399.         (t (setf (aref curs j) 0)))
  400.        (cond ((eql j 0) (return-from aset-by-cursor nil))))))
  401.  
  402. (defun fillarray (ar x)
  403.   (when (symbolp ar)
  404.     (setq ar (get ar 'ARRAY)))
  405.   #+cl
  406.   (when (/= (array-rank ar) 1)
  407.     (setq ar (make-array (array-total-size ar) :displaced-to ar)))
  408.   (setq x
  409.     (cond ((null x)
  410.            (ecase (array-element-type ar)
  411.          (fixnum '(0))
  412.          (float '(0.0))
  413.          ((t) '(nil))))
  414.           ((arrayp x)(listarray x))
  415.           ((atom x) (list x))
  416.           (t x)))
  417.    (when (> (length ar) 0)  
  418.      (set-up-cursor ar)
  419.      (sloop while (aset-by-cursor ar (car x))
  420.     do (and (cdr x) (setq x (cdr x))))))
  421.  
  422. ;(defun fillarray (ar x)
  423. ;  (when (symbolp ar)
  424. ;    (setq ar (get ar 'ARRAY)))
  425. ;  (let ((leng (length (the (lisp:array  t ) ar))))
  426. ;    (declare (fixnum leng))
  427. ;  (cond ((null x)
  428. ;     (setq x (ecase (array-element-type ar)
  429. ;                 (fixnum 0)
  430. ;                 (float 0.0)
  431. ;                 ((t) nil)))
  432. ;     (sloop for i below leng
  433. ;        do (setf (aref ar i) x)))
  434. ;    ((consp x)
  435. ;     (sloop for i below leng
  436. ;        for u in x
  437. ;        do (setf (aref ar i) u)
  438. ;        finally
  439. ;        (sloop for j from i below leng
  440. ;               do (setf (aref ar j) u))))
  441. ;    ((arrayp x)
  442. ;     (sloop for i below (min leng (length x))
  443. ;        do (setf (aref ar i) (aref x i))
  444. ;        finally (sloop for j from i below leng
  445. ;                   with u = (aref x (f- i 1))
  446. ;                   do (setf (aref ar j ) u))))
  447. ;    (t (error "bad second arg to fillarray")))))
  448.  
  449.  
  450. (defun listarray (x)
  451.   (when (symbolp x)
  452.     (setq x (get x 'ARRAY)))
  453.   (cond ((eql (array-rank x) 1)
  454.      (coerce x 'list))
  455.     (t (coerce (make-array (apply '* (array-dimensions x)) :displaced-to x
  456.                    :element-type (array-element-type x))
  457.            'list)))) 
  458.  
  459.  
  460. (defmacro check-arg (place pred &rest res)
  461.   (cond ((atom pred ) (setq pred (list pred place))))
  462.   `(assert ,pred (,place) ,@ res))
  463.  
  464. (defmacro deff (fun val)
  465.   `(setf (symbol-function ',fun) ,val))
  466.  
  467.  
  468. (defmacro xcons (x y)
  469.   (cond ((atom x) `(cons ,y,x))
  470.     (t (let ((g (gensym)))
  471.          `(let ((,g ,x))
  472.         (cons ,y ,g))))))
  473.  
  474. (defun nleft (n x &optional tail)
  475.   (sloop for v on (nthcdr n x)
  476.      for w on x
  477.      when (eq v tail) do (return w)
  478.      finally (return w)))
  479.  
  480.  
  481.  
  482. (defun make-equal-hash-table (not-dim1)
  483.   (let ((table (make-hash-table :test 'equal)))
  484.     (or not-dim1 (setf (gethash 'dim1 table) t))
  485.     table))
  486.   
  487.  
  488. ;;to do check this!!
  489. ;;the following statement does ot seem to be true.
  490. ;;thus range cl::atan = 0,2pi on explorer.
  491. ;; (zl:atan y x) == (cl:atan y x) + 2 pi if latter is  negative
  492.  
  493. ;;range of atan should be [0,2*pi]
  494. (defun atan (y x)
  495.  (let ((tem (lisp::atan y x)))
  496.    (cond((>= tem 0) tem)
  497.     (t (+ tem (* 2 pi))))))
  498.  
  499.  
  500. ;;range of atan2 should be (-pi,pi]
  501. ;;CL manual says that's what lisp::atan is supposed to have.
  502.  
  503. ;;need xcons,nleft, simple-vector-length,make-equal-hash-table
  504.  
  505. (setf (symbol-function 'atan2) (symbol-function 'lisp::atan))
  506.  
  507. (setq *READ-DEFAULT-FLOAT-FORMAT* 'double-float)
  508. #+CLISP (setq *DEFAULT-FLOAT-FORMAT* 'double-float)
  509.  
  510. (defmacro float (x &optional (y 1.0d0)) `(lisp::float ,x ,y))
  511.  
  512. ;; Use the same type as the default value of float..
  513.  
  514. (defconstant *small-flonum* (float least-positive-short-float 1.0d0))
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.