1 ;; Calculator for GNU Emacs, part II [calc-graph.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-graph () nil)
34 ;;; Note that some of the following initial values also occur in calc.el.
35 (defvar calc-gnuplot-tempfile "/tmp/calc")
37 (defvar calc-gnuplot-default-device "default")
38 (defvar calc-gnuplot-default-output "STDOUT")
39 (defvar calc-gnuplot-print-device "postscript")
40 (defvar calc-gnuplot-print-output "auto")
41 (defvar calc-gnuplot-keep-outfile nil)
42 (defvar calc-gnuplot-version nil)
44 (defvar calc-gnuplot-display (getenv "DISPLAY"))
45 (defvar calc-gnuplot-geometry nil)
47 (defvar calc-graph-default-resolution 15)
48 (defvar calc-graph-default-resolution-3d 5)
49 (defvar calc-graph-default-precision 5)
51 (defvar calc-gnuplot-buffer nil)
52 (defvar calc-gnuplot-input nil)
54 (defvar calc-gnuplot-last-error-pos 1)
55 (defvar calc-graph-last-device nil)
56 (defvar calc-graph-last-output nil)
57 (defvar calc-graph-file-cache nil)
58 (defvar calc-graph-var-cache nil)
59 (defvar calc-graph-data-cache nil)
60 (defvar calc-graph-data-cache-limit 10)
62 (defun calc-graph-fast (many)
64 (let ((calc-graph-no-auto-view t))
67 (calc-graph-plot nil))
70 (defun calc-graph-fast-3d (many)
72 (let ((calc-graph-no-auto-view t))
74 (calc-graph-add-3d many)
75 (calc-graph-plot nil))
78 (defun calc-graph-delete (all)
83 (set-buffer calc-gnuplot-input)
84 (and (calc-graph-find-plot t all)
86 (if (looking-at "s?plot")
88 (setq calc-graph-var-cache nil)
89 (delete-region (point) (point-max)))
90 (delete-region (point) (1- (point-max)))))))
91 (calc-graph-view-commands))
94 (defun calc-graph-find-plot (&optional before all)
95 (goto-char (point-min))
96 (and (re-search-forward "^s?plot[ \t]+" nil t)
98 (goto-char (point-max))
100 (not (search-backward "," nil t))
105 (beginning-of-line)))
107 (re-search-forward ",[ \t]+")))
111 (defun calc-graph-add (many)
116 (calc-graph-add-curve (calc-graph-lookup (calc-top-n 2))
117 (calc-graph-lookup (calc-top-n 1))))
118 ((or (consp many) (eq many 0))
119 (let ((xdata (calc-graph-lookup (calc-top-n 2)))
120 (ylist (calc-top-n 1)))
121 (or (eq (car-safe ylist) 'vec)
122 (error "Y argument must be a vector"))
123 (while (setq ylist (cdr ylist))
124 (calc-graph-add-curve xdata (calc-graph-lookup (car ylist))))))
125 ((> (setq many (prefix-numeric-value many)) 0)
126 (let ((xdata (calc-graph-lookup (calc-top-n (1+ many)))))
128 (calc-graph-add-curve xdata
129 (calc-graph-lookup (calc-top-n many)))
130 (setq many (1- many)))))
135 (setq pair (calc-top-n many))
136 (or (and (eq (car-safe pair) 'vec)
138 (error "Argument must be an [x,y] vector"))
139 (calc-graph-add-curve (calc-graph-lookup (nth 1 pair))
140 (calc-graph-lookup (nth 2 pair)))
141 (setq many (1- many))))))
142 (calc-graph-view-commands))
145 (defun calc-graph-add-3d (many)
150 (calc-graph-add-curve (calc-graph-lookup (calc-top-n 3))
151 (calc-graph-lookup (calc-top-n 2))
152 (calc-graph-lookup (calc-top-n 1))))
153 ((or (consp many) (eq many 0))
154 (let ((xdata (calc-graph-lookup (calc-top-n 3)))
155 (ydata (calc-graph-lookup (calc-top-n 2)))
156 (zlist (calc-top-n 1)))
157 (or (eq (car-safe zlist) 'vec)
158 (error "Z argument must be a vector"))
159 (while (setq zlist (cdr zlist))
160 (calc-graph-add-curve xdata ydata
161 (calc-graph-lookup (car zlist))))))
162 ((> (setq many (prefix-numeric-value many)) 0)
163 (let ((xdata (calc-graph-lookup (calc-top-n (+ many 2))))
164 (ydata (calc-graph-lookup (calc-top-n (+ many 1)))))
166 (calc-graph-add-curve xdata ydata
167 (calc-graph-lookup (calc-top-n many)))
168 (setq many (1- many)))))
173 (setq curve (calc-top-n many))
174 (or (and (eq (car-safe curve) 'vec)
175 (= (length curve) 4))
176 (error "Argument must be an [x,y,z] vector"))
177 (calc-graph-add-curve (calc-graph-lookup (nth 1 curve))
178 (calc-graph-lookup (nth 2 curve))
179 (calc-graph-lookup (nth 3 curve)))
180 (setq many (1- many))))))
181 (calc-graph-view-commands))
184 (defun calc-graph-add-curve (xdata ydata &optional zdata)
185 (let ((num (calc-graph-count-curves))
186 (pstyle (calc-var-value 'var-PointStyles))
187 (lstyle (calc-var-value 'var-LineStyles)))
189 (set-buffer calc-gnuplot-input)
190 (goto-char (point-min))
191 (if (re-search-forward (if zdata "^plot[ \t]" "^splot[ \t]")
193 (error "Can't mix 2d and 3d curves on one graph"))
194 (if (re-search-forward "^s?plot[ \t]" nil t)
198 (goto-char (point-max))
199 (or (eq (preceding-char) ?\n)
201 (insert (if zdata "splot" "plot") " \n")
203 (insert "{" (symbol-name (nth 1 xdata))
204 ":" (symbol-name (nth 1 ydata)))
206 (insert ":" (symbol-name (nth 1 zdata))))
208 "title \"" (symbol-name (nth 1 ydata)) "\" "
210 (setq pstyle (and (eq (car-safe pstyle) 'vec) (nth (1+ num) pstyle)))
211 (setq lstyle (and (eq (car-safe lstyle) 'vec) (nth (1+ num) lstyle)))
212 (calc-graph-set-styles
213 (or (and (Math-num-integerp lstyle) (math-trunc lstyle))
215 (or (and (Math-num-integerp pstyle) (math-trunc pstyle))
216 (if (eq (car-safe (calc-var-value (nth 2 ydata))) 'vec)
220 (defun calc-graph-lookup (thing)
221 (if (and (eq (car-safe thing) 'var)
222 (calc-var-value (nth 2 thing)))
224 (let ((found (assoc thing calc-graph-var-cache)))
227 (setq varname (concat "PlotData"
229 (1+ (length calc-graph-var-cache))))
230 var (list 'var (intern varname)
231 (intern (concat "var-" varname)))
232 found (cons thing var)
233 calc-graph-var-cache (cons found calc-graph-var-cache))
234 (set (nth 2 var) thing)))
238 (defun calc-graph-juggle (arg)
242 (set-buffer calc-gnuplot-input)
244 (let ((num (calc-graph-count-curves)))
247 (setq arg (+ arg num))))))
248 (while (>= (setq arg (1- arg)) 0)
249 (calc-graph-do-juggle)))
252 (defun calc-graph-count-curves ()
254 (set-buffer calc-gnuplot-input)
255 (if (re-search-forward "^s?plot[ \t]" nil t)
257 (goto-char (point-min))
258 (while (search-forward "," nil t)
264 (defun calc-graph-do-juggle ()
266 (and (calc-graph-find-plot t t)
269 (calc-graph-find-plot t nil)
270 (or (eq base (point))
271 (let ((str (buffer-substring (+ (point) 2) (1- (point-max)))))
272 (delete-region (point) (1- (point-max)))
273 (goto-char (+ base 5))
274 (insert str ", "))))))
277 (defun calc-graph-print (flag)
279 (calc-graph-plot flag t)
282 (defun calc-graph-plot (flag &optional printing)
285 (let ((calcbuf (current-buffer))
286 (tempbuf (get-buffer-create "*Gnuplot Temp-2*"))
290 (refine (and flag (> (prefix-numeric-value flag) 0)))
291 (recompute (and flag (< (prefix-numeric-value flag) 0)))
294 cache-env is-splot device output resolution precision samples-pos)
295 (or (boundp 'calc-graph-prev-kill-hook)
296 (if calc-emacs-type-19
298 (setq calc-graph-prev-kill-hook nil)
299 (add-hook 'kill-emacs-hook 'calc-graph-kill-hook))
300 (setq calc-graph-prev-kill-hook kill-emacs-hook)
301 (setq kill-emacs-hook 'calc-graph-kill-hook)))
306 (set-buffer calc-gnuplot-input)
307 (goto-char (point-min))
308 (setq is-splot (re-search-forward "^splot[ \t]" nil t))
309 (let ((str (buffer-string))
310 (ver calc-gnuplot-version))
311 (set-buffer (get-buffer-create "*Gnuplot Temp*"))
313 (insert "# (Note: This is a temporary copy---do not edit!)\n")
315 (insert "set noarrow\nset nolabel\n"
316 "set autoscale xy\nset nologscale xy\n"
317 "set xlabel\nset ylabel\nset title\n"
318 "set noclip points\nset clip one\nset clip two\n"
319 "set format \"%g\"\nset tics\nset xtics\nset ytics\n"
320 "set data style linespoints\n"
321 "set nogrid\nset nokey\nset nopolar\n"))
323 (insert "set surface\nset nocontour\n"
324 "set " (if is-splot "" "no") "parametric\n"
325 "set notime\nset border\nset ztics\nset zeroaxis\n"
326 "set view 60,30,1,1\nset offsets 0,0,0,0\n"))
327 (setq samples-pos (point))
329 (goto-char (point-min))
332 (error "This option works only for 2d plots")
334 (let ((calc-gnuplot-input (current-buffer))
335 (calc-graph-no-auto-view t))
337 (setq device calc-gnuplot-print-device
338 output calc-gnuplot-print-output)
339 (setq device (calc-graph-find-command "terminal")
340 output (calc-graph-find-command "output"))
342 (setq device calc-gnuplot-default-device))
344 (setq output (car (read-from-string output)))
345 (setq output calc-gnuplot-default-output)))
346 (if (or (equal device "") (equal device "default"))
347 (setq device (if printing
349 (if (or (eq window-system 'x) (getenv "DISPLAY"))
351 (if (>= calc-gnuplot-version 3)
352 "dumb" "postscript")))))
353 (if (equal device "dumb")
354 (setq device (format "dumb %d %d"
355 (1- (frame-width)) (1- (frame-height)))))
356 (if (equal device "big")
357 (setq device (format "dumb %d %d"
358 (* 4 (- (frame-width) 3))
359 (* 4 (- (frame-height) 3)))))
361 (if (or (equal output "auto")
362 (and (equal output "tty") (setq tty-output t)))
363 (setq tempoutfile (calc-temp-file-name -1)
365 (setq output (eval output)))
366 (or (equal device calc-graph-last-device)
368 (setq calc-graph-last-device device)
369 (calc-gnuplot-command "set terminal" device)))
370 (or (equal output calc-graph-last-output)
372 (setq calc-graph-last-output output)
373 (calc-gnuplot-command "set output"
374 (if (equal output "STDOUT")
376 (prin1-to-string output)))))
377 (setq resolution (calc-graph-find-command "samples"))
379 (setq resolution (string-to-int resolution))
380 (setq resolution (if is-splot
381 calc-graph-default-resolution-3d
382 calc-graph-default-resolution)))
383 (setq precision (calc-graph-find-command "precision"))
385 (setq precision (string-to-int precision))
386 (setq precision calc-graph-default-precision))
387 (calc-graph-set-command "terminal")
388 (calc-graph-set-command "output")
389 (calc-graph-set-command "samples")
390 (calc-graph-set-command "precision"))
391 (goto-char samples-pos)
392 (insert "set samples " (int-to-string (max (if is-splot 20 200)
393 (+ 5 resolution))) "\n")
394 (while (re-search-forward "{\\*[^}]+}[^,\n]*" nil t)
395 (delete-region (match-beginning 0) (match-end 0))
398 (while (memq (preceding-char) '(?\ ?\t))
400 (if (eq (preceding-char) ?\,)
401 (delete-backward-char 1))))
404 (setq cache-env (list calc-angle-mode
410 (if (and (not recompute)
411 (equal (cdr (car calc-graph-data-cache)) cache-env))
412 (while (> (length calc-graph-data-cache)
413 calc-graph-data-cache-limit)
414 (setcdr calc-graph-data-cache
415 (cdr (cdr calc-graph-data-cache))))
416 (setq calc-graph-data-cache (list (cons nil cache-env)))))
417 (calc-graph-find-plot t t)
418 (while (re-search-forward
420 "{\\([^{}:\n]+\\):\\([^{}:\n]+\\):\\([^{}:\n]+\\)}"
421 "{\\([^{}:\n]+\\)\\(:\\)\\([^{}:\n]+\\)}")
423 (setq curve-num (1+ curve-num))
424 (let* ((xname (buffer-substring (match-beginning 1) (match-end 1)))
425 (xvar (intern (concat "var-" xname)))
426 (xvalue (math-evaluate-expr (calc-var-value xvar)))
427 (y3name (and is-splot
428 (buffer-substring (match-beginning 2)
430 (y3var (and is-splot (intern (concat "var-" y3name))))
431 (y3value (and is-splot (calc-var-value y3var)))
432 (yname (buffer-substring (match-beginning 3) (match-end 3)))
433 (yvar (intern (concat "var-" yname)))
434 (yvalue (calc-var-value yvar))
436 (delete-region (match-beginning 0) (match-end 0))
437 (setq filename (calc-temp-file-name curve-num))
444 (xlow nil) (xhigh nil) (y3low nil) (y3high nil)
445 xvec xval xstep var-DUMMY
446 y3vec y3val y3step var-DUMMY2 (zval nil)
447 yvec yval ycache ycacheptr yvector
449 (keep-file (and (not is-splot) (file-exists-p filename)))
451 (calc-symbolic-mode nil)
452 (calc-prefer-frac nil)
453 (calc-internal-prec (max 3 precision))
454 (calc-simplify-mode (and (not (memq calc-simplify-mode
459 (math-working-step 0)
460 (math-working-step-2 nil))
463 (calc-graph-compute-3d)
464 (calc-graph-compute-2d))
466 (goto-char (point-max))
470 (insert ":" yname "\n\n")
471 (setq tempbuftop (point))
472 (let ((calc-group-digits nil)
473 (calc-leading-zeros nil)
474 (calc-number-radix 10)
475 (entry (and (not is-splot)
476 (list xp yp xhigh numsteps))))
478 (nth 1 (nth (1+ curve-num)
479 calc-graph-file-cache)))
480 (setq keep-file nil))
481 (setcar (cdr (nth (1+ curve-num) calc-graph-file-cache))
484 (calc-graph-format-data)))
488 (error "No valid data points for %s:%s"
490 (write-region tempbuftop (point-max) filename
492 (insert (prin1-to-string filename))))
494 (setcdr cache-env nil))
497 (calc-gnuplot-command "clear")
498 (calc-clear-command-flag 'clear-message)
499 (message "No data to plot!"))
500 (setq calc-graph-data-cache-limit (max curve-num
501 calc-graph-data-cache-limit)
502 filename (calc-temp-file-name 0))
503 (write-region (point-min) (point-max) filename nil 'quiet)
504 (calc-gnuplot-command "load" (prin1-to-string filename))
505 (or (equal output "STDOUT")
506 calc-gnuplot-keep-outfile
507 (progn ; need to close the output file before printing/plotting
508 (setq calc-graph-last-output "STDOUT")
509 (calc-gnuplot-command "set output")))
510 (let ((command (if printing
511 calc-gnuplot-print-command
512 (or calc-gnuplot-plot-command
513 (and (string-match "^dumb" device)
514 'calc-graph-show-dumb)
516 'calc-graph-show-tty)))))
518 (if (stringp command)
519 (calc-gnuplot-command
522 calc-gnuplot-print-output)))
523 (if (symbolp command)
524 (funcall command output)
525 (eval command)))))))))
528 (defun calc-graph-compute-2d ()
529 (if (setq yvec (eq (car-safe yvalue) 'vec))
530 (if (= (setq numsteps (1- (length yvalue))) 0)
531 (error "Can't plot an empty vector")
532 (if (setq xvec (eq (car-safe xvalue) 'vec))
533 (or (= (1- (length xvalue)) numsteps)
534 (error "%s and %s have different lengths" xname yname))
535 (if (and (eq (car-safe xvalue) 'intv)
536 (math-constp xvalue))
537 (setq xstep (math-div (math-sub (nth 3 xvalue)
540 xvalue (nth 2 xvalue))
541 (if (math-realp xvalue)
543 (error "%s is not a suitable basis for %s" xname yname)))))
544 (or (math-realp yvalue)
546 (setq yvalue (math-evaluate-expr yvalue))
547 (calc-default-formula-arglist yvalue)
549 (error "%s does not contain any unassigned variables" yname))
551 (error "%s contains more than one variable: %s"
553 (setq yvalue (math-expr-subst yvalue
554 (math-build-var-name (car arglist))
555 '(var DUMMY var-DUMMY)))))
556 (setq ycache (assoc yvalue calc-graph-data-cache))
557 (delq ycache calc-graph-data-cache)
558 (nconc calc-graph-data-cache
559 (list (or ycache (setq ycache (list yvalue)))))
560 (if (and (not (setq xvec (eq (car-safe xvalue) 'vec)))
561 refine (cdr (cdr ycache)))
562 (calc-graph-refine-2d)
563 (calc-graph-recompute-2d)))
566 (defun calc-graph-refine-2d ()
568 ycacheptr (cdr ycache))
569 (if (and (setq xval (calc-graph-find-command "xrange"))
570 (string-match "\\`\\[\\([0-9.eE+-]*\\):\\([0-9.eE+-]*\\)\\]\\'"
572 (let ((b2 (match-beginning 2))
574 (setq xlow (math-read-number (substring xval
577 xhigh (math-read-number (substring xval b2 e2))))
579 (while (and (cdr ycacheptr)
580 (Math-lessp (car (nth 1 ycacheptr)) xlow))
581 (setq ycacheptr (cdr ycacheptr)))))
582 (setq math-working-step-2 (1- (length ycacheptr)))
583 (while (and (cdr ycacheptr)
585 (Math-lessp (car (car ycacheptr)) xhigh)))
586 (setq var-DUMMY (math-div (math-add (car (car ycacheptr))
587 (car (nth 1 ycacheptr)))
589 math-working-step (1+ math-working-step)
590 yval (math-evaluate-expr yvalue))
591 (setcdr ycacheptr (cons (cons var-DUMMY yval)
593 (setq ycacheptr (cdr (cdr ycacheptr))))
598 (defun calc-graph-recompute-2d ()
599 (setq ycacheptr ycache)
601 (setq numsteps (1- (length xvalue))
603 (if (and (eq (car-safe xvalue) 'intv)
604 (math-constp xvalue))
605 (setq numsteps resolution
609 xstep (math-div (math-sub xhigh xlow)
611 xvalue (nth 2 xvalue))
612 (error "%s is not a suitable basis for %s"
614 (setq math-working-step-2 numsteps)
615 (while (>= (setq numsteps (1- numsteps)) 0)
616 (setq math-working-step (1+ math-working-step))
621 (and (not (eq ycacheptr ycache))
622 (consp (car ycacheptr))
623 (not (Math-lessp (car (car ycacheptr)) xval))
624 (setq ycacheptr ycache)))
626 (setq xval xhigh) ; avoid cumulative roundoff
628 xvalue (math-add xvalue xstep))))
629 (while (and (cdr ycacheptr)
630 (Math-lessp (car (nth 1 ycacheptr)) xval))
631 (setq ycacheptr (cdr ycacheptr)))
632 (or (and (cdr ycacheptr)
633 (Math-equal (car (nth 1 ycacheptr)) xval))
637 (setcdr ycacheptr (cons (cons xval (math-evaluate-expr yvalue))
639 (setq ycacheptr (cdr ycacheptr))
641 (setq yvector (cons (cdr (car ycacheptr)) yvector))
642 (or yp (setq yp ycacheptr))))
646 yp (cons 'vec (nreverse yvector))
647 numsteps (1- (length xp)))
648 (setq numsteps 1000000))
651 (defun calc-graph-compute-3d ()
652 (if (setq yvec (eq (car-safe yvalue) 'vec))
653 (if (math-matrixp yvalue)
655 (setq numsteps (1- (length yvalue))
656 numsteps3 (1- (length (nth 1 yvalue))))
657 (if (eq (car-safe xvalue) 'vec)
658 (or (= (1- (length xvalue)) numsteps)
659 (error "%s has wrong length" xname))
660 (if (and (eq (car-safe xvalue) 'intv)
661 (math-constp xvalue))
662 (setq xvalue (calcFunc-index numsteps
665 (math-sub (nth 3 xvalue)
668 (if (math-realp xvalue)
669 (setq xvalue (calcFunc-index numsteps xvalue 1))
670 (error "%s is not a suitable basis for %s" xname yname))))
671 (if (eq (car-safe y3value) 'vec)
672 (or (= (1- (length y3value)) numsteps3)
673 (error "%s has wrong length" y3name))
674 (if (and (eq (car-safe y3value) 'intv)
675 (math-constp y3value))
676 (setq y3value (calcFunc-index numsteps3
679 (math-sub (nth 3 y3value)
682 (if (math-realp y3value)
683 (setq y3value (calcFunc-index numsteps3 y3value 1))
684 (error "%s is not a suitable basis for %s" y3name yname))))
689 (while (setq xvalue (cdr xvalue) yvalue (cdr yvalue))
690 (setq xp (nconc xp (make-list (1+ numsteps3) (car xvalue)))
691 yp (nconc yp (cons 0 (copy-sequence (cdr y3value))))
692 zp (nconc zp (cons '(skip)
693 (copy-sequence (cdr (car yvalue)))))))
694 (setq numsteps (1- (* numsteps (1+ numsteps3)))))
695 (if (= (setq numsteps (1- (length yvalue))) 0)
696 (error "Can't plot an empty vector"))
697 (or (and (eq (car-safe xvalue) 'vec)
698 (= (1- (length xvalue)) numsteps))
699 (error "%s is not a suitable basis for %s" xname yname))
700 (or (and (eq (car-safe y3value) 'vec)
701 (= (1- (length y3value)) numsteps))
702 (error "%s is not a suitable basis for %s" y3name yname))
707 (or (math-realp yvalue)
709 (setq yvalue (math-evaluate-expr yvalue))
710 (calc-default-formula-arglist yvalue)
711 (setq arglist (sort arglist 'string-lessp))
713 (error "%s does not contain enough unassigned variables" yname))
714 (and (cdr (cdr arglist))
715 (error "%s contains too many variables: %s" yname arglist))
716 (setq yvalue (math-multi-subst yvalue
717 (mapcar 'math-build-var-name
719 '((var DUMMY var-DUMMY)
720 (var DUMMY2 var-DUMMY2))))))
721 (if (setq xvec (eq (car-safe xvalue) 'vec))
722 (setq numsteps (1- (length xvalue)))
723 (if (and (eq (car-safe xvalue) 'intv)
724 (math-constp xvalue))
725 (setq numsteps resolution
726 xvalue (calcFunc-index numsteps
728 (math-div (math-sub (nth 3 xvalue)
731 (error "%s is not a suitable basis for %s"
733 (if (setq y3vec (eq (car-safe y3value) 'vec))
734 (setq numsteps3 (1- (length y3value)))
735 (if (and (eq (car-safe y3value) 'intv)
736 (math-constp y3value))
737 (setq numsteps3 resolution
738 y3value (calcFunc-index numsteps3
740 (math-div (math-sub (nth 3 y3value)
743 (error "%s is not a suitable basis for %s"
749 (setq math-working-step 0)
750 (while (setq xvalue (cdr xvalue))
751 (setq xp (nconc xp (make-list (1+ numsteps3) (car xvalue)))
752 yp (nconc yp (cons 0 (copy-sequence (cdr y3value))))
755 var-DUMMY (car xvalue)
756 math-working-step-2 0
757 math-working-step (1+ math-working-step))
758 (while (setq y3step (cdr y3step))
759 (setq math-working-step-2 (1+ math-working-step-2)
760 var-DUMMY2 (car y3step)
761 zp (cons (math-evaluate-expr yvalue) zp))))
762 (setq zp (nreverse zp)
763 numsteps (1- (* numsteps (1+ numsteps3)))))
766 (defun calc-graph-format-data ()
767 (while (<= (setq stepcount (1+ stepcount)) numsteps)
777 xvalue (math-add xvalue xstep)
780 (setq xval (car (car yp))
784 (and xhigh (equal xval xhigh)))
787 (if (and (eq (car-safe zval) 'calcFunc-xyz)
789 (setq xval (nth 1 zval)
792 (if (and (eq (car-safe yval) 'calcFunc-xyz)
797 (set-buffer (get-buffer-create "*Gnuplot Temp*"))
799 (goto-char (point-max))
800 (re-search-backward "^plot[ \t]")
801 (insert "set parametric\ns")
802 (setq surprise-splot t))))
803 (setq xval (nth 1 yval)
806 (if (and (eq (car-safe yval) 'calcFunc-xy)
808 (setq xval (nth 1 yval)
809 yval (nth 2 yval)))))
810 (if (and (Math-realp xval)
812 (or (not zval) (Math-realp zval)))
816 (if (Math-integerp xval)
817 (insert (math-format-number xval))
818 (if (eq (car xval) 'frac)
819 (setq xval (math-float xval)))
820 (insert (math-format-number (nth 1 xval))
821 "e" (int-to-string (nth 2 xval))))
823 (if (Math-integerp yval)
824 (insert (math-format-number yval))
825 (if (eq (car yval) 'frac)
826 (setq yval (math-float yval)))
827 (insert (math-format-number (nth 1 yval))
828 "e" (int-to-string (nth 2 yval))))
832 (if (Math-integerp zval)
833 (insert (math-format-number zval))
834 (if (eq (car zval) 'frac)
835 (setq zval (math-float zval)))
836 (insert (math-format-number (nth 1 zval))
837 "e" (int-to-string (nth 2 zval))))))
839 (and (not (equal zval '(skip)))
840 (boundp 'var-PlotRejects)
841 (eq (car-safe var-PlotRejects) 'vec)
842 (nconc var-PlotRejects
847 (calc-refresh-evaltos 'var-PlotRejects))
854 (defun calc-temp-file-name (num)
855 (while (<= (length calc-graph-file-cache) (1+ num))
856 (setq calc-graph-file-cache (nconc calc-graph-file-cache (list nil))))
857 (car (or (nth (1+ num) calc-graph-file-cache)
858 (setcar (nthcdr (1+ num) calc-graph-file-cache)
859 (list (make-temp-name
860 (concat calc-gnuplot-tempfile
862 (char-to-string (- ?A num))
863 (int-to-string num))))
867 (defun calc-graph-delete-temps ()
868 (while calc-graph-file-cache
869 (and (car calc-graph-file-cache)
870 (file-exists-p (car (car calc-graph-file-cache)))
872 (delete-file (car (car calc-graph-file-cache)))
874 (setq calc-graph-file-cache (cdr calc-graph-file-cache)))
877 (defun calc-graph-kill-hook ()
878 (calc-graph-delete-temps)
879 (if calc-graph-prev-kill-hook
880 (funcall calc-graph-prev-kill-hook))
883 (defun calc-graph-show-tty (output)
884 "Default calc-gnuplot-plot-command for \"tty\" output mode.
885 This is useful for tek40xx and other graphics-terminal types."
886 (call-process-region 1 1 shell-file-name
887 nil calc-gnuplot-buffer nil
888 "-c" (format "cat %s >/dev/tty; rm %s" output output))
891 (defun calc-graph-show-dumb (&optional output)
892 "Default calc-gnuplot-plot-command for Pinard's \"dumb\" terminal type.
893 This \"dumb\" driver will be present in Gnuplot 3.0."
895 (save-window-excursion
896 (switch-to-buffer calc-gnuplot-buffer)
897 (delete-other-windows)
898 (goto-char calc-gnuplot-trail-mark)
899 (or (search-forward "\f" nil t)
901 (goto-char (point-max))
902 (re-search-backward "\f\\|^[ \t]+\\^$\\|G N U P L O T")
903 (setq found-pt (point))
904 (if (looking-at "\f")
907 (if (eolp) (forward-line 1))
908 (or (calc-graph-find-command "time")
909 (calc-graph-find-command "title")
910 (calc-graph-find-command "ylabel")
912 (insert-before-markers (format "(%s)" (current-time-string)))
914 (set-window-start (selected-window) (point))
915 (goto-char (point-max)))
919 (or (boundp 'calc-dumb-map)
921 (setq calc-dumb-map (make-sparse-keymap))
922 (define-key calc-dumb-map "\n" 'scroll-up)
923 (define-key calc-dumb-map " " 'scroll-up)
924 (define-key calc-dumb-map "\177" 'scroll-down)
925 (define-key calc-dumb-map "<" 'scroll-left)
926 (define-key calc-dumb-map ">" 'scroll-right)
927 (define-key calc-dumb-map "{" 'scroll-down)
928 (define-key calc-dumb-map "}" 'scroll-up)
929 (define-key calc-dumb-map "q" 'exit-recursive-edit)
930 (define-key calc-dumb-map "\C-c\C-c" 'exit-recursive-edit)))
931 (use-local-map calc-dumb-map)
932 (setq truncate-lines t)
933 (message "Type `q'%s to return to Calc."
934 (if (eq (lookup-key (current-global-map) "\e#") 'calc-dispatch)
937 (bury-buffer "*Gnuplot Trail*"))
940 (defun calc-graph-clear ()
942 (if calc-graph-last-device
943 (if (or (equal calc-graph-last-device "x11")
944 (equal calc-graph-last-device "X11"))
945 (calc-gnuplot-command "set output"
946 (if (equal calc-graph-last-output "STDOUT")
948 (prin1-to-string calc-graph-last-output)))
949 (calc-gnuplot-command "clear")))
952 (defun calc-graph-title-x (title)
953 (interactive "sX axis title: ")
954 (calc-graph-set-command "xlabel" (if (not (equal title ""))
955 (prin1-to-string title)))
958 (defun calc-graph-title-y (title)
959 (interactive "sY axis title: ")
960 (calc-graph-set-command "ylabel" (if (not (equal title ""))
961 (prin1-to-string title)))
964 (defun calc-graph-title-z (title)
965 (interactive "sZ axis title: ")
966 (calc-graph-set-command "zlabel" (if (not (equal title ""))
967 (prin1-to-string title)))
970 (defun calc-graph-range-x (range)
971 (interactive "sX axis range: ")
972 (calc-graph-set-range "xrange" range)
975 (defun calc-graph-range-y (range)
976 (interactive "sY axis range: ")
977 (calc-graph-set-range "yrange" range)
980 (defun calc-graph-range-z (range)
981 (interactive "sZ axis range: ")
982 (calc-graph-set-range "zrange" range)
985 (defun calc-graph-set-range (cmd range)
986 (if (equal range "$")
988 (let ((val (calc-top-n 1)))
989 (if (and (eq (car-safe val) 'intv) (math-constp val))
991 (math-format-number (math-float (nth 2 val))) ":"
992 (math-format-number (math-float (nth 3 val)))))
993 (if (and (eq (car-safe val) 'vec)
996 (math-format-number (math-float (nth 1 val))) ":"
997 (math-format-number (math-float (nth 2 val)))))
998 (error "Range specification must be an interval or 2-vector")))
999 (calc-pop-stack 1))))
1000 (if (string-match "\\[.+\\]" range)
1001 (setq range (substring range 1 -1)))
1002 (if (and (not (string-match ":" range))
1003 (or (string-match "," range)
1004 (string-match " " range)))
1005 (aset range (match-beginning 0) ?\:))
1006 (calc-graph-set-command cmd (if (not (equal range ""))
1007 (concat "[" range "]")))
1010 (defun calc-graph-log-x (flag)
1012 (calc-graph-set-log flag 0 0)
1015 (defun calc-graph-log-y (flag)
1017 (calc-graph-set-log 0 flag 0)
1020 (defun calc-graph-log-z (flag)
1022 (calc-graph-set-log 0 0 flag)
1025 (defun calc-graph-set-log (xflag yflag zflag)
1026 (let* ((old (or (calc-graph-find-command "logscale") ""))
1027 (xold (string-match "x" old))
1028 (yold (string-match "y" old))
1029 (zold (string-match "z" old))
1031 (setq str (concat (if (if xflag
1032 (if (eq xflag 0) xold
1033 (> (prefix-numeric-value xflag) 0))
1036 (if (eq yflag 0) yold
1037 (> (prefix-numeric-value yflag) 0))
1040 (if (eq zflag 0) zold
1041 (> (prefix-numeric-value zflag) 0))
1042 (not zold)) "z" "")))
1043 (calc-graph-set-command "logscale" (if (not (equal str "")) str)))
1046 (defun calc-graph-line-style (style)
1048 (calc-graph-set-styles (and style (prefix-numeric-value style)) t)
1051 (defun calc-graph-point-style (style)
1053 (calc-graph-set-styles t (and style (prefix-numeric-value style)))
1056 (defun calc-graph-set-styles (lines points)
1059 (set-buffer calc-gnuplot-input)
1060 (or (calc-graph-find-plot nil nil)
1061 (error "No data points have been set!"))
1062 (let ((base (point))
1063 (mode nil) (lstyle nil) (pstyle nil)
1064 start end lenbl penbl)
1065 (re-search-forward "[,\n]")
1067 (setq end (point) start end)
1069 (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+with\\)")
1071 (setq start (match-beginning 1))
1072 (goto-char (match-end 0))
1073 (if (looking-at "[ \t]+\\([a-z]+\\)")
1074 (setq mode (buffer-substring (match-beginning 1)
1076 (if (looking-at "[ \ta-z]+\\([0-9]+\\)")
1077 (setq lstyle (string-to-int
1078 (buffer-substring (match-beginning 1)
1080 (if (looking-at "[ \ta-z]+[0-9]+[ \t]+\\([0-9]+\\)")
1081 (setq pstyle (string-to-int
1082 (buffer-substring (match-beginning 1)
1084 (setq lenbl (or (equal mode "lines") (equal mode "linespoints"))
1085 penbl (or (equal mode "points") (equal mode "linespoints")))
1089 lenbl (>= lines 0)))
1090 (setq lenbl (not lenbl)))
1094 penbl (>= points 0)))
1095 (setq penbl (not penbl)))
1096 (delete-region start end)
1100 (if penbl "linespoints" "lines")
1101 (if penbl "points" "dots")))
1102 (if (and pstyle (> pstyle 0))
1103 (insert " " (if (and lstyle (> lstyle 0)) (int-to-string lstyle) "1")
1104 " " (int-to-string pstyle))
1105 (if (and lstyle (> lstyle 0))
1106 (insert " " (int-to-string lstyle))))))
1107 (calc-graph-view-commands)
1110 (defun calc-graph-zero-x (flag)
1112 (calc-graph-set-command "noxzeroaxis"
1114 (<= (prefix-numeric-value flag) 0)
1115 (not (calc-graph-find-command "noxzeroaxis")))
1119 (defun calc-graph-zero-y (flag)
1121 (calc-graph-set-command "noyzeroaxis"
1123 (<= (prefix-numeric-value flag) 0)
1124 (not (calc-graph-find-command "noyzeroaxis")))
1128 (defun calc-graph-name (name)
1129 (interactive "sTitle for current curve: ")
1132 (set-buffer calc-gnuplot-input)
1133 (or (calc-graph-find-plot nil nil)
1134 (error "No data points have been set!"))
1135 (let ((base (point))
1137 (re-search-forward "[,\n]\\|[ \t]+with")
1138 (setq end (match-beginning 0))
1140 (if (looking-at "[^,\n]*[^,\n \t]\\([ \t]+title\\)")
1142 (goto-char (match-beginning 1))
1143 (delete-region (point) end))
1145 (insert " title " (prin1-to-string name))))
1146 (calc-graph-view-commands)
1149 (defun calc-graph-hide (flag)
1152 (and (calc-graph-find-plot nil nil)
1154 (or (looking-at "{")
1155 (error "Can't hide this curve (wrong format)"))
1157 (if (looking-at "*")
1158 (if (or (null flag) (<= (prefix-numeric-value flag) 0))
1160 (if (or (null flag) (> (prefix-numeric-value flag) 0))
1164 (defun calc-graph-header (title)
1165 (interactive "sTitle for entire graph: ")
1166 (calc-graph-set-command "title" (if (not (equal title ""))
1167 (prin1-to-string title)))
1170 (defun calc-graph-border (flag)
1172 (calc-graph-set-command "noborder"
1174 (<= (prefix-numeric-value flag) 0)
1175 (not (calc-graph-find-command "noborder")))
1179 (defun calc-graph-grid (flag)
1181 (calc-graph-set-command "grid" (and (if flag
1182 (> (prefix-numeric-value flag) 0)
1183 (not (calc-graph-find-command "grid")))
1187 (defun calc-graph-key (flag)
1189 (calc-graph-set-command "key" (and (if flag
1190 (> (prefix-numeric-value flag) 0)
1191 (not (calc-graph-find-command "key")))
1195 (defun calc-graph-num-points (res flag)
1196 (interactive "sNumber of data points: \nP")
1198 (if (> (prefix-numeric-value flag) 0)
1200 (message "Default resolution is %d."
1201 calc-graph-default-resolution)
1202 (setq calc-graph-default-resolution (string-to-int res)))
1204 (message "Default 3D resolution is %d."
1205 calc-graph-default-resolution-3d)
1206 (setq calc-graph-default-resolution-3d (string-to-int res))))
1207 (calc-graph-set-command "samples" (if (not (equal res "")) res)))
1210 (defun calc-graph-device (name flag)
1211 (interactive "sDevice name: \nP")
1212 (if (equal name "?")
1214 (calc-gnuplot-command "set terminal")
1215 (calc-graph-view-trail))
1217 (if (> (prefix-numeric-value flag) 0)
1219 (message "Default GNUPLOT device is \"%s\"."
1220 calc-gnuplot-default-device)
1221 (setq calc-gnuplot-default-device name))
1223 (message "GNUPLOT device for Print command is \"%s\"."
1224 calc-gnuplot-print-device)
1225 (setq calc-gnuplot-print-device name)))
1226 (calc-graph-set-command "terminal" (if (not (equal name ""))
1230 (defun calc-graph-output (name flag)
1231 (interactive "FOutput file name: \np")
1232 (cond ((string-match "\\<[aA][uU][tT][oO]$" name)
1234 ((string-match "\\<[tT][tT][yY]$" name)
1236 ((string-match "\\<[sS][tT][dD][oO][uU][tT]$" name)
1237 (setq name "STDOUT"))
1238 ((equal (file-name-nondirectory name) "")
1240 (t (setq name (expand-file-name name))))
1242 (if (> (prefix-numeric-value flag) 0)
1244 (message "Default GNUPLOT output file is \"%s\"."
1245 calc-gnuplot-default-output)
1246 (setq calc-gnuplot-default-output name))
1248 (message "GNUPLOT output file for Print command is \"%s\"."
1249 calc-gnuplot-print-output)
1250 (setq calc-gnuplot-print-output name)))
1251 (calc-graph-set-command "output" (if (not (equal name ""))
1252 (prin1-to-string name))))
1255 (defun calc-graph-display (name)
1256 (interactive "sX display name: ")
1258 (message "Current X display is \"%s\"."
1259 (or calc-gnuplot-display "<none>"))
1260 (setq calc-gnuplot-display name)
1261 (if (calc-gnuplot-alive)
1262 (calc-gnuplot-command "exit")))
1265 (defun calc-graph-geometry (name)
1266 (interactive "sX geometry spec (or \"default\"): ")
1268 (message "Current X geometry is \"%s\"."
1269 (or calc-gnuplot-geometry "default"))
1270 (setq calc-gnuplot-geometry (and (not (equal name "default")) name))
1271 (if (calc-gnuplot-alive)
1272 (calc-gnuplot-command "exit")))
1275 (defun calc-graph-find-command (cmd)
1278 (set-buffer calc-gnuplot-input)
1279 (goto-char (point-min))
1280 (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t]*\\(.*\\)$") nil t)
1281 (buffer-substring (match-beginning 1) (match-end 1))))
1284 (defun calc-graph-set-command (cmd &rest args)
1287 (set-buffer calc-gnuplot-input)
1288 (goto-char (point-min))
1289 (if (re-search-forward (concat "^set[ \t]+" cmd "[ \t\n]") nil t)
1293 (let ((end (point)))
1295 (delete-region (point) (1+ end))))
1296 (if (calc-graph-find-plot t t)
1297 (if (eq (preceding-char) ?\n)
1299 (goto-char (1- (point-max)))))
1300 (if (and args (car args))
1304 (insert "set " (mapconcat 'identity (cons cmd args) " ") "\n"))))
1305 (calc-graph-view-commands)
1308 (defun calc-graph-command (cmd)
1309 (interactive "sGNUPLOT command: ")
1312 (calc-graph-view-trail)
1313 (calc-gnuplot-command cmd)
1314 (accept-process-output)
1315 (calc-graph-view-trail))
1318 (defun calc-graph-kill (&optional no-view)
1320 (calc-graph-delete-temps)
1321 (if (calc-gnuplot-alive)
1323 (or no-view (calc-graph-view-trail))
1324 (let ((calc-graph-no-wait t))
1325 (calc-gnuplot-command "exit"))
1327 (if (process-status calc-gnuplot-process)
1328 (delete-process calc-gnuplot-process))
1329 (setq calc-gnuplot-process nil)))
1332 (defun calc-graph-quit ()
1334 (if (get-buffer-window calc-gnuplot-input)
1335 (calc-graph-view-commands t))
1336 (if (get-buffer-window calc-gnuplot-buffer)
1337 (calc-graph-view-trail t))
1341 (defun calc-graph-view-commands (&optional no-need)
1343 (or calc-graph-no-auto-view (calc-graph-init-buffers))
1344 (calc-graph-view calc-gnuplot-input calc-gnuplot-buffer (null no-need))
1347 (defun calc-graph-view-trail (&optional no-need)
1349 (or calc-graph-no-auto-view (calc-graph-init-buffers))
1350 (calc-graph-view calc-gnuplot-buffer calc-gnuplot-input (null no-need))
1353 (defun calc-graph-view (buf other-buf need)
1355 (or calc-graph-no-auto-view
1356 (if (setq win (get-buffer-window buf))
1358 (and (eq buf calc-gnuplot-buffer)
1361 (not (pos-visible-in-window-p (point-max) win))))
1364 (bury-buffer other-buf)
1365 (let ((curwin (selected-window)))
1367 (switch-to-buffer nil)
1368 (select-window curwin))))
1369 (if (setq win (get-buffer-window other-buf))
1370 (set-window-buffer win buf)
1371 (if (eq major-mode 'calc-mode)
1373 (< (window-height) (1- (frame-height))))
1374 (display-buffer buf))
1375 (switch-to-buffer buf)))))
1378 (if (and (eq buf calc-gnuplot-buffer)
1379 (setq win (get-buffer-window buf))
1380 (not (pos-visible-in-window-p (point-max) win)))
1382 (goto-char (point-max))
1383 (vertical-motion (- 6 (window-height win)))
1384 (set-window-start win (point))
1385 (goto-char (point-max)))))
1386 (or calc-graph-no-auto-view (sit-for 0)))
1388 (setq calc-graph-no-auto-view nil)
1390 (defun calc-gnuplot-check-for-errors ()
1394 (set-buffer calc-gnuplot-buffer)
1395 (goto-char calc-gnuplot-last-error-pos))
1396 (re-search-forward "^[ \t]+\\^$" nil t)
1397 (goto-char (point-max))
1398 (setq calc-gnuplot-last-error-pos (point-max))))
1399 (calc-graph-view-trail))
1402 (defun calc-gnuplot-command (&rest args)
1404 (let ((cmd (concat (mapconcat 'identity args " ") "\n")))
1405 (accept-process-output)
1407 (set-buffer calc-gnuplot-buffer)
1408 (calc-gnuplot-check-for-errors)
1409 (goto-char (point-max))
1410 (setq calc-gnuplot-trail-mark (point))
1411 (or (>= calc-gnuplot-version 3)
1413 (set-marker (process-mark calc-gnuplot-process) (point))
1414 (process-send-string calc-gnuplot-process cmd)
1415 (if (get-buffer-window calc-gnuplot-buffer)
1416 (calc-graph-view-trail))
1417 (accept-process-output (and (not calc-graph-no-wait)
1418 calc-gnuplot-process))
1419 (calc-gnuplot-check-for-errors)
1420 (if (get-buffer-window calc-gnuplot-buffer)
1421 (calc-graph-view-trail))))
1423 (setq calc-graph-no-wait nil)
1425 (defun calc-graph-init-buffers ()
1426 (or (and calc-gnuplot-buffer
1427 (buffer-name calc-gnuplot-buffer))
1428 (setq calc-gnuplot-buffer (get-buffer-create "*Gnuplot Trail*")))
1429 (or (and calc-gnuplot-input
1430 (buffer-name calc-gnuplot-input))
1431 (setq calc-gnuplot-input (get-buffer-create "*Gnuplot Commands*")))
1434 (defun calc-graph-init ()
1435 (or (calc-gnuplot-alive)
1436 (let ((process-connection-type t)
1438 (if calc-gnuplot-process
1440 (delete-process calc-gnuplot-process)
1441 (setq calc-gnuplot-process nil)))
1442 (calc-graph-init-buffers)
1444 (set-buffer calc-gnuplot-buffer)
1445 (insert "\nStarting gnuplot...\n")
1446 (setq origin (point)))
1447 (setq calc-graph-last-device nil)
1448 (setq calc-graph-last-output nil)
1450 (let ((args (append (and calc-gnuplot-display
1451 (not (equal calc-gnuplot-display
1452 (getenv "DISPLAY")))
1454 calc-gnuplot-display))
1455 (and calc-gnuplot-geometry
1457 calc-gnuplot-geometry)))))
1458 (setq calc-gnuplot-process
1459 (apply 'start-process
1464 (process-kill-without-query calc-gnuplot-process))
1466 (error "Sorry, can't find \"%s\" on your system."
1467 calc-gnuplot-name)))
1469 (set-buffer calc-gnuplot-buffer)
1470 (while (and (not (save-excursion
1472 (search-forward "gnuplot> " nil t)))
1473 (memq (process-status calc-gnuplot-process) '(run stop)))
1474 (accept-process-output calc-gnuplot-process))
1475 (or (memq (process-status calc-gnuplot-process) '(run stop))
1476 (error "Unable to start GNUPLOT process."))
1480 "G N U P L O T.*\n.*version \\([0-9]+\\)\\." nil t))
1481 (setq calc-gnuplot-version (string-to-int (buffer-substring
1484 (setq calc-gnuplot-version 1))
1485 (goto-char (point-max)))))
1487 (set-buffer calc-gnuplot-input)
1488 (if (= (buffer-size) 0)
1489 (insert "# Commands for running gnuplot\n\n\n")
1490 (or calc-graph-no-auto-view
1491 (eq (char-after (1- (point-max))) ?\n)
1493 (goto-char (point-max))