home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume13 / gmcalc / part11 < prev    next >
Encoding:
Text File  |  1990-06-05  |  58.2 KB  |  1,813 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@csvax.caltech.edu (David Gillespie)
  3. Subject: v13i037: Emacs Calculator 1.01, part 11/19
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 13, Issue 37
  7. Submitted-by: daveg@csvax.caltech.edu (David Gillespie)
  8. Archive-name: gmcalc/part11
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # this is part 11 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file calc-ext.el continued
  15. #
  16. CurArch=11
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. echo "x - Continuing file calc-ext.el"
  27. sed 's/^X//' << 'SHAR_EOF' >> calc-ext.el
  28. X          exp)
  29. X         (t
  30. X          (intern (concat "var-" name))))))
  31. X    ((integerp exp)
  32. X     (if (or (<= exp -1000000) (>= exp 1000000))
  33. X         (list 'quote (math-normalize exp))
  34. X       exp))
  35. X    (t exp))
  36. X)
  37. X
  38. X(defun math-define-cond (forms)
  39. X  (and forms
  40. X       (cons (math-define-list (car forms))
  41. X         (math-define-cond (cdr forms))))
  42. X)
  43. X
  44. X(defun math-complicated-lhs (body)
  45. X  (and body
  46. X       (or (not (symbolp (car body)))
  47. X       (math-complicated-lhs (cdr (cdr body)))))
  48. X)
  49. X
  50. X(defun math-define-setf-list (body)
  51. X  (and body
  52. X       (cons (math-define-setf (nth 0 body) (nth 1 body))
  53. X         (math-define-setf-list (cdr (cdr body)))))
  54. X)
  55. X
  56. X(defun math-define-setf (place value)
  57. X  (setq place (math-define-exp place)
  58. X    value (math-define-exp value))
  59. X  (cond ((symbolp place)
  60. X     (list 'setq place value))
  61. X    ((eq (car-safe place) 'nth)
  62. X     (list 'setcar (list 'nthcdr (nth 1 place) (nth 2 place)) value))
  63. X    ((eq (car-safe place) 'elt)
  64. X     (list 'setcar (list 'nthcdr (nth 2 place) (nth 1 place)) value))
  65. X    ((eq (car-safe place) 'car)
  66. X     (list 'setcar (nth 1 place) value))
  67. X    ((eq (car-safe place) 'cdr)
  68. X     (list 'setcdr (nth 1 place) value))
  69. X    (t
  70. X     (error "Bad place form for setf: %s" place)))
  71. X)
  72. X
  73. X(defun math-define-binop (op ident arg1 rest)
  74. X  (if rest
  75. X      (math-define-binop op ident
  76. X             (list op arg1 (car rest))
  77. X             (cdr rest))
  78. X    (or arg1 ident))
  79. X)
  80. X
  81. X(defun math-define-let (vlist)
  82. X  (and vlist
  83. X       (cons (if (consp (car vlist))
  84. X         (cons (car (car vlist))
  85. X               (math-define-list (cdr (car vlist))))
  86. X           (car vlist))
  87. X         (math-define-let (cdr vlist))))
  88. X)
  89. X
  90. X(defun math-define-let-env (vlist)
  91. X  (and vlist
  92. X       (cons (if (consp (car vlist))
  93. X         (car (car vlist))
  94. X           (car vlist))
  95. X         (math-define-let-env (cdr vlist))))
  96. X)
  97. X
  98. X(defun math-define-lambda (exp exp-env)
  99. X  (nconc (list (nth 0 exp)   ; 'lambda
  100. X           (nth 1 exp))  ; arg list
  101. X     (math-define-function-body (cdr (cdr exp))
  102. X                    (append (nth 1 exp) exp-env)))
  103. X)
  104. X
  105. X(defun math-define-elt (seq idx)
  106. X  (if idx
  107. X      (math-define-elt (list 'elt seq (car idx)) (cdr idx))
  108. X    seq)
  109. X)
  110. X
  111. X
  112. X
  113. X;;; Useful programming macros.
  114. X
  115. X(defmacro math-while (head &rest body)
  116. X  (let ((body (cons 'while (cons head body))))
  117. X    (if (math-body-refers-to body 'math-break)
  118. X    (cons 'catch (cons '(quote math-break) (list body)))
  119. X      body))
  120. X)
  121. X(put 'math-while 'lisp-indent-hook 1)
  122. X
  123. X
  124. X(defmacro math-for (head &rest body)
  125. X  (let ((body (if head
  126. X          (math-handle-for head body)
  127. X        (cons 'while (cons t body)))))
  128. X    (if (math-body-refers-to body 'math-break)
  129. X    (cons 'catch (cons '(quote math-break) (list body)))
  130. X      body))
  131. X)
  132. X(put 'math-for 'lisp-indent-hook 1)
  133. X
  134. X(defun math-handle-for (head body)
  135. X  (let* ((var (nth 0 (car head)))
  136. X     (init (nth 1 (car head)))
  137. X     (limit (nth 2 (car head)))
  138. X     (step (or (nth 3 (car head)) 1))
  139. X     (body (if (cdr head)
  140. X           (list (math-handle-for (cdr head) body))
  141. X         body))
  142. X     (all-ints (and (integerp init) (integerp limit) (integerp step)))
  143. X     (const-limit (or (integerp limit)
  144. X              (and (eq (car-safe limit) 'quote)
  145. X                   (math-realp (nth 1 limit)))))
  146. X     (const-step (or (integerp step)
  147. X             (and (eq (car-safe step) 'quote)
  148. X                  (math-realp (nth 1 step)))))
  149. X     (save-limit (if const-limit limit (make-symbol "<limit>")))
  150. X     (save-step (if const-step step (make-symbol "<step>"))))
  151. X    (cons 'let
  152. X      (cons (append (if const-limit nil (list (list save-limit limit)))
  153. X            (if const-step nil (list (list save-step step)))
  154. X            (list (list var init)))
  155. X        (list
  156. X         (cons 'while
  157. X               (cons (if all-ints
  158. X                 (if (> step 0)
  159. X                     (list '<= var save-limit)
  160. X                   (list '>= var save-limit))
  161. X                   (list 'not
  162. X                     (if const-step
  163. X                     (if (or (math-posp step)
  164. X                         (math-posp
  165. X                          (cdr-safe step)))
  166. X                         (list 'math-lessp
  167. X                           save-limit
  168. X                           var)
  169. X                       (list 'math-lessp
  170. X                         var
  171. X                         save-limit))
  172. X                       (list 'if
  173. X                         (list 'math-posp
  174. X                           save-step)
  175. X                         (list 'math-lessp
  176. X                           save-limit
  177. X                           var)
  178. X                         (list 'math-lessp
  179. X                           var
  180. X                           save-limit)))))
  181. X                 (append body
  182. X                     (list (list 'setq
  183. X                         var
  184. X                         (list (if all-ints
  185. X                               '+
  186. X                             'math-add)
  187. X                               var
  188. X                               save-step))))))))))
  189. X)
  190. X
  191. X
  192. X(defmacro math-foreach (head &rest body)
  193. X  (let ((body (math-handle-foreach head body)))
  194. X    (if (math-body-refers-to body 'math-break)
  195. X    (cons 'catch (cons '(quote math-break) (list body)))
  196. X      body))
  197. X)
  198. X(put 'math-foreach 'lisp-indent-hook 1)
  199. X
  200. X(defun math-handle-foreach (head body)
  201. X  (let ((var (nth 0 (car head)))
  202. X    (data (nth 1 (car head)))
  203. X    (body (if (cdr head)
  204. X          (list (math-handle-foreach (cdr head) body))
  205. X        body)))
  206. X    (cons 'let
  207. X      (cons (list (list var data))
  208. X        (list
  209. X         (cons 'while
  210. X               (cons var
  211. X                 (append body
  212. X                     (list (list 'setq
  213. X                         var
  214. X                         (list 'cdr var))))))))))
  215. X)
  216. X
  217. X
  218. X(defun math-body-refers-to (body thing)
  219. X  (or (equal body thing)
  220. X      (and (consp body)
  221. X       (or (math-body-refers-to (car body) thing)
  222. X           (math-body-refers-to (cdr body) thing))))
  223. X)
  224. X
  225. X(defun math-break (&optional value)
  226. X  (throw 'math-break value)
  227. X)
  228. X
  229. X(defun math-return (&optional value)
  230. X  (throw 'math-return value)
  231. X)
  232. X
  233. X
  234. X
  235. X
  236. X;;; Nontrivial number parsing.
  237. X
  238. X(defun math-read-number-fancy (s)
  239. X
  240. X  (cond
  241. X
  242. X   ;; Modulo forms
  243. X   ((string-match "^\\(.*\\) *mod *\\(.*\\)$" s)
  244. X    (let* ((n (math-match-substring s 1))
  245. X       (m (math-match-substring s 2))
  246. X       (n (math-read-number n))
  247. X       (m (math-read-number m)))
  248. X      (and n m (math-anglep n) (math-anglep m)
  249. X       (list 'mod n m))))
  250. X
  251. X   ;; Error forms
  252. X   ((string-match "^\\(.*\\) *\\+/- *\\(.*\\)$" s)
  253. X    (let* ((x (math-match-substring s 1))
  254. X       (sigma (math-match-substring s 2))
  255. X       (x (math-read-number x))
  256. X       (sigma (math-read-number sigma)))
  257. X      (and x sigma (math-anglep x) (math-anglep sigma)
  258. X       (list 'sdev x sigma))))
  259. X
  260. X   ;; Hours (or degrees)
  261. X   ((or (string-match "^\\([^#^]+\\)[@oOhH]\\(.*\\)$" s)
  262. X    (string-match "^\\([^#^]+\\)[dD][eE]?[gG]?\\(.*\\)$" s))
  263. X    (let* ((hours (math-match-substring s 1))
  264. X       (minsec (math-match-substring s 2))
  265. X       (hours (math-read-number hours))
  266. X       (minsec (if (> (length minsec) 0) (math-read-number minsec) 0)))
  267. X      (and hours minsec
  268. X       (math-num-integerp hours)
  269. X       (not (math-negp hours)) (not (math-negp minsec))
  270. X       (cond ((math-num-integerp minsec)
  271. X          (and (Math-lessp minsec 60)
  272. X               (list 'hms hours minsec 0)))
  273. X         ((and (eq (car-safe minsec) 'hms)
  274. X               (math-zerop (nth 1 minsec)))
  275. X          (math-add (list 'hms hours 0 0) minsec))
  276. X         (t nil)))))
  277. X   
  278. X   ;; Minutes
  279. X   ((string-match "^\\([^'#^]+\\)[mM']\\(.*\\)$" s)
  280. X    (let* ((minutes (math-match-substring s 1))
  281. X       (seconds (math-match-substring s 2))
  282. X       (minutes (math-read-number minutes))
  283. X       (seconds (if (> (length seconds) 0) (math-read-number seconds) 0)))
  284. X      (and minutes seconds
  285. X       (math-num-integerp minutes)
  286. X       (not (math-negp minutes)) (not (math-negp seconds))
  287. X       (cond ((math-realp seconds)
  288. X          (and (Math-lessp minutes 60)
  289. X               (list 'hms 0 minutes seconds)))
  290. X         ((and (eq (car-safe seconds) 'hms)
  291. X               (math-zerop (nth 1 seconds))
  292. X               (math-zerop (nth 2 seconds)))
  293. X          (math-add (list 'hms 0 minutes 0) seconds))
  294. X         (t nil)))))
  295. X   
  296. X   ;; Seconds
  297. X   ((string-match "^\\([^\"#^]+\\)[sS\"]$" s)
  298. X    (let ((seconds (math-read-number (math-match-substring s 1))))
  299. X      (and seconds (math-realp seconds)
  300. X       (not (math-negp seconds))
  301. X       (Math-lessp seconds 60)
  302. X       (list 'hms 0 0 seconds))))
  303. X   
  304. X   ;; Integer+fraction with explicit radix
  305. X   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]\\)$" s)
  306. X    (let ((radix (string-to-int (math-match-substring s 1)))
  307. X      (int (math-match-substring s 3))
  308. X      (num (math-match-substring s 4))
  309. X      (den (math-match-substring s 5)))
  310. X      (let ((int (if (> (length int) 0) (math-read-radix int radix) 0))
  311. X        (num (if (> (length num) 0) (math-read-radix num radix) 1))
  312. X        (den (if (> (length num) 0) (math-read-radix den radix) 1)))
  313. X    (and int num den (not (math-zerop den))
  314. X         (list 'frac
  315. X           (math-add num (math-mul int den))
  316. X           den)))))
  317. X   
  318. X   ;; Fraction with explicit radix
  319. X   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]*\\)[:/]\\([0-9a-zA-Z]*\\)$" s)
  320. X    (let ((radix (string-to-int (math-match-substring s 1)))
  321. X      (num (math-match-substring s 3))
  322. X      (den (math-match-substring s 4)))
  323. X      (let ((num (if (> (length num) 0) (math-read-radix num radix) 1))
  324. X        (den (if (> (length num) 0) (math-read-radix den radix) 1)))
  325. X    (and num den (not (math-zerop den)) (list 'frac num den)))))
  326. X   
  327. X   ;; Integer with explicit radix
  328. X   ((string-match "^\\([0-9]+\\)\\(#\\|\\^\\^\\)\\([0-9a-zA-Z]+\\)$" s)
  329. X    (math-read-radix (math-match-substring s 3)
  330. X             (string-to-int (math-match-substring s 1))))
  331. X   
  332. X   ;; C language hexadecimal notation
  333. X   ((and (eq calc-language 'c)
  334. X     (string-match "^0[xX]\\([0-9a-fA-F]+\\)$" s))
  335. X    (let ((digs (math-match-substring s 1)))
  336. X      (math-read-radix digs 16)))
  337. X   
  338. X   ;; Fraction using "/" instead of ":"
  339. X   ((string-match "^\\([0-9]+\\)/\\([0-9/]+\\)$" s)
  340. X    (math-read-number (concat (math-match-substring s 1) ":"
  341. X                  (math-match-substring s 2))))
  342. X
  343. X   ;; Syntax error!
  344. X   (t nil))
  345. X)
  346. X
  347. X(defun math-read-radix (s r)   ; [I X D]
  348. X  (catch 'gonzo
  349. X    (math-read-radix-loop (upcase s) (1- (length s)) r))
  350. X)
  351. X
  352. X(defun math-read-radix-loop (s i r)   ; [I X S D]
  353. X  (if (< i 0)
  354. X      0
  355. X    (let ((dig (math-read-radix-digit (elt s i))))
  356. X      (if (and dig (< dig r))
  357. X      (math-add (math-mul (math-read-radix-loop s (1- i) r)
  358. X                  r)
  359. X            dig)
  360. X    (throw 'gonzo nil))))
  361. X)
  362. X
  363. X
  364. X
  365. X;;; Expression parsing.
  366. X
  367. X(defun math-read-expr (exp-str)
  368. X  (let ((exp-pos 0)
  369. X    (exp-old-pos 0)
  370. X    (exp-keep-spaces nil)
  371. X    exp-token exp-data)
  372. X    (while (setq exp-token (string-match "\\.\\." exp-str))
  373. X      (setq exp-str (concat (substring exp-str exp-token) "\\dots"
  374. X                (substring exp-str (+ exp-token 2)))))
  375. X    (math-read-token)
  376. X    (let ((val (catch 'syntax (math-read-expr-level 0))))
  377. X      (if (stringp val)
  378. X      (list 'error exp-old-pos val)
  379. X    (if (equal exp-token 'end)
  380. X        val
  381. X      (list 'error exp-old-pos "Syntax error")))))
  382. X)
  383. X
  384. X(defun math-read-brackets (space-sep close)
  385. X  (and space-sep (setq space-sep (not (math-check-for-commas))))
  386. X  (math-read-token)
  387. X  (while (eq exp-token 'space)
  388. X    (math-read-token))
  389. X  (if (or (equal exp-data close)
  390. X      (eq exp-token 'end))
  391. X      (progn
  392. X    (math-read-token)
  393. X    '(vec))
  394. X    (let ((vals (let ((exp-keep-spaces space-sep))
  395. X          (math-read-vector))))
  396. X      (if (equal exp-data "\\dots")
  397. X      (progn
  398. X        (math-read-token)
  399. X        (setq vals (if (> (length vals) 2)
  400. X               (cons 'calcFunc-mul (cdr vals)) (nth 1 vals)))
  401. X        (let ((exp2 (math-read-expr-level 0)))
  402. X          (setq vals
  403. X            (list 'intv
  404. X              (if (equal exp-data ")") 2 3)
  405. X              vals
  406. X              exp2)))
  407. X        (if (not (or (equal exp-data close)
  408. X             (equal exp-data ")")
  409. X             (eq exp-token 'end)))
  410. X        (throw 'syntax "Expected `]'")))
  411. X    (if (equal exp-data ";")
  412. X        (let ((exp-keep-spaces space-sep))
  413. X          (setq vals (cons 'vec (math-read-matrix (list vals))))))
  414. X    (if (not (or (equal exp-data close)
  415. X             (eq exp-token 'end)))
  416. X        (throw 'syntax "Expected `]'")))
  417. X      (math-read-token)
  418. X      vals))
  419. X)
  420. X
  421. X(defun math-check-for-commas ()
  422. X  (let ((count 0)
  423. X    (pos (1- exp-pos)))
  424. X    (while (and (>= count 0)
  425. X        (setq pos (string-match "[],[{}()]" exp-str (1+ pos)))
  426. X        (or (/= (aref exp-str pos) ?,) (> count 0)))
  427. X      (cond ((memq (aref exp-str pos) '(?\[ ?\{ ?\())
  428. X         (setq count (1+ count)))
  429. X        ((memq (aref exp-str pos) '(?\] ?\} ?\)))
  430. X         (setq count (1- count)))))
  431. X    (and pos (= (aref exp-str pos) ?,)))
  432. X)
  433. X
  434. X(defun math-read-vector ()
  435. X  (let* ((val (list (math-read-expr-level 0)))
  436. X     (last val))
  437. X    (while (progn
  438. X         (while (eq exp-token 'space)
  439. X           (math-read-token))
  440. X         (and (not (eq exp-token 'end))
  441. X          (not (equal exp-data ";"))
  442. X          (not (equal exp-data close))
  443. X          (not (equal exp-data "\\dots"))))
  444. X      (if (equal exp-data ",")
  445. X      (math-read-token))
  446. X      (while (eq exp-token 'space)
  447. X    (math-read-token))
  448. X      (let ((rest (list (math-read-expr-level 0))))
  449. X    (setcdr last rest)
  450. X    (setq last rest)))
  451. X    (cons 'vec val))
  452. X)
  453. X
  454. X(defun math-read-matrix (mat)
  455. X  (while (equal exp-data ";")
  456. X    (math-read-token)
  457. X    (while (eq exp-token 'space)
  458. X      (math-read-token))
  459. X    (setq mat (nconc mat (list (math-read-vector)))))
  460. X  mat
  461. X)
  462. X
  463. X(defun math-read-string ()
  464. X  (let ((str (read-from-string (concat exp-data "\""))))
  465. X    (or (and (= (cdr str) (1+ (length exp-data)))
  466. X         (stringp (car str)))
  467. X    (throw 'syntax "Error in string constant"))
  468. X    (math-read-token)
  469. X    (append '(vec) (car str) nil))
  470. X)
  471. X
  472. X
  473. X
  474. X
  475. X
  476. X;;; Nontrivial "flat" formatting.
  477. X
  478. X(defun math-format-flat-expr-fancy (a prec)
  479. X  (cond
  480. X   ((eq (car a) 'incomplete)
  481. X    (concat "'" (prin1-to-string a)))
  482. X   ((eq (car a) 'vec)
  483. X    (concat "[" (math-format-flat-vector (cdr a) ", "
  484. X                     (if (cdr (cdr a)) 0 1000)) "]"))
  485. X   ((eq (car a) 'intv)
  486. X    (concat (if (memq (nth 1 a) '(0 1)) "(" "[")
  487. X        (math-format-flat-expr (nth 2 a) 1000)
  488. X        " .. "
  489. X        (math-format-flat-expr (nth 3 a) 1000)
  490. X        (if (memq (nth 1 a) '(0 2)) ")" "]")))
  491. X   ((eq (car a) 'var)
  492. X    (symbol-name (nth 1 a)))
  493. X   (t
  494. X    (let ((op (math-assq2 (car a) math-standard-opers)))
  495. X      (cond ((and op (= (length a) 3))
  496. X         (if (> prec (min (nth 2 op) (nth 3 op)))
  497. X         (concat "(" (math-format-flat-expr a 0) ")")
  498. X           (let ((lhs (math-format-flat-expr (nth 1 a) (nth 2 op)))
  499. X             (rhs (math-format-flat-expr (nth 2 a) (nth 3 op))))
  500. X         (setq op (car op))
  501. X         (if (equal op "^")
  502. X             (if (= (aref lhs 0) ?-)
  503. X             (setq lhs (concat "(" lhs ")")))
  504. X           (setq op (concat " " op " ")))
  505. X         (concat lhs op rhs))))
  506. X        ((eq (car a) 'neg)
  507. X         (concat "-" (math-format-flat-expr (nth 1 a) 1000)))
  508. X        (t
  509. X         (concat (math-remove-dashes
  510. X              (if (string-match "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
  511. X                    (symbol-name (car a)))
  512. X              (math-match-substring (symbol-name (car a)) 1)
  513. X            (symbol-name (car a))))
  514. X             "("
  515. X             (math-format-flat-vector (cdr a) ", " 0)
  516. X             ")"))))))
  517. X)
  518. X
  519. X(defun math-format-flat-vector (vec sep prec)
  520. X  (if vec
  521. X      (let ((buf (math-format-flat-expr (car vec) prec)))
  522. X    (while (setq vec (cdr vec))
  523. X      (setq buf (concat buf sep (math-format-flat-expr (car vec) prec))))
  524. X    buf)
  525. X    "")
  526. X)
  527. X
  528. X(defun math-assq2 (v a)
  529. X  (cond ((null a) nil)
  530. X    ((eq v (nth 1 (car a))) (car a))
  531. X    (t (math-assq2 v (cdr a))))
  532. X)
  533. X
  534. X
  535. X(defun math-format-number-fancy (a)
  536. X  (cond
  537. X   ((eq (car a) 'cplx)
  538. X    (if (null calc-complex-format)
  539. X    (concat "(" (math-format-number (nth 1 a))
  540. X        ", " (math-format-number (nth 2 a)) ")")
  541. X      (if (math-zerop (nth 1 a))
  542. X      (concat (math-format-number (nth 2 a))
  543. X          (symbol-name calc-complex-format))
  544. X    (concat (math-format-number (nth 1 a))
  545. X        (if (math-negp (nth 2 a)) " - " " + ")
  546. X        (math-format-number (math-abs (nth 2 a)))
  547. X        (symbol-name calc-complex-format)))))
  548. X   ((eq (car a) 'polar)
  549. X    (concat "(" (math-format-number (nth 1 a))
  550. X        "; " (math-format-number (nth 2 a)) ")"))
  551. X   ((eq (car a) 'hms)
  552. X    (if (math-negp a)
  553. X    (concat "-" (math-format-number (math-neg a)))
  554. X      (let ((calc-number-radix 10)
  555. X        (calc-leading-zeros nil)
  556. X        (calc-group-digits nil))
  557. X    (format calc-hms-format
  558. X        (math-format-number (nth 1 a))
  559. X        (math-format-number (nth 2 a))
  560. X        (math-format-number (nth 3 a))))))
  561. X   (t (format "%s" a)))
  562. X)
  563. X
  564. X(defun math-format-bignum-fancy (a)   ; [X L]
  565. X  (let ((str (cond ((= calc-number-radix 10)
  566. X            (math-format-bignum-decimal a))
  567. X           ((= calc-number-radix 2)
  568. X            (math-format-bignum-binary a))
  569. X           ((= calc-number-radix 8)
  570. X            (math-format-bignum-octal a))
  571. X           ((= calc-number-radix 16)
  572. X            (math-format-bignum-hex a))
  573. X           (t (math-format-bignum-radix a)))))
  574. X    (if calc-leading-zeros
  575. X    (let* ((calc-internal-prec 6)
  576. X           (digs (math-compute-max-digits (math-abs calc-word-size)
  577. X                          calc-number-radix))
  578. X           (len (length str)))
  579. X      (if (< len digs)
  580. X          (setq str (concat (make-string (- digs len) ?0) str)))))
  581. X    (if calc-group-digits
  582. X    (let ((i (length str))
  583. X          (g (if (integerp calc-group-digits)
  584. X             (math-abs calc-group-digits)
  585. X           (if (memq calc-number-radix '(2 16)) 4 3))))
  586. X      (while (> i g)
  587. X        (setq i (- i g)
  588. X          str (concat (substring str 0 i)
  589. X                  calc-group-char
  590. X                  (substring str i))))
  591. X      str))
  592. X    (if (and (/= calc-number-radix 10)
  593. X         math-radix-explicit-format)
  594. X    (if calc-radix-formatter
  595. X        (funcall calc-radix-formatter calc-number-radix str)
  596. X      (format "%d#%s" calc-number-radix str))
  597. X      str))
  598. X)
  599. X
  600. X(defvar math-max-digits-cache nil)
  601. X(defun math-compute-max-digits (w r)
  602. X  (let* ((pair (+ (* r 100000) w))
  603. X     (res (assq pair math-max-digits-cache)))
  604. X    (if res
  605. X    (cdr res)
  606. X      (let* ((calc-command-flags nil)
  607. X         (digs (math-ceiling (math-div w (math-real-log2 r)))))
  608. X    (setq math-max-digits-cache (cons (cons pair digs)
  609. X                      math-max-digits-cache))
  610. X    digs)))
  611. X)
  612. X
  613. X(defvar math-log2-cache (list '(2 . 1)
  614. X                  '(4 . 2)
  615. X                  '(8 . 3)
  616. X                  '(10 . (float 332193 -5))
  617. X                  '(16 . 4)
  618. X                  '(32 . 5)))
  619. X(defun math-real-log2 (x)   ;;; calc-internal-prec must be 6
  620. X  (let ((res (assq x math-log2-cache)))
  621. X    (if res
  622. X    (cdr res)
  623. X      (let* ((calc-symbolic-mode nil)
  624. X         (log (math-log x 2)))
  625. X    (setq math-log2-cache (cons (cons x log) math-log2-cache))
  626. X    log)))
  627. X)
  628. X
  629. X(defun math-group-float (str)   ; [X X]
  630. X  (let* ((pt (or (string-match "[^0-9]" str) (length str)))
  631. X     (g (if (integerp calc-group-digits) (math-abs calc-group-digits) 3))
  632. X     (i pt))
  633. X    (if (and (integerp calc-group-digits) (< calc-group-digits 0))
  634. X    (while (< (setq i (+ (1+ i) g)) (length str))
  635. X      (setq str (concat (substring str 0 i)
  636. X                calc-group-char
  637. X                (substring str i)))))
  638. X    (setq i pt)
  639. X    (while (> i g)
  640. X      (setq i (- i g)
  641. X        str (concat (substring str 0 i)
  642. X            calc-group-char
  643. X            (substring str i))))
  644. X    str)
  645. X)
  646. X
  647. X(defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
  648. X                 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
  649. X                 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
  650. X                 "U" "V" "W" "X" "Y" "Z"])
  651. X(defmacro math-format-radix-digit (a)   ; [X D]
  652. X  (` (aref math-radix-digits (, a)))
  653. X)
  654. X
  655. X(defun math-format-radix (a)   ; [X S]
  656. X  (if (< a calc-number-radix)
  657. X      (if (< a 0)
  658. X      (concat "-" (math-format-radix (- a)))
  659. X    (math-format-radix-digit a))
  660. X    (let ((s ""))
  661. X      (while (> a 0)
  662. X    (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
  663. X          a (/ a calc-number-radix)))
  664. X      s))
  665. X)
  666. X
  667. X(defconst math-binary-digits ["000" "001" "010" "011"
  668. X                  "100" "101" "110" "111"])
  669. X(defun math-format-binary (a)   ; [X S]
  670. X  (if (< a 8)
  671. X      (if (< a 0)
  672. X      (concat "-" (math-format-binary (- a)))
  673. X    (math-format-radix a))
  674. X    (let ((s ""))
  675. X      (while (> a 7)
  676. X    (setq s (concat (aref math-binary-digits (% a 8)) s)
  677. X          a (/ a 8)))
  678. X      (concat (math-format-radix a) s)))
  679. X)
  680. X
  681. X(defun math-format-bignum-radix (a)   ; [X L]
  682. X  (cond ((null a) "0")
  683. X    ((and (null (cdr a))
  684. X          (< (car a) calc-number-radix))
  685. X     (math-format-radix-digit (car a)))
  686. X    (t
  687. X     (let ((q (math-div-bignum-digit a calc-number-radix)))
  688. X       (concat (math-format-bignum-radix (math-norm-bignum (car q)))
  689. X           (math-format-radix-digit (cdr q))))))
  690. X)
  691. X
  692. X(defun math-format-bignum-binary (a)   ; [X L]
  693. X  (cond ((null a) "0")
  694. X    ((null (cdr a))
  695. X     (math-format-binary (car a)))
  696. X    (t
  697. X     (let ((q (math-div-bignum-digit a 512)))
  698. X       (concat (math-format-bignum-binary (math-norm-bignum (car q)))
  699. X           (aref math-binary-digits (/ (cdr q) 64))
  700. X           (aref math-binary-digits (% (/ (cdr q) 8) 8))
  701. X           (aref math-binary-digits (% (cdr q) 8))))))
  702. X)
  703. X
  704. X(defun math-format-bignum-octal (a)   ; [X L]
  705. X  (cond ((null a) "0")
  706. X    ((null (cdr a))
  707. X     (math-format-radix (car a)))
  708. X    (t
  709. X     (let ((q (math-div-bignum-digit a 512)))
  710. X       (concat (math-format-bignum-octal (math-norm-bignum (car q)))
  711. X           (math-format-radix-digit (/ (cdr q) 64))
  712. X           (math-format-radix-digit (% (/ (cdr q) 8) 8))
  713. X           (math-format-radix-digit (% (cdr q) 8))))))
  714. X)
  715. X
  716. X(defun math-format-bignum-hex (a)   ; [X L]
  717. X  (cond ((null a) "0")
  718. X    ((null (cdr a))
  719. X     (math-format-radix (car a)))
  720. X    (t
  721. X     (let ((q (math-div-bignum-digit a 256)))
  722. X       (concat (math-format-bignum-hex (math-norm-bignum (car q)))
  723. X           (math-format-radix-digit (/ (cdr q) 16))
  724. X           (math-format-radix-digit (% (cdr q) 16))))))
  725. X)
  726. X
  727. X
  728. X
  729. X
  730. X
  731. X
  732. X
  733. X;;; A "composition" has one of the following forms:
  734. X;;;
  735. X;;;    "string"              A literal string
  736. X;;;
  737. X;;;    (horiz C1 C2 ...)     Horizontally abutted sub-compositions
  738. X;;;
  739. X;;;    (break LEVEL)         A potential line-break point
  740. X;;;
  741. X;;;    (vleft N C1 C2 ...)   Vertically stacked, left-justified sub-comps
  742. X;;;    (vcent N C1 C2 ...)   Vertically stacked, centered sub-comps
  743. X;;;    (vright N C1 C2 ...)  Vertically stacked, right-justified sub-comps
  744. X;;;                          N specifies baseline of the stack, 0=top line.
  745. X;;;
  746. X;;;    (supscr C1 C2)        Composition C1 with superscript C2
  747. X;;;    (subscr C1 C2)        Composition C1 with subscript C2
  748. X;;;    (rule)                Horizontal line, full width of enclosing comp
  749. X
  750. X(defun math-compose-expr (a prec)
  751. X  (let ((math-compose-level (1+ math-compose-level)))
  752. X    (cond
  753. X     ((math-scalarp a)
  754. X      (if (and (eq (car-safe a) 'frac)
  755. X           (memq calc-language '(tex math)))
  756. X      (math-compose-expr (list '/ (nth 1 a) (nth 2 a)) prec)
  757. X    (math-format-number a)))
  758. X     ((not (consp a)) (concat "'" (prin1-to-string a)))
  759. X     ((eq (car a) 'vec)
  760. X      (let ((left-bracket (if calc-vector-brackets
  761. X                  (substring calc-vector-brackets 0 1) ""))
  762. X        (right-bracket (if calc-vector-brackets
  763. X                   (substring calc-vector-brackets 1 2) ""))
  764. X        (comma (or calc-vector-commas " "))
  765. X        (just (cond ((eq calc-matrix-just 'right) 'vright)
  766. X            ((eq calc-matrix-just 'center) 'vcent)
  767. X            (t 'vleft))))
  768. X    (if (and (math-matrixp a) (not (math-matrixp (nth 1 a)))
  769. X         (memq calc-language '(nil big)))
  770. X        (if (= (length a) 2)
  771. X        (list 'horiz
  772. X              (concat left-bracket left-bracket " ")
  773. X              (math-compose-vector (cdr (nth 1 a))
  774. X                       (concat comma " "))
  775. X              (concat " " right-bracket right-bracket))
  776. X          (let* ((rows (1- (length a)))
  777. X             (cols (1- (length (nth 1 a))))
  778. X             (base (/ (1- rows) 2))
  779. X             (calc-language 'flat))
  780. X        (append '(horiz)
  781. X            (list (append '(vleft)
  782. X                      (list base)
  783. X                      (list (concat left-bracket
  784. X                            " "
  785. X                            left-bracket
  786. X                            " "))
  787. X                      (make-list (1- rows)
  788. X                         (concat "  "
  789. X                             left-bracket
  790. X                             " "))))
  791. X            (math-compose-matrix (cdr a) 1 cols base)
  792. X            (list (append '(vleft)
  793. X                      (list base)
  794. X                      (make-list (1- rows)
  795. X                         (concat " "
  796. X                             right-bracket
  797. X                             comma))
  798. X                      (list (concat " "
  799. X                            right-bracket
  800. X                            " "
  801. X                            right-bracket)))))))
  802. X      (if (and calc-display-strings
  803. X           (math-vector-is-string a))
  804. X          (prin1-to-string (concat (cdr a)))
  805. X        (list 'horiz
  806. X          left-bracket
  807. X          (math-compose-vector (cdr a)
  808. X                       (concat (or calc-vector-commas "") " "))
  809. X          right-bracket)))))
  810. X     ((eq (car a) 'incomplete)
  811. X      (if (cdr (cdr a))
  812. X      (cond ((eq (nth 1 a) 'vec)
  813. X         (list 'horiz "["
  814. X               (math-compose-vector (cdr (cdr a)) ", ")
  815. X               " ..."))
  816. X        ((eq (nth 1 a) 'cplx)
  817. X         (list 'horiz "("
  818. X               (math-compose-vector (cdr (cdr a)) ", ")
  819. X               ", ..."))
  820. X        ((eq (nth 1 a) 'polar)
  821. X         (list 'horiz "("
  822. X               (math-compose-vector (cdr (cdr a)) "; ")
  823. X               "; ..."))
  824. X        ((eq (nth 1 a) 'intv)
  825. X         (list 'horiz
  826. X               (if (memq (nth 2 a) '(0 1)) "(" "[")
  827. X               (math-compose-vector (cdr (cdr (cdr a))) " .. ")
  828. X               " .. ..."))
  829. X        (t (format "%s" a)))
  830. X    (cond ((eq (nth 1 a) 'vec) "[ ...")
  831. X          ((eq (nth 1 a) 'intv)
  832. X           (if (memq (nth 2 a) '(0 1)) "( ..." "[ ..."))
  833. X          (t "( ..."))))
  834. X     ((eq (car a) 'var)
  835. X      (let ((v (rassq (nth 2 a) math-expr-variable-mapping)))
  836. X    (if v
  837. X        (symbol-name (car v))
  838. X      (if (and (eq calc-language 'tex)
  839. X           calc-language-option
  840. X           (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'"
  841. X                 (symbol-name (nth 1 a))))
  842. X          (format "\\hbox{%s}" (symbol-name (nth 1 a)))
  843. X        (symbol-name (nth 1 a))))))
  844. X     ((eq (car a) 'intv)
  845. X      (list 'horiz
  846. X        (if (memq (nth 1 a) '(0 1)) "(" "[")
  847. X        (math-compose-expr (nth 2 a) 0)
  848. X        (if (eq calc-language 'tex) " \\dots " " .. ")
  849. X        (math-compose-expr (nth 3 a) 0)
  850. X        (if (memq (nth 1 a) '(0 2)) ")" "]")))
  851. X     ((and (eq (car a) 'calcFunc-subscr)
  852. X       (memq calc-language '(c pascal fortran)))
  853. X      (list 'horiz
  854. X        (math-compose-expr (nth 1 a) 1000)
  855. X        (if (eq calc-language 'fortran) "(" "[")
  856. X        (math-compose-vector (cdr (cdr a)) ", ")
  857. X        (if (eq calc-language 'fortran) ")" "]")))
  858. X     ((and (eq (car a) 'calcFunc-subscr)
  859. X       (eq calc-language 'big))
  860. X      (let ((a1 (math-compose-expr (nth 1 a) 1000))
  861. X        (a2 (math-compose-expr (nth 2 a) 0)))
  862. X    (if (eq (car-safe a1) 'subscr)
  863. X        (list 'subscr
  864. X          (nth 1 a1)
  865. X          (list 'horiz
  866. X            (nth 2 a1)
  867. X            ", "
  868. X            a2))
  869. X      (list 'subscr a1 a2))))
  870. X     ((and (eq (car a) 'calcFunc-sqrt)
  871. X       (eq calc-language 'tex))
  872. X      (list 'horiz
  873. X        "\\sqrt{"
  874. X        (math-compose-expr (nth 1 a) 0)
  875. X        "}"))
  876. X     ((and (eq (car a) '^)
  877. X       (eq calc-language 'big))
  878. X      (list 'supscr
  879. X        (if (math-looks-negp (nth 1 a))
  880. X        (list 'horiz "(" (math-compose-expr (nth 1 a) 0) ")")
  881. X          (math-compose-expr (nth 1 a) 201))
  882. X        (let ((calc-language 'flat))
  883. X          (math-compose-expr (nth 2 a) 0))))
  884. X     ((and (eq (car a) '/)
  885. X       (eq calc-language 'big))
  886. X      (let ((a1 (math-compose-expr (nth 1 a) 0))
  887. X        (a2 (math-compose-expr (nth 2 a) 0)))
  888. X    (list 'vcent
  889. X          (math-comp-height a1)
  890. X          a1 '(rule) a2)))
  891. X     (t
  892. X      (let ((op (and (not (eq calc-language 'unform))
  893. X             (math-assq2 (car a) math-expr-opers))))
  894. X    (cond ((and op (= (length a) 3)
  895. X            (/= (nth 3 op) -1)
  896. X            (not (eq (car a) 'calcFunc-if)))
  897. X           (cond
  898. X        ((> prec (min (nth 2 op) (nth 3 op)))
  899. X         (if (and (eq calc-language 'tex)
  900. X              (not (math-tex-expr-is-flat a)))
  901. X             (if (eq (car-safe a) '/)
  902. X             (list 'horiz "{" (math-compose-expr a -1) "}")
  903. X               (list 'horiz "\\left( "
  904. X                 (math-compose-expr a -1)
  905. X                 " \\right)"))
  906. X           (list 'horiz "(" (math-compose-expr a 0) ")")))
  907. X        ((and (eq calc-language 'tex)
  908. X              (memq (car a) '(/ calcFunc-choose))
  909. X              (>= prec 0))
  910. X         (list 'horiz "{" (math-compose-expr a -1) "}"))
  911. X        (t
  912. X         (let ((lhs (math-compose-expr (nth 1 a) (nth 2 op)))
  913. X               (rhs (math-compose-expr (nth 2 a) (nth 3 op))))
  914. X           (and (equal (car op) "^")
  915. X            (= (math-comp-first-char lhs) ?-)
  916. X            (setq lhs (list 'horiz "(" lhs ")")))
  917. X           (and (eq calc-language 'tex)
  918. X            (or (equal (car op) "^") (equal (car op) "_"))
  919. X            (not (and (stringp rhs) (= (length rhs) 1)))
  920. X            (setq rhs (list 'horiz "{" rhs "}")))
  921. X           (or (and (eq (car a) '*)
  922. X                (or (null calc-language)
  923. X                (assoc "2x" math-expr-opers))
  924. X                (let ((prevt (math-prod-last-term (nth 1 a)))
  925. X                  (nextt (math-prod-first-term (nth 2 a)))
  926. X                  (prevc (math-comp-last-char lhs))
  927. X                  (nextc (math-comp-first-char rhs)))
  928. X                  (and prevc nextc
  929. X                   (or (and (>= nextc ?a) (<= nextc ?z))
  930. X                       (and (>= nextc ?A) (<= nextc ?Z))
  931. X                       (and (>= nextc ?0) (<= nextc ?9))
  932. X                       (memq nextc '(?. ?_ ?\( ?\[ ?\{ ?\\)))
  933. X                   (not (and (eq (car-safe prevt) 'var)
  934. X                         (equal nextc ?\()))
  935. X                   (list 'horiz
  936. X                     lhs
  937. X                     (list 'break math-compose-level)
  938. X                     " "
  939. X                     rhs))))
  940. X               (list 'horiz
  941. X                 lhs
  942. X                 (list 'break math-compose-level)
  943. X                 (if (or (equal (car op) "^")
  944. X                     (equal (car op) "_")
  945. X                     (equal (car op) "*"))
  946. X                 (car op)
  947. X                   (concat " " (car op) " "))
  948. X                 rhs))))))
  949. X          ((and op (= (length a) 2) (= (nth 3 op) -1))
  950. X           (cond
  951. X        ((> prec (nth 2 op))
  952. X         (if (and (eq calc-language 'tex)
  953. X              (not (math-tex-expr-is-flat a)))
  954. X             (list 'horiz "\\left( "
  955. X               (math-compose-expr a -1)
  956. X               " \\right)")
  957. X           (list 'horiz "(" (math-compose-expr a 0) ")")))
  958. X        (t
  959. X         (let ((lhs (math-compose-expr (nth 1 a) (nth 2 op))))
  960. X         (list 'horiz
  961. X               lhs
  962. X               (if (or (> (length (car op)) 1)
  963. X                   (not (math-comp-is-flat lhs)))
  964. X               (concat " " (car op))
  965. X             (car op)))))))
  966. X          ((and op (= (length a) 2) (= (nth 2 op) -1))
  967. X           (cond
  968. X        ((eq (nth 3 op) 0)
  969. X         (let ((lr (and (eq calc-language 'tex)
  970. X                (not (math-tex-expr-is-flat (nth 1 a))))))
  971. X           (list 'horiz
  972. X             (if lr "\\left" "")
  973. X             (if (string-match "\\`u\\([^a-zA-Z]\\)\\'" (car op))
  974. X                 (substring (car op) 1)
  975. X               (car op))
  976. X             (if (or lr (> (length (car op)) 2)) " " "")
  977. X             (math-compose-expr (nth 1 a) -1)
  978. X             (if (or lr (> (length (car op)) 2)) " " "")
  979. X             (if lr "\\right" "")
  980. X             (car (nth 1 (memq op math-expr-opers))))))
  981. X        ((> prec (nth 3 op))
  982. X         (if (and (eq calc-language 'tex)
  983. X              (not (math-tex-expr-is-flat a)))
  984. X             (list 'horiz "\\left( "
  985. X               (math-compose-expr a -1)
  986. X               " \\right)")
  987. X           (list 'horiz "(" (math-compose-expr a 0) ")")))
  988. X        (t
  989. X         (let ((rhs (math-compose-expr (nth 1 a) (nth 3 op))))
  990. X           (list 'horiz
  991. X             (let ((ops (if (string-match "\\`u\\([^a-zA-Z]\\)\\'"
  992. X                              (car op))
  993. X                    (substring (car op) 1)
  994. X                      (car op))))
  995. X               (if (or (> (length ops) 1)
  996. X                   (not (math-comp-is-flat rhs)))
  997. X                   (concat ops " ")
  998. X                 ops))
  999. X             rhs)))))
  1000. X          ((and op (= (length a) 4) (eq (car a) 'calcFunc-if))
  1001. X           (list 'horiz
  1002. X             (math-compose-expr (nth 1 a) (nth 2 op))
  1003. X             " ? "
  1004. X             (math-compose-expr (nth 2 a) 0)
  1005. X             " : "
  1006. X             (math-compose-expr (nth 3 a) (nth 3 op))))
  1007. X          ((and (eq calc-language 'big)
  1008. X            (setq op (get (car a) 'math-compose-big)))
  1009. X           (funcall op a prec))
  1010. X          (t
  1011. X           (let* ((func (car a))
  1012. X              (func2 (assq func '(( mod . calcFunc-makemod )
  1013. X                      ( sdev . calcFunc-sdev )
  1014. X                      ( + . calcFunc-add )
  1015. X                      ( - . calcFunc-sub )
  1016. X                      ( * . calcFunc-mul )
  1017. X                      ( / . calcFunc-div )
  1018. X                      ( % . calcFunc-mod )
  1019. X                      ( ^ . calcFunc-pow )
  1020. X                      ( neg . calcFunc-neg )
  1021. X                      ( | . calcFunc-vconcat ))))
  1022. X              left right args)
  1023. X         (if func2
  1024. X             (setq func (cdr func2)))
  1025. X         (if (setq func2 (rassq func math-expr-function-mapping))
  1026. X             (setq func (car func2)))
  1027. X         (setq func (math-remove-dashes
  1028. X                 (if (string-match
  1029. X                  "\\`calcFunc-\\([a-zA-Z0-9']+\\)\\'"
  1030. X                  (symbol-name func))
  1031. X                 (math-match-substring (symbol-name func) 1)
  1032. X                   (symbol-name func))))
  1033. X         (if (and (eq calc-language 'tex)
  1034. X              calc-language-option
  1035. X              (string-match "\\`[a-zA-Z][a-zA-Z0-9]+\\'" func))
  1036. X             (setq func (format "\\hbox{%s}" func)))
  1037. X         (cond ((and (eq calc-language 'tex)
  1038. X                 (or (> (length a) 2)
  1039. X                 (not (math-tex-expr-is-flat (nth 1 a)))))
  1040. X            (setq left "\\left( "
  1041. X                  right " \\right)"))
  1042. X               ((and (eq calc-language 'tex)
  1043. X                 (eq (aref func 0) ?\\)
  1044. X                 (= (length a) 2)
  1045. X                 (or (Math-realp (nth 1 a))
  1046. X                 (memq (car (nth 1 a)) '(var *))))
  1047. X            (setq left "{"
  1048. X                  right "}"))
  1049. X               (t (setq left calc-function-open
  1050. X                right calc-function-close)))
  1051. X         (list 'horiz func left
  1052. X               (math-compose-vector (cdr a) ", ")
  1053. X               right))))))))
  1054. X)
  1055. X(setq math-compose-level 0)
  1056. X
  1057. X(defun math-prod-first-term (x)
  1058. X  (if (eq (car-safe x) '*)
  1059. X      (math-prod-first-term (nth 1 x))
  1060. X    x)
  1061. X)
  1062. X
  1063. X(defun math-prod-last-term (x)
  1064. X  (if (eq (car-safe x) '*)
  1065. X      (math-prod-last-term (nth (1- (length x)) x))
  1066. X    x)
  1067. X)
  1068. X
  1069. X(defun math-compose-vector (a sep)
  1070. X  (if a
  1071. X      (cons 'horiz
  1072. X        (cons (math-compose-expr (car a) 0)
  1073. X          (math-compose-vector-step (cdr a))))
  1074. X    "")
  1075. X)
  1076. X
  1077. X(defun math-compose-vector-step (a)
  1078. X  (and a
  1079. X       (cons sep
  1080. X         (cons (list 'break math-compose-level)
  1081. X           (cons (math-compose-expr (car a) 0)
  1082. X             (math-compose-vector-step (cdr a))))))
  1083. X)
  1084. X
  1085. X(defun math-compose-matrix (a col cols base)
  1086. X  (math-compose-matrix-step a col)
  1087. X)
  1088. X
  1089. X(defun math-compose-matrix-step (a col)
  1090. X  (if (= col cols)
  1091. X      (list (cons just
  1092. X          (cons base
  1093. X            (mapcar (function (lambda (r)
  1094. X                        (math-compose-expr (nth col r) 0)))
  1095. X                a))))
  1096. X    (cons (cons just
  1097. X        (cons base
  1098. X              (mapcar (function
  1099. X                   (lambda (r) (list 'horiz
  1100. X                         (math-compose-expr (nth col r)
  1101. X                                    0)
  1102. X                         (concat comma " "))))
  1103. X                  a)))
  1104. X      (math-compose-matrix-step a (1+ col))))
  1105. X)
  1106. X
  1107. X(defun math-vector-is-string (a)
  1108. X  (and (cdr a)
  1109. X       (progn
  1110. X     (while (and (setq a (cdr a))
  1111. X             (natnump (car a))
  1112. X             (<= (car a) 255)))
  1113. X     (null a)))
  1114. X)
  1115. X
  1116. X(defun math-tex-expr-is-flat (a)
  1117. X  (or (Math-integerp a)
  1118. X      (memq (car a) '(float var))
  1119. X      (and (memq (car a) '(+ - *))
  1120. X       (progn
  1121. X         (while (and (setq a (cdr a))
  1122. X             (math-tex-expr-is-flat (car a))))
  1123. X         (null a))))
  1124. X)
  1125. X
  1126. X
  1127. X
  1128. X;;; Convert a composition to string form, with embedded \n's if necessary.
  1129. X
  1130. X(defun math-composition-to-string (c &optional width)
  1131. X  (or width (setq width (calc-window-width)))
  1132. X  (if calc-display-raw
  1133. X      (math-comp-to-string-raw c 0)
  1134. X    (if (math-comp-is-flat c)
  1135. X    (math-comp-to-string-flat c width)
  1136. X      (math-vert-comp-to-string
  1137. X       (math-comp-simplify c width))))
  1138. X)
  1139. X
  1140. X(defun math-comp-is-flat (c)     ; check if c's height is 1.
  1141. X  (cond ((not (consp c)) t)
  1142. X    ((eq (car c) 'break) t)
  1143. X    ((eq (car c) 'horiz)
  1144. X     (while (and (setq c (cdr c))
  1145. X             (math-comp-is-flat (car c))))
  1146. X     (null c))
  1147. X    ((memq (car c) '(vleft vcent vright))
  1148. X     (and (= (length c) 3)
  1149. X          (= (nth 1 c) 0)
  1150. X          (math-comp-is-flat (nth 2 c))))
  1151. X    (t nil))
  1152. X)
  1153. X
  1154. X
  1155. X;;; Convert a one-line composition to a string.
  1156. X
  1157. X(defun math-comp-to-string-flat (c full-width)
  1158. X  (let ((comp-buf "")
  1159. X    (comp-word "")
  1160. X    (comp-pos 0)
  1161. X    (comp-wlen 0))
  1162. X    (math-comp-to-string-flat-term c)
  1163. X    (math-comp-to-string-flat-term '(break -1))
  1164. X    comp-buf)
  1165. X)
  1166. X
  1167. X(defun math-comp-to-string-flat-term (c)
  1168. X  (cond ((not (consp c))
  1169. X     (setq comp-word (concat comp-word c)
  1170. X           comp-wlen (+ comp-wlen (length c))))
  1171. X    ((eq (car c) 'horiz)
  1172. X     (while (setq c (cdr c))
  1173. X       (math-comp-to-string-flat-term (car c))))
  1174. X    ((eq (car c) 'break)
  1175. X     (if (or (<= (+ comp-pos comp-wlen) full-width)
  1176. X         (= (length comp-buf) 0)
  1177. X         (not calc-line-breaking))
  1178. X         (setq comp-buf (concat comp-buf comp-word)
  1179. X           comp-pos (+ comp-pos comp-wlen))
  1180. X       (if calc-line-numbering
  1181. X           (setq comp-buf (concat comp-buf "\n     " comp-word)
  1182. X             comp-pos (+ comp-wlen 5))
  1183. X         (setq comp-buf (concat comp-buf "\n " comp-word)
  1184. X           comp-pos (1+ comp-wlen))))
  1185. X     (setq comp-word ""
  1186. X           comp-wlen 0))
  1187. X    (t (math-comp-to-string-flat-term (nth 2 c))))
  1188. X)
  1189. X
  1190. X
  1191. X;;; Simplify a composition to a canonical form consisting of
  1192. X;;;   (vleft n "string" "string" "string" ...)
  1193. X;;; where 0 <= n < number-of-strings.
  1194. X
  1195. X(defun math-comp-simplify (c full-width)
  1196. X  (let ((comp-buf (list ""))
  1197. X    (comp-base 0)
  1198. X    (comp-height 1)
  1199. X    (comp-hpos 0)
  1200. X    (comp-vpos 0))
  1201. X    (math-comp-simplify-term c)
  1202. X    (cons 'vleft (cons comp-base comp-buf)))
  1203. X)
  1204. X
  1205. X(defun math-comp-add-string (s h v)
  1206. X  (and (> (length s) 0)
  1207. X       (let ((vv (+ v comp-base)))
  1208. X     (if (< vv 0)
  1209. X         (setq comp-buf (nconc (make-list (- vv) "") comp-buf)
  1210. X           comp-base (- v)
  1211. X           comp-height (- comp-height vv)
  1212. X           vv 0)
  1213. X       (if (>= vv comp-height)
  1214. X           (setq comp-buf (nconc comp-buf
  1215. X                     (make-list (1+ (- vv comp-height)) ""))
  1216. X             comp-height (1+ vv))))
  1217. X     (let ((str (nthcdr vv comp-buf)))
  1218. X       (setcar str (concat (car str)
  1219. X                   (make-string (- h (length (car str))) 32)
  1220. X                   s)))))
  1221. X)
  1222. X
  1223. X(defun math-comp-simplify-term (c)
  1224. X  (cond ((stringp c)
  1225. X     (math-comp-add-string c comp-hpos comp-vpos)
  1226. X     (setq comp-hpos (+ comp-hpos (length c))))
  1227. X    ((eq (car c) 'break)
  1228. X     nil)
  1229. X    ((eq (car c) 'horiz)
  1230. X     (while (setq c (cdr c))
  1231. X       (math-comp-simplify-term (car c))))
  1232. X    ((memq (car c) '(vleft vcent vright))
  1233. X     (let* ((comp-vpos (+ (- comp-vpos (nth 1 c))
  1234. X                  (1- (math-comp-ascent (nth 2 c)))))
  1235. X        (widths (mapcar 'math-comp-width (cdr (cdr c))))
  1236. X        (maxwid (apply 'max widths))
  1237. X        (bias (cond ((eq (car c) 'vleft) 0)
  1238. X                ((eq (car c) 'vcent) 1)
  1239. X                (t 2))))
  1240. X       (setq c (cdr c))
  1241. X       (while (setq c (cdr c))
  1242. X         (if (eq (car-safe (car c)) 'rule)
  1243. X         (math-comp-add-string (make-string maxwid ?-)
  1244. X                       comp-hpos comp-vpos)
  1245. X           (let ((comp-hpos (+ comp-hpos (/ (* bias (- maxwid
  1246. X                               (car widths)))
  1247. X                        2))))
  1248. X         (math-comp-simplify-term (car c))))
  1249. X         (and (cdr c)
  1250. X          (setq comp-vpos (+ comp-vpos
  1251. X                     (+ (math-comp-descent (car c))
  1252. X                    (math-comp-ascent (nth 1 c))))
  1253. X            widths (cdr widths))))
  1254. X       (setq comp-hpos (+ comp-hpos maxwid))))
  1255. X    ((eq (car c) 'supscr)
  1256. X     (math-comp-simplify-term (nth 1 c))
  1257. X     (let* ((asc (math-comp-ascent (nth 1 c)))
  1258. X        (desc (math-comp-descent (nth 2 c)))
  1259. X        (comp-vpos (- comp-vpos (+ asc desc))))
  1260. X       (math-comp-simplify-term (nth 2 c))))
  1261. X    ((eq (car c) 'subscr)
  1262. X     (math-comp-simplify-term (nth 1 c))
  1263. X     (let* ((asc (math-comp-ascent (nth 2 c)))
  1264. X        (desc (math-comp-descent (nth 1 c)))
  1265. X        (comp-vpos (+ comp-vpos (+ asc desc))))
  1266. X       (math-comp-simplify-term (nth 2 c)))))
  1267. X)
  1268. X
  1269. X
  1270. X;;; Measuring a composition.
  1271. X
  1272. X(defun math-comp-first-char (c)
  1273. X  (cond ((stringp c)
  1274. X     (and (> (length c) 0)
  1275. X          (elt c 0)))
  1276. X    ((memq (car c) '(horiz subscr supscr))
  1277. X     (let (ch)
  1278. X       (while (and (setq c (cdr c))
  1279. X               (not (setq ch (math-comp-first-char (car c))))))
  1280. X       ch)))
  1281. X)
  1282. X
  1283. X(defun math-comp-last-char (c)
  1284. X  (cond ((stringp c)
  1285. X     (and (> (length c) 0)
  1286. X          (elt c (1- (length c)))))
  1287. X    ((eq (car c) 'horiz)
  1288. X     (let ((c (reverse (cdr c))) ch)
  1289. X       (while (and c
  1290. X               (not (setq ch (math-comp-last-char (car c)))))
  1291. X         (setq c (cdr c)))
  1292. X       ch)))
  1293. X)
  1294. X
  1295. X(defun math-comp-width (c)
  1296. X  (cond ((not (consp c)) (length c))
  1297. X    ((memq (car c) '(horiz subscr supscr))
  1298. X     (let ((accum 0))
  1299. X       (while (setq c (cdr c))
  1300. X         (setq accum (+ accum (math-comp-width (car c)))))
  1301. X       accum))
  1302. X    ((memq (car c) '(vcent vleft vright))
  1303. X     (setq c (cdr c))
  1304. X     (let ((accum 0))
  1305. X       (while (setq c (cdr c))
  1306. X         (setq accum (max accum (math-comp-width (car c)))))
  1307. X       accum))
  1308. X    (t 0))
  1309. X)
  1310. X
  1311. X(defun math-comp-height (c)
  1312. X  (if (stringp c)
  1313. X      1
  1314. X    (+ (math-comp-ascent c) (math-comp-descent c)))
  1315. X)
  1316. X
  1317. X(defun math-comp-ascent (c)
  1318. X  (cond ((not (consp c)) 1)
  1319. X    ((eq (car c) 'horiz)
  1320. X     (let ((accum 0))
  1321. X       (while (setq c (cdr c))
  1322. X         (setq accum (max accum (math-comp-ascent (car c)))))
  1323. X       accum))
  1324. X    ((memq (car c) '(vcent vleft vright))
  1325. X     (if (> (nth 1 c) 0) (1+ (nth 1 c)) 1))
  1326. X    ((eq (car c) 'supscr)
  1327. X     (+ (math-comp-ascent (nth 1 c)) (math-comp-height (nth 2 c))))
  1328. X    ((eq (car c) 'subscr)
  1329. X     (math-comp-ascent (nth 1 c)))
  1330. X    (t 1))
  1331. X)
  1332. X
  1333. X(defun math-comp-descent (c)
  1334. X  (cond ((not (consp c)) 0)
  1335. X    ((eq (car c) 'horiz)
  1336. X     (let ((accum 0))
  1337. X       (while (setq c (cdr c))
  1338. X         (setq accum (max accum (math-comp-descent (car c)))))
  1339. X       accum))
  1340. X    ((memq (car c) '(vcent vleft vright))
  1341. X     (let ((accum (- (nth 1 c))))
  1342. X       (setq c (cdr c))
  1343. X       (while (setq c (cdr c))
  1344. X         (setq accum (+ accum (math-comp-height (car c)))))
  1345. X       (max (1- accum) 0)))
  1346. X    ((eq (car c) 'supscr)
  1347. X     (math-comp-descent (nth 1 c)))
  1348. X    ((eq (car c) 'subscr)
  1349. X     (+ (math-comp-descent (nth 1 c)) (math-comp-height (nth 2 c))))
  1350. X    (t 0))
  1351. X)
  1352. X
  1353. X
  1354. X;;; Convert a simplified composition into string form.
  1355. X
  1356. X(defun math-vert-comp-to-string (c)
  1357. X  (if (stringp c)
  1358. X      c
  1359. X    (math-vert-comp-to-string-step (cdr (cdr c))))
  1360. X)
  1361. X
  1362. X(defun math-vert-comp-to-string-step (c)
  1363. X  (if (cdr c)
  1364. X      (concat (car c) "\n" (math-vert-comp-to-string-step (cdr c)))
  1365. X    (car c))
  1366. X)
  1367. X
  1368. X
  1369. X;;; Convert a composition to a string in "raw" form (for debugging).
  1370. X
  1371. X(defun math-comp-to-string-raw (c indent)
  1372. X  (cond ((not (consp c))
  1373. X     (prin1-to-string c))
  1374. X    (t
  1375. X     (let ((next-indent (+ indent 2 (length (symbol-name (car c))))))
  1376. X       (if (null (cdr c))
  1377. X           (concat "(" (symbol-name (car c)) ")")
  1378. X         (concat "("
  1379. X             (symbol-name (car c))
  1380. X             " "
  1381. X             (math-comp-to-string-raw (nth 1 c) next-indent)
  1382. X             (math-comp-to-string-raw-step (cdr (cdr c))
  1383. X                           next-indent)
  1384. X             ")")))))
  1385. X)
  1386. X
  1387. X(defun math-comp-to-string-raw-step (cl indent)
  1388. X  (if cl
  1389. X      (concat "\n"
  1390. X          (make-string indent 32)
  1391. X          (math-comp-to-string-raw (car cl) indent)
  1392. X          (math-comp-to-string-raw-step (cdr cl) indent))
  1393. X    "")
  1394. X)
  1395. X
  1396. X
  1397. X
  1398. X
  1399. X
  1400. X
  1401. X;;;; End.
  1402. X
  1403. SHAR_EOF
  1404. echo "File calc-ext.el is complete"
  1405. chmod 0664 calc-ext.el || echo "restore of calc-ext.el fails"
  1406. set `wc -c calc-ext.el`;Sum=$1
  1407. if test "$Sum" != "460649"
  1408. then echo original size 460649, current size $Sum;fi
  1409. echo "x - extracting calc.texinfo (Text)"
  1410. sed 's/^X//' << 'SHAR_EOF' > calc.texinfo &&
  1411. X\input texinfo                  @c -*-texinfo-*-
  1412. X@comment %**start of header (This is for running Texinfo on a region.)
  1413. X@setfilename calc-info
  1414. X@settitle GNU Emacs Calc 1.01 Manual
  1415. X@setchapternewpage odd
  1416. X@comment %**end of header (This is for running Texinfo on a region.)
  1417. X
  1418. X@ifinfo
  1419. XThis file documents Calc, the GNU Emacs calculator.
  1420. X
  1421. XCopyright (C) 1990 Dave Gillespie
  1422. X
  1423. XPermission is granted to make and distribute verbatim copies of this
  1424. Xmanual provided the copyright notice and this permission notice are
  1425. Xpreserved on all copies.
  1426. X
  1427. X@ignore
  1428. XPermission is granted to process this file through TeX and print the
  1429. Xresults, provided the printed document carries copying permission notice
  1430. Xidentical to this one except for the removal of this paragraph (this
  1431. Xparagraph not being relevant to the printed manual).
  1432. X
  1433. X@end ignore
  1434. XPermission is granted to copy and distribute modified versions of this
  1435. Xmanual under the conditions for verbatim copying, provided also that the
  1436. Xsection entitled ``GNU General Public License'' is included exactly as
  1437. Xin the original, and provided that the entire resulting derived work is
  1438. Xdistributed under the terms of a permission notice identical to this one.
  1439. X
  1440. XPermission is granted to copy and distribute translations of this manual
  1441. Xinto another language, under the above conditions for modified versions,
  1442. Xexcept that the section entitled ``GNU General Public License'' may be
  1443. Xincluded in a translation approved by the author instead of in the
  1444. Xoriginal English.
  1445. X@end ifinfo
  1446. X
  1447. X@titlepage
  1448. X@sp 6
  1449. X@center @titlefont{Calc Manual}
  1450. X@sp 4
  1451. X@center GNU Emacs Calc Version 1.01
  1452. X@sp 1
  1453. X@center May 1990
  1454. X@sp 5
  1455. X@center Dave Gillespie
  1456. X@page
  1457. X
  1458. X@vskip 0pt plus 1filll
  1459. XCopyright @copyright{} 1990 Dave Gillespie
  1460. X
  1461. XPermission is granted to make and distribute verbatim copies of
  1462. Xthis manual provided the copyright notice and this permission notice
  1463. Xare preserved on all copies.
  1464. X
  1465. X@ignore
  1466. XPermission is granted to process this file through TeX and print the
  1467. Xresults, provided the printed document carries copying permission notice
  1468. Xidentical to this one except for the removal of this paragraph (this
  1469. Xparagraph not being relevant to the printed manual).
  1470. X
  1471. X@end ignore
  1472. XPermission is granted to copy and distribute modified versions of this
  1473. Xmanual under the conditions for verbatim copying, provided also that the
  1474. Xsection entitled ``GNU General Public License'' is included exactly as
  1475. Xin the original, and provided that the entire resulting derived work is
  1476. Xdistributed under the terms of a permission notice identical to this one.
  1477. X
  1478. XPermission is granted to copy and distribute translations of this manual
  1479. Xinto another language, under the above conditions for modified versions,
  1480. Xexcept that the section entitled ``GNU General Public License'' may be
  1481. Xincluded in a translation approved by the author instead of in the
  1482. Xoriginal English.
  1483. X@end titlepage
  1484. X
  1485. X@ifinfo
  1486. X@node Top, Introduction,, (dir)
  1487. X@ichapter The GNU Emacs Calculator
  1488. X
  1489. X@dfn{Calc} is an advanced desk calculator and mathematical tool that runs
  1490. Xas part of the GNU Emacs environment.
  1491. X
  1492. XThis manual is divided into two major parts, the Tutorial and the
  1493. XReference.  The Tutorial introduces all the major aspects of Calculator
  1494. Xuse in an easy, hands-on way.  The remainder of the manual is a
  1495. Xcomplete reference on the features of the Calculator.
  1496. X
  1497. X@end ifinfo
  1498. X@menu
  1499. X* Copying::               How you can copy and share Calc.
  1500. X
  1501. X* Quick Overview::      If you're in a hurry to use Calc.
  1502. X* Tutorial::              A step-by-step introduction for beginners.
  1503. X
  1504. X* Introduction::      A full introduction to Calc.
  1505. X* Data Types::          Types of objects manipulated by Calc.
  1506. X* Stack and Trail::      Manipulating the stack and trail buffers.
  1507. X* Mode Settings::      Adjusting display format and other modes.
  1508. X* Arithmetic::          Basic arithmetic functions.
  1509. X* Scientific Functions::  Trancendentals and other scientific functions.
  1510. X* Binary Functions::      Bitwise operations on integers.
  1511. X* Matrix Functions::      Operations on vectors and matrices.
  1512. X* Algebra::              Manipulating expressions algebraically.
  1513. X* Units::              Operations on numbers with units.
  1514. X* Store and Recall::      Storing and recalling variables.
  1515. X* Kill and Yank::      Moving data into and out of Calc.
  1516. X* Programming::          Calc as a programmable calculator.
  1517. X
  1518. X* Installation::      Installing Calc as a part of GNU Emacs.
  1519. X* Reporting Bugs::      How to report bugs and make suggestions.
  1520. X
  1521. X* Key Index::          The standard Calc key sequences.
  1522. X* Command Index::      The interactive Calc commands.
  1523. X* Function Index::      Functions (in algebraic formulas).
  1524. X* Concept Index::      General concepts.
  1525. X* Lisp Function Index::      Internal Lisp math functions.
  1526. X* Lisp Variable Index::      Internal Lisp variables used by Calc.
  1527. X@end menu
  1528. X
  1529. X@node Copying, Quick Overview, Top, Top
  1530. X@unnumbered GNU GENERAL PUBLIC LICENSE
  1531. X@center Version 1, February 1989
  1532. X
  1533. X@display
  1534. XCopyright @copyright{} 1989 Free Software Foundation, Inc.
  1535. X675 Mass Ave, Cambridge, MA 02139, USA
  1536. X
  1537. XEveryone is permitted to copy and distribute verbatim copies
  1538. Xof this license document, but changing it is not allowed.
  1539. X@end display
  1540. X
  1541. X@unnumberedsec Preamble
  1542. X
  1543. X  The license agreements of most software companies try to keep users
  1544. Xat the mercy of those companies.  By contrast, our General Public
  1545. XLicense is intended to guarantee your freedom to share and change free
  1546. Xsoftware---to make sure the software is free for all its users.  The
  1547. XGeneral Public License applies to the Free Software Foundation's
  1548. Xsoftware and to any other program whose authors commit to using it.
  1549. XYou can use it for your programs, too.
  1550. X
  1551. X  When we speak of free software, we are referring to freedom, not
  1552. Xprice.  Specifically, the General Public License is designed to make
  1553. Xsure that you have the freedom to give away or sell copies of free
  1554. Xsoftware, that you receive source code or can get it if you want it,
  1555. Xthat you can change the software or use pieces of it in new free
  1556. Xprograms; and that you know you can do these things.
  1557. X
  1558. X  To protect your rights, we need to make restrictions that forbid
  1559. Xanyone to deny you these rights or to ask you to surrender the rights.
  1560. XThese restrictions translate to certain responsibilities for you if you
  1561. Xdistribute copies of the software, or if you modify it.
  1562. X
  1563. X  For example, if you distribute copies of a such a program, whether
  1564. Xgratis or for a fee, you must give the recipients all the rights that
  1565. Xyou have.  You must make sure that they, too, receive or can get the
  1566. Xsource code.  And you must tell them their rights.
  1567. X
  1568. X  We protect your rights with two steps: (1) copyright the software, and
  1569. X(2) offer you this license which gives you legal permission to copy,
  1570. Xdistribute and/or modify the software.
  1571. X
  1572. X  Also, for each author's protection and ours, we want to make certain
  1573. Xthat everyone understands that there is no warranty for this free
  1574. Xsoftware.  If the software is modified by someone else and passed on, we
  1575. Xwant its recipients to know that what they have is not the original, so
  1576. Xthat any problems introduced by others will not reflect on the original
  1577. Xauthors' reputations.
  1578. X
  1579. X  The precise terms and conditions for copying, distribution and
  1580. Xmodification follow.
  1581. X
  1582. X@iftex
  1583. X@unnumberedsec TERMS AND CONDITIONS
  1584. X@end iftex
  1585. X@ifinfo
  1586. X@center TERMS AND CONDITIONS
  1587. X@end ifinfo
  1588. X
  1589. X@enumerate
  1590. X@item
  1591. XThis License Agreement applies to any program or other work which
  1592. Xcontains a notice placed by the copyright holder saying it may be
  1593. Xdistributed under the terms of this General Public License.  The
  1594. X``Program'', below, refers to any such program or work, and a ``work based
  1595. Xon the Program'' means either the Program or any work containing the
  1596. XProgram or a portion of it, either verbatim or with modifications.  Each
  1597. Xlicensee is addressed as ``you''.
  1598. X
  1599. X@item
  1600. XYou may copy and distribute verbatim copies of the Program's source
  1601. Xcode as you receive it, in any medium, provided that you conspicuously and
  1602. Xappropriately publish on each copy an appropriate copyright notice and
  1603. Xdisclaimer of warranty; keep intact all the notices that refer to this
  1604. XGeneral Public License and to the absence of any warranty; and give any
  1605. Xother recipients of the Program a copy of this General Public License
  1606. Xalong with the Program.  You may charge a fee for the physical act of
  1607. Xtransferring a copy.
  1608. X
  1609. X@item
  1610. XYou may modify your copy or copies of the Program or any portion of
  1611. Xit, and copy and distribute such modifications under the terms of Paragraph
  1612. X1 above, provided that you also do the following:
  1613. X
  1614. X@itemize @bullet
  1615. X@item
  1616. Xcause the modified files to carry prominent notices stating that
  1617. Xyou changed the files and the date of any change; and
  1618. X
  1619. X@item
  1620. Xcause the whole of any work that you distribute or publish, that
  1621. Xin whole or in part contains the Program or any part thereof, either
  1622. Xwith or without modifications, to be licensed at no charge to all
  1623. Xthird parties under the terms of this General Public License (except
  1624. Xthat you may choose to grant warranty protection to some or all
  1625. Xthird parties, at your option).
  1626. X
  1627. X@item
  1628. XIf the modified program normally reads commands interactively when
  1629. Xrun, you must cause it, when started running for such interactive use
  1630. Xin the simplest and most usual way, to print or display an
  1631. Xannouncement including an appropriate copyright notice and a notice
  1632. Xthat there is no warranty (or else, saying that you provide a
  1633. Xwarranty) and that users may redistribute the program under these
  1634. Xconditions, and telling the user how to view a copy of this General
  1635. XPublic License.
  1636. X
  1637. X@item
  1638. XYou may charge a fee for the physical act of transferring a
  1639. Xcopy, and you may at your option offer warranty protection in
  1640. Xexchange for a fee.
  1641. X@end itemize
  1642. X
  1643. XMere aggregation of another independent work with the Program (or its
  1644. Xderivative) on a volume of a storage or distribution medium does not bring
  1645. Xthe other work under the scope of these terms.
  1646. X
  1647. X@item
  1648. XYou may copy and distribute the Program (or a portion or derivative of
  1649. Xit, under Paragraph 2) in object code or executable form under the terms of
  1650. XParagraphs 1 and 2 above provided that you also do one of the following:
  1651. X
  1652. X@itemize @bullet
  1653. X@item
  1654. Xaccompany it with the complete corresponding machine-readable
  1655. Xsource code, which must be distributed under the terms of
  1656. XParagraphs 1 and 2 above; or,
  1657. X
  1658. X@item
  1659. Xaccompany it with a written offer, valid for at least three
  1660. Xyears, to give any third party free (except for a nominal charge
  1661. Xfor the cost of distribution) a complete machine-readable copy of the
  1662. Xcorresponding source code, to be distributed under the terms of
  1663. XParagraphs 1 and 2 above; or,
  1664. X
  1665. X@item
  1666. Xaccompany it with the information you received as to where the
  1667. Xcorresponding source code may be obtained.  (This alternative is
  1668. Xallowed only for noncommercial distribution and only if you
  1669. Xreceived the program in object code or executable form alone.)
  1670. X@end itemize
  1671. X
  1672. XSource code for a work means the preferred form of the work for making
  1673. Xmodifications to it.  For an executable file, complete source code means
  1674. Xall the source code for all modules it contains; but, as a special
  1675. Xexception, it need not include source code for modules which are standard
  1676. Xlibraries that accompany the operating system on which the executable
  1677. Xfile runs, or for standard header files or definitions files that
  1678. Xaccompany that operating system.
  1679. X
  1680. X@item
  1681. XYou may not copy, modify, sublicense, distribute or transfer the
  1682. XProgram except as expressly provided under this General Public License.
  1683. XAny attempt otherwise to copy, modify, sublicense, distribute or transfer
  1684. Xthe Program is void, and will automatically terminate your rights to use
  1685. Xthe Program under this License.  However, parties who have received
  1686. Xcopies, or rights to use copies, from you under this General Public
  1687. XLicense will not have their licenses terminated so long as such parties
  1688. Xremain in full compliance.
  1689. X
  1690. X@item
  1691. XBy copying, distributing or modifying the Program (or any work based
  1692. Xon the Program) you indicate your acceptance of this license to do so,
  1693. Xand all its terms and conditions.
  1694. X
  1695. X@item
  1696. XEach time you redistribute the Program (or any work based on the
  1697. XProgram), the recipient automatically receives a license from the original
  1698. Xlicensor to copy, distribute or modify the Program subject to these
  1699. Xterms and conditions.  You may not impose any further restrictions on the
  1700. Xrecipients' exercise of the rights granted herein.
  1701. X
  1702. X@item
  1703. XThe Free Software Foundation may publish revised and/or new versions
  1704. Xof the General Public License from time to time.  Such new versions will
  1705. Xbe similar in spirit to the present version, but may differ in detail to
  1706. Xaddress new problems or concerns.
  1707. X
  1708. XEach version is given a distinguishing version number.  If the Program
  1709. Xspecifies a version number of the license which applies to it and ``any
  1710. Xlater version'', you have the option of following the terms and conditions
  1711. Xeither of that version or of any later version published by the Free
  1712. XSoftware Foundation.  If the Program does not specify a version number of
  1713. Xthe license, you may choose any version ever published by the Free Software
  1714. XFoundation.
  1715. X
  1716. X@item
  1717. XIf you wish to incorporate parts of the Program into other free
  1718. Xprograms whose distribution conditions are different, write to the author
  1719. Xto ask for permission.  For software which is copyrighted by the Free
  1720. XSoftware Foundation, write to the Free Software Foundation; we sometimes
  1721. Xmake exceptions for this.  Our decision will be guided by the two goals
  1722. Xof preserving the free status of all derivatives of our free software and
  1723. Xof promoting the sharing and reuse of software generally.
  1724. X
  1725. X@iftex
  1726. X@heading NO WARRANTY
  1727. X@end iftex
  1728. X@ifinfo
  1729. X@center NO WARRANTY
  1730. X@end ifinfo
  1731. X
  1732. X@item
  1733. XBECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  1734. XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
  1735. XOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  1736. XPROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  1737. XOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  1738. XMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
  1739. XTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  1740. XPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  1741. XREPAIR OR CORRECTION.
  1742. X
  1743. X@item
  1744. XIN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
  1745. XANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  1746. XREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  1747. XINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
  1748. XARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT
  1749. XLIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES
  1750. XSUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
  1751. XWITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN
  1752. XADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  1753. X@end enumerate
  1754. X
  1755. X@node Introduction, Data Types, Quick Overview, Top
  1756. X@chapter Introduction
  1757. X
  1758. X@dfn{Calc} is an advanced calculator and mathematical tool that runs as
  1759. Xpart of the GNU Emacs environment.  Very roughly based on the HP-28/48
  1760. Xseries of calculators, its many features include:
  1761. X
  1762. X@itemize @bullet
  1763. X@item
  1764. XChoice of algebraic or RPN style entry of calculations.
  1765. X@item
  1766. XArbitrary precision integers and floating-point numbers.
  1767. X@item
  1768. XArithmetic on rational numbers, complex numbers (rectangular and polar),
  1769. Xerror forms with standard deviations, open and closed intervals, vectors
  1770. Xand matrices, quantities with units, and simple algebraic expressions.
  1771. X@item
  1772. XMathematical operations such as logarithms and trig functions.
  1773. X@item
  1774. XProgrammer's features (bitwise operations, non-decimal integers).
  1775. X@item
  1776. XNumber theoretical features such as prime factorization and arithmetic
  1777. Xmodulo M for any M.
  1778. X@item
  1779. XAlgebraic manipulation features, including symbolic calculus.
  1780. X@item
  1781. XKill and yank to and from regular editing buffers.
  1782. X@item
  1783. XEasy programming using keyboard macros, algebraic formulas,
  1784. Xalgebraic rewrite rules, or Lisp code.
  1785. X@end itemize
  1786. X
  1787. XCalc tries to include a little something for everyone; as a result it is
  1788. Xlarge and might be intimidating to the first-time user.  If you plan to
  1789. Xuse Calc only as a traditional desk calculator, all you really need to
  1790. Xread is the ``Quick Overview'' section of this manual and possibly a few
  1791. Xof the other introductory sections.  As you become more comfortable with
  1792. Xthe program you can learn its additional features.  In terms of efficiency,
  1793. Xscope and depth, Calc cannot replace a powerful tool like Mathematica (tm).
  1794. XBut Calc has the advantages of convenience, portability, and availability
  1795. Xof the source code.  And, of course, it's free!
  1796. X
  1797. X@pindex calc
  1798. X@pindex calc-mode
  1799. X@cindex Starting the Calculator
  1800. X@cindex Running the Calculator
  1801. XTo start the Calculator, type @kbd{M-x calc}.  By default this creates
  1802. Xa pair of small windows, @samp{*Calculator*} and @samp{*Calc Trail*}.
  1803. XThe former displays the contents of the Calculator stack and is manipulated
  1804. Xexclusively through Calc commands.  It is possible (though not usually
  1805. Xnecessary) to create several Calc Mode buffers each of which has an
  1806. Xindependent stack, undo list, and mode settings.  There is exactly one
  1807. XCalc Trail buffer; it records a list of the results of all calculations
  1808. SHAR_EOF
  1809. echo "End of part 11"
  1810. echo "File calc.texinfo is continued in part 12"
  1811. echo "12" > s2_seq_.tmp
  1812. exit 0
  1813.