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

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i065:  gnucalc - GNU Emacs Calculator, v2.00, Part17/56
  4. Message-ID: <1991Oct29.230348.20633@sparky.imd.sterling.com>
  5. X-Md4-Signature: 59d8d4f7efa51197627b2983be2f9fa6
  6. Date: Tue, 29 Oct 1991 23:03:48 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 65
  11. Archive-name: gnucalc/part17
  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-funcs.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" != 17; 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-funcs.el'
  34. else
  35. echo 'x - continuing file calc-funcs.el'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc-funcs.el' &&
  37. X               by byp))
  38. X           by)))))
  39. )
  40. X
  41. (defun math-besY0 (x)
  42. X  (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
  43. X     (let ((y (math-sqr x)))
  44. X       (math-add
  45. X        (math-div (math-poly-eval y
  46. X                      '((float (bigpos 733 622 284 2) -7)
  47. X                    (float (bigneg 757 792 632 8) -5)
  48. X                    (float (bigpos 129 988 087 1) -2)
  49. X                    (float (bigneg 036 598 123 5) -1)
  50. X                    (float (bigpos 065 834 062 7) 0)
  51. X                    (float (bigneg 389 821 957 2) 0)))
  52. X              (math-poly-eval y
  53. X                      '((float 1 0)
  54. X                    (float (bigpos 244 030 261 2) -7)
  55. X                    (float (bigpos 647 472 474) -4)
  56. X                    (float (bigpos 438 466 189 7) -3)
  57. X                    (float (bigpos 648 499 452 7) -1)
  58. X                    (float (bigpos 269 544 076 40) 0))))
  59. X        (math-mul '(float (bigpos 772 619 636) -9)
  60. X              (math-mul (math-besJ0 x) (math-ln-raw x))))))
  61. X    ((math-negp (calcFunc-re x))
  62. X     (math-add (math-besJ0 (math-neg x) t)
  63. X           (math-mul '(cplx 0 2)
  64. X                 (math-besJ0 (math-neg x)))))
  65. X    (t
  66. X     (math-besJ0 x t)))
  67. )
  68. X
  69. (defun math-besY1 (x)
  70. X  (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
  71. X     (let ((y (math-sqr x)))
  72. X       (math-add
  73. X        (math-mul
  74. X         x
  75. X         (math-div (math-poly-eval y
  76. X                       '((float (bigpos 935 937 511 8) -6)
  77. X                     (float (bigneg 726 922 237 4) -3)
  78. X                     (float (bigpos 551 264 349 7) -1)
  79. X                     (float (bigneg 139 438 153 5) 1)
  80. X                     (float (bigpos 439 527 127) 4)
  81. X                     (float (bigneg 943 604 900 4) 3)))
  82. X               (math-poly-eval y
  83. X                       '((float 1 0)
  84. X                     (float (bigpos 885 632 549 3) -7)
  85. X                     (float (bigpos 605 042 102) -3)
  86. X                     (float (bigpos 002 904 245 2) -2)
  87. X                     (float (bigpos 367 650 733 3) 0)
  88. X                     (float (bigpos 664 419 244 4) 2)
  89. X                     (float (bigpos 057 958 249) 5)))))
  90. X        (math-mul '(float (bigpos 772 619 636) -9)
  91. X              (math-sub (math-mul (math-besJ1 x) (math-ln-raw x))
  92. X                (math-div 1 x))))))
  93. X    ((math-negp (calcFunc-re x))
  94. X     (math-neg
  95. X      (math-add (math-besJ1 (math-neg x) t)
  96. X            (math-mul '(cplx 0 2)
  97. X                  (math-besJ1 (math-neg x))))))
  98. X    (t
  99. X     (math-besJ1 x t)))
  100. )
  101. X
  102. (defun math-poly-eval (x coefs)
  103. X  (let ((accum (car coefs)))
  104. X    (while (setq coefs (cdr coefs))
  105. X      (setq accum (math-add (car coefs) (math-mul accum x))))
  106. X    accum)
  107. )
  108. X
  109. X
  110. ;;;; Bernoulli and Euler polynomials and numbers.
  111. X
  112. (defun calcFunc-bern (n &optional x)
  113. X  (if (and x (not (math-zerop x)))
  114. X      (if (and calc-symbolic-mode (math-floatp x))
  115. X      (math-inexact-result)
  116. X    (math-build-polynomial-expr (math-bernoulli-coefs n) x))
  117. X    (or (math-num-natnump n) (math-reject-arg n 'natnump))
  118. X    (if (consp n)
  119. X    (progn
  120. X      (math-inexact-result)
  121. X      (math-float (math-bernoulli-number (math-trunc n))))
  122. X      (math-bernoulli-number n)))
  123. )
  124. X
  125. (defun calcFunc-euler (n &optional x)
  126. X  (or (math-num-natnump n) (math-reject-arg n 'natnump))
  127. X  (if x
  128. X      (let* ((n1 (math-add n 1))
  129. X         (coefs (math-bernoulli-coefs n1))
  130. X         (fac (math-div (math-pow 2 n1) n1))
  131. X         (k -1)
  132. X         (x1 (math-div (math-add x 1) 2))
  133. X         (x2 (math-div x 2)))
  134. X    (if (math-numberp x)
  135. X        (if (and calc-symbolic-mode (math-floatp x))
  136. X        (math-inexact-result)
  137. X          (math-mul fac
  138. X            (math-sub (math-build-polynomial-expr coefs x1)
  139. X                  (math-build-polynomial-expr coefs x2))))
  140. X      (calcFunc-collect
  141. X       (math-reduce-vec
  142. X        'math-add
  143. X        (cons 'vec
  144. X          (mapcar (function
  145. X               (lambda (c)
  146. X                 (setq k (1+ k))
  147. X                 (math-mul (math-mul fac c)
  148. X                       (math-sub (math-pow x1 k)
  149. X                         (math-pow x2 k)))))
  150. X              coefs)))
  151. X       x)))
  152. X    (math-mul (math-pow 2 n)
  153. X          (if (consp n)
  154. X          (progn
  155. X            (math-inexact-result)
  156. X            (calcFunc-euler n '(float 5 -1)))
  157. X        (calcFunc-euler n '(frac 1 2)))))
  158. )
  159. X
  160. (defun math-bernoulli-coefs (n)
  161. X  (let* ((coefs (list (calcFunc-bern n)))
  162. X     (nn (math-trunc n))
  163. X     (k nn)
  164. X     (term nn)
  165. X     coef
  166. X     (calc-prefer-frac (or (integerp n) calc-prefer-frac)))
  167. X    (while (>= (setq k (1- k)) 0)
  168. X      (setq term (math-div term (- nn k))
  169. X        coef (math-mul term (math-bernoulli-number k))
  170. X        coefs (cons (if (consp n) (math-float coef) coef) coefs)
  171. X        term (math-mul term k)))
  172. X    (nreverse coefs))
  173. )
  174. X
  175. (defun math-bernoulli-number (n)
  176. X  (if (= (% n 2) 1)
  177. X      (if (= n 1)
  178. X      '(frac -1 2)
  179. X    0)
  180. X    (setq n (/ n 2))
  181. X    (while (>= n math-bernoulli-cache-size)
  182. X      (let* ((sum 0)
  183. X         (nk 1)     ; nk = n-k+1
  184. X         (fact 1)   ; fact = (n-k+1)!
  185. X         ofact
  186. X         (p math-bernoulli-b-cache)
  187. X         (calc-prefer-frac t))
  188. X    (math-working "bernoulli B" (* 2 math-bernoulli-cache-size))
  189. X    (while p
  190. X      (setq nk (+ nk 2)
  191. X        ofact fact
  192. X        fact (math-mul fact (* nk (1- nk)))
  193. X        sum (math-add sum (math-div (car p) fact))
  194. X        p (cdr p)))
  195. X    (setq ofact (math-mul ofact (1- nk))
  196. X          sum (math-sub (math-div '(frac 1 2) ofact) sum)
  197. X          math-bernoulli-b-cache (cons sum math-bernoulli-b-cache)
  198. X          math-bernoulli-B-cache (cons (math-mul sum ofact)
  199. X                       math-bernoulli-B-cache)
  200. X          math-bernoulli-cache-size (1+ math-bernoulli-cache-size))))
  201. X    (nth (- math-bernoulli-cache-size n 1) math-bernoulli-B-cache))
  202. )
  203. X
  204. ;;;   Bn = n! bn
  205. ;;;   bn = - sum_k=0^n-1 bk / (n-k+1)!
  206. X
  207. ;;; A faster method would be to use "tangent numbers", c.f., Concrete
  208. ;;; Mathematics pg. 273.
  209. X
  210. (setq math-bernoulli-b-cache '( (frac -174611
  211. X                      (bigpos 0 200 291 698 662 857 802))
  212. X                (frac 43867 (bigpos 0 944 170 217 94 109 5))
  213. X                (frac -3617 (bigpos 0 880 842 622 670 10))
  214. X                (frac 1 (bigpos 600 249 724 74))
  215. X                (frac -691 (bigpos 0 368 674 307 1))
  216. X                (frac 1 (bigpos 160 900 47))
  217. X                (frac -1 (bigpos 600 209 1))
  218. X                (frac 1 30240) (frac -1 720)
  219. X                (frac 1 12) 1 ))
  220. X
  221. (setq math-bernoulli-B-cache '( (frac -174611 330) (frac 43867 798)
  222. X                (frac -3617 510) (frac 7 6) (frac -691 2730)
  223. X                (frac 5 66) (frac -1 30) (frac 1 42)
  224. X                (frac -1 30) (frac 1 6) 1 ))
  225. X
  226. (setq math-bernoulli-cache-size 11)
  227. X
  228. X
  229. X
  230. ;;; Probability distributions.
  231. X
  232. ;;; Binomial.
  233. (defun calcFunc-utpb (x n p)
  234. X  (if math-expand-formulas
  235. X      (math-normalize (list 'calcFunc-betaI p x (list '+ (list '- n x) 1)))
  236. X    (calcFunc-betaI p x (math-add (math-sub n x) 1)))
  237. )
  238. (put 'calcFunc-utpb 'math-expandable t)
  239. X
  240. (defun calcFunc-ltpb (x n p)
  241. X  (math-sub 1 (calcFunc-utpb x n p))
  242. )
  243. (put 'calcFunc-ltpb 'math-expandable t)
  244. X
  245. ;;; Chi-square.
  246. (defun calcFunc-utpc (chisq v)
  247. X  (if math-expand-formulas
  248. X      (math-normalize (list 'calcFunc-gammaQ (list '/ v 2) (list '/ chisq 2)))
  249. X    (calcFunc-gammaQ (math-div v 2) (math-div chisq 2)))
  250. )
  251. (put 'calcFunc-utpc 'math-expandable t)
  252. X
  253. (defun calcFunc-ltpc (chisq v)
  254. X  (if math-expand-formulas
  255. X      (math-normalize (list 'calcFunc-gammaP (list '/ v 2) (list '/ chisq 2)))
  256. X    (calcFunc-gammaP (math-div v 2) (math-div chisq 2)))
  257. )
  258. (put 'calcFunc-ltpc 'math-expandable t)
  259. X
  260. ;;; F-distribution.
  261. (defun calcFunc-utpf (f v1 v2)
  262. X  (if math-expand-formulas
  263. X      (math-normalize (list 'calcFunc-betaI
  264. X                (list '/ v2 (list '+ v2 (list '* v1 f)))
  265. X                (list '/ v2 2)
  266. X                (list '/ v1 2)))
  267. X    (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f)))
  268. X            (math-div v2 2)
  269. X            (math-div v1 2)))
  270. )
  271. (put 'calcFunc-utpf 'math-expandable t)
  272. X
  273. (defun calcFunc-ltpf (f v1 v2)
  274. X  (math-sub 1 (calcFunc-utpf f v1 v2))
  275. )
  276. (put 'calcFunc-ltpf 'math-expandable t)
  277. X
  278. ;;; Normal.
  279. (defun calcFunc-utpn (x mean sdev)
  280. X  (if math-expand-formulas
  281. X      (math-normalize
  282. X       (list '/
  283. X         (list '+ 1
  284. X           (list 'calcFunc-erf
  285. X             (list '/ (list '- mean x)
  286. X                   (list '* sdev (list 'calcFunc-sqrt 2)))))
  287. X         2))
  288. X    (math-mul (math-add '(float 1 0)
  289. X            (calcFunc-erf
  290. X             (math-div (math-sub mean x)
  291. X                   (math-mul sdev (math-sqrt-2)))))
  292. X          '(float 5 -1)))
  293. )
  294. (put 'calcFunc-utpn 'math-expandable t)
  295. X
  296. (defun calcFunc-ltpn (x mean sdev)
  297. X  (if math-expand-formulas
  298. X      (math-normalize
  299. X       (list '/
  300. X         (list '+ 1
  301. X           (list 'calcFunc-erf
  302. X             (list '/ (list '- x mean)
  303. X                   (list '* sdev (list 'calcFunc-sqrt 2)))))
  304. X         2))
  305. X    (math-mul (math-add '(float 1 0)
  306. X            (calcFunc-erf
  307. X             (math-div (math-sub x mean)
  308. X                   (math-mul sdev (math-sqrt-2)))))
  309. X          '(float 5 -1)))
  310. )
  311. (put 'calcFunc-ltpn 'math-expandable t)
  312. X
  313. ;;; Poisson.
  314. (defun calcFunc-utpp (n x)
  315. X  (if math-expand-formulas
  316. X      (math-normalize (list 'calcFunc-gammaP x n))
  317. X    (calcFunc-gammaP x n))
  318. )
  319. (put 'calcFunc-utpp 'math-expandable t)
  320. X
  321. (defun calcFunc-ltpp (n x)
  322. X  (if math-expand-formulas
  323. X      (math-normalize (list 'calcFunc-gammaQ x n))
  324. X    (calcFunc-gammaQ x n))
  325. )
  326. (put 'calcFunc-ltpp 'math-expandable t)
  327. X
  328. ;;; Student's t.  (As defined in Abramowitz & Stegun and Numerical Recipes.)
  329. (defun calcFunc-utpt (tt v)
  330. X  (if math-expand-formulas
  331. X      (math-normalize (list 'calcFunc-betaI
  332. X                (list '/ v (list '+ v (list '^ tt 2)))
  333. X                (list '/ v 2)
  334. X                '(float 5 -1)))
  335. X    (calcFunc-betaI (math-div v (math-add v (math-sqr tt)))
  336. X            (math-div v 2)
  337. X            '(float 5 -1)))
  338. )
  339. (put 'calcFunc-utpt 'math-expandable t)
  340. X
  341. (defun calcFunc-ltpt (tt v)
  342. X  (math-sub 1 (calcFunc-utpt tt v))
  343. )
  344. (put 'calcFunc-ltpt 'math-expandable t)
  345. X
  346. X
  347. X
  348. X
  349. SHAR_EOF
  350. echo 'File calc-funcs.el is complete' &&
  351. chmod 0644 calc-funcs.el ||
  352. echo 'restore of calc-funcs.el failed'
  353. Wc_c="`wc -c < 'calc-funcs.el'`"
  354. test 30406 -eq "$Wc_c" ||
  355.     echo 'calc-funcs.el: original size 30406, current size' "$Wc_c"
  356. rm -f _shar_wnt_.tmp
  357. fi
  358. # ============= calc-graph.el ==============
  359. if test -f 'calc-graph.el' -a X"$1" != X"-c"; then
  360.     echo 'x - skipping calc-graph.el (File already exists)'
  361.     rm -f _shar_wnt_.tmp
  362. else
  363. > _shar_wnt_.tmp
  364. echo 'x - extracting calc-graph.el (Text)'
  365. sed 's/^X//' << 'SHAR_EOF' > 'calc-graph.el' &&
  366. ;; Calculator for GNU Emacs, part II [calc-graph.el]
  367. ;; Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  368. ;; Written by Dave Gillespie, daveg@csvax.cs.caltech.edu.
  369. X
  370. ;; This file is part of GNU Emacs.
  371. X
  372. ;; GNU Emacs is distributed in the hope that it will be useful,
  373. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  374. ;; accepts responsibility to anyone for the consequences of using it
  375. ;; or for whether it serves any particular purpose or works at all,
  376. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  377. ;; License for full details.
  378. X
  379. ;; Everyone is granted permission to copy, modify and redistribute
  380. ;; GNU Emacs, but only under the conditions described in the
  381. ;; GNU Emacs General Public License.   A copy of this license is
  382. ;; supposed to have been given to you along with GNU Emacs so you
  383. ;; can know your rights and responsibilities.  It should be in a
  384. ;; file named COPYING.  Among other things, the copyright notice
  385. ;; and this notice must be preserved on all copies.
  386. X
  387. X
  388. X
  389. ;; This file is autoloaded from calc-ext.el.
  390. (require 'calc-ext)
  391. X
  392. (require 'calc-macs)
  393. X
  394. (defun calc-Need-calc-graph () nil)
  395. X
  396. X
  397. ;;; Graphics
  398. X
  399. ;;; Note that some of the following initial values also occur in calc.el.
  400. (defvar calc-gnuplot-tempfile "/tmp/calc")
  401. X
  402. (defvar calc-gnuplot-default-device "default")
  403. (defvar calc-gnuplot-default-output "STDOUT")
  404. (defvar calc-gnuplot-print-device "postscript")
  405. (defvar calc-gnuplot-print-output "auto")
  406. (defvar calc-gnuplot-keep-outfile nil)
  407. (defvar calc-gnuplot-version nil)
  408. X
  409. (defvar calc-gnuplot-display (getenv "DISPLAY"))
  410. (defvar calc-gnuplot-geometry nil)
  411. X
  412. (defvar calc-graph-default-resolution 15)
  413. (defvar calc-graph-default-resolution-3d 5)
  414. (defvar calc-graph-default-precision 5)
  415. X
  416. (defvar calc-gnuplot-buffer nil)
  417. (defvar calc-gnuplot-input nil)
  418. X
  419. (defvar calc-gnuplot-last-error-pos 1)
  420. (defvar calc-graph-last-device nil)
  421. (defvar calc-graph-last-output nil)
  422. (defvar calc-graph-file-cache nil)
  423. (defvar calc-graph-var-cache nil)
  424. (defvar calc-graph-data-cache nil)
  425. (defvar calc-graph-data-cache-limit 10)
  426. X
  427. (defun calc-graph-fast (many)
  428. X  (interactive "P")
  429. X  (let ((calc-graph-no-auto-view t))
  430. X    (calc-graph-delete t)
  431. X    (calc-graph-add many)
  432. X    (calc-graph-plot nil))
  433. )
  434. X
  435. (defun calc-graph-fast-3d (many)
  436. X  (interactive "P")
  437. X  (let ((calc-graph-no-auto-view t))
  438. X    (calc-graph-delete t)
  439. X    (calc-graph-add-3d many)
  440. X    (calc-graph-plot nil))
  441. )
  442. X
  443. (defun calc-graph-delete (all)
  444. X  (interactive "P")
  445. X  (calc-wrapper
  446. X   (calc-graph-init)
  447. X   (save-excursion
  448. X     (set-buffer calc-gnuplot-input)
  449. X     (and (calc-graph-find-plot t all)
  450. X      (progn
  451. X        (if (looking-at "s?plot")
  452. X        (progn
  453. X          (setq calc-graph-var-cache nil)
  454. X          (delete-region (point) (point-max)))
  455. X          (delete-region (point) (1- (point-max)))))))
  456. X   (calc-graph-view-commands))
  457. )
  458. X
  459. (defun calc-graph-find-plot (&optional before all)
  460. X  (goto-char (point-min))
  461. X  (and (re-search-forward "^s?plot[ \t]+" nil t)
  462. X       (let ((beg (point)))
  463. X     (goto-char (point-max))
  464. X     (if (or all
  465. X         (not (search-backward "," nil t))
  466. X         (< (point) beg))
  467. X         (progn
  468. X           (goto-char beg)
  469. X           (if before
  470. X           (beginning-of-line)))
  471. X       (or before
  472. X           (re-search-forward ",[ \t]+")))
  473. X     t))
  474. )
  475. X
  476. (defun calc-graph-add (many)
  477. X  (interactive "P")
  478. X  (calc-wrapper
  479. X   (calc-graph-init)
  480. X   (cond ((null many)
  481. X      (calc-graph-add-curve (calc-graph-lookup (calc-top-n 2))
  482. X                (calc-graph-lookup (calc-top-n 1))))
  483. X     ((or (consp many) (eq many 0))
  484. X      (let ((xdata (calc-graph-lookup (calc-top-n 2)))
  485. X        (ylist (calc-top-n 1)))
  486. X        (or (eq (car-safe ylist) 'vec)
  487. X        (error "Y argument must be a vector"))
  488. X        (while (setq ylist (cdr ylist))
  489. X          (calc-graph-add-curve xdata (calc-graph-lookup (car ylist))))))
  490. X     ((> (setq many (prefix-numeric-value many)) 0)
  491. X      (let ((xdata (calc-graph-lookup (calc-top-n (1+ many)))))
  492. X        (while (> many 0)
  493. X          (calc-graph-add-curve xdata
  494. X                    (calc-graph-lookup (calc-top-n many)))
  495. X          (setq many (1- many)))))
  496. X     (t
  497. X      (let (pair)
  498. X        (setq many (- many))
  499. X        (while (> many 0)
  500. X          (setq pair (calc-top-n many))
  501. X          (or (and (eq (car-safe pair) 'vec)
  502. X               (= (length pair) 3))
  503. X          (error "Argument must be an [x,y] vector"))
  504. X          (calc-graph-add-curve (calc-graph-lookup (nth 1 pair))
  505. X                    (calc-graph-lookup (nth 2 pair)))
  506. X          (setq many (1- many))))))
  507. X   (calc-graph-view-commands))
  508. )
  509. X
  510. (defun calc-graph-add-3d (many)
  511. X  (interactive "P")
  512. X  (calc-wrapper
  513. X   (calc-graph-init)
  514. X   (cond ((null many)
  515. X      (calc-graph-add-curve (calc-graph-lookup (calc-top-n 3))
  516. X                (calc-graph-lookup (calc-top-n 2))
  517. X                (calc-graph-lookup (calc-top-n 1))))
  518. X     ((or (consp many) (eq many 0))
  519. X      (let ((xdata (calc-graph-lookup (calc-top-n 3)))
  520. X        (ydata (calc-graph-lookup (calc-top-n 2)))
  521. X        (zlist (calc-top-n 1)))
  522. X        (or (eq (car-safe zlist) 'vec)
  523. X        (error "Z argument must be a vector"))
  524. X        (while (setq zlist (cdr zlist))
  525. X          (calc-graph-add-curve xdata ydata
  526. X                    (calc-graph-lookup (car zlist))))))
  527. X     ((> (setq many (prefix-numeric-value many)) 0)
  528. X      (let ((xdata (calc-graph-lookup (calc-top-n (+ many 2))))
  529. X        (ydata (calc-graph-lookup (calc-top-n (+ many 1)))))
  530. X        (while (> many 0)
  531. X          (calc-graph-add-curve xdata ydata
  532. X                    (calc-graph-lookup (calc-top-n many)))
  533. X          (setq many (1- many)))))
  534. X     (t
  535. X      (let (curve)
  536. X        (setq many (- many))
  537. X        (while (> many 0)
  538. X          (setq curve (calc-top-n many))
  539. X          (or (and (eq (car-safe curve) 'vec)
  540. X               (= (length curve) 4))
  541. X          (error "Argument must be an [x,y,z] vector"))
  542. X          (calc-graph-add-curve (calc-graph-lookup (nth 1 curve))
  543. X                    (calc-graph-lookup (nth 2 curve))
  544. X                    (calc-graph-lookup (nth 3 curve)))
  545. X          (setq many (1- many))))))
  546. X   (calc-graph-view-commands))
  547. )
  548. X
  549. (defun calc-graph-add-curve (xdata ydata &optional zdata)
  550. X  (let ((num (calc-graph-count-curves))
  551. X    (pstyle (calc-var-value 'var-PointStyles))
  552. X    (lstyle (calc-var-value 'var-LineStyles)))
  553. X    (save-excursion
  554. X      (set-buffer calc-gnuplot-input)
  555. X      (goto-char (point-min))
  556. X      (if (re-search-forward (if zdata "^plot[ \t]" "^splot[ \t]")
  557. X                 nil t)
  558. X      (error "Can't mix 2d and 3d curves on one graph"))
  559. X      (if (re-search-forward "^s?plot[ \t]" nil t)
  560. X      (progn
  561. X        (end-of-line)
  562. X        (insert ", "))
  563. X    (goto-char (point-max))
  564. X    (or (eq (preceding-char) ?\n)
  565. X        (insert "\n"))
  566. X    (insert (if zdata "splot" "plot") " \n")
  567. X    (forward-char -1))
  568. X      (insert "{" (symbol-name (nth 1 xdata))
  569. X          ":" (symbol-name (nth 1 ydata)))
  570. X      (if zdata
  571. X      (insert ":" (symbol-name (nth 1 zdata))))
  572. X      (insert "} "
  573. X          "title \"" (symbol-name (nth 1 ydata)) "\" "
  574. X          "with dots")
  575. X      (setq pstyle (and (eq (car-safe pstyle) 'vec) (nth (1+ num) pstyle)))
  576. X      (setq lstyle (and (eq (car-safe lstyle) 'vec) (nth (1+ num) lstyle)))
  577. X      (calc-graph-set-styles
  578. X       (or (and (Math-num-integerp lstyle) (math-trunc lstyle))
  579. X       0)
  580. X       (or (and (Math-num-integerp pstyle) (math-trunc pstyle))
  581. X       (if (eq (car-safe (calc-var-value (nth 2 ydata))) 'vec)
  582. X           0 -1)))))
  583. )
  584. X
  585. (defun calc-graph-lookup (thing)
  586. X  (if (and (eq (car-safe thing) 'var)
  587. X       (calc-var-value (nth 2 thing)))
  588. X      thing
  589. X    (let ((found (assoc thing calc-graph-var-cache)))
  590. X      (or found
  591. X      (progn
  592. X        (setq varname (concat "PlotData"
  593. X                  (int-to-string
  594. X                   (1+ (length calc-graph-var-cache))))
  595. X          var (list 'var (intern varname)
  596. X                (intern (concat "var-" varname)))
  597. X          found (cons thing var)
  598. X          calc-graph-var-cache (cons found calc-graph-var-cache))
  599. X        (set (nth 2 var) thing)))
  600. X      (cdr found)))
  601. )
  602. X
  603. (defun calc-graph-juggle (arg)
  604. X  (interactive "p")
  605. X  (calc-graph-init)
  606. X  (save-excursion
  607. X    (set-buffer calc-gnuplot-input)
  608. X    (if (< arg 0)
  609. X    (let ((num (calc-graph-count-curves)))
  610. X      (if (> num 0)
  611. X          (while (< arg 0)
  612. X        (setq arg (+ arg num))))))
  613. X    (while (>= (setq arg (1- arg)) 0)
  614. X      (calc-graph-do-juggle)))
  615. )
  616. X
  617. (defun calc-graph-count-curves ()
  618. X  (save-excursion
  619. X    (set-buffer calc-gnuplot-input)
  620. X    (if (re-search-forward "^s?plot[ \t]" nil t)
  621. X    (let ((num 1))
  622. X      (goto-char (point-min))
  623. X      (while (search-forward "," nil t)
  624. X        (setq num (1+ num)))
  625. X      num)
  626. X      0))
  627. )
  628. X
  629. (defun calc-graph-do-juggle ()
  630. X  (let (base)
  631. X    (and (calc-graph-find-plot t t)
  632. X     (progn
  633. X       (setq base (point))
  634. X       (calc-graph-find-plot t nil)
  635. X       (or (eq base (point))
  636. X           (let ((str (buffer-substring (+ (point) 2) (1- (point-max)))))
  637. X         (delete-region (point) (1- (point-max)))
  638. X         (goto-char (+ base 5))
  639. X         (insert str ", "))))))
  640. )
  641. X
  642. (defun calc-graph-print (flag)
  643. X  (interactive "P")
  644. X  (calc-graph-plot flag t)
  645. )
  646. X
  647. (defun calc-graph-plot (flag &optional printing)
  648. X  (interactive "P")
  649. X  (calc-slow-wrapper
  650. X   (let ((calcbuf (current-buffer))
  651. X     (tempbuf (get-buffer-create "*Gnuplot Temp-2*"))
  652. X     (tempbuftop 1)
  653. X     (tempoutfile nil)
  654. X     (curve-num 0)
  655. X     (refine (and flag (> (prefix-numeric-value flag) 0)))
  656. X     (recompute (and flag (< (prefix-numeric-value flag) 0)))
  657. X     (surprise-splot nil)
  658. X     (tty-output nil)
  659. X     cache-env is-splot device output resolution precision samples-pos)
  660. X     (or (boundp 'calc-graph-prev-kill-hook)
  661. X     (progn
  662. X       (setq calc-graph-prev-kill-hook kill-emacs-hook)
  663. X       (setq kill-emacs-hook 'calc-graph-kill-hook)))
  664. X     (save-excursion
  665. X       (calc-graph-init)
  666. X       (set-buffer tempbuf)
  667. X       (erase-buffer)
  668. X       (set-buffer calc-gnuplot-input)
  669. X       (goto-char (point-min))
  670. X       (setq is-splot (re-search-forward "^splot[ \t]" nil t))
  671. X       (let ((str (buffer-string))
  672. X         (ver calc-gnuplot-version))
  673. X     (set-buffer (get-buffer-create "*Gnuplot Temp*"))
  674. X     (erase-buffer)
  675. X     (insert "# (Note: This is a temporary copy---do not edit!)\n")
  676. X     (if (>= ver 2)
  677. X         (insert "set noarrow\nset nolabel\n"
  678. X             "set autoscale xy\nset nologscale xy\n"
  679. X             "set xlabel\nset ylabel\nset title\n"
  680. X             "set noclip points\nset clip one\nset clip two\n"
  681. X             "set format \"%g\"\nset tics\nset xtics\nset ytics\n"
  682. X             "set data style linespoints\n"
  683. X             "set nogrid\nset nokey\nset nopolar\n"))
  684. X     (if (>= ver 3)
  685. X         (insert "set surface\nset nocontour\n"
  686. X             "set " (if is-splot "" "no") "parametric\n"
  687. X             "set notime\nset border\nset ztics\nset zeroaxis\n"
  688. X             "set view 60,30,1,1\nset offsets 0,0,0,0\n"))
  689. X     (setq samples-pos (point))
  690. X     (insert "\n\n" str))
  691. X       (goto-char (point-min))
  692. X       (if is-splot
  693. X       (if refine
  694. X           (error "This option works only for 2d plots")
  695. X         (setq recompute t)))
  696. X       (let ((calc-gnuplot-input (current-buffer))
  697. X         (calc-graph-no-auto-view t))
  698. X     (if printing
  699. X         (setq device calc-gnuplot-print-device
  700. X           output calc-gnuplot-print-output)
  701. X       (setq device (calc-graph-find-command "terminal")
  702. X         output (calc-graph-find-command "output"))
  703. X       (or device
  704. X           (setq device calc-gnuplot-default-device))
  705. X       (if output
  706. X           (setq output (car (read-from-string output)))
  707. X         (setq output calc-gnuplot-default-output)))
  708. X     (if (or (equal device "") (equal device "default"))
  709. X         (setq device (if printing
  710. X                  "postscript"
  711. X                (if (or (eq window-system 'x) (getenv "DISPLAY"))
  712. X                "x11"
  713. X                  (if (>= calc-gnuplot-version 3)
  714. X                  "dumb" "postscript")))))
  715. X     (if (equal device "dumb")
  716. X         (setq device (format "dumb %d %d"
  717. X                  (1- (screen-width)) (1- (screen-height)))))
  718. X     (if (equal device "big")
  719. X         (setq device (format "dumb %d %d"
  720. X                  (* 4 (- (screen-width) 3))
  721. X                  (* 4 (- (screen-height) 3)))))
  722. X     (if (stringp output)
  723. X         (if (or (equal output "auto")
  724. X             (and (equal output "tty") (setq tty-output t)))
  725. X         (setq tempoutfile (calc-temp-file-name -1)
  726. X               output tempoutfile))
  727. X       (setq output (eval output)))
  728. X     (or (equal device calc-graph-last-device)
  729. X         (progn
  730. X           (setq calc-graph-last-device device)
  731. X           (calc-gnuplot-command "set terminal" device)))
  732. X     (or (equal output calc-graph-last-output)
  733. X         (progn
  734. X           (setq calc-graph-last-output output)
  735. X           (calc-gnuplot-command "set output"
  736. X                     (if (equal output "STDOUT")
  737. X                     ""
  738. X                       (prin1-to-string output)))))
  739. X     (setq resolution (calc-graph-find-command "samples"))
  740. X     (if resolution
  741. X         (setq resolution (string-to-int resolution))
  742. X       (setq resolution (if is-splot
  743. X                calc-graph-default-resolution-3d
  744. X                  calc-graph-default-resolution)))
  745. X     (setq precision (calc-graph-find-command "precision"))
  746. X     (if precision
  747. X         (setq precision (string-to-int precision))
  748. X       (setq precision calc-graph-default-precision))
  749. X     (calc-graph-set-command "terminal")
  750. X     (calc-graph-set-command "output")
  751. X     (calc-graph-set-command "samples")
  752. X     (calc-graph-set-command "precision"))
  753. X       (goto-char samples-pos)
  754. X       (insert "set samples " (int-to-string (max (if is-splot 20 200)
  755. X                          (+ 5 resolution))) "\n")
  756. X       (while (re-search-forward "{\\*[^}]+}[^,\n]*" nil t)
  757. X     (delete-region (match-beginning 0) (match-end 0))
  758. X     (if (looking-at ",")
  759. X         (delete-char 1)
  760. X       (while (memq (preceding-char) '(?\ ?\t))
  761. X         (forward-char -1))
  762. X       (if (eq (preceding-char) ?\,)
  763. X           (delete-backward-char 1))))
  764. X       (save-excursion
  765. X     (set-buffer calcbuf)
  766. X     (setq cache-env (list calc-angle-mode
  767. X                   calc-complex-mode
  768. X                   calc-simplify-mode
  769. X                   calc-infinite-mode
  770. X                   calc-word-size
  771. X                   precision is-splot))
  772. X     (if (and (not recompute)
  773. X          (equal (cdr (car calc-graph-data-cache)) cache-env))
  774. X         (while (> (length calc-graph-data-cache)
  775. X               calc-graph-data-cache-limit)
  776. X           (setcdr calc-graph-data-cache
  777. X               (cdr (cdr calc-graph-data-cache))))
  778. X       (setq calc-graph-data-cache (list (cons nil cache-env)))))
  779. X       (calc-graph-find-plot t t)
  780. X       (while (re-search-forward
  781. X           (if is-splot
  782. X           "{\\([^{}:\n]+\\):\\([^{}:\n]+\\):\\([^{}:\n]+\\)}"
  783. X         "{\\([^{}:\n]+\\)\\(:\\)\\([^{}:\n]+\\)}")
  784. X           nil t)
  785. X     (setq curve-num (1+ curve-num))
  786. X     (let* ((xname (buffer-substring (match-beginning 1) (match-end 1)))
  787. X        (xvar (intern (concat "var-" xname)))
  788. X        (xvalue (math-evaluate-expr (calc-var-value xvar)))
  789. X        (y3name (and is-splot
  790. X                 (buffer-substring (match-beginning 2)
  791. X                           (match-end 2))))
  792. X        (y3var (and is-splot (intern (concat "var-" y3name))))
  793. X        (y3value (and is-splot (calc-var-value y3var)))
  794. X        (yname (buffer-substring (match-beginning 3) (match-end 3)))
  795. X        (yvar (intern (concat "var-" yname)))
  796. X        (yvalue (calc-var-value yvar))
  797. X        filename)
  798. X       (delete-region (match-beginning 0) (match-end 0))
  799. X       (setq filename (calc-temp-file-name curve-num))
  800. X       (save-excursion
  801. X         (set-buffer calcbuf)
  802. X         (let (tempbuftop
  803. X           (xp xvalue)
  804. X           (yp yvalue)
  805. X           (zp nil)
  806. X           (xlow nil) (xhigh nil) (y3low nil) (y3high nil)
  807. X           xvec xval xstep var-DUMMY
  808. X           y3vec y3val y3step var-DUMMY2 (zval nil)
  809. X           yvec yval ycache ycacheptr yvector
  810. X           numsteps numsteps3
  811. X           (keep-file (and (not is-splot) (file-exists-p filename)))
  812. X           (stepcount 0)
  813. X           (calc-symbolic-mode nil)
  814. X           (calc-prefer-frac nil)
  815. X           (calc-internal-prec (max 3 precision))
  816. X           (calc-simplify-mode (and (not (memq calc-simplify-mode
  817. X                               '(none num)))
  818. X                        calc-simplify-mode))
  819. X           (blank t)
  820. X           (non-blank nil)
  821. X           (math-working-step 0)
  822. X           (math-working-step-2 nil))
  823. X           (save-excursion
  824. X         (if is-splot
  825. X             (calc-graph-compute-3d)
  826. X           (calc-graph-compute-2d))
  827. X         (set-buffer tempbuf)
  828. X         (goto-char (point-max))
  829. X         (insert "\n" xname)
  830. X         (if is-splot
  831. X             (insert ":" y3name))
  832. X         (insert ":" yname "\n\n")
  833. X         (setq tempbuftop (point))
  834. X         (let ((calc-group-digits nil)
  835. X               (calc-leading-zeros nil)
  836. X               (calc-number-radix 10)
  837. X               (entry (and (not is-splot)
  838. X                   (list xp yp xhigh numsteps))))
  839. X           (or (equal entry
  840. X                  (nth 1 (nth (1+ curve-num)
  841. X                      calc-graph-file-cache)))
  842. X               (setq keep-file nil))
  843. X           (setcar (cdr (nth (1+ curve-num) calc-graph-file-cache))
  844. X               entry)
  845. X           (or keep-file
  846. X               (calc-graph-format-data)))
  847. X         (or keep-file
  848. X             (progn
  849. X               (or non-blank
  850. X               (error "No valid data points for %s:%s"
  851. X                  xname yname))
  852. X               (write-region tempbuftop (point-max) filename
  853. X                     nil 'quiet))))))
  854. X       (insert (prin1-to-string filename))))
  855. X       (if surprise-splot
  856. X       (setcdr cache-env nil))
  857. X       (if (= curve-num 0)
  858. X       (progn
  859. X         (calc-gnuplot-command "clear")
  860. X         (calc-clear-command-flag 'clear-message)
  861. X         (message "No data to plot!"))
  862. X     (setq calc-graph-data-cache-limit (max curve-num
  863. X                        calc-graph-data-cache-limit)
  864. X           filename (calc-temp-file-name 0))
  865. X     (write-region (point-min) (point-max) filename nil 'quiet)
  866. X     (calc-gnuplot-command "load" (prin1-to-string filename))
  867. X     (or (equal output "STDOUT")
  868. X         calc-gnuplot-keep-outfile
  869. X         (progn   ; need to close the output file before printing/plotting
  870. X           (setq calc-graph-last-output "STDOUT")
  871. X           (calc-gnuplot-command "set output")))
  872. X     (let ((command (if printing
  873. X                calc-gnuplot-print-command
  874. X              (or calc-gnuplot-plot-command
  875. X                  (and (string-match "^dumb" device)
  876. X                   'calc-graph-show-dumb)
  877. X                  (and tty-output
  878. X                   'calc-graph-show-tty)))))
  879. X       (if command
  880. X           (if (stringp command)
  881. X           (calc-gnuplot-command
  882. X            "!" (format command
  883. X                (or tempoutfile
  884. X                    calc-gnuplot-print-output)))
  885. X         (if (symbolp command)
  886. X             (funcall command output)
  887. X           (eval command)))))))))
  888. )
  889. X
  890. (defun calc-graph-compute-2d ()
  891. X  (if (setq yvec (eq (car-safe yvalue) 'vec))
  892. X      (if (= (setq numsteps (1- (length yvalue))) 0)
  893. X      (error "Can't plot an empty vector")
  894. X    (if (setq xvec (eq (car-safe xvalue) 'vec))
  895. X        (or (= (1- (length xvalue)) numsteps)
  896. X        (error "%s and %s have different lengths" xname yname))
  897. X      (if (and (eq (car-safe xvalue) 'intv)
  898. X           (math-constp xvalue))
  899. X          (setq xstep (math-div (math-sub (nth 3 xvalue)
  900. X                          (nth 2 xvalue))
  901. X                    (1- numsteps))
  902. X            xvalue (nth 2 xvalue))
  903. X        (if (math-realp xvalue)
  904. X        (setq xstep 1)
  905. X          (error "%s is not a suitable basis for %s" xname yname)))))
  906. X    (or (math-realp yvalue)
  907. X    (let ((arglist nil))
  908. X      (setq yvalue (math-evaluate-expr yvalue))
  909. X      (calc-default-formula-arglist yvalue)
  910. X      (or arglist
  911. X          (error "%s does not contain any unassigned variables" yname))
  912. X      (and (cdr arglist)
  913. X           (error "%s contains more than one variable: %s"
  914. X              yname arglist))
  915. X      (setq yvalue (math-expr-subst yvalue
  916. X                    (math-build-var-name (car arglist))
  917. X                    '(var DUMMY var-DUMMY)))))
  918. X    (setq ycache (assoc yvalue calc-graph-data-cache))
  919. X    (delq ycache calc-graph-data-cache)
  920. X    (nconc calc-graph-data-cache
  921. X       (list (or ycache (setq ycache (list yvalue)))))
  922. X    (if (and (not (setq xvec (eq (car-safe xvalue) 'vec)))
  923. X         refine (cdr (cdr ycache)))
  924. X    (calc-graph-refine-2d)
  925. X      (calc-graph-recompute-2d)))
  926. )
  927. X
  928. (defun calc-graph-refine-2d ()
  929. X  (setq keep-file nil
  930. X    ycacheptr (cdr ycache))
  931. X  (if (and (setq xval (calc-graph-find-command "xrange"))
  932. X       (string-match "\\`\\[\\([0-9.eE+-]*\\):\\([0-9.eE+-]*\\)\\]\\'"
  933. X             xval))
  934. X      (let ((b2 (match-beginning 2))
  935. X        (e2 (match-end 2)))
  936. X    (setq xlow (math-read-number (substring xval
  937. X                        (match-beginning 1)
  938. X                        (match-end 1)))
  939. X          xhigh (math-read-number (substring xval b2 e2))))
  940. X    (if xlow
  941. X    (while (and (cdr ycacheptr)
  942. X            (Math-lessp (car (nth 1 ycacheptr)) xlow))
  943. X      (setq ycacheptr (cdr ycacheptr)))))
  944. X  (setq math-working-step-2 (1- (length ycacheptr)))
  945. X  (while (and (cdr ycacheptr)
  946. X          (or (not xhigh)
  947. X          (Math-lessp (car (car ycacheptr)) xhigh)))
  948. X    (setq var-DUMMY (math-div (math-add (car (car ycacheptr))
  949. X                    (car (nth 1 ycacheptr)))
  950. X                  2)
  951. X      math-working-step (1+ math-working-step)
  952. X      yval (math-evaluate-expr yvalue))
  953. X    (setcdr ycacheptr (cons (cons var-DUMMY yval)
  954. X                (cdr ycacheptr)))
  955. X    (setq ycacheptr (cdr (cdr ycacheptr))))
  956. X  (setq yp ycache
  957. X    numsteps 1000000)
  958. )
  959. X
  960. (defun calc-graph-recompute-2d ()
  961. X  (setq ycacheptr ycache)
  962. X  (if xvec
  963. X      (setq numsteps (1- (length xvalue))
  964. X        yvector nil)
  965. X    (if (and (eq (car-safe xvalue) 'intv)
  966. X         (math-constp xvalue))
  967. X    (setq numsteps resolution
  968. X          yp nil
  969. X          xlow (nth 2 xvalue)
  970. X          xhigh (nth 3 xvalue)
  971. X          xstep (math-div (math-sub xhigh xlow)
  972. X                  (1- numsteps))
  973. X          xvalue (nth 2 xvalue))
  974. X      (error "%s is not a suitable basis for %s"
  975. X         xname yname)))
  976. X  (setq math-working-step-2 numsteps)
  977. X  (while (>= (setq numsteps (1- numsteps)) 0)
  978. X    (setq math-working-step (1+ math-working-step))
  979. X    (if xvec
  980. X    (progn
  981. X      (setq xp (cdr xp)
  982. X        xval (car xp))
  983. X      (and (not (eq ycacheptr ycache))
  984. X           (consp (car ycacheptr))
  985. X           (not (Math-lessp (car (car ycacheptr)) xval))
  986. X           (setq ycacheptr ycache)))
  987. X      (if (= numsteps 0)
  988. X      (setq xval xhigh)   ; avoid cumulative roundoff
  989. X    (setq xval xvalue
  990. X          xvalue (math-add xvalue xstep))))
  991. X    (while (and (cdr ycacheptr)
  992. X        (Math-lessp (car (nth 1 ycacheptr)) xval))
  993. X      (setq ycacheptr (cdr ycacheptr)))
  994. X    (or (and (cdr ycacheptr)
  995. X         (Math-equal (car (nth 1 ycacheptr)) xval))
  996. X    (progn
  997. X      (setq keep-file nil
  998. X        var-DUMMY xval)
  999. X      (setcdr ycacheptr (cons (cons xval (math-evaluate-expr yvalue))
  1000. X                  (cdr ycacheptr)))))
  1001. X    (setq ycacheptr (cdr ycacheptr))
  1002. X    (if xvec
  1003. X    (setq yvector (cons (cdr (car ycacheptr)) yvector))
  1004. X      (or yp (setq yp ycacheptr))))
  1005. X  (if xvec
  1006. X      (setq xp xvalue
  1007. X        yvec t
  1008. X        yp (cons 'vec (nreverse yvector))
  1009. X        numsteps (1- (length xp)))
  1010. X    (setq numsteps 1000000))
  1011. )
  1012. X
  1013. (defun calc-graph-compute-3d ()
  1014. X  (if (setq yvec (eq (car-safe yvalue) 'vec))
  1015. X      (if (math-matrixp yvalue)
  1016. X      (progn
  1017. X        (setq numsteps (1- (length yvalue))
  1018. X          numsteps3 (1- (length (nth 1 yvalue))))
  1019. X        (if (eq (car-safe xvalue) 'vec)
  1020. X        (or (= (1- (length xvalue)) numsteps)
  1021. X            (error "%s has wrong length" xname))
  1022. X          (if (and (eq (car-safe xvalue) 'intv)
  1023. X               (math-constp xvalue))
  1024. X          (setq xvalue (calcFunc-index numsteps
  1025. X                           (nth 2 xvalue)
  1026. X                           (math-div
  1027. X                        (math-sub (nth 3 xvalue)
  1028. X                              (nth 2 xvalue))
  1029. X                        (1- numsteps))))
  1030. X        (if (math-realp xvalue)
  1031. X            (setq xvalue (calcFunc-index numsteps xvalue 1))
  1032. X          (error "%s is not a suitable basis for %s" xname yname))))
  1033. X        (if (eq (car-safe y3value) 'vec)
  1034. X        (or (= (1- (length y3value)) numsteps3)
  1035. X            (error "%s has wrong length" y3name))
  1036. X          (if (and (eq (car-safe y3value) 'intv)
  1037. X               (math-constp y3value))
  1038. X          (setq y3value (calcFunc-index numsteps3
  1039. X                        (nth 2 y3value)
  1040. X                        (math-div
  1041. X                         (math-sub (nth 3 y3value)
  1042. X                               (nth 2 y3value))
  1043. X                         (1- numsteps3))))
  1044. X        (if (math-realp y3value)
  1045. X            (setq y3value (calcFunc-index numsteps3 y3value 1))
  1046. X          (error "%s is not a suitable basis for %s" y3name yname))))
  1047. X        (setq xp nil
  1048. X          yp nil
  1049. X          zp nil
  1050. X          xvec t)
  1051. X        (while (setq xvalue (cdr xvalue) yvalue (cdr yvalue))
  1052. X          (setq xp (nconc xp (make-list (1+ numsteps3) (car xvalue)))
  1053. X            yp (nconc yp (cons 0 (copy-sequence (cdr y3value))))
  1054. X            zp (nconc zp (cons '(skip)
  1055. X                       (copy-sequence (cdr (car yvalue)))))))
  1056. X        (setq numsteps (1- (* numsteps (1+ numsteps3)))))
  1057. X    (if (= (setq numsteps (1- (length yvalue))) 0)
  1058. X        (error "Can't plot an empty vector"))
  1059. X    (or (and (eq (car-safe xvalue) 'vec)
  1060. X         (= (1- (length xvalue)) numsteps))
  1061. X        (error "%s is not a suitable basis for %s" xname yname))
  1062. X    (or (and (eq (car-safe y3value) 'vec)
  1063. X         (= (1- (length y3value)) numsteps))
  1064. X        (error "%s is not a suitable basis for %s" y3name yname))
  1065. X    (setq xp xvalue
  1066. X          yp y3value
  1067. X          zp yvalue
  1068. X          xvec t))
  1069. X    (or (math-realp yvalue)
  1070. X    (let ((arglist nil))
  1071. X      (setq yvalue (math-evaluate-expr yvalue))
  1072. X      (calc-default-formula-arglist yvalue)
  1073. X      (setq arglist (sort arglist 'string-lessp))
  1074. X      (or (cdr arglist)
  1075. X          (error "%s does not contain enough unassigned variables" yname))
  1076. X      (and (cdr (cdr arglist))
  1077. X           (error "%s contains too many variables: %s" yname arglist))
  1078. X      (setq yvalue (math-multi-subst yvalue
  1079. X                     (mapcar 'math-build-var-name
  1080. X                         arglist)
  1081. X                     '((var DUMMY var-DUMMY)
  1082. X                       (var DUMMY2 var-DUMMY2))))))
  1083. X    (if (setq xvec (eq (car-safe xvalue) 'vec))
  1084. X    (setq numsteps (1- (length xvalue)))
  1085. X      (if (and (eq (car-safe xvalue) 'intv)
  1086. X           (math-constp xvalue))
  1087. X      (setq numsteps resolution
  1088. X        xvalue (calcFunc-index numsteps
  1089. X                       (nth 2 xvalue)
  1090. X                       (math-div (math-sub (nth 3 xvalue)
  1091. X                               (nth 2 xvalue))
  1092. X                         (1- numsteps))))
  1093. X    (error "%s is not a suitable basis for %s"
  1094. X           xname yname)))
  1095. X    (if (setq y3vec (eq (car-safe y3value) 'vec))
  1096. X    (setq numsteps3 (1- (length y3value)))
  1097. X      (if (and (eq (car-safe y3value) 'intv)
  1098. X           (math-constp y3value))
  1099. X      (setq numsteps3 resolution
  1100. X        y3value (calcFunc-index numsteps3
  1101. X                    (nth 2 y3value)
  1102. X                    (math-div (math-sub (nth 3 y3value)
  1103. X                                (nth 2 y3value))
  1104. X                          (1- numsteps3))))
  1105. X    (error "%s is not a suitable basis for %s"
  1106. X           y3name yname)))
  1107. X    (setq xp nil
  1108. X      yp nil
  1109. X      zp nil
  1110. X      xvec t)
  1111. X    (setq math-working-step 0)
  1112. X    (while (setq xvalue (cdr xvalue))
  1113. X      (setq xp (nconc xp (make-list (1+ numsteps3) (car xvalue)))
  1114. X        yp (nconc yp (cons 0 (copy-sequence (cdr y3value))))
  1115. X        zp (cons '(skip) zp)
  1116. X        y3step y3value
  1117. X        var-DUMMY (car xvalue)
  1118. X        math-working-step-2 0
  1119. X        math-working-step (1+ math-working-step))
  1120. X      (while (setq y3step (cdr y3step))
  1121. X    (setq math-working-step-2 (1+ math-working-step-2)
  1122. X          var-DUMMY2 (car y3step)
  1123. X          zp (cons (math-evaluate-expr yvalue) zp))))
  1124. X    (setq zp (nreverse zp)
  1125. X      numsteps (1- (* numsteps (1+ numsteps3)))))
  1126. )
  1127. X
  1128. (defun calc-graph-format-data ()
  1129. X  (while (<= (setq stepcount (1+ stepcount)) numsteps)
  1130. X    (if xvec
  1131. X    (setq xp (cdr xp)
  1132. X          xval (car xp)
  1133. X          yp (cdr yp)
  1134. X          yval (car yp)
  1135. X          zp (cdr zp)
  1136. X          zval (car zp))
  1137. X      (if yvec
  1138. X      (setq xval xvalue
  1139. X        xvalue (math-add xvalue xstep)
  1140. X        yp (cdr yp)
  1141. X        yval (car yp))
  1142. X    (setq xval (car (car yp))
  1143. X          yval (cdr (car yp))
  1144. X          yp (cdr yp))
  1145. X    (if (or (not yp)
  1146. X        (and xhigh (equal xval xhigh)))
  1147. X        (setq numsteps 0))))
  1148. X    (if is-splot
  1149. X    (if (and (eq (car-safe zval) 'calcFunc-xyz)
  1150. X         (= (length zval) 4))
  1151. X        (setq xval (nth 1 zval)
  1152. X          yval (nth 2 zval)
  1153. X          zval (nth 3 zval)))
  1154. X      (if (and (eq (car-safe yval) 'calcFunc-xyz)
  1155. X           (= (length yval) 4))
  1156. X      (progn
  1157. X        (or surprise-splot
  1158. X        (save-excursion
  1159. X          (set-buffer (get-buffer-create "*Gnuplot Temp*"))
  1160. X          (save-excursion
  1161. X            (goto-char (point-max))
  1162. X            (re-search-backward "^plot[ \t]")
  1163. X            (insert "set parametric\ns")
  1164. X            (setq surprise-splot t))))
  1165. X        (setq xval (nth 1 yval)
  1166. X          zval (nth 3 yval)
  1167. X          yval (nth 2 yval)))
  1168. X    (if (and (eq (car-safe yval) 'calcFunc-xy)
  1169. X         (= (length yval) 3))
  1170. X        (setq xval (nth 1 yval)
  1171. X          yval (nth 2 yval)))))
  1172. X    (if (and (Math-realp xval)
  1173. X         (Math-realp yval)
  1174. X         (or (not zval) (Math-realp zval)))
  1175. X    (progn
  1176. X      (setq blank nil
  1177. X        non-blank t)
  1178. X      (if (Math-integerp xval)
  1179. X          (insert (math-format-number xval))
  1180. X        (if (eq (car xval) 'frac)
  1181. X        (setq xval (math-float xval)))
  1182. X        (insert (math-format-number (nth 1 xval))
  1183. X            "e" (int-to-string (nth 2 xval))))
  1184. X      (insert " ")
  1185. X      (if (Math-integerp yval)
  1186. X          (insert (math-format-number yval))
  1187. X        (if (eq (car yval) 'frac)
  1188. X        (setq yval (math-float yval)))
  1189. X        (insert (math-format-number (nth 1 yval))
  1190. X            "e" (int-to-string (nth 2 yval))))
  1191. X      (if zval
  1192. X          (progn
  1193. X        (insert " ")
  1194. X        (if (Math-integerp zval)
  1195. X            (insert (math-format-number zval))
  1196. X          (if (eq (car zval) 'frac)
  1197. X              (setq zval (math-float zval)))
  1198. X          (insert (math-format-number (nth 1 zval))
  1199. X              "e" (int-to-string (nth 2 zval))))))
  1200. X      (insert "\n"))
  1201. X      (and (not (equal zval '(skip)))
  1202. X       (boundp 'var-PlotRejects)
  1203. X       (eq (car-safe var-PlotRejects) 'vec)
  1204. X       (nconc var-PlotRejects
  1205. X          (list (list 'vec
  1206. X                  curve-num
  1207. X                  stepcount
  1208. X                  xval yval)))
  1209. X       (calc-refresh-evaltos 'var-PlotRejects))
  1210. X      (or blank
  1211. X      (progn
  1212. X        (insert "\n")
  1213. X        (setq blank t)))))
  1214. )
  1215. X
  1216. (defun calc-temp-file-name (num)
  1217. X  (while (<= (length calc-graph-file-cache) (1+ num))
  1218. X    (setq calc-graph-file-cache (nconc calc-graph-file-cache (list nil))))
  1219. X  (car (or (nth (1+ num) calc-graph-file-cache)
  1220. X       (setcar (nthcdr (1+ num) calc-graph-file-cache)
  1221. X           (list (make-temp-name
  1222. X              (concat calc-gnuplot-tempfile
  1223. X                  (if (<= num 0)
  1224. X                      (char-to-string (- ?A num))
  1225. X                    (int-to-string num))))
  1226. X             nil))))
  1227. )
  1228. X
  1229. (defun calc-graph-delete-temps ()
  1230. X  (while calc-graph-file-cache
  1231. X    (and (car calc-graph-file-cache)
  1232. X     (file-exists-p (car (car calc-graph-file-cache)))
  1233. X     (condition-case err
  1234. X         (delete-file (car (car calc-graph-file-cache)))
  1235. X       (error nil)))
  1236. X    (setq calc-graph-file-cache (cdr calc-graph-file-cache)))
  1237. )
  1238. X
  1239. (defun calc-graph-kill-hook ()
  1240. X  (calc-graph-delete-temps)
  1241. X  (if calc-graph-prev-kill-hook
  1242. X      (funcall calc-graph-prev-kill-hook))
  1243. )
  1244. X
  1245. (defun calc-graph-show-tty (output)
  1246. X  "Default calc-gnuplot-plot-command for \"tty\" output mode.
  1247. This is useful for tek40xx and other graphics-terminal types."
  1248. X  (call-process-region 1 1 shell-file-name
  1249. X               nil calc-gnuplot-buffer nil
  1250. X               "-c" (format "cat %s >/dev/tty; rm %s" output output))
  1251. )
  1252. X
  1253. (defun calc-graph-show-dumb (&optional output)
  1254. X  "Default calc-gnuplot-plot-command for Pinard's \"dumb\" terminal type.
  1255. This \"dumb\" driver will be present in Gnuplot 3.0."
  1256. X  (interactive)
  1257. X  (save-window-excursion
  1258. X    (switch-to-buffer calc-gnuplot-buffer)
  1259. X    (delete-other-windows)
  1260. X    (goto-char calc-gnuplot-trail-mark)
  1261. X    (or (search-forward "\f" nil t)
  1262. X    (sleep-for 1))
  1263. X    (goto-char (point-max))
  1264. X    (re-search-backward "\f\\|^[ \t]+\\^$\\|G N U P L O T")
  1265. X    (setq found-pt (point))
  1266. X    (if (looking-at "\f")
  1267. X    (progn
  1268. X      (forward-char 1)
  1269. X      (if (eolp) (forward-line 1))
  1270. X      (or (calc-graph-find-command "time")
  1271. X          (calc-graph-find-command "title")
  1272. X          (calc-graph-find-command "ylabel")
  1273. X          (let ((pt (point)))
  1274. X        (insert-before-markers (format "(%s)" (current-time-string)))
  1275. X        (goto-char pt)))
  1276. X      (set-window-start (selected-window) (point))
  1277. X      (goto-char (point-max)))
  1278. X      (end-of-line)
  1279. X      (backward-char 1)
  1280. X      (recenter '(4)))
  1281. X    (or (boundp 'calc-dumb-map)
  1282. X    (progn
  1283. X      (setq calc-dumb-map (make-sparse-keymap))
  1284. X      (define-key calc-dumb-map "\n" 'scroll-up)
  1285. X      (define-key calc-dumb-map " " 'scroll-up)
  1286. X      (define-key calc-dumb-map "\177" 'scroll-down)
  1287. X      (define-key calc-dumb-map "<" 'scroll-left)
  1288. X      (define-key calc-dumb-map ">" 'scroll-right)
  1289. X      (define-key calc-dumb-map "{" 'scroll-down)
  1290. X      (define-key calc-dumb-map "}" 'scroll-up)
  1291. X      (define-key calc-dumb-map "q" 'exit-recursive-edit)
  1292. X      (define-key calc-dumb-map "\C-c\C-c" 'exit-recursive-edit)))
  1293. X    (use-local-map calc-dumb-map)
  1294. X    (setq truncate-lines t)
  1295. X    (message "Type `q'%s to return to Calc."
  1296. X         (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
  1297. X            " or `M-# M-#'" ""))
  1298. X    (recursive-edit)
  1299. X    (bury-buffer "*Gnuplot Trail*"))
  1300. )
  1301. X
  1302. (defun calc-graph-clear ()
  1303. X  (interactive)
  1304. X  (if calc-graph-last-device
  1305. X      (if (or (equal calc-graph-last-device "x11")
  1306. X          (equal calc-graph-last-device "X11"))
  1307. X      (calc-gnuplot-command "set output"
  1308. X                (if (equal calc-graph-last-output "STDOUT")
  1309. X                    ""
  1310. X                  (prin1-to-string calc-graph-last-output)))
  1311. X    (calc-gnuplot-command "clear")))
  1312. )
  1313. X
  1314. (defun calc-graph-title-x (title)
  1315. X  (interactive "sX axis title: ")
  1316. X  (calc-graph-set-command "xlabel" (if (not (equal title ""))
  1317. X                       (prin1-to-string title)))
  1318. )
  1319. X
  1320. (defun calc-graph-title-y (title)
  1321. X  (interactive "sY axis title: ")
  1322. X  (calc-graph-set-command "ylabel" (if (not (equal title ""))
  1323. X                       (prin1-to-string title)))
  1324. )
  1325. X
  1326. (defun calc-graph-title-z (title)
  1327. X  (interactive "sZ axis title: ")
  1328. X  (calc-graph-set-command "zlabel" (if (not (equal title ""))
  1329. X                       (prin1-to-string title)))
  1330. )
  1331. X
  1332. (defun calc-graph-range-x (range)
  1333. X  (interactive "sX axis range: ")
  1334. X  (calc-graph-set-range "xrange" range)
  1335. )
  1336. X
  1337. (defun calc-graph-range-y (range)
  1338. X  (interactive "sY axis range: ")
  1339. X  (calc-graph-set-range "yrange" range)
  1340. )
  1341. X
  1342. (defun calc-graph-range-z (range)
  1343. X  (interactive "sZ axis range: ")
  1344. X  (calc-graph-set-range "zrange" range)
  1345. )
  1346. X
  1347. (defun calc-graph-set-range (cmd range)
  1348. X  (if (equal range "$")
  1349. X      (calc-wrapper
  1350. X       (let ((val (calc-top-n 1)))
  1351. X     (if (and (eq (car-safe val) 'intv) (math-constp val))
  1352. X         (setq range (concat
  1353. X              (math-format-number (math-float (nth 2 val))) ":"
  1354. X              (math-format-number (math-float (nth 3 val)))))
  1355. X       (if (and (eq (car-safe val) 'vec)
  1356. X            (= (length val) 3))
  1357. X           (setq range (concat
  1358. X                (math-format-number (math-float (nth 1 val))) ":"
  1359. X                (math-format-number (math-float (nth 2 val)))))
  1360. X         (error "Range specification must be an interval or 2-vector")))
  1361. X     (calc-pop-stack 1))))
  1362. X  (if (string-match "\\[.+\\]" range)
  1363. X      (setq range (substring range 1 -1)))
  1364. X  (if (and (not (string-match ":" range))
  1365. X       (or (string-match "," range)
  1366. X           (string-match " " range)))
  1367. X      (aset range (match-beginning 0) ?\:))
  1368. X  (calc-graph-set-command cmd (if (not (equal range ""))
  1369. X                  (concat "[" range "]")))
  1370. )
  1371. X
  1372. (defun calc-graph-log-x (flag)
  1373. X  (interactive "P")
  1374. X  (calc-graph-set-log flag 0 0)
  1375. )
  1376. X
  1377. (defun calc-graph-log-y (flag)
  1378. X  (interactive "P")
  1379. X  (calc-graph-set-log 0 flag 0)
  1380. )
  1381. X
  1382. (defun calc-graph-log-z (flag)
  1383. X  (interactive "P")
  1384. X  (calc-graph-set-log 0 0 flag)
  1385. )
  1386. X
  1387. (defun calc-graph-set-log (xflag yflag zflag)
  1388. X  (let* ((old (or (calc-graph-find-command "logscale") ""))
  1389. X     (xold (string-match "x" old))
  1390. X     (yold (string-match "y" old))
  1391. X     (zold (string-match "z" old))
  1392. X     str)
  1393. X    (setq str (concat (if (if xflag
  1394. X                  (if (eq xflag 0) xold
  1395. X                (> (prefix-numeric-value xflag) 0))
  1396. X                (not xold)) "x" "")
  1397. X              (if (if yflag
  1398. X                  (if (eq yflag 0) yold
  1399. X                (> (prefix-numeric-value yflag) 0))
  1400. X                (not yold)) "y" "")
  1401. X              (if (if zflag
  1402. X                  (if (eq zflag 0) zold
  1403. X                (> (prefix-numeric-value zflag) 0))
  1404. X                (not zold)) "z" "")))
  1405. X    (calc-graph-set-command "logscale" (if (not (equal str "")) str)))
  1406. )
  1407. X
  1408. (defun calc-graph-line-style (style)
  1409. X  (interactive "P")
  1410. X  (calc-graph-set-styles (and style (prefix-numeric-value style)) t)
  1411. )
  1412. X
  1413. (defun calc-graph-point-style (style)
  1414. X  (interactive "P")
  1415. X  (calc-graph-set-styles t (and style (prefix-numeric-value style)))
  1416. )
  1417. X
  1418. (defun calc-graph-set-styles (lines points)
  1419. X  (calc-graph-init)
  1420. X  (save-excursion
  1421. X    (set-buffer calc-gnuplot-input)
  1422. X    (or (calc-graph-find-plot nil nil)
  1423. X    (error "No data points have been set!"))
  1424. X    (let ((base (point))
  1425. X      (mode nil) (lstyle nil) (pstyle nil)
  1426. X      start end lenbl penbl)
  1427. X      (re-search-forward "[,\n]")
  1428. X      (forward-char -1)
  1429. X      (setq end (point) start end)
  1430. X      (goto-char base)
  1431. X      (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+with\\)")
  1432. X      (progn
  1433. X        (setq start (match-beginning 1))
  1434. X        (goto-char (match-end 0))
  1435. X        (if (looking-at "[ \t]+\\([a-z]+\\)")
  1436. X        (setq mode (buffer-substring (match-beginning 1)
  1437. X                         (match-end 1))))
  1438. X        (if (looking-at "[ \ta-z]+\\([0-9]+\\)")
  1439. X        (setq lstyle (string-to-int
  1440. X                  (buffer-substring (match-beginning 1)
  1441. X                        (match-end 1)))))
  1442. X        (if (looking-at "[ \ta-z]+[0-9]+[ \t]+\\([0-9]+\\)")
  1443. X        (setq pstyle (string-to-int
  1444. X                  (buffer-substring (match-beginning 1)
  1445. X                        (match-end 1)))))))
  1446. X      (setq lenbl (or (equal mode "lines") (equal mode "linespoints"))
  1447. X        penbl (or (equal mode "points") (equal mode "linespoints")))
  1448. X      (if lines
  1449. X      (or (eq lines t)
  1450. X          (setq lstyle lines
  1451. X            lenbl (>= lines 0)))
  1452. X    (setq lenbl (not lenbl)))
  1453. X      (if points
  1454. X      (or (eq points t)
  1455. X          (setq pstyle points
  1456. X            penbl (>= points 0)))
  1457. X    (setq penbl (not penbl)))
  1458. X      (delete-region start end)
  1459. X      (goto-char start)
  1460. X      (insert " with "
  1461. X          (if lenbl
  1462. X          (if penbl "linespoints" "lines")
  1463. X        (if penbl "points" "dots")))
  1464. X      (if (and pstyle (> pstyle 0))
  1465. X      (insert " " (if (and lstyle (> lstyle 0)) (int-to-string lstyle) "1")
  1466. X          " " (int-to-string pstyle))
  1467. X    (if (and lstyle (> lstyle 0))
  1468. X        (insert " " (int-to-string lstyle))))))
  1469. X  (calc-graph-view-commands)
  1470. )
  1471. X
  1472. (defun calc-graph-zero-x (flag)
  1473. X  (interactive "P")
  1474. X  (calc-graph-set-command "noxzeroaxis"
  1475. X              (and (if flag
  1476. X                   (<= (prefix-numeric-value flag) 0)
  1477. X                 (not (calc-graph-find-command "noxzeroaxis")))
  1478. X                   " "))
  1479. )
  1480. X
  1481. (defun calc-graph-zero-y (flag)
  1482. X  (interactive "P")
  1483. X  (calc-graph-set-command "noyzeroaxis"
  1484. X              (and (if flag
  1485. X                   (<= (prefix-numeric-value flag) 0)
  1486. X                 (not (calc-graph-find-command "noyzeroaxis")))
  1487. X                   " "))
  1488. )
  1489. X
  1490. (defun calc-graph-name (name)
  1491. X  (interactive "sTitle for current curve: ")
  1492. X  (calc-graph-init)
  1493. X  (save-excursion
  1494. X    (set-buffer calc-gnuplot-input)
  1495. X    (or (calc-graph-find-plot nil nil)
  1496. X    (error "No data points have been set!"))
  1497. X    (let ((base (point))
  1498. X      start)
  1499. X      (re-search-forward "[,\n]\\|[ \t]+with")
  1500. X      (setq end (match-beginning 0))
  1501. X      (goto-char base)
  1502. X      (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+title\\)")
  1503. X      (progn
  1504. X        (goto-char (match-beginning 1))
  1505. X        (delete-region (point) end))
  1506. X    (goto-char end))
  1507. X      (insert " title " (prin1-to-string name))))
  1508. X  (calc-graph-view-commands)
  1509. )
  1510. X
  1511. (defun calc-graph-hide (flag)
  1512. X  (interactive "P")
  1513. X  (calc-graph-init)
  1514. X  (and (calc-graph-find-plot nil nil)
  1515. X       (progn
  1516. X     (or (looking-at "{")
  1517. X         (error "Can't hide this curve (wrong format)"))
  1518. X     (forward-char 1)
  1519. X     (if (looking-at "*")
  1520. X         (if (or (null flag) (<= (prefix-numeric-value flag) 0))
  1521. X         (delete-char 1))
  1522. X       (if (or (null flag) (> (prefix-numeric-value flag) 0))
  1523. X           (insert "*")))))
  1524. )
  1525. X
  1526. (defun calc-graph-header (title)
  1527. X  (interactive "sTitle for entire graph: ")
  1528. X  (calc-graph-set-command "title" (if (not (equal title ""))
  1529. X                      (prin1-to-string title)))
  1530. )
  1531. X
  1532. (defun calc-graph-border (flag)
  1533. X  (interactive "P")
  1534. X  (calc-graph-set-command "noborder"
  1535. X              (and (if flag
  1536. X                   (<= (prefix-numeric-value flag) 0)
  1537. X                 (not (calc-graph-find-command "noborder")))
  1538. X                   " "))
  1539. )
  1540. X
  1541. (defun calc-graph-grid (flag)
  1542. X  (interactive "P")
  1543. X  (calc-graph-set-command "grid" (and (if flag
  1544. X                      (> (prefix-numeric-value flag) 0)
  1545. X                    (not (calc-graph-find-command "grid")))
  1546. X                      " "))
  1547. )
  1548. X
  1549. (defun calc-graph-key (flag)
  1550. X  (interactive "P")
  1551. X  (calc-graph-set-command "key" (and (if flag
  1552. X                     (> (prefix-numeric-value flag) 0)
  1553. X                       (not (calc-graph-find-command "key")))
  1554. X                     " "))
  1555. )
  1556. X
  1557. (defun calc-graph-num-points (res flag)
  1558. X  (interactive "sNumber of data points: \nP")
  1559. X  (if flag
  1560. X      (if (> (prefix-numeric-value flag) 0)
  1561. X      (if (equal res "")
  1562. X          (message "Default resolution is %d."
  1563. X               calc-graph-default-resolution)
  1564. X        (setq calc-graph-default-resolution (string-to-int res)))
  1565. X    (if (equal res "")
  1566. X        (message "Default 3D resolution is %d."
  1567. X             calc-graph-default-resolution-3d)
  1568. X      (setq calc-graph-default-resolution-3d (string-to-int res))))
  1569. X    (calc-graph-set-command "samples" (if (not (equal res "")) res)))
  1570. )
  1571. X
  1572. (defun calc-graph-device (name flag)
  1573. X  (interactive "sDevice name: \nP")
  1574. X  (if (equal name "?")
  1575. X      (progn
  1576. X    (calc-gnuplot-command "set terminal")
  1577. X    (calc-graph-view-trail))
  1578. X    (if flag
  1579. X    (if (> (prefix-numeric-value flag) 0)
  1580. X        (if (equal name "")
  1581. X        (message "Default GNUPLOT device is \"%s\"."
  1582. X             calc-gnuplot-default-device)
  1583. X          (setq calc-gnuplot-default-device name))
  1584. X      (if (equal name "")
  1585. X          (message "GNUPLOT device for Print command is \"%s\"."
  1586. X               calc-gnuplot-print-device)
  1587. X        (setq calc-gnuplot-print-device name)))
  1588. X      (calc-graph-set-command "terminal" (if (not (equal name ""))
  1589. X                         name))))
  1590. )
  1591. X
  1592. (defun calc-graph-output (name flag)
  1593. X  (interactive "sOutput file name: \nP")
  1594. X  (if flag
  1595. X      (if (> (prefix-numeric-value flag) 0)
  1596. X      (if (equal name "")
  1597. X          (message "Default GNUPLOT output file is \"%s\"."
  1598. X               calc-gnuplot-default-output)
  1599. X        (if (string-match "^[sS][tT][dD][oO][uU][tT]$" name)
  1600. X        (setq name "STDOUT"))
  1601. X        (setq calc-gnuplot-default-output name))
  1602. X    (if (equal name "")
  1603. X        (message "GNUPLOT output file for Print command is \"%s\"."
  1604. X             calc-gnuplot-print-output)
  1605. X      (setq calc-gnuplot-print-output name)))
  1606. X    (calc-graph-set-command "output" (if (not (equal name ""))
  1607. X                     (prin1-to-string name))))
  1608. )
  1609. X
  1610. (defun calc-graph-display (name)
  1611. X  (interactive "sX display name: ")
  1612. X  (if (equal name "")
  1613. X      (message "Current X display is \"%s\"."
  1614. X           (or calc-gnuplot-display "<none>"))
  1615. X    (setq calc-gnuplot-display name)
  1616. X    (if (calc-gnuplot-alive)
  1617. X    (calc-gnuplot-command "exit")))
  1618. )
  1619. X
  1620. (defun calc-graph-geometry (name)
  1621. X  (interactive "sX geometry spec (or \"default\"): ")
  1622. X  (if (equal name "")
  1623. X      (message "Current X geometry is \"%s\"."
  1624. X           (or calc-gnuplot-geometry "default"))
  1625. X    (setq calc-gnuplot-geometry (and (not (equal name "default")) name))
  1626. X    (if (calc-gnuplot-alive)
  1627. X    (calc-gnuplot-command "exit")))
  1628. )
  1629. X
  1630. (defun calc-graph-find-command (cmd)
  1631. X  (calc-graph-init)
  1632. X  (save-excursion
  1633. X    (set-buffer calc-gnuplot-input)
  1634. X    (goto-char (point-min))
  1635. X    (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t]*\\(.*\\)$") nil t)
  1636. X    (buffer-substring (match-beginning 1) (match-end 1))))
  1637. )
  1638. X
  1639. (defun calc-graph-set-command (cmd &rest args)
  1640. X  (calc-graph-init)
  1641. X  (save-excursion
  1642. X    (set-buffer calc-gnuplot-input)
  1643. X    (goto-char (point-min))
  1644. X    (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t\n]") nil t)
  1645. X    (progn
  1646. X      (forward-char -1)
  1647. X      (end-of-line)
  1648. X      (let ((end (point)))
  1649. X        (beginning-of-line)
  1650. X        (delete-region (point) (1+ end))))
  1651. X      (if (calc-graph-find-plot t t)
  1652. X      (if (eq (preceding-char) ?\n)
  1653. X          (forward-char -1))
  1654. X    (goto-char (1- (point-max)))))
  1655. X    (if (and args (car args))
  1656. X    (progn
  1657. X      (or (bolp)
  1658. X          (insert "\n"))
  1659. X      (insert "set " (mapconcat 'identity (cons cmd args) " ") "\n"))))
  1660. X  (calc-graph-view-commands)
  1661. )
  1662. X
  1663. (defun calc-graph-command (cmd)
  1664. X  (interactive "sGNUPLOT command: ")
  1665. X  (calc-wrapper
  1666. X   (calc-graph-init)
  1667. X   (calc-graph-view-trail)
  1668. X   (calc-gnuplot-command cmd)
  1669. X   (accept-process-output)
  1670. X   (calc-graph-view-trail))
  1671. )
  1672. X
  1673. (defun calc-graph-kill (&optional no-view)
  1674. X  (interactive)
  1675. X  (calc-graph-delete-temps)
  1676. X  (if (calc-gnuplot-alive)
  1677. X      (calc-wrapper
  1678. X       (or no-view (calc-graph-view-trail))
  1679. X       (let ((calc-graph-no-wait t))
  1680. X     (calc-gnuplot-command "exit"))
  1681. X       (sit-for 1)
  1682. X       (if (process-status calc-gnuplot-process)
  1683. X       (delete-process calc-gnuplot-process))
  1684. X       (setq calc-gnuplot-process nil)))
  1685. )
  1686. X
  1687. (defun calc-graph-quit ()
  1688. X  (interactive)
  1689. X  (if (get-buffer-window calc-gnuplot-input)
  1690. X      (calc-graph-view-commands t))
  1691. X  (if (get-buffer-window calc-gnuplot-buffer)
  1692. X      (calc-graph-view-trail t))
  1693. X  (calc-graph-kill t)
  1694. )
  1695. X
  1696. (defun calc-graph-view-commands (&optional no-need)
  1697. X  (interactive "p")
  1698. X  (or calc-graph-no-auto-view (calc-graph-init-buffers))
  1699. X  (calc-graph-view calc-gnuplot-input calc-gnuplot-buffer (null no-need))
  1700. )
  1701. X
  1702. (defun calc-graph-view-trail (&optional no-need)
  1703. X  (interactive "p")
  1704. X  (or calc-graph-no-auto-view (calc-graph-init-buffers))
  1705. X  (calc-graph-view calc-gnuplot-buffer calc-gnuplot-input (null no-need))
  1706. )
  1707. X
  1708. (defun calc-graph-view (buf other-buf need)
  1709. X  (let (win)
  1710. X    (or calc-graph-no-auto-view
  1711. X    (if (setq win (get-buffer-window buf))
  1712. X        (or need
  1713. X        (and (eq buf calc-gnuplot-buffer)
  1714. X             (save-excursion
  1715. X               (set-buffer buf)
  1716. X               (not (pos-visible-in-window-p (point-max) win))))
  1717. X        (progn
  1718. X          (bury-buffer buf)
  1719. X          (bury-buffer other-buf)
  1720. X          (let ((curwin (selected-window)))
  1721. X            (select-window win)
  1722. X            (switch-to-buffer nil)
  1723. X            (select-window curwin))))
  1724. X      (if (setq win (get-buffer-window other-buf))
  1725. X          (set-window-buffer win buf)
  1726. X        (if (eq major-mode 'calc-mode)
  1727. X        (if (or need
  1728. X            (< (window-height) (1- (screen-height))))
  1729. X            (display-buffer buf))
  1730. X          (switch-to-buffer buf)))))
  1731. X    (save-excursion
  1732. X      (set-buffer buf)
  1733. X      (if (and (eq buf calc-gnuplot-buffer)
  1734. X           (setq win (get-buffer-window buf))
  1735. X           (not (pos-visible-in-window-p (point-max) win)))
  1736. X      (progn
  1737. X        (goto-char (point-max))
  1738. X        (vertical-motion (- 6 (window-height win)))
  1739. X        (set-window-start win (point))
  1740. X        (goto-char (point-max)))))
  1741. X    (or calc-graph-no-auto-view (sit-for 0)))
  1742. )
  1743. (setq calc-graph-no-auto-view nil)
  1744. X
  1745. (defun calc-gnuplot-check-for-errors ()
  1746. X  (if (save-excursion
  1747. X    (prog2
  1748. X     (progn
  1749. X       (set-buffer calc-gnuplot-buffer)
  1750. X       (goto-char calc-gnuplot-last-error-pos))
  1751. X     (re-search-forward "^[ \t]+\\^$" nil t)
  1752. X     (goto-char (point-max))
  1753. X     (setq calc-gnuplot-last-error-pos (point-max))))
  1754. X      (calc-graph-view-trail))
  1755. )
  1756. X
  1757. (defun calc-gnuplot-command (&rest args)
  1758. X  (calc-graph-init)
  1759. X  (let ((cmd (concat (mapconcat 'identity args " ") "\n")))
  1760. X    (accept-process-output)
  1761. X    (save-excursion
  1762. X      (set-buffer calc-gnuplot-buffer)
  1763. X      (calc-gnuplot-check-for-errors)
  1764. X      (goto-char (point-max))
  1765. X      (setq calc-gnuplot-trail-mark (point))
  1766. SHAR_EOF
  1767. true || echo 'restore of calc-graph.el failed'
  1768. fi
  1769. echo 'End of  part 17'
  1770. echo 'File calc-graph.el is continued in part 18'
  1771. echo 18 > _shar_seq_.tmp
  1772. exit 0
  1773. exit 0 # Just in case...
  1774. -- 
  1775. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1776. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1777. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1778. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1779.