home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume24 / gnucalc / part28 < prev    next >
Encoding:
Text File  |  1991-10-31  |  55.2 KB  |  1,971 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i076:  gnucalc - GNU Emacs Calculator, v2.00, Part28/56
  4. Message-ID: <1991Oct31.072857.18466@sparky.imd.sterling.com>
  5. X-Md4-Signature: 4a70651159786a97b9715548ab7d38bf
  6. Date: Thu, 31 Oct 1991 07:28:57 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 76
  11. Archive-name: gnucalc/part28
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc-undo.el continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 28; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc-undo.el'
  34. else
  35. echo 'x - continuing file calc-undo.el'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc-undo.el' &&
  37. ;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  38. ;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
  39. X
  40. ;; This file is part of GNU Emacs.
  41. X
  42. ;; GNU Emacs is distributed in the hope that it will be useful,
  43. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  44. ;; accepts responsibility to anyone for the consequences of using it
  45. ;; or for whether it serves any particular purpose or works at all,
  46. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  47. ;; License for full details.
  48. X
  49. ;; Everyone is granted permission to copy, modify and redistribute
  50. ;; GNU Emacs, but only under the conditions described in the
  51. ;; GNU Emacs General Public License.   A copy of this license is
  52. ;; supposed to have been given to you along with GNU Emacs so you
  53. ;; can know your rights and responsibilities.  It should be in a
  54. ;; file named COPYING.  Among other things, the copyright notice
  55. ;; and this notice must be preserved on all copies.
  56. X
  57. X
  58. X
  59. ;; This file is autoloaded from calc-ext.el.
  60. (require 'calc-ext)
  61. X
  62. (require 'calc-macs)
  63. X
  64. (defun calc-Need-calc-undo () nil)
  65. X
  66. X
  67. ;;; Undo.
  68. X
  69. (defun calc-undo (n)
  70. X  (interactive "p")
  71. X  (and calc-executing-macro
  72. X       (error "Use C-x e, not X, to run a keyboard macro that uses Undo."))
  73. X  (if (<= n 0)
  74. X      (if (< n 0)
  75. X      (calc-redo (- n))
  76. X    (calc-last-args 1))
  77. X    (calc-wrapper
  78. X     (if (null (nthcdr (1- n) calc-undo-list))
  79. X     (error "No further undo information available"))
  80. X     (setq calc-undo-list
  81. X       (prog1
  82. X           (nthcdr n calc-undo-list)
  83. X         (let ((saved-stack-top calc-stack-top))
  84. X           (let ((calc-stack-top 0))
  85. X         (calc-handle-undos calc-undo-list n))
  86. X           (setq calc-stack-top saved-stack-top))))
  87. X     (message "Undo!")))
  88. )
  89. X
  90. (defun calc-handle-undos (cl n)
  91. X  (if (> n 0)
  92. X      (progn
  93. X    (let ((old-redo calc-redo-list))
  94. X      (setq calc-undo-list nil)
  95. X      (calc-handle-undo (car cl))
  96. X      (setq calc-redo-list (append calc-undo-list old-redo)))
  97. X    (calc-handle-undos (cdr cl) (1- n))))
  98. )
  99. X
  100. (defun calc-handle-undo (list)
  101. X  (and list
  102. X       (let ((action (car list)))
  103. X     (cond
  104. X      ((eq (car action) 'push)
  105. X       (calc-pop-stack 1 (nth 1 action) t))
  106. X      ((eq (car action) 'pop)
  107. X       (calc-push-list (nth 2 action) (nth 1 action)))
  108. X      ((eq (car action) 'set)
  109. X       (calc-record-undo (list 'set (nth 1 action)
  110. X                   (symbol-value (nth 1 action))))
  111. X       (set (nth 1 action) (nth 2 action)))
  112. X      ((eq (car action) 'store)
  113. X       (let ((v (intern (nth 1 action))))
  114. X         (calc-record-undo (list 'store (nth 1 action)
  115. X                     (and (boundp v) (symbol-value v))))
  116. X         (if (y-or-n-p (format "Un-store variable %s? " (nth 1 action)))
  117. X         (progn
  118. X           (if (nth 2 action)
  119. X               (set v (nth 2 action))
  120. X             (makunbound v))
  121. X           (calc-refresh-evaltos v)))))
  122. X      ((eq (car action) 'eval)
  123. X       (calc-record-undo (append (list 'eval (nth 2 action) (nth 1 action))
  124. X                     (cdr (cdr (cdr action)))))
  125. X       (apply (nth 1 action) (cdr (cdr (cdr action))))))
  126. X     (calc-handle-undo (cdr list))))
  127. )
  128. X
  129. (defun calc-redo (n)
  130. X  (interactive "p")
  131. X  (and calc-executing-macro
  132. X       (error "Use C-x e, not X, to run a keyboard macro that uses Redo."))
  133. X  (if (<= n 0)
  134. X      (calc-undo (- n))
  135. X    (calc-wrapper
  136. X     (if (null (nthcdr (1- n) calc-redo-list))
  137. X     (error "Unable to redo"))
  138. X     (setq calc-redo-list
  139. X       (prog1
  140. X           (nthcdr n calc-redo-list)
  141. X         (let ((saved-stack-top calc-stack-top))
  142. X           (let ((calc-stack-top 0))
  143. X         (calc-handle-redos calc-redo-list n))
  144. X           (setq calc-stack-top saved-stack-top))))
  145. X     (message "Redo!")))
  146. )
  147. X
  148. (defun calc-handle-redos (cl n)
  149. X  (if (> n 0)
  150. X      (progn
  151. X    (let ((old-undo calc-undo-list))
  152. X      (setq calc-undo-list nil)
  153. X      (calc-handle-undo (car cl))
  154. X      (setq calc-undo-list (append calc-undo-list old-undo)))
  155. X    (calc-handle-redos (cdr cl) (1- n))))
  156. )
  157. X
  158. (defun calc-last-args (n)
  159. X  (interactive "p")
  160. X  (and calc-executing-macro
  161. X       (error "Use C-x e, not X, to run a keyboard macro that uses last-args."))
  162. X  (calc-wrapper
  163. X   (let ((urec (calc-find-last-x calc-undo-list n)))
  164. X     (if urec
  165. X     (calc-handle-last-x urec)
  166. X       (error "Not enough undo information available"))))
  167. )
  168. X
  169. (defun calc-handle-last-x (list)
  170. X  (and list
  171. X       (let ((action (car list)))
  172. X     (if (eq (car action) 'pop)
  173. X         (calc-pop-push-record-list 0 "larg"
  174. X                    (delq 'top-of-stack (nth 2 action))))
  175. X     (calc-handle-last-x (cdr list))))
  176. )
  177. X
  178. (defun calc-find-last-x (ul n)
  179. X  (and ul
  180. X       (if (calc-undo-does-pushes (car ul))
  181. X       (if (<= n 1)
  182. X           (car ul)
  183. X         (calc-find-last-x (cdr ul) (1- n)))
  184. X     (calc-find-last-x (cdr ul) n)))
  185. )
  186. X
  187. (defun calc-undo-does-pushes (list)
  188. X  (and list
  189. X       (or (eq (car (car list)) 'pop)
  190. X       (calc-undo-does-pushes (cdr list))))
  191. )
  192. X
  193. X
  194. X
  195. SHAR_EOF
  196. echo 'File calc-undo.el is complete' &&
  197. chmod 0644 calc-undo.el ||
  198. echo 'restore of calc-undo.el failed'
  199. Wc_c="`wc -c < 'calc-undo.el'`"
  200. test 4666 -eq "$Wc_c" ||
  201.     echo 'calc-undo.el: original size 4666, current size' "$Wc_c"
  202. rm -f _shar_wnt_.tmp
  203. fi
  204. # ============= calc-vec.el ==============
  205. if test -f 'calc-vec.el' -a X"$1" != X"-c"; then
  206.     echo 'x - skipping calc-vec.el (File already exists)'
  207.     rm -f _shar_wnt_.tmp
  208. else
  209. > _shar_wnt_.tmp
  210. echo 'x - extracting calc-vec.el (Text)'
  211. sed 's/^X//' << 'SHAR_EOF' > 'calc-vec.el' &&
  212. ;; Calculator for GNU Emacs, part II [calc-vec.el]
  213. ;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  214. ;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
  215. X
  216. ;; This file is part of GNU Emacs.
  217. X
  218. ;; GNU Emacs is distributed in the hope that it will be useful,
  219. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  220. ;; accepts responsibility to anyone for the consequences of using it
  221. ;; or for whether it serves any particular purpose or works at all,
  222. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  223. ;; License for full details.
  224. X
  225. ;; Everyone is granted permission to copy, modify and redistribute
  226. ;; GNU Emacs, but only under the conditions described in the
  227. ;; GNU Emacs General Public License.   A copy of this license is
  228. ;; supposed to have been given to you along with GNU Emacs so you
  229. ;; can know your rights and responsibilities.  It should be in a
  230. ;; file named COPYING.  Among other things, the copyright notice
  231. ;; and this notice must be preserved on all copies.
  232. X
  233. X
  234. X
  235. ;; This file is autoloaded from calc-ext.el.
  236. (require 'calc-ext)
  237. X
  238. (require 'calc-macs)
  239. X
  240. (defun calc-Need-calc-vec () nil)
  241. X
  242. X
  243. (defun calc-display-strings (n)
  244. X  (interactive "P")
  245. X  (calc-wrapper
  246. X   (message (if (calc-change-mode 'calc-display-strings n t t)
  247. X        "Displaying vectors of integers as quoted strings."
  248. X          "Displaying vectors of integers normally.")))
  249. )
  250. X
  251. X
  252. (defun calc-pack (n)
  253. X  (interactive "P")
  254. X  (calc-wrapper
  255. X   (let* ((nn (if n 1 2))
  256. X      (mode (if n (prefix-numeric-value n) (calc-top-n 1)))
  257. X      (mode (if (and (Math-vectorp mode) (cdr mode)) (cdr mode)
  258. X          (if (integerp mode) mode
  259. X            (error "Packing mode must be an integer or vector of integers"))))
  260. X      (num (calc-pack-size mode))
  261. X      (items (calc-top-list num nn)))
  262. X     (calc-enter-result (+ nn num -1) "pack" (calc-pack-items mode items))))
  263. )
  264. X
  265. (defun calc-pack-size (mode)
  266. X  (cond ((consp mode)
  267. X     (let ((size 1))
  268. X       (while mode
  269. X         (or (integerp (car mode)) (error "Vector of integers expected"))
  270. X         (setq size (* size (calc-pack-size (car mode)))
  271. X           mode (cdr mode)))
  272. X       (if (= size 0)
  273. X           (error "Zero dimensions not allowed")
  274. X         size)))
  275. X    ((>= mode 0) mode)
  276. X    (t (or (cdr (assq mode '((-3 . 3) (-13 . 1) (-14 . 3) (-15 . 6))))
  277. X           2)))
  278. )
  279. X
  280. (defun calc-pack-items (mode items)
  281. X  (cond ((consp mode)
  282. X     (if (cdr mode)
  283. X         (let* ((size (calc-pack-size (cdr mode)))
  284. X            (len (length items))
  285. X            (new nil)
  286. X            p row)
  287. X           (while (> len 0)
  288. X         (setq p (nthcdr (1- size) items)
  289. X               row items
  290. X               items (cdr p)
  291. X               len (- len size))
  292. X         (setcdr p nil)
  293. X         (setq new (cons (calc-pack-items (cdr mode) row) new)))
  294. X           (calc-pack-items (car mode) (nreverse new)))
  295. X       (calc-pack-items (car mode) items)))
  296. X    ((>= mode 0)
  297. X     (cons 'vec items))
  298. X    ((= mode -3)
  299. X     (if (and (math-objvecp (car items))
  300. X          (math-objvecp (nth 1 items))
  301. X          (math-objvecp (nth 2 items)))
  302. X         (if (and (math-num-integerp (car items))
  303. X              (math-num-integerp (nth 1 items)))
  304. X         (if (math-realp (nth 2 items))
  305. X             (cons 'hms items)
  306. X           (error "Seconds must be real"))
  307. X           (error "Hours and minutes must be integers"))
  308. X       (math-normalize (list '+
  309. X                 (list '+
  310. X                       (if (eq calc-angle-mode 'rad)
  311. X                       (list '* (car items)
  312. X                         '(hms 1 0 0))
  313. X                     (car items))
  314. X                       (list '* (nth 1 items) '(hms 0 1 0)))
  315. X                 (list '* (nth 2 items) '(hms 0 0 1))))))
  316. X    ((= mode -13)
  317. X     (if (math-realp (car items))
  318. X         (cons 'date items)
  319. X       (if (eq (car-safe (car items)) 'date)
  320. X           (car items)
  321. X         (if (math-objvecp (car items))
  322. X         (error "Date value must be real")
  323. X           (cons 'calcFunc-date items)))))
  324. X    ((memq mode '(-14 -15))
  325. X     (let ((p items))
  326. X       (while (and p (math-objvecp (car p)))
  327. X         (or (math-integerp (car p))
  328. X         (error "Components must be integers"))
  329. X         (setq p (cdr p)))
  330. X       (if p
  331. X           (cons 'calcFunc-date items)
  332. X         (list 'date (math-dt-to-date items)))))
  333. X    ((or (eq (car-safe (car items)) 'vec)
  334. X         (eq (car-safe (nth 1 items)) 'vec))
  335. X     (let* ((x (car items))
  336. X        (vx (eq (car-safe x) 'vec))
  337. X        (y (nth 1 items))
  338. X        (vy (eq (car-safe y) 'vec))
  339. X        (z nil)
  340. X        (n (1- (length (if vx x y)))))
  341. X       (and vx vy
  342. X        (/= n (1- (length y)))
  343. X        (error "Vectors must be the same length"))
  344. X       (while (>= (setq n (1- n)) 0)
  345. X         (setq z (cons (calc-pack-items
  346. X                mode
  347. X                (list (if vx (car (setq x (cdr x))) x)
  348. X                  (if vy (car (setq y (cdr y))) y)))
  349. X               z)))
  350. X       (cons 'vec (nreverse z))))
  351. X    ((= mode -1)
  352. X     (if (and (math-realp (car items)) (math-realp (nth 1 items)))
  353. X         (cons 'cplx items)
  354. X       (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  355. X           (error "Components must be real"))
  356. X       (math-normalize (list '+ (car items)
  357. X                 (list '* (nth 1 items) '(cplx 0 1))))))
  358. X    ((= mode -2)
  359. X     (if (and (math-realp (car items)) (math-anglep (nth 1 items)))
  360. X         (cons 'polar items)
  361. X       (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  362. X           (error "Components must be real"))
  363. X       (math-normalize (list '* (car items)
  364. X                 (if (math-anglep (nth 1 items))
  365. X                     (list 'polar 1 (nth 1 items))
  366. X                   (list 'calcFunc-exp
  367. X                     (list '*
  368. X                           (math-to-radians-2
  369. X                        (nth 1 items))
  370. X                           (list 'polar
  371. X                             1
  372. X                             (math-quarter-circle
  373. X                              nil)))))))))
  374. X    ((= mode -4)
  375. X     (let ((x (car items))
  376. X           (sigma (nth 1 items)))
  377. X       (if (or (math-scalarp x) (not (math-objvecp x)))
  378. X           (if (or (math-anglep sigma) (not (math-objvecp sigma)))
  379. X           (math-make-sdev x sigma)
  380. X         (error "Error component must be real"))
  381. X         (error "Mean component must be real or complex"))))
  382. X    ((= mode -5)
  383. X     (let ((a (car items))
  384. X           (m (nth 1 items)))
  385. X       (if (and (math-anglep a) (math-anglep m))
  386. X           (if (math-posp m)
  387. X           (math-make-mod a m)
  388. X         (error "Modulus must be positive"))
  389. X         (if (and (math-objectp a) (math-objectp m))
  390. X         (error "Components must be real"))
  391. X         (list 'calcFunc-makemod a m))))
  392. X    ((memq mode '(-6 -7 -8 -9))
  393. X     (let ((lo (car items))
  394. X           (hi (nth 1 items)))
  395. X       (if (and (or (math-anglep lo) (eq (car lo) 'date)
  396. X            (not (math-objvecp lo)))
  397. X            (or (math-anglep hi) (eq (car hi) 'date)
  398. X            (not (math-objvecp hi))))
  399. X           (math-make-intv (+ mode 9) lo hi)
  400. X         (error "Components must be real"))))
  401. X    ((eq mode -10)
  402. X     (if (math-zerop (nth 1 items))
  403. X         (error "Denominator must not be zero")
  404. X       (if (and (math-integerp (car items)) (math-integerp (nth 1 items)))
  405. X           (math-normalize (cons 'frac items))
  406. X         (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  407. X         (error "Components must be integers"))
  408. X         (cons 'calcFunc-fdiv items))))
  409. X    ((memq mode '(-11 -12))
  410. X     (if (and (math-realp (car items)) (math-integerp (nth 1 items)))
  411. X         (calcFunc-scf (math-float (car items)) (nth 1 items))
  412. X       (if (and (math-objectp (car items)) (math-objectp (nth 1 items)))
  413. X           (error "Components must be integers"))
  414. X       (math-normalize
  415. X        (list 'calcFunc-scf
  416. X          (list 'calcFunc-float (car items))
  417. X          (nth 1 items)))))
  418. X    (t
  419. X     (error "Invalid packing mode: %d" mode)))
  420. )
  421. X
  422. (defun calc-unpack (mode)
  423. X  (interactive "P")
  424. X  (calc-wrapper
  425. X   (let ((calc-unpack-with-type t))
  426. X     (calc-pop-push-record-list 1 "unpk" (calc-unpack-item
  427. X                      (and mode
  428. X                           (prefix-numeric-value mode))
  429. X                      (calc-top)))))
  430. )
  431. X
  432. (defun calc-unpack-type (item)
  433. X  (cond ((eq (car-safe item) 'vec)
  434. X     (1- (length item)))
  435. X    ((eq (car-safe item) 'intv)
  436. X     (- (nth 1 item) 9))
  437. X    (t
  438. X     (or (cdr (assq (car-safe item) '( (cplx . -1) (polar . -2)
  439. X                       (hms . -3) (sdev . -4) (mod . -5)
  440. X                       (frac . -10) (float . -11)
  441. X                       (date . -13) )))
  442. X         (error "Argument must be a composite object"))))
  443. )
  444. X
  445. (defun calc-unpack-item (mode item)
  446. X  (cond ((not mode)
  447. X     (if (or (and (not (memq (car-safe item) '(frac float cplx polar vec
  448. X                            hms date sdev mod
  449. X                            intv)))
  450. X              (math-objvecp item))
  451. X         (eq (car-safe item) 'var))
  452. X         (error "Argument must be a composite object or function call"))
  453. X     (if (eq (car item) 'intv)
  454. X         (cdr (cdr item))
  455. X       (cdr item)))
  456. X    ((> mode 0)
  457. X     (let ((dims nil)
  458. X           type new row)
  459. X       (setq item (list item))
  460. X       (while (> mode 0)
  461. X         (setq type (calc-unpack-type (car item))
  462. X           dims (cons type dims)
  463. X           new (calc-unpack-item nil (car item)))
  464. X         (while (setq item (cdr item))
  465. X           (or (= (calc-unpack-type (car item)) type)
  466. X           (error "Inconsistent types or dimensions in vector elements"))
  467. X           (setq new (append new (calc-unpack-item nil (car item)))))
  468. X         (setq item new
  469. X           mode (1- mode)))
  470. X       (if (cdr dims) (setq dims (list (cons 'vec (nreverse dims)))))
  471. X       (cond ((eq calc-unpack-with-type 'pair)
  472. X          (list (car dims) (cons 'vec item)))
  473. X         (calc-unpack-with-type
  474. X          (append item dims))
  475. X         (t item))))
  476. X    ((eq calc-unpack-with-type 'pair)
  477. X     (let ((calc-unpack-with-type nil))
  478. X       (list mode (cons 'vec (calc-unpack-item mode item)))))
  479. X    ((= mode -3)
  480. X     (if (eq (car-safe item) 'hms)
  481. X         (cdr item)
  482. X       (error "Argument must be an HMS form")))
  483. X    ((= mode -13)
  484. X     (if (eq (car-safe item) 'date)
  485. X         (cdr item)
  486. X       (error "Argument must be a date form")))
  487. X    ((= mode -14)
  488. X     (if (eq (car-safe item) 'date)
  489. X         (math-date-to-dt (math-floor (nth 1 item)))
  490. X       (error "Argument must be a date form")))
  491. X    ((= mode -15)
  492. X     (if (eq (car-safe item) 'date)
  493. X         (append (math-date-to-dt (nth 1 item))
  494. X             (and (not (math-integerp (nth 1 item)))
  495. X              (list 0 0 0)))
  496. X       (error "Argument must be a date form")))
  497. X    ((eq (car-safe item) 'vec)
  498. X     (let ((x nil)
  499. X           (y nil)
  500. X           res)
  501. X       (while (setq item (cdr item))
  502. X         (setq res (calc-unpack-item mode (car item))
  503. X           x (cons (car res) x)
  504. X           y (cons (nth 1 res) y)))
  505. X       (list (cons 'vec (nreverse x))
  506. X         (cons 'vec (nreverse y)))))
  507. X    ((= mode -1)
  508. X     (if (eq (car-safe item) 'cplx)
  509. X         (cdr item)
  510. X       (if (eq (car-safe item) 'polar)
  511. X           (cdr (math-complex item))
  512. X         (if (Math-realp item)
  513. X         (list item 0)
  514. X           (error "Argument must be a complex number")))))
  515. X    ((= mode -2)
  516. X     (if (or (memq (car-safe item) '(cplx polar))
  517. X         (Math-realp item))
  518. X         (cdr (math-polar item))
  519. X       (error "Argument must be a complex number")))
  520. X    ((= mode -4)
  521. X     (if (eq (car-safe item) 'sdev)
  522. X         (cdr item)
  523. X       (list item 0)))
  524. X    ((= mode -5)
  525. X     (if (eq (car-safe item) 'mod)
  526. X         (cdr item)
  527. X       (error "Argument must be a modulo form")))
  528. X    ((memq mode '(-6 -7 -8 -9))
  529. X     (if (eq (car-safe item) 'intv)
  530. X         (cdr (cdr item))
  531. X       (list item item)))
  532. X    ((= mode -10)
  533. X     (if (eq (car-safe item) 'frac)
  534. X         (cdr item)
  535. X       (if (Math-integerp item)
  536. X           (list item 1)
  537. X         (error "Argument must be a rational number"))))
  538. X    ((= mode -11)
  539. X     (if (eq (car-safe item) 'float)
  540. X         (list (nth 1 item) (math-normalize (nth 2 item)))
  541. X       (error "Expected a floating-point number")))
  542. X    ((= mode -12)
  543. X     (if (eq (car-safe item) 'float)
  544. X         (list (calcFunc-mant item) (calcFunc-xpon item))
  545. X       (error "Expected a floating-point number")))
  546. X    (t
  547. X     (error "Invalid unpacking mode: %d" mode)))
  548. )
  549. (setq calc-unpack-with-type nil)
  550. X
  551. (defun calc-diag (n)
  552. X  (interactive "P")
  553. X  (calc-wrapper
  554. X   (calc-enter-result 1 "diag" (if n
  555. X                   (list 'calcFunc-diag (calc-top-n 1)
  556. X                     (prefix-numeric-value n))
  557. X                 (list 'calcFunc-diag (calc-top-n 1)))))
  558. )
  559. X
  560. (defun calc-ident (n)
  561. X  (interactive "NDimension of identity matrix = ")
  562. X  (calc-wrapper
  563. X   (calc-enter-result 0 "idn" (if (eq n 0)
  564. X                  '(calcFunc-idn 1)
  565. X                (list 'calcFunc-idn 1
  566. X                      (prefix-numeric-value n)))))
  567. )
  568. X
  569. (defun calc-index (n &optional stack)
  570. X  (interactive "NSize of vector = \nP")
  571. X  (calc-wrapper
  572. X   (if (consp stack)
  573. X       (calc-enter-result 3 "indx" (cons 'calcFunc-index (calc-top-list-n 3)))
  574. X     (calc-enter-result 0 "indx" (list 'calcFunc-index
  575. X                       (prefix-numeric-value n)))))
  576. )
  577. X
  578. (defun calc-build-vector (n)
  579. X  (interactive "NSize of vector = ")
  580. X  (calc-wrapper
  581. X   (calc-enter-result 1 "bldv" (list 'calcFunc-cvec
  582. X                     (calc-top-n 1)
  583. X                     (prefix-numeric-value n))))
  584. )
  585. X
  586. (defun calc-cons (arg)
  587. X  (interactive "P")
  588. X  (calc-wrapper
  589. X   (if (calc-is-hyperbolic)
  590. X       (calc-binary-op "rcns" 'calcFunc-rcons arg)
  591. X     (calc-binary-op "cons" 'calcFunc-cons arg)))
  592. )
  593. X
  594. X
  595. (defun calc-head (arg)
  596. X  (interactive "P")
  597. X  (calc-wrapper
  598. X   (if (calc-is-inverse)
  599. X       (if (calc-is-hyperbolic)
  600. X       (calc-unary-op "rtai" 'calcFunc-rtail arg)
  601. X     (calc-unary-op "tail" 'calcFunc-tail arg))
  602. X     (if (calc-is-hyperbolic)
  603. X     (calc-unary-op "rhed" 'calcFunc-rhead arg)
  604. X       (calc-unary-op "head" 'calcFunc-head arg))))
  605. )
  606. X
  607. (defun calc-tail (arg)
  608. X  (interactive "P")
  609. X  (calc-invert-func)
  610. X  (calc-head arg)
  611. )
  612. X
  613. (defun calc-vlength (arg)
  614. X  (interactive "P")
  615. X  (calc-wrapper
  616. X   (if (calc-is-hyperbolic)
  617. X       (calc-unary-op "dims" 'calcFunc-mdims arg)
  618. X     (calc-unary-op "len" 'calcFunc-vlen arg)))
  619. )
  620. X
  621. (defun calc-arrange-vector (n)
  622. X  (interactive "NNumber of columns = ")
  623. X  (calc-wrapper
  624. X   (calc-enter-result 1 "arng" (list 'calcFunc-arrange (calc-top-n 1)
  625. X                     (prefix-numeric-value n))))
  626. )
  627. X
  628. (defun calc-vector-find (arg)
  629. X  (interactive "P")
  630. X  (calc-wrapper
  631. X   (let ((func (cons 'calcFunc-find (calc-top-list-n 2))))
  632. X     (calc-enter-result
  633. X      2 "find"
  634. X      (if arg (append func (list (prefix-numeric-value arg))) func))))
  635. )
  636. X
  637. (defun calc-subvector ()
  638. X  (interactive)
  639. X  (calc-wrapper
  640. X   (if (calc-is-inverse)
  641. X       (calc-enter-result 3 "rsvc" (cons 'calcFunc-rsubvec
  642. X                     (calc-top-list-n 3)))
  643. X     (calc-enter-result 3 "svec" (cons 'calcFunc-subvec (calc-top-list-n 3)))))
  644. )
  645. X
  646. (defun calc-reverse-vector (arg)
  647. X  (interactive "P")
  648. X  (calc-wrapper
  649. X   (calc-unary-op "rev" 'calcFunc-rev arg))
  650. )
  651. X
  652. (defun calc-mask-vector (arg)
  653. X  (interactive "P")
  654. X  (calc-wrapper
  655. X   (calc-binary-op "vmsk" 'calcFunc-vmask arg))
  656. )
  657. X
  658. (defun calc-expand-vector (arg)
  659. X  (interactive "P")
  660. X  (calc-wrapper
  661. X   (if (calc-is-hyperbolic)
  662. X       (calc-enter-result 3 "vexp" (cons 'calcFunc-vexp (calc-top-list-n 3)))
  663. X     (calc-binary-op "vexp" 'calcFunc-vexp arg)))
  664. )
  665. X
  666. (defun calc-sort ()
  667. X  (interactive)
  668. X  (calc-slow-wrapper
  669. X   (if (calc-is-inverse)
  670. X       (calc-enter-result 1 "rsrt" (list 'calcFunc-rsort (calc-top-n 1)))
  671. X     (calc-enter-result 1 "sort" (list 'calcFunc-sort (calc-top-n 1)))))
  672. )
  673. X
  674. (defun calc-grade ()
  675. X  (interactive)
  676. X  (calc-slow-wrapper
  677. X   (if (calc-is-inverse)
  678. X       (calc-enter-result 1 "rgrd" (list 'calcFunc-rgrade (calc-top-n 1)))
  679. X     (calc-enter-result 1 "grad" (list 'calcFunc-grade (calc-top-n 1)))))
  680. )
  681. X
  682. (defun calc-histogram (n)
  683. X  (interactive "NNumber of bins: ")
  684. X  (calc-slow-wrapper
  685. X   (if calc-hyperbolic-flag
  686. X       (calc-enter-result 2 "hist" (list 'calcFunc-histogram
  687. X                     (calc-top-n 2)
  688. X                     (calc-top-n 1)
  689. X                     (prefix-numeric-value n)))
  690. X     (calc-enter-result 1 "hist" (list 'calcFunc-histogram
  691. X                       (calc-top-n 1)
  692. X                       (prefix-numeric-value n)))))
  693. )
  694. X
  695. (defun calc-transpose (arg)
  696. X  (interactive "P")
  697. X  (calc-wrapper
  698. X   (calc-unary-op "trn" 'calcFunc-trn arg))
  699. )
  700. X
  701. (defun calc-conj-transpose (arg)
  702. X  (interactive "P")
  703. X  (calc-wrapper
  704. X   (calc-unary-op "ctrn" 'calcFunc-ctrn arg))
  705. )
  706. X
  707. (defun calc-cross (arg)
  708. X  (interactive "P")
  709. X  (calc-wrapper
  710. X   (calc-binary-op "cros" 'calcFunc-cross arg))
  711. )
  712. X
  713. (defun calc-remove-duplicates (arg)
  714. X  (interactive "P")
  715. X  (calc-wrapper
  716. X   (calc-unary-op "rdup" 'calcFunc-rdup arg))
  717. )
  718. X
  719. (defun calc-set-union (arg)
  720. X  (interactive "P")
  721. X  (calc-wrapper
  722. X   (calc-binary-op "unio" 'calcFunc-vunion arg '(vec) 'calcFunc-rdup))
  723. )
  724. X
  725. (defun calc-set-intersect (arg)
  726. X  (interactive "P")
  727. X  (calc-wrapper
  728. X   (calc-binary-op "intr" 'calcFunc-vint arg '(vec) 'calcFunc-rdup))
  729. )
  730. X
  731. (defun calc-set-difference (arg)
  732. X  (interactive "P")
  733. X  (calc-wrapper
  734. X   (calc-binary-op "diff" 'calcFunc-vdiff arg '(vec) 'calcFunc-rdup))
  735. )
  736. X
  737. (defun calc-set-xor (arg)
  738. X  (interactive "P")
  739. X  (calc-wrapper
  740. X   (calc-binary-op "xor" 'calcFunc-vxor arg '(vec) 'calcFunc-rdup))
  741. )
  742. X
  743. (defun calc-set-complement (arg)
  744. X  (interactive "P")
  745. X  (calc-wrapper
  746. X   (calc-unary-op "cmpl" 'calcFunc-vcompl arg))
  747. )
  748. X
  749. (defun calc-set-floor (arg)
  750. X  (interactive "P")
  751. X  (calc-wrapper
  752. X   (calc-unary-op "vflr" 'calcFunc-vfloor arg))
  753. )
  754. X
  755. (defun calc-set-enumerate (arg)
  756. X  (interactive "P")
  757. X  (calc-wrapper
  758. X   (calc-unary-op "enum" 'calcFunc-venum arg))
  759. )
  760. X
  761. (defun calc-set-span (arg)
  762. X  (interactive "P")
  763. X  (calc-wrapper
  764. X   (calc-unary-op "span" 'calcFunc-vspan arg))
  765. )
  766. X
  767. (defun calc-set-cardinality (arg)
  768. X  (interactive "P")
  769. X  (calc-wrapper
  770. X   (calc-unary-op "card" 'calcFunc-vcard arg))
  771. )
  772. X
  773. (defun calc-unpack-bits (arg)
  774. X  (interactive "P")
  775. X  (calc-wrapper
  776. X   (if (calc-is-inverse)
  777. X       (calc-unary-op "bpck" 'calcFunc-vpack arg)
  778. X     (calc-unary-op "bupk" 'calcFunc-vunpack arg)))
  779. )
  780. X
  781. (defun calc-pack-bits (arg)
  782. X  (interactive "P")
  783. X  (calc-invert-func)
  784. X  (calc-unpack-bits arg)
  785. )
  786. X
  787. X
  788. (defun calc-rnorm (arg)
  789. X  (interactive "P")
  790. X  (calc-wrapper
  791. X   (calc-unary-op "rnrm" 'calcFunc-rnorm arg))
  792. )
  793. X
  794. (defun calc-cnorm (arg)
  795. X  (interactive "P")
  796. X  (calc-wrapper
  797. X   (calc-unary-op "cnrm" 'calcFunc-cnorm arg))
  798. )
  799. X
  800. (defun calc-mrow (n &optional nn)
  801. X  (interactive "NRow number: \nP")
  802. X  (calc-wrapper
  803. X   (if (consp nn)
  804. X       (calc-enter-result 2 "mrow" (cons 'calcFunc-mrow (calc-top-list-n 2)))
  805. X     (setq n (prefix-numeric-value n))
  806. X     (if (= n 0)
  807. X     (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1)))
  808. X       (if (< n 0)
  809. X       (calc-enter-result 1 "rrow" (list 'calcFunc-mrrow
  810. X                         (calc-top-n 1) (- n)))
  811. X     (calc-enter-result 1 "mrow" (list 'calcFunc-mrow
  812. X                       (calc-top-n 1) n))))))
  813. )
  814. X
  815. (defun calc-mcol (n &optional nn)
  816. X  (interactive "NColumn number: \nP")
  817. X  (calc-wrapper
  818. X   (if (consp nn)
  819. X       (calc-enter-result 2 "mcol" (cons 'calcFunc-mcol (calc-top-list-n 2)))
  820. X     (setq n (prefix-numeric-value n))
  821. X     (if (= n 0)
  822. X     (calc-enter-result 1 "getd" (list 'calcFunc-getdiag (calc-top-n 1)))
  823. X       (if (< n 0)
  824. X       (calc-enter-result 1 "rcol" (list 'calcFunc-mrcol
  825. X                         (calc-top-n 1) (- n)))
  826. X     (calc-enter-result 1 "mcol" (list 'calcFunc-mcol
  827. X                       (calc-top-n 1) n))))))
  828. )
  829. X
  830. X
  831. ;;;; Vectors.
  832. X
  833. (defun calcFunc-mdims (m)
  834. X  (or (math-vectorp m)
  835. X      (math-reject-arg m 'vectorp))
  836. X  (cons 'vec (math-mat-dimens m))
  837. )
  838. X
  839. X
  840. ;;; Apply a function elementwise to vector A.  [V X V; N X N] [Public]
  841. (defun math-map-vec (f a)
  842. X  (if (math-vectorp a)
  843. X      (cons 'vec (mapcar f (cdr a)))
  844. X    (funcall f a))
  845. )
  846. X
  847. (defun math-dimension-error ()
  848. X  (calc-record-why "*Dimension error")
  849. X  (signal 'wrong-type-argument nil)
  850. )
  851. X
  852. X
  853. ;;; Build a vector out of a list of objects.  [Public]
  854. (defun calcFunc-vec (&rest objs)
  855. X  (cons 'vec objs)
  856. )
  857. X
  858. X
  859. ;;; Build a constant vector or matrix.  [Public]
  860. (defun calcFunc-cvec (obj &rest dims)
  861. X  (math-make-vec-dimen obj dims)
  862. )
  863. X
  864. (defun math-make-vec-dimen (obj dims)
  865. X  (if dims
  866. X      (if (natnump (car dims))
  867. X      (if (or (cdr dims)
  868. X          (not (math-numberp obj)))
  869. X          (cons 'vec (copy-sequence
  870. X              (make-list (car dims)
  871. X                     (math-make-vec-dimen obj (cdr dims)))))
  872. X        (cons 'vec (make-list (car dims) obj)))
  873. X    (math-reject-arg (car dims) 'fixnatnump))
  874. X    obj)
  875. )
  876. X
  877. (defun calcFunc-head (vec)
  878. X  (if (and (Math-vectorp vec)
  879. X       (cdr vec))
  880. X      (nth 1 vec)
  881. X    (calc-record-why 'vectorp vec)
  882. X    (list 'calcFunc-head vec))
  883. )
  884. X
  885. (defun calcFunc-tail (vec)
  886. X  (if (and (Math-vectorp vec)
  887. X       (cdr vec))
  888. X      (cons 'vec (cdr (cdr vec)))
  889. X    (calc-record-why 'vectorp vec)
  890. X    (list 'calcFunc-tail vec))
  891. )
  892. X
  893. (defun calcFunc-cons (head tail)
  894. X  (if (Math-vectorp tail)
  895. X      (cons 'vec (cons head (cdr tail)))
  896. X    (calc-record-why 'vectorp tail)
  897. X    (list 'calcFunc-cons head tail))
  898. )
  899. X
  900. (defun calcFunc-rhead (vec)
  901. X  (if (and (Math-vectorp vec)
  902. X       (cdr vec))
  903. X      (let ((vec (copy-sequence vec)))
  904. X    (setcdr (nthcdr (- (length vec) 2) vec) nil)
  905. X    vec)
  906. X    (calc-record-why 'vectorp vec)
  907. X    (list 'calcFunc-rhead vec))
  908. )
  909. X
  910. (defun calcFunc-rtail (vec)
  911. X  (if (and (Math-vectorp vec)
  912. X       (cdr vec))
  913. X      (nth (1- (length vec)) vec)
  914. X    (calc-record-why 'vectorp vec)
  915. X    (list 'calcFunc-rtail vec))
  916. )
  917. X
  918. (defun calcFunc-rcons (head tail)
  919. X  (if (Math-vectorp head)
  920. X      (append head (list tail))
  921. X    (calc-record-why 'vectorp head)
  922. X    (list 'calcFunc-rcons head tail))
  923. )
  924. X
  925. X
  926. X
  927. ;;; Apply a function elementwise to vectors A and B.  [O X O O] [Public]
  928. (defun math-map-vec-2 (f a b)
  929. X  (if (math-vectorp a)
  930. X      (if (math-vectorp b)
  931. X      (let ((v nil))
  932. X        (while (setq a (cdr a))
  933. X          (or (setq b (cdr b))
  934. X          (math-dimension-error))
  935. X          (setq v (cons (funcall f (car a) (car b)) v)))
  936. X        (if a (math-dimension-error))
  937. X        (cons 'vec (nreverse v)))
  938. X    (let ((v nil))
  939. X      (while (setq a (cdr a))
  940. X        (setq v (cons (funcall f (car a) b) v)))
  941. X      (cons 'vec (nreverse v))))
  942. X    (if (math-vectorp b)
  943. X    (let ((v nil))
  944. X      (while (setq b (cdr b))
  945. X        (setq v (cons (funcall f a (car b)) v)))
  946. X      (cons 'vec (nreverse v)))
  947. X      (funcall f a b)))
  948. )
  949. X
  950. X
  951. X
  952. ;;; "Reduce" a function over a vector (left-associatively).  [O X V] [Public]
  953. (defun math-reduce-vec (f a)
  954. X  (if (math-vectorp a)
  955. X      (if (cdr a)
  956. X      (let ((accum (car (setq a (cdr a)))))
  957. X        (while (setq a (cdr a))
  958. X          (setq accum (funcall f accum (car a))))
  959. X        accum)
  960. X    0)
  961. X    a)
  962. )
  963. X
  964. ;;; Reduce a function over the columns of matrix A.  [V X V] [Public]
  965. (defun math-reduce-cols (f a)
  966. X  (if (math-matrixp a)
  967. X      (cons 'vec (math-reduce-cols-col-step f (cdr a) 1 (length (nth 1 a))))
  968. X    a)
  969. )
  970. X
  971. (defun math-reduce-cols-col-step (f a col cols)
  972. X  (and (< col cols)
  973. X       (cons (math-reduce-cols-row-step f (nth col (car a)) col (cdr a))
  974. X         (math-reduce-cols-col-step f a (1+ col) cols)))
  975. )
  976. X
  977. (defun math-reduce-cols-row-step (f tot col a)
  978. X  (if a
  979. X      (math-reduce-cols-row-step f
  980. X                 (funcall f tot (nth col (car a)))
  981. X                 col
  982. X                 (cdr a))
  983. X    tot)
  984. )
  985. X
  986. X
  987. X
  988. (defun math-dot-product (a b)
  989. X  (if (setq a (cdr a) b (cdr b))
  990. X      (let ((accum (math-mul (car a) (car b))))
  991. X    (while (setq a (cdr a) b (cdr b))
  992. X      (setq accum (math-add accum (math-mul (car a) (car b)))))
  993. X    accum)
  994. X    0)
  995. )
  996. X
  997. X
  998. ;;; Return the number of elements in vector V.  [Public]
  999. (defun calcFunc-vlen (v)
  1000. X  (if (math-vectorp v)
  1001. X      (1- (length v))
  1002. X    (if (math-objectp v)
  1003. X    0
  1004. X      (list 'calcFunc-vlen v)))
  1005. )
  1006. X
  1007. ;;; Get the Nth row of a matrix.
  1008. (defun calcFunc-mrow (mat n)   ; [Public]
  1009. X  (if (Math-vectorp n)
  1010. X      (math-map-vec (function (lambda (x) (calcFunc-mrow mat x))) n)
  1011. X    (if (and (eq (car-safe n) 'intv) (math-constp n))
  1012. X    (calcFunc-subvec mat
  1013. X             (math-add (nth 2 n) (if (memq (nth 1 n) '(2 3)) 0 1))
  1014. X             (math-add (nth 3 n) (if (memq (nth 1 n) '(1 3)) 1 0)))
  1015. X      (or (and (integerp (setq n (math-check-integer n)))
  1016. X           (> n 0))
  1017. X      (math-reject-arg n 'fixposintp))
  1018. X      (or (Math-vectorp mat)
  1019. X      (math-reject-arg mat 'vectorp))
  1020. X      (or (nth n mat)
  1021. X      (math-reject-arg n "*Index out of range"))))
  1022. )
  1023. X
  1024. (defun calcFunc-subscr (mat n &optional m)
  1025. X  (setq mat (calcFunc-mrow mat n))
  1026. X  (if m
  1027. X      (if (math-num-integerp n)
  1028. X      (calcFunc-mrow mat m)
  1029. X    (calcFunc-mcol mat m))
  1030. X    mat)
  1031. )
  1032. X
  1033. ;;; Get the Nth column of a matrix.
  1034. (defun math-mat-col (mat n)
  1035. X  (cons 'vec (mapcar (function (lambda (x) (elt x n))) (cdr mat)))
  1036. )
  1037. X
  1038. (defun calcFunc-mcol (mat n)   ; [Public]
  1039. X  (if (Math-vectorp n)
  1040. X      (calcFunc-trn
  1041. X       (math-map-vec (function (lambda (x) (calcFunc-mcol mat x))) n))
  1042. X    (if (and (eq (car-safe n) 'intv) (math-constp n))
  1043. X    (if (math-matrixp mat)
  1044. X        (math-map-vec (function (lambda (x) (calcFunc-mrow x n))) mat)
  1045. X      (calcFunc-mrow mat n))
  1046. X      (or (and (integerp (setq n (math-check-integer n)))
  1047. X           (> n 0))
  1048. X      (math-reject-arg n 'fixposintp))
  1049. X      (or (Math-vectorp mat)
  1050. X      (math-reject-arg mat 'vectorp))
  1051. X      (or (if (math-matrixp mat)
  1052. X          (and (< n (length (nth 1 mat)))
  1053. X           (math-mat-col mat n))
  1054. X        (nth n mat))
  1055. X      (math-reject-arg n "*Index out of range"))))
  1056. )
  1057. X
  1058. ;;; Remove the Nth row from a matrix.
  1059. (defun math-mat-less-row (mat n)
  1060. X  (if (<= n 0)
  1061. X      (cdr mat)
  1062. X    (cons (car mat)
  1063. X      (math-mat-less-row (cdr mat) (1- n))))
  1064. )
  1065. X
  1066. (defun calcFunc-mrrow (mat n)   ; [Public]
  1067. X  (and (integerp (setq n (math-check-integer n)))
  1068. X       (> n 0)
  1069. X       (< n (length mat))
  1070. X       (math-mat-less-row mat n))
  1071. )
  1072. X
  1073. ;;; Remove the Nth column from a matrix.
  1074. (defun math-mat-less-col (mat n)
  1075. X  (cons 'vec (mapcar (function (lambda (x) (math-mat-less-row x n)))
  1076. X             (cdr mat)))
  1077. )
  1078. X
  1079. (defun calcFunc-mrcol (mat n)   ; [Public]
  1080. X  (and (integerp (setq n (math-check-integer n)))
  1081. X       (> n 0)
  1082. X       (if (math-matrixp mat)
  1083. X       (and (< n (length (nth 1 mat)))
  1084. X        (math-mat-less-col mat n))
  1085. X     (math-mat-less-row mat n)))
  1086. )
  1087. X
  1088. (defun calcFunc-getdiag (mat)   ; [Public]
  1089. X  (if (math-square-matrixp mat)
  1090. X      (cons 'vec (math-get-diag-step (cdr mat) 1))
  1091. X    (calc-record-why 'square-matrixp mat)
  1092. X    (list 'calcFunc-getdiag mat))
  1093. )
  1094. X
  1095. (defun math-get-diag-step (row n)
  1096. X  (and row
  1097. X       (cons (nth n (car row))
  1098. X         (math-get-diag-step (cdr row) (1+ n))))
  1099. )
  1100. X
  1101. (defun math-transpose (mat)   ; [Public]
  1102. X  (let ((m nil)
  1103. X    (col (length (nth 1 mat))))
  1104. X    (while (> (setq col (1- col)) 0)
  1105. X      (setq m (cons (math-mat-col mat col) m)))
  1106. X    (cons 'vec m))
  1107. )
  1108. X
  1109. (defun calcFunc-trn (mat)
  1110. X  (if (math-vectorp mat)
  1111. X      (if (math-matrixp mat)
  1112. X      (math-transpose mat)
  1113. X    (math-col-matrix mat))
  1114. X    (if (math-numberp mat)
  1115. X    mat
  1116. X      (math-reject-arg mat 'matrixp)))
  1117. )
  1118. X
  1119. (defun calcFunc-ctrn (mat)
  1120. X  (calcFunc-conj (calcFunc-trn mat))
  1121. )
  1122. X
  1123. (defun calcFunc-pack (mode els)
  1124. X  (or (Math-vectorp els) (math-reject-arg els 'vectorp))
  1125. X  (if (and (Math-vectorp mode) (cdr mode))
  1126. X      (setq mode (cdr mode))
  1127. X    (or (integerp mode) (math-reject-arg mode 'fixnump)))
  1128. X  (condition-case err
  1129. X      (if (= (calc-pack-size mode) (1- (length els)))
  1130. X      (calc-pack-items mode (cdr els))
  1131. X    (math-reject-arg els "*Wrong number of elements"))
  1132. X    (error (math-reject-arg els (nth 1 err))))
  1133. )
  1134. X
  1135. (defun calcFunc-unpack (mode thing)
  1136. X  (or (integerp mode) (math-reject-arg mode 'fixnump))
  1137. X  (condition-case err
  1138. X      (cons 'vec (calc-unpack-item mode thing))
  1139. X    (error (math-reject-arg thing (nth 1 err))))
  1140. )
  1141. X
  1142. (defun calcFunc-unpackt (mode thing)
  1143. X  (let ((calc-unpack-with-type 'pair))
  1144. X    (calcFunc-unpack mode thing))
  1145. )
  1146. X
  1147. (defun calcFunc-arrange (vec cols)   ; [Public]
  1148. X  (setq cols (math-check-fixnum cols t))
  1149. X  (if (math-vectorp vec)
  1150. X      (let* ((flat (math-flatten-vector vec))
  1151. X         (mat (list 'vec))
  1152. X         next)
  1153. X    (if (<= cnls 0)
  1154. X        (nconc mat flat)
  1155. X      (while (>= (length flat) cols)
  1156. X        (setq next (nthcdr cols flat))
  1157. X        (setcdr (nthcdr (1- cols) flat) nil)
  1158. X        (setq mat (nconc mat (list (cons 'vec flat)))
  1159. X          flat next))
  1160. X      (if flat
  1161. X          (setq mat (nconc mat (list (cons 'vec flat)))))
  1162. X      mat)))
  1163. )
  1164. X
  1165. (defun math-flatten-vector (vec)   ; [L V]
  1166. X  (if (math-vectorp vec)
  1167. X      (apply 'append (mapcar 'math-flatten-vector (cdr vec)))
  1168. X    (list vec))
  1169. )
  1170. X
  1171. (defun calcFunc-vconcat (a b)
  1172. X  (math-normalize (list '| a b))
  1173. )
  1174. X
  1175. (defun calcFunc-vconcatrev (a b)
  1176. X  (math-normalize (list '| b a))
  1177. )
  1178. X
  1179. (defun calcFunc-append (v1 v2)
  1180. X  (if (and (math-vectorp v1) (math-vectorp v2))
  1181. X      (append v1 (cdr v2))
  1182. X    (list 'calcFunc-append v1 v2))
  1183. )
  1184. X
  1185. (defun calcFunc-appendrev (v1 v2)
  1186. X  (calcFunc-append v2 v1)
  1187. )
  1188. X
  1189. X
  1190. ;;; Copy a matrix.  [Public]
  1191. (defun math-copy-matrix (m)
  1192. X  (if (math-vectorp (nth 1 m))
  1193. X      (cons 'vec (mapcar 'copy-sequence (cdr m)))
  1194. X    (copy-sequence m))
  1195. )
  1196. X
  1197. ;;; Convert a scalar or vector into an NxN diagonal matrix.  [Public]
  1198. (defun calcFunc-diag (a &optional n)
  1199. X  (and n (not (integerp n))
  1200. X       (setq n (math-check-fixnum n)))
  1201. X  (if (math-vectorp a)
  1202. X      (if (and n (/= (length a) (1+ n)))
  1203. X      (list 'calcFunc-diag a n)
  1204. X    (if (math-matrixp a)
  1205. X        (if (and n (/= (length (elt a 1)) (1+ n)))
  1206. X        (list 'calcFunc-diag a n)
  1207. X          a)
  1208. X      (cons 'vec (math-diag-step (cdr a) 0 (1- (length a))))))
  1209. X    (if n
  1210. X    (cons 'vec (math-diag-step (make-list n a) 0 n))
  1211. X      (list 'calcFunc-diag a)))
  1212. )
  1213. X
  1214. (defun calcFunc-idn (a &optional n)
  1215. X  (if n
  1216. X      (if (math-vectorp a)
  1217. X      (math-reject-arg a 'numberp)
  1218. X    (calcFunc-diag a n))
  1219. X    (if (integerp calc-matrix-mode)
  1220. X    (calcFunc-idn a calc-matrix-mode)
  1221. X      (list 'calcFunc-idn a)))
  1222. )
  1223. X
  1224. (defun math-mimic-ident (a m)
  1225. X  (if (math-square-matrixp m)
  1226. X      (calcFunc-idn a (1- (length m)))
  1227. X    (if (math-vectorp m)
  1228. X    (if (math-zerop a)
  1229. X        (cons 'vec (mapcar (function (lambda (x)
  1230. X                       (if (math-vectorp x)
  1231. X                           (math-mimic-ident a x)
  1232. X                         a)))
  1233. X                   (cdr m)))
  1234. X      (math-dimension-error))
  1235. X      (calcFunc-idn a)))
  1236. )
  1237. X
  1238. (defun math-diag-step (a n m)
  1239. X  (if (< n m)
  1240. X      (cons (cons 'vec
  1241. X          (nconc (make-list n 0)
  1242. X             (cons (car a)
  1243. X                   (make-list (1- (- m n)) 0))))
  1244. X        (math-diag-step (cdr a) (1+ n) m))
  1245. X    nil)
  1246. )
  1247. X
  1248. ;;; Create a vector of consecutive integers. [Public]
  1249. (defun calcFunc-index (n &optional start incr)
  1250. X  (if (math-messy-integerp n)
  1251. X      (math-float (calcFunc-index (math-trunc n) start incr))
  1252. X    (and (not (integerp n))
  1253. X     (setq n (math-check-fixnum n)))
  1254. X    (let ((vec nil))
  1255. X      (if start
  1256. X      (progn
  1257. X        (if (>= n 0)
  1258. X        (while (>= (setq n (1- n)) 0)
  1259. X          (setq vec (cons start vec)
  1260. X            start (math-add start (or incr 1))))
  1261. X          (while (<= (setq n (1+ n)) 0)
  1262. X        (setq vec (cons start vec)
  1263. X              start (math-mul start (or incr 2)))))
  1264. X        (setq vec (nreverse vec)))
  1265. X    (if (>= n 0)
  1266. X        (while (> n 0)
  1267. X          (setq vec (cons n vec)
  1268. X            n (1- n)))
  1269. X      (let ((i -1))
  1270. X        (while (>= i n)
  1271. X          (setq vec (cons i vec)
  1272. X            i (1- i))))))
  1273. X      (cons 'vec vec)))
  1274. )
  1275. X
  1276. ;;; Find an element in a vector.
  1277. (defun calcFunc-find (vec x &optional start)
  1278. X  (setq start (if start (math-check-fixnum start t) 1))
  1279. X  (if (< start 1) (math-reject-arg start 'posp))
  1280. X  (setq vec (nthcdr start vec))
  1281. X  (let ((n start))
  1282. X    (while (and vec (not (Math-equal x (car vec))))
  1283. X      (setq n (1+ n)
  1284. X        vec (cdr vec)))
  1285. X    (if vec n 0))
  1286. )
  1287. X
  1288. ;;; Return a subvector of a vector.
  1289. (defun calcFunc-subvec (vec start &optional end)
  1290. X  (setq start (math-check-fixnum start t)
  1291. X    end (math-check-fixnum (or end 0) t))
  1292. X  (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  1293. X  (let ((len (1- (length vec))))
  1294. X    (if (<= start 0)
  1295. X    (setq start (+ len start 1)))
  1296. X    (if (<= end 0)
  1297. X    (setq end (+ len end 1)))
  1298. X    (if (or (> start len)
  1299. X        (<= end start))
  1300. X    '(vec)
  1301. X      (setq vec (nthcdr start vec))
  1302. X      (if (<= end len)
  1303. X      (let ((chop (nthcdr (- end start 1) (setq vec (copy-sequence vec)))))
  1304. X        (setcdr chop nil)))
  1305. X      (cons 'vec vec)))
  1306. )
  1307. X
  1308. ;;; Remove a subvector from a vector.
  1309. (defun calcFunc-rsubvec (vec start &optional end)
  1310. X  (setq start (math-check-fixnum start t)
  1311. X    end (math-check-fixnum (or end 0) t))
  1312. X  (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  1313. X  (let ((len (1- (length vec))))
  1314. X    (if (<= start 0)
  1315. X    (setq start (+ len start 1)))
  1316. X    (if (<= end 0)
  1317. X    (setq end (+ len end 1)))
  1318. X    (if (or (> start len)
  1319. X        (<= end start))
  1320. X    vec
  1321. X      (let ((tail (nthcdr end vec))
  1322. X        (chop (nthcdr (1- start) (setq vec (copy-sequence vec)))))
  1323. X    (setcdr chop nil)
  1324. X    (append vec tail))))
  1325. )
  1326. X
  1327. ;;; Reverse the order of the elements of a vector.
  1328. (defun calcFunc-rev (vec)
  1329. X  (if (math-vectorp vec)
  1330. X      (cons 'vec (reverse (cdr vec)))
  1331. X    (math-reject-arg vec 'vectorp))
  1332. )
  1333. X
  1334. ;;; Compress a vector according to a mask vector.
  1335. (defun calcFunc-vmask (mask vec)
  1336. X  (if (math-numberp mask)
  1337. X      (if (math-zerop mask)
  1338. X      '(vec)
  1339. X    vec)
  1340. X    (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  1341. X    (or (math-constp mask) (math-reject-arg mask 'constp))
  1342. X    (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  1343. X    (or (= (length mask) (length vec)) (math-dimension-error))
  1344. X    (let ((new nil))
  1345. X      (while (setq mask (cdr mask) vec (cdr vec))
  1346. X    (or (math-zerop (car mask))
  1347. X        (setq new (cons (car vec) new))))
  1348. X      (cons 'vec (nreverse new))))
  1349. )
  1350. X
  1351. ;;; Expand a vector according to a mask vector.
  1352. (defun calcFunc-vexp (mask vec &optional filler)
  1353. X  (or (math-vectorp mask) (math-reject-arg mask 'vectorp))
  1354. X  (or (math-constp mask) (math-reject-arg mask 'constp))
  1355. X  (or (math-vectorp vec) (math-reject-arg vec 'vectorp))
  1356. X  (let ((new nil)
  1357. X    (fvec (and filler (math-vectorp filler))))
  1358. X    (while (setq mask (cdr mask))
  1359. X      (if (math-zerop (car mask))
  1360. X      (setq new (cons (or (if fvec
  1361. X                  (car (setq filler (cdr filler)))
  1362. X                filler)
  1363. X                  (car mask)) new))
  1364. X    (setq vec (cdr vec)
  1365. X          new (cons (or (car vec) (car mask)) new))))
  1366. X    (cons 'vec (nreverse new)))
  1367. )
  1368. X
  1369. X
  1370. ;;; Compute the row and column norms of a vector or matrix.  [Public]
  1371. (defun calcFunc-rnorm (a)
  1372. X  (if (and (Math-vectorp a)
  1373. X       (math-constp a))
  1374. X      (if (math-matrixp a)
  1375. X      (math-reduce-vec 'math-max (math-map-vec 'calcFunc-cnorm a))
  1376. X    (math-reduce-vec 'math-max (math-map-vec 'math-abs a)))
  1377. X    (calc-record-why 'vectorp a)
  1378. X    (list 'calcFunc-rnorm a))
  1379. )
  1380. X
  1381. (defun calcFunc-cnorm (a)
  1382. X  (if (and (Math-vectorp a)
  1383. X       (math-constp a))
  1384. X      (if (math-matrixp a)
  1385. X      (math-reduce-vec 'math-max
  1386. X               (math-reduce-cols 'math-add-abs a))
  1387. X    (math-reduce-vec 'math-add-abs a))
  1388. X    (calc-record-why 'vectorp a)
  1389. X    (list 'calcFunc-cnorm a))
  1390. )
  1391. X
  1392. (defun math-add-abs (a b)
  1393. X  (math-add (math-abs a) (math-abs b))
  1394. )
  1395. X
  1396. X
  1397. ;;; Sort the elements of a vector into increasing order.
  1398. (defun calcFunc-sort (vec)   ; [Public]
  1399. X  (if (math-vectorp vec)
  1400. X      (cons 'vec (sort (copy-sequence (cdr vec)) 'math-beforep))
  1401. X    (math-reject-arg vec 'vectorp))
  1402. )
  1403. X
  1404. (defun calcFunc-rsort (vec)   ; [Public]
  1405. X  (if (math-vectorp vec)
  1406. X      (cons 'vec (nreverse (sort (copy-sequence (cdr vec)) 'math-beforep)))
  1407. X    (math-reject-arg vec 'vectorp))
  1408. )
  1409. X
  1410. (defun calcFunc-grade (grade-vec)
  1411. X  (if (math-vectorp grade-vec)
  1412. X      (let* ((len (1- (length grade-vec))))
  1413. X    (cons 'vec (sort (cdr (calcFunc-index len)) 'math-grade-beforep)))
  1414. X    (math-reject-arg grade-vec 'vectorp))
  1415. )
  1416. X
  1417. (defun calcFunc-rgrade (grade-vec)
  1418. X  (if (math-vectorp grade-vec)
  1419. X      (let* ((len (1- (length grade-vec))))
  1420. X    (cons 'vec (nreverse (sort (cdr (calcFunc-index len))
  1421. X                   'math-grade-beforep))))
  1422. X    (math-reject-arg grade-vec 'vectorp))
  1423. )
  1424. X
  1425. (defun math-grade-beforep (i j)
  1426. X  (math-beforep (nth i grade-vec) (nth j grade-vec))
  1427. )
  1428. X
  1429. X
  1430. ;;; Compile a histogram of data from a vector.
  1431. (defun calcFunc-histogram (vec wts &optional n)
  1432. X  (or n (setq n wts wts 1))
  1433. X  (or (Math-vectorp vec)
  1434. X      (math-reject-arg vec 'vectorp))
  1435. X  (if (Math-vectorp wts)
  1436. X      (or (= (length vec) (length wts))
  1437. X      (math-dimension-error)))
  1438. X  (or (natnump n)
  1439. X      (math-reject-arg n 'fixnatnump))
  1440. X  (let ((res (make-vector n 0))
  1441. X    (vp vec)
  1442. X    (wvec (Math-vectorp wts))
  1443. X    (wp wts)
  1444. X    bin)
  1445. X    (while (setq vp (cdr vp))
  1446. X      (setq bin (car vp))
  1447. X      (or (natnump bin)
  1448. X      (setq bin (math-floor bin)))
  1449. X      (and (natnump bin)
  1450. X       (< bin n)
  1451. X       (aset res bin (math-add (aref res bin)
  1452. X                   (if wvec (car (setq wp (cdr wp))) wts)))))
  1453. X    (cons 'vec (append res nil)))
  1454. )
  1455. X
  1456. X
  1457. ;;; Set operations.
  1458. X
  1459. (defun calcFunc-vunion (a b)
  1460. X  (if (Math-objectp a)
  1461. X      (setq a (list 'vec a))
  1462. X    (or (math-vectorp a) (math-reject-arg a 'vectorp)))
  1463. X  (if (Math-objectp b)
  1464. X      (setq b (list b))
  1465. X    (or (math-vectorp b) (math-reject-arg b 'vectorp))
  1466. X    (setq b (cdr b)))
  1467. X  (calcFunc-rdup (append a b))
  1468. )
  1469. X
  1470. (defun calcFunc-vint (a b)
  1471. X  (if (and (math-simple-set a) (math-simple-set b))
  1472. X      (progn
  1473. X    (setq a (cdr (calcFunc-rdup a)))
  1474. X    (setq b (cdr (calcFunc-rdup b)))
  1475. X    (let ((vec (list 'vec)))
  1476. X      (while (and a b)
  1477. X        (if (math-beforep (car a) (car b))
  1478. X        (setq a (cdr a))
  1479. X          (if (Math-equal (car a) (car b))
  1480. X          (setq vec (cons (car a) vec)
  1481. X            a (cdr a)))
  1482. X          (setq b (cdr b))))
  1483. X      (nreverse vec)))
  1484. X    (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a)
  1485. X                      (calcFunc-vcompl b))))
  1486. )
  1487. X
  1488. (defun calcFunc-vdiff (a b)
  1489. X  (if (and (math-simple-set a) (math-simple-set b))
  1490. X      (progn
  1491. X    (setq a (cdr (calcFunc-rdup a)))
  1492. X    (setq b (cdr (calcFunc-rdup b)))
  1493. X    (let ((vec (list 'vec)))
  1494. X      (while a
  1495. X        (while (and b (math-beforep (car b) (car a)))
  1496. X          (setq b (cdr b)))
  1497. X        (if (and b (Math-equal (car a) (car b)))
  1498. X        (setq a (cdr a)
  1499. X              b (cdr b))
  1500. X          (setq vec (cons (car a) vec)
  1501. X            a (cdr a))))
  1502. X      (nreverse vec)))
  1503. X    (calcFunc-vcompl (calcFunc-vunion (calcFunc-vcompl a) b)))
  1504. )
  1505. X
  1506. (defun calcFunc-vxor (a b)
  1507. X  (if (and (math-simple-set a) (math-simple-set b))
  1508. X      (progn
  1509. X    (setq a (cdr (calcFunc-rdup a)))
  1510. X    (setq b (cdr (calcFunc-rdup b)))
  1511. X    (let ((vec (list 'vec)))
  1512. X      (while (or a b)
  1513. X        (if (and a
  1514. X             (or (not b)
  1515. X             (math-beforep (car a) (car b))))
  1516. X        (setq vec (cons (car a) vec)
  1517. X              a (cdr a))
  1518. X          (if (and a (Math-equal (car a) (car b)))
  1519. X          (setq a (cdr a))
  1520. X        (setq vec (cons (car b) vec)))
  1521. X          (setq b (cdr b))))
  1522. X      (nreverse vec)))
  1523. X    (let ((ca (calcFunc-vcompl a))
  1524. X      (cb (calcFunc-vcompl b)))
  1525. X      (calcFunc-vunion (calcFunc-vcompl (calcFunc-vunion ca b))
  1526. X               (calcFunc-vcompl (calcFunc-vunion a cb)))))
  1527. )
  1528. X
  1529. (defun calcFunc-vcompl (a)
  1530. X  (setq a (math-prepare-set a))
  1531. X  (let ((vec (list 'vec))
  1532. X    (prev '(neg (var inf var-inf)))
  1533. X    (closed 2))
  1534. X    (while (setq a (cdr a))
  1535. X      (or (and (equal (nth 2 (car a)) '(neg (var inf var-inf)))
  1536. X           (memq (nth 1 (car a)) '(2 3)))
  1537. X      (setq vec (cons (list 'intv
  1538. X                (+ closed
  1539. X                   (if (memq (nth 1 (car a)) '(0 1)) 1 0))
  1540. X                prev
  1541. X                (nth 2 (car a)))
  1542. X              vec)))
  1543. X      (setq prev (nth 3 (car a))
  1544. X        closed (if (memq (nth 1 (car a)) '(0 2)) 2 0)))
  1545. X    (or (and (equal prev '(var inf var-inf))
  1546. X         (= closed 0))
  1547. X    (setq vec (cons (list 'intv (+ closed 1)
  1548. X                  prev '(var inf var-inf))
  1549. X            vec)))
  1550. X    (math-clean-set (nreverse vec)))
  1551. )
  1552. X
  1553. (defun calcFunc-vspan (a)
  1554. X  (setq a (math-prepare-set a))
  1555. X  (if (cdr a)
  1556. X      (let ((last (nth (1- (length a)) a)))
  1557. X    (math-make-intv (+ (logand (nth 1 (nth 1 a)) 2)
  1558. X               (logand (nth 1 last) 1))
  1559. X            (nth 2 (nth 1 a))
  1560. X            (nth 3 last)))
  1561. X    '(intv 2 0 0))
  1562. )
  1563. X
  1564. (defun calcFunc-vfloor (a &optional always-vec)
  1565. X  (setq a (math-prepare-set a))
  1566. X  (let ((vec (list 'vec)) (p a) (prev nil) b mask)
  1567. X    (while (setq p (cdr p))
  1568. X      (setq mask (nth 1 (car p))
  1569. X        a (nth 2 (car p))
  1570. X        b (nth 3 (car p)))
  1571. X      (and (memq mask '(0 1))
  1572. X       (not (math-infinitep a))
  1573. X       (setq mask (logior mask 2))
  1574. X       (math-num-integerp a)
  1575. X       (setq a (math-add a 1)))
  1576. X      (setq a (math-ceiling a))
  1577. X      (and (memq mask '(0 2))
  1578. X       (not (math-infinitep b))
  1579. X       (setq mask (logior mask 1))
  1580. X       (math-num-integerp b)
  1581. X       (setq b (math-sub b 1)))
  1582. X      (setq b (math-floor b))
  1583. X      (if (and prev (Math-equal (math-sub a 1) (nth 3 prev)))
  1584. X      (setcar (nthcdr 3 prev) b)
  1585. X    (or (Math-lessp b a)
  1586. X        (setq vec (cons (setq prev (list 'intv mask a b)) vec)))))
  1587. X    (setq vec (nreverse vec))
  1588. X    (math-clean-set vec always-vec))
  1589. )
  1590. X
  1591. (defun calcFunc-vcard (a)
  1592. X  (setq a (calcFunc-vfloor a t))
  1593. X  (or (math-constp a) (math-reject-arg a "*Set must be finite"))
  1594. X  (let ((count 0))
  1595. X    (while (setq a (cdr a))
  1596. X      (if (eq (car-safe (car a)) 'intv)
  1597. X      (setq count (math-add count (math-sub (nth 3 (car a))
  1598. X                        (nth 2 (car a))))))
  1599. X      (setq count (math-add count 1)))
  1600. X    count)
  1601. )
  1602. X
  1603. (defun calcFunc-venum (a)
  1604. X  (setq a (calcFunc-vfloor a t))
  1605. X  (or (math-constp a) (math-reject-arg a "*Set must be finite"))
  1606. X  (let ((p a) next)
  1607. X    (while (cdr p)
  1608. X      (setq next (cdr p))
  1609. X      (if (eq (car-safe (nth 1 p)) 'intv)
  1610. X      (setcdr p (nconc (cdr (calcFunc-index (math-add
  1611. X                         (math-sub (nth 3 (nth 1 p))
  1612. X                               (nth 2 (nth 1 p)))
  1613. X                         1)
  1614. X                        (nth 2 (nth 1 p))))
  1615. X               (cdr (cdr p)))))
  1616. X      (setq p next))
  1617. X    a)
  1618. )
  1619. X
  1620. (defun calcFunc-vpack (a)
  1621. X  (setq a (calcFunc-vfloor a t))
  1622. X  (if (and (cdr a)
  1623. X       (math-negp (if (eq (car-safe (nth 1 a)) 'intv)
  1624. X              (nth 2 (nth 1 a))
  1625. X            (nth 1 a))))
  1626. X      (math-reject-arg (nth 1 a) 'posp))
  1627. X  (let ((accum 0))
  1628. X    (while (setq a (cdr a))
  1629. X      (if (eq (car-safe (car a)) 'intv)
  1630. X      (if (equal (nth 3 (car a)) '(var inf var-inf))
  1631. X          (setq accum (math-sub accum
  1632. X                    (math-power-of-2 (nth 2 (car a)))))
  1633. X        (setq accum (math-add accum
  1634. X                  (math-sub
  1635. X                   (math-power-of-2 (1+ (nth 3 (car a))))
  1636. X                   (math-power-of-2 (nth 2 (car a)))))))
  1637. X    (setq accum (math-add accum (math-power-of-2 (car a))))))
  1638. X    accum)
  1639. )
  1640. X
  1641. (defun calcFunc-vunpack (a &optional w)
  1642. X  (or (math-num-integerp a) (math-reject-arg a 'integerp))
  1643. X  (if w (setq a (math-clip a w)))
  1644. X  (if (math-messy-integerp a) (setq a (math-trunc a)))
  1645. X  (let* ((calc-number-radix 2)
  1646. X     (neg (math-negp a))
  1647. X     (aa (if neg (math-sub -1 a) a))
  1648. X     (str (if (eq aa 0)
  1649. X          ""
  1650. X        (if (consp aa)
  1651. X            (math-format-bignum-binary (cdr aa))
  1652. X          (math-format-binary aa))))
  1653. X     (zero (if neg ?1 ?0))
  1654. X     (one (if neg ?0 ?1))
  1655. X     (len (length str))
  1656. X     (vec (list 'vec))
  1657. X     (pos (1- len)) pos2)
  1658. X    (while (>= pos 0)
  1659. X      (if (eq (aref str pos) zero)
  1660. X      (setq pos (1- pos))
  1661. X    (setq pos2 pos)
  1662. X    (while (and (>= pos 0) (eq (aref str pos) one))
  1663. X      (setq pos (1- pos)))
  1664. X    (setq vec (cons (if (= pos (1- pos2))
  1665. X                (- len pos2 1)
  1666. X              (list 'intv 3 (- len pos2 1) (- len pos 2)))
  1667. X            vec))))
  1668. X    (if neg
  1669. X    (setq vec (cons (list 'intv 2 len '(var inf var-inf)) vec)))
  1670. X    (math-clean-set (nreverse vec)))
  1671. )
  1672. X
  1673. (defun calcFunc-rdup (a)
  1674. X  (if (math-simple-set a)
  1675. X      (progn
  1676. X    (and (Math-objectp a) (setq a (list 'vec a)))
  1677. X    (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1678. X    (setq a (sort (copy-sequence (cdr a)) 'math-beforep))
  1679. X    (let ((p a))
  1680. X      (while (cdr p)
  1681. X        (if (Math-equal (car p) (nth 1 p))
  1682. X        (setcdr p (cdr (cdr p)))
  1683. X          (setq p (cdr p)))))
  1684. X    (cons 'vec a))
  1685. X    (math-clean-set (math-prepare-set a)))
  1686. )
  1687. X
  1688. (defun math-prepare-set (a)
  1689. X  (if (Math-objectp a)
  1690. X      (setq a (list 'vec a))
  1691. X    (or (math-vectorp a) (math-reject-arg a 'vectorp))
  1692. X    (setq a (cons 'vec (sort (copy-sequence (cdr a)) 'math-beforep))))
  1693. X  (let ((p a) res)
  1694. X
  1695. X    ;; Convert all elements to non-empty intervals.
  1696. X    (while (cdr p)
  1697. X      (if (eq (car-safe (nth 1 p)) 'intv)
  1698. X      (if (math-intv-constp (nth 1 p))
  1699. X          (if (and (memq (nth 1 (nth 1 p)) '(0 1 2))
  1700. X               (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p))))
  1701. X          (setcdr p (cdr (cdr p)))
  1702. X        (setq p (cdr p)))
  1703. X        (math-reject-arg (nth 1 p) 'constp))
  1704. X    (or (Math-anglep (nth 1 p))
  1705. X        (eq (car (nth 1 p)) 'date)
  1706. X        (equal (nth 1 p) '(var inf var-inf))
  1707. X        (equal (nth 1 p) '(neg (var inf var-inf)))
  1708. X        (math-reject-arg (nth 1 p) 'realp))
  1709. X    (setcar (cdr p) (list 'intv 3 (nth 1 p) (nth 1 p)))
  1710. X    (setq p (cdr p))))
  1711. X
  1712. X    ;; Combine redundant intervals.
  1713. X    (setq p a)
  1714. X    (while (cdr (cdr p))
  1715. X      (if (or (memq (setq res (math-compare (nth 3 (nth 1 p))
  1716. X                        (nth 2 (nth 2 p))))
  1717. X            '(-1 2))
  1718. X          (and (eq res 0)
  1719. X           (memq (nth 1 (nth 1 p)) '(0 2))
  1720. X           (memq (nth 1 (nth 2 p)) '(0 1))))
  1721. X      (setq p (cdr p))
  1722. X    (setq res (math-compare (nth 3 (nth 1 p)) (nth 3 (nth 2 p))))
  1723. X    (setcdr p (cons (list 'intv
  1724. X                  (+ (logand (logior (nth 1 (nth 1 p))
  1725. X                         (if (Math-equal
  1726. X                              (nth 2 (nth 1 p))
  1727. X                              (nth 2 (nth 2 p)))
  1728. X                             (nth 1 (nth 2 p))
  1729. X                           0))
  1730. X                     2)
  1731. X                 (logand (logior (if (memq res '(1 0 2))
  1732. X                             (nth 1 (nth 1 p)) 0)
  1733. X                         (if (memq res '(-1 0 2))
  1734. X                             (nth 1 (nth 2 p)) 0))
  1735. X                     1))
  1736. X                  (nth 2 (nth 1 p))
  1737. X                  (if (eq res 1)
  1738. X                  (nth 3 (nth 1 p))
  1739. X                (nth 3 (nth 2 p))))
  1740. X            (cdr (cdr (cdr p))))))))
  1741. X  a
  1742. )
  1743. X
  1744. (defun math-clean-set (a &optional always-vec)
  1745. X  (let ((p a) res)
  1746. X    (while (cdr p)
  1747. X      (if (and (eq (car-safe (nth 1 p)) 'intv)
  1748. X           (Math-equal (nth 2 (nth 1 p)) (nth 3 (nth 1 p))))
  1749. X      (setcar (cdr p) (nth 2 (nth 1 p))))
  1750. X      (setq p (cdr p)))
  1751. X    (if (and (not (cdr (cdr a)))
  1752. X         (eq (car-safe (nth 1 a)) 'intv)
  1753. X         (not always-vec))
  1754. X    (nth 1 a)
  1755. X      a))
  1756. )
  1757. X
  1758. (defun math-simple-set (a)
  1759. X  (or (and (Math-objectp a)
  1760. X       (not (eq (car-safe a) 'intv)))
  1761. X      (and (Math-vectorp a)
  1762. X       (progn
  1763. X         (while (and (setq a (cdr a))
  1764. X             (not (eq (car-safe (car a)) 'intv))))
  1765. X         (null a))))
  1766. )
  1767. X
  1768. X
  1769. X
  1770. X
  1771. ;;; Compute a right-handed vector cross product.  [O O O] [Public]
  1772. (defun calcFunc-cross (a b)
  1773. X  (if (and (eq (car-safe a) 'vec)
  1774. X       (= (length a) 4))
  1775. X      (if (and (eq (car-safe b) 'vec)
  1776. X           (= (length b) 4))
  1777. X      (list 'vec
  1778. X        (math-sub (math-mul (nth 2 a) (nth 3 b))
  1779. X              (math-mul (nth 3 a) (nth 2 b)))
  1780. X        (math-sub (math-mul (nth 3 a) (nth 1 b))
  1781. X              (math-mul (nth 1 a) (nth 3 b)))
  1782. X        (math-sub (math-mul (nth 1 a) (nth 2 b))
  1783. X              (math-mul (nth 2 a) (nth 1 b))))
  1784. X    (math-reject-arg b "*Three-vector expected"))
  1785. X    (math-reject-arg a "*Three-vector expected"))
  1786. )
  1787. X
  1788. X
  1789. X
  1790. X
  1791. X
  1792. (defun math-read-brackets (space-sep close)
  1793. X  (and space-sep (setq space-sep (not (math-check-for-commas))))
  1794. X  (math-read-token)
  1795. X  (while (eq exp-token 'space)
  1796. X    (math-read-token))
  1797. X  (if (or (equal exp-data close)
  1798. X      (eq exp-token 'end))
  1799. X      (progn
  1800. X    (math-read-token)
  1801. X    '(vec))
  1802. X    (let ((vals (let ((exp-keep-spaces space-sep))
  1803. X          (if (or (equal exp-data "\\dots")
  1804. X              (equal exp-data "\\ldots"))
  1805. X              '(vec (neg (var inf var-inf)))
  1806. X            (math-read-vector)))))
  1807. X      (if (or (equal exp-data "\\dots")
  1808. X          (equal exp-data "\\ldots"))
  1809. X      (progn
  1810. X        (math-read-token)
  1811. X        (setq vals (if (> (length vals) 2)
  1812. X               (cons 'calcFunc-mul (cdr vals)) (nth 1 vals)))
  1813. X        (let ((exp2 (if (or (equal exp-data close)
  1814. X                (equal exp-data ")")
  1815. X                (eq exp-token 'end))
  1816. X                '(var inf var-inf)
  1817. X              (math-read-expr-level 0))))
  1818. X          (setq vals
  1819. X            (list 'intv
  1820. X              (if (equal exp-data ")") 2 3)
  1821. X              vals
  1822. X              exp2)))
  1823. X        (if (not (or (equal exp-data close)
  1824. X             (equal exp-data ")")
  1825. X             (eq exp-token 'end)))
  1826. X        (throw 'syntax "Expected `]'")))
  1827. X    (if (equal exp-data ";")
  1828. X        (let ((exp-keep-spaces space-sep))
  1829. X          (setq vals (cons 'vec (math-read-matrix (list vals))))))
  1830. X    (if (not (or (equal exp-data close)
  1831. X             (eq exp-token 'end)))
  1832. X        (throw 'syntax "Expected `]'")))
  1833. X      (or (eq exp-token 'end)
  1834. X      (math-read-token))
  1835. X      vals))
  1836. )
  1837. X
  1838. (defun math-check-for-commas (&optional balancing)
  1839. X  (let ((count 0)
  1840. X    (pos (1- exp-pos)))
  1841. X    (while (and (>= count 0)
  1842. X        (setq pos (string-match
  1843. X               (if balancing "[],[{}()<>]" "[],[{}()]")
  1844. X               exp-str (1+ pos)))
  1845. X        (or (/= (aref exp-str pos) ?,) (> count 0) balancing))
  1846. X      (cond ((memq (aref exp-str pos) '(?\[ ?\{ ?\( ?\<))
  1847. X         (setq count (1+ count)))
  1848. X        ((memq (aref exp-str pos) '(?\] ?\} ?\) ?\>))
  1849. X         (setq count (1- count)))))
  1850. X    (if balancing
  1851. X    pos
  1852. X      (and pos (= (aref exp-str pos) ?,))))
  1853. )
  1854. X
  1855. (defun math-read-vector ()
  1856. X  (let* ((val (list (math-read-expr-level 0)))
  1857. X     (last val))
  1858. X    (while (progn
  1859. X         (while (eq exp-token 'space)
  1860. X           (math-read-token))
  1861. X         (and (not (eq exp-token 'end))
  1862. X          (not (equal exp-data ";"))
  1863. X          (not (equal exp-data close))
  1864. X          (not (equal exp-data "\\dots"))
  1865. X          (not (equal exp-data "\\ldots"))))
  1866. X      (if (equal exp-data ",")
  1867. X      (math-read-token))
  1868. X      (while (eq exp-token 'space)
  1869. X    (math-read-token))
  1870. X      (let ((rest (list (math-read-expr-level 0))))
  1871. X    (setcdr last rest)
  1872. X    (setq last rest)))
  1873. X    (cons 'vec val))
  1874. )
  1875. X
  1876. (defun math-read-matrix (mat)
  1877. X  (while (equal exp-data ";")
  1878. X    (math-read-token)
  1879. X    (while (eq exp-token 'space)
  1880. X      (math-read-token))
  1881. X    (setq mat (nconc mat (list (math-read-vector)))))
  1882. X  mat
  1883. )
  1884. X
  1885. SHAR_EOF
  1886. chmod 0644 calc-vec.el ||
  1887. echo 'restore of calc-vec.el failed'
  1888. Wc_c="`wc -c < 'calc-vec.el'`"
  1889. test 46113 -eq "$Wc_c" ||
  1890.     echo 'calc-vec.el: original size 46113, current size' "$Wc_c"
  1891. rm -f _shar_wnt_.tmp
  1892. fi
  1893. # ============= calc-units.el ==============
  1894. if test -f 'calc-units.el' -a X"$1" != X"-c"; then
  1895.     echo 'x - skipping calc-units.el (File already exists)'
  1896.     rm -f _shar_wnt_.tmp
  1897. else
  1898. > _shar_wnt_.tmp
  1899. echo 'x - extracting calc-units.el (Text)'
  1900. sed 's/^X//' << 'SHAR_EOF' > 'calc-units.el' &&
  1901. ;; Calculator for GNU Emacs, part II [calc-units.el]
  1902. ;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  1903. ;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
  1904. X
  1905. ;; This file is part of GNU Emacs.
  1906. X
  1907. ;; GNU Emacs is distributed in the hope that it will be useful,
  1908. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  1909. ;; accepts responsibility to anyone for the consequences of using it
  1910. ;; or for whether it serves any particular purpose or works at all,
  1911. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  1912. ;; License for full details.
  1913. X
  1914. ;; Everyone is granted permission to copy, modify and redistribute
  1915. ;; GNU Emacs, but only under the conditions described in the
  1916. ;; GNU Emacs General Public License.   A copy of this license is
  1917. ;; supposed to have been given to you along with GNU Emacs so you
  1918. ;; can know your rights and responsibilities.  It should be in a
  1919. ;; file named COPYING.  Among other things, the copyright notice
  1920. ;; and this notice must be preserved on all copies.
  1921. X
  1922. X
  1923. X
  1924. ;; This file is autoloaded from calc-ext.el.
  1925. (require 'calc-ext)
  1926. X
  1927. (require 'calc-macs)
  1928. X
  1929. (defun calc-Need-calc-units () nil)
  1930. X
  1931. X
  1932. ;;; Units commands.
  1933. X
  1934. (defun calc-base-units ()
  1935. X  (interactive)
  1936. X  (calc-slow-wrapper
  1937. X   (let ((calc-autorange-units nil))
  1938. X     (calc-enter-result 1 "bsun" (math-simplify-units
  1939. X                  (math-to-standard-units (calc-top-n 1)
  1940. X                              nil)))))
  1941. )
  1942. X
  1943. (defun calc-convert-units (&optional old-units new-units)
  1944. X  (interactive)
  1945. X  (calc-slow-wrapper
  1946. X   (let ((expr (calc-top-n 1))
  1947. X     (uoldname nil)
  1948. X     unew)
  1949. X     (or (math-units-in-expr-p expr t)
  1950. X     (let ((uold (or old-units
  1951. X             (progn
  1952. X               (setq uoldname (read-string "Old units: "))
  1953. X               (if (equal uoldname "")
  1954. X                   (progn
  1955. X                 (setq uoldname "1")
  1956. X                 1)
  1957. X                 (if (string-match "\\` */" uoldname)
  1958. SHAR_EOF
  1959. true || echo 'restore of calc-units.el failed'
  1960. fi
  1961. echo 'End of  part 28'
  1962. echo 'File calc-units.el is continued in part 29'
  1963. echo 29 > _shar_seq_.tmp
  1964. exit 0
  1965. exit 0 # Just in case...
  1966. -- 
  1967. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1968. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1969. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1970. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1971.