Fixes bug #3648 "plot output depends on *read-default-float-format*"
[maxima.git] / src / plot.lisp
blob50f533ab04f9de65465979b0673ef0076b1b546b
1 ;;Copyright William F. Schelter 1990, All Rights Reserved
3 (in-package :maxima)
5 #|
6 Examples
8 /* plot of z^(1/3)...*/
9 plot3d(r^.33*cos(th/3),[r,0,1],[th,0,6*%pi],['grid,12,80],['transform_xy,polar_to_xy],['plot_format,geomview]);
11 /* plot of z^(1/2)...*/
12 plot3d(r^.5*cos(th/2),[r,0,1],[th,0,6*%pi],['grid,12,80],['transform_xy,polar_to_xy],['plot_format,xmaxima]);
14 /* moebius */
15 plot3d([cos(x)*(3+y*cos(x/2)),sin(x)*(3+y*cos(x/2)),y*sin(x/2)],[x,-%pi,%pi],[y,-1,1],['grid,50,15]);
17 /* klein bottle */
18 plot3d([5*cos(x)*(cos(x/2)*cos(y)+sin(x/2)*sin(2*y)+3.0) - 10.0,
19 -5*sin(x)*(cos(x/2)*cos(y)+sin(x/2)*sin(2*y)+3.0),
20 5*(-sin(x/2)*cos(y)+cos(x/2)*sin(2*y))],[x,-%pi,%pi],[y,-%pi,%pi],
21 ['grid,40,40]);
22 /* torus */
23 plot3d([cos(y)*(10.0+6*cos(x)), sin(y)*(10.0+6*cos(x)),-6*sin(x)],
24 [x,0,2*%pi],[y,0,2*%pi],['grid,40,40]);
27 (defclass gnuplot-plot ()
28 ((data :initarg :data :initform "")
29 (pipe :initarg :pipe :initform nil)))
31 (defclass xmaxima-plot ()
32 ((data :initarg :data :initform "")
33 (pipe :initarg :pipe :initform nil)))
35 (defclass geomview-plot ()
36 ((data :initarg :data :initform "")
37 (pipe :initarg :pipe :initform nil)))
39 (defgeneric plot-preamble (plot options)
40 (:documentation "Plots the preamble for a plot."))
42 (defgeneric plot2d-command (plot fun options range)
43 (:documentation "Writes the command that creates a plot."))
45 (defgeneric plot3d-command (plot functions options titles)
46 (:documentation "Writes the command that creates a plot."))
48 (defgeneric plot-shipout (plot options &optional output-file)
49 (:documentation "Sends the plot commands to the graphic program."))
51 (defun ensure-string (x)
52 (cond
53 ((stringp x) x)
54 ((symbolp x) (print-invert-case (stripdollar x)))
55 (t (maybe-invert-string-case (string (implode (strgrind x)))))))
57 (defmfun $join (x y)
58 (if (and ($listp x) ($listp y))
59 (cons '(mlist) (loop for w in (cdr x) for u in (cdr y) collect w collect u))
60 (merror (intl:gettext "join: both arguments must be lists."))))
62 (defun coerce-float (x) ($float (meval* x)))
64 (defvar *maxima-plotdir* "")
65 (declare-top (special *maxima-tempdir* *maxima-prefix*))
67 ;; *ROT* AND FRIENDS ($ROT, $ROTATE_PTS, $ROTATE_LIST) CAN PROBABLY GO AWAY !!
68 ;; THEY ARE UNDOCUMENTED AND UNUSED !!
69 (defvar *rot* (make-array 9 :element-type 'flonum))
70 (defvar $rot nil)
72 ;; Global plot options list; this is a property list.. It is not a
73 ;; Maxima variable, to discourage users from changing it directly; it
74 ;; should be changed via set_plot_option
76 (defvar *plot-options*
77 `(:plot_format
78 ,(if (string= *autoconf-windows* "true")
79 '$gnuplot
80 '$gnuplot_pipes)
81 :grid (30 30) :run_viewer t :axes t
82 ;; With adaptive plotting, 29 nticks should be enough; adapt_depth
83 ;; controls the number of splittings adaptive-plotting will do.
84 :nticks 29 :adapt_depth 5
85 :color ($blue $red $green $magenta $black $cyan)
86 :point_type ($bullet $box $triangle $plus $times $asterisk)
87 :palette (((mlist) $gradient $green $cyan $blue $violet)
88 ((mlist) $gradient $magenta $violet $blue $cyan $green $yellow
89 $orange $red $brown $black))
90 :gnuplot_preamble "" :gnuplot_term $default))
92 (defvar $plot_options
93 `((mlist)
94 ((mlist) $plot_format
95 ,(if (string= *autoconf-windows* "true")
96 '$gnuplot
97 '$gnuplot_pipes))))
99 ;; $plot_realpart option is false by default but *plot-realpart* is true
100 ;; because coerce-float-fun is used outside of plot package too.
101 (defvar *plot-realpart* t)
103 (defun maybe-realpart (x)
104 (if *plot-realpart*
105 ($realpart x)
106 (if (zerop1 ($imagpart x))
107 ($realpart x)
108 nil)))
110 (defvar *missing-data-indicator* "NaN")
112 (defvar *gnuplot-stream* nil)
113 (defvar *gnuplot-command* "")
115 (defvar $gnuplot_command "gnuplot")
117 (defun start-gnuplot-process (path)
118 ;; TODO: Forward gnuplot's stderr stream to maxima's stderr output
119 #+clisp (setq *gnuplot-stream* (ext:make-pipe-output-stream path))
120 ;; TODO: Forward gnuplot's stderr stream to maxima's stderr output
121 #+lispworks (setq *gnuplot-stream* (system:open-pipe path))
122 #+cmu (setq *gnuplot-stream*
123 (ext:process-input (ext:run-program path nil :input :stream
124 :output *error-output* :wait nil)))
125 #+scl (setq *gnuplot-stream*
126 (ext:process-input (ext:run-program path nil :input :stream
127 :output *error-output* :wait nil)))
128 #+sbcl (setq *gnuplot-stream*
129 (sb-ext:process-input (sb-ext:run-program path nil
130 :input :stream
131 :output *error-output* :wait nil
132 :search t)))
133 #+gcl (setq *gnuplot-stream*
134 (open (concatenate 'string "| " path) :direction :output))
135 #+ecl (progn
136 (setq *gnuplot-stream* (ext:run-program path nil :input :stream :output *error-output* :error :output :wait nil)))
137 #+ccl (setf *gnuplot-stream*
138 (ccl:external-process-input-stream
139 (ccl:run-program path nil
140 :wait nil :output *error-output*
141 :input :stream)))
142 #+allegro (setf *gnuplot-stream* (excl:run-shell-command
143 path :input :stream :output *error-output* :wait nil))
144 #+abcl (setq *gnuplot-stream* (system::process-input (system::run-program path nil :wait nil)))
145 #-(or clisp cmu sbcl gcl scl lispworks ecl ccl allegro abcl)
146 (merror (intl:gettext "plotting: I don't know how to tell this Lisp to run Gnuplot."))
148 (if (null *gnuplot-stream*)
149 (merror (intl:gettext "plotting: I tried to execute ~s but *GNUPLOT-STREAM* is still null.~%") path))
151 ;; set mouse must be the first command send to gnuplot
152 (send-gnuplot-command "set mouse"))
154 (defun check-gnuplot-process ()
155 (if (null *gnuplot-stream*)
156 (start-gnuplot-process $gnuplot_command)))
158 (defmfun $gnuplot_close ()
159 (stop-gnuplot-process)
162 (defmfun $gnuplot_start ()
163 (check-gnuplot-process)
166 (defmfun $gnuplot_restart ()
167 ($gnuplot_close)
168 ($gnuplot_start))
170 (defun stop-gnuplot-process ()
171 (unless (null *gnuplot-stream*)
172 (progn
173 (close *gnuplot-stream*)
174 (setq *gnuplot-stream* nil))))
176 (defun send-gnuplot-command (command &optional recursive)
177 (if (null *gnuplot-stream*)
178 (start-gnuplot-process $gnuplot_command))
179 (handler-case (unless (null command)
180 (format *gnuplot-stream* "~a ~%" command)
181 (finish-output *gnuplot-stream*))
182 (error (e)
183 ;; allow gnuplot to restart if stream-error, or just an error is signaled
184 ;; only try to restart once, to prevent an infinite loop
185 (cond (recursive
186 (error e))
188 (warn "~a~%Trying new stream.~%" e)
189 (setq *gnuplot-stream* nil)
190 (send-gnuplot-command command t))))))
192 (defmfun $gnuplot_reset ()
193 (send-gnuplot-command "unset output")
194 (send-gnuplot-command "reset"))
196 (defmfun $gnuplot_replot (&optional s)
197 (if (null *gnuplot-stream*)
198 (merror (intl:gettext "gnuplot_replot: Gnuplot is not running.")))
199 (cond ((null s)
200 (send-gnuplot-command "replot"))
201 ((stringp s)
202 (send-gnuplot-command s)
203 (send-gnuplot-command "replot"))
205 (merror (intl:gettext "gnuplot_replot: argument, if present, must be a string; found: ~M") s)))
208 ;; allow this to be set in a system init file (sys-init.lsp)
210 (defmfun $get_plot_option (&optional name n)
211 (let (options)
212 ;; Converts the options property list into a Maxima list
213 (do* ((list (copy-tree *plot-options*) (cddr list))
214 (key (first list) (first list))
215 (value (second list) (second list)))
216 ((endp list))
217 (let ((max-key (intern (concatenate 'string "$" (symbol-name key)))))
218 (if (consp value)
219 (push (cons '(mlist) (cons max-key value)) options)
220 (push (list '(mlist) max-key value) options))))
221 (setf options (cons '(mlist) (nreverse options)))
222 (if name
223 (let ((value (find name (cdr options) :key #'second)))
224 (if n
225 (nth n value)
226 value))
227 options)))
229 (defun quote-strings (opt)
230 (if (atom opt)
231 (if (stringp opt)
232 (format nil "~s" opt)
233 opt)
234 (cons (quote-strings (car opt))
235 (quote-strings (cdr opt)))))
237 (defun get-plot-option-string (option &optional (index 1))
238 (let* ((val ($get_plot_option option 2))
239 (val-list (if ($listp val)
240 (cdr val)
241 `(,val))))
242 (ensure-string (nth (mod (- index 1) (length val-list)) val-list))))
244 (defmfun $set_plot_option (&rest value)
245 (setq *plot-options* (plot-options-parser value *plot-options*))
246 ($get_plot_option))
248 (defmfun $remove_plot_option (name)
249 (remf *plot-options*
250 (case name
251 ($adapt_depth :adapt_depth) ($axes :axes) ($azimuth :azimuth)
252 ($box :box) ($color :color) ($color_bar :color_bar)
253 ($color_bar_tics :color_bar_tics) ($elevation :elevation)
254 ($grid :grid) ($grid2d :grid2d) ($iterations :iterations)
255 ($label :label) ($legend :legend) ($levels :levels)
256 ($logx :logx) ($logy :logy)
257 ($mesh_lines_color :mesh_lines_color) ($nticks :nticks)
258 ($palette :palette) ($plotepsilon :plotepsilon)
259 ($plot_format :plot_format) ($plot_realpart :plot_realpart)
260 ($point_type :point_type) ($pdf_file :pdf_file)
261 ($png_file :png_file) ($ps_file :ps_file)
262 ($run_viewer :run_viewer) ($same_xy :samexy)
263 ($same_xyz :same_xyz) ($sample :sample) ($style :style)
264 ($svg_file :svg_file) ($t :t) ($title :title)
265 ($transform_xy :transform_xy) ($x :x) ($xbounds :xbounds)
266 ($xlabel :xlabel) ($xtics :xtics) ($xy_scale :xy_scale)
267 ($y :y) ($ybounds :ybounds) ($ylabel :ylabel) ($ytics :ytics)
268 ($yx_ratio :yx_ratio) ($z :z) ($zlabel :zlabel) ($zmin :zmin)
269 ($ztics :ztics)
270 ($gnuplot_4_0 :gnuplot_4_0)
271 ($gnuplot_curve_titles :gnuplot_curve_titles)
272 ($gnuplot_curve_styles :gnuplot_curve_styles)
273 ($gnuplot_default_term_command :gnuplot_default_term_command)
274 ($gnuplot_dumb_term_command :gnuplot_dumb_term_command)
275 ($gnuplot_out_file :gnuplot_out_file)
276 ($gnuplot_pm3d :gnuplot_pm3d)
277 ($gnuplot_strings :gnuplot_strings)
278 ($gnuplot_preamble :gnuplot_preamble)
279 ($gnuplot_postamble :gnuplot_postamble)
280 ($gnuplot_pdf_term_command :gnuplot_pdf_term_command)
281 ($gnuplot_png_term_command :gnuplot_png_term_command)
282 ($gnuplot_ps_term_command :gnuplot_ps_term_command)
283 ($gnuplot_svg_term_command :gnuplot_svg_term_command)
284 ($gnuplot_term :gnuplot_term))))
286 (defun get-gnuplot-term (term)
287 (let* ((sterm (string-downcase (ensure-string term)))
288 (pos (search " " sterm)))
289 (if pos
290 (subseq sterm 0 pos)
291 sterm)))
293 (defvar $pstream nil)
295 (defun print-pt1 (f str)
296 (if (floatp f)
297 (format str "~,8,,,,,'eg " f)
298 (format str "~a " *missing-data-indicator*)))
300 (defstruct (polygon (:type list)
301 (:constructor %make-polygon (pts edges)))
302 (dummy '($polygon simp))
303 pts edges)
305 (eval-when
306 #+gcl (compile eval)
307 #-gcl (:compile-toplevel :execute)
309 (defmacro z-pt (ar i) `(aref ,ar (the fixnum (+ 2 (* ,i 3)))))
310 (defmacro y-pt (ar i) `(aref ,ar (the fixnum (1+ (* ,i 3)))))
311 (defmacro x-pt (ar i) `(aref ,ar (the fixnum (* ,i 3))))
312 (defmacro rot (m i j) `(aref ,m (the fixnum (+ ,i (the fixnum (* 3 ,j))))))
314 (defmacro print-pt (f)
315 `(print-pt1 ,f $pstream ))
317 (defmacro make-polygon (a b)
318 `(list '($polygon) ,a ,b)))
320 (defun draw3d (f minx maxx miny maxy nxint nyint)
321 (let* ((epsx (/ (- maxx minx) nxint))
322 (x 0.0) ( y 0.0)
323 (epsy (/ (- maxy miny) nyint))
324 (nx (+ nxint 1))
325 (l 0)
326 (ny (+ nyint 1))
327 (ar (make-array (+ 12 ; 12 for axes
328 (* 3 nx ny)) :fill-pointer (* 3 nx ny)
329 :element-type t :adjustable t)))
330 (declare (type flonum x y epsy epsx)
331 (fixnum nx ny l)
332 (type (cl:array t) ar))
333 (loop for j below ny
334 initially (setq y miny)
335 do (setq x minx)
336 (loop for i below nx
338 (setf (x-pt ar l) x)
339 (setf (y-pt ar l) y)
340 (setf (z-pt ar l) (funcall f x y))
341 (incf l)
342 (setq x (+ x epsx))
344 (setq y (+ y epsy)))
345 (make-polygon ar (make-grid-vertices nxint nyint))))
347 ;; The following is 3x2 = 6 rectangles
348 ;; call (make-vertices 3 2)
349 ;; there are 4x3 = 12 points.
350 ;; ordering is x0,y0,z0,x1,y1,z1,....,x11,y11,z11
351 ;; ----
352 ;; ||||
353 ;; ----
354 ;; ||||
355 ;; ----
357 (defun make-grid-vertices (nx ny)
358 (declare (fixnum nx ny))
359 (let* ((tem (make-array (+ 15 (* 5 nx ny)) :fill-pointer (* 5 nx ny)
360 :adjustable t
361 :element-type '(mod #x80000000)))
362 (m nx )
363 (nxpt (+ nx 1))
364 (i 0)
366 (declare (fixnum i nxpt m)
367 (type (cl:array (mod #x80000000)) tem))
368 (loop for k below (length tem)
370 (setf (aref tem k) i)
371 (setf (aref tem (incf k))
372 (+ nxpt i))
373 (setf (aref tem (incf k))
374 (+ nxpt (incf i )))
375 (setf (aref tem (incf k)) i)
376 (setf (aref tem (incf k)) 0) ;place for max
377 (setq m (- m 1))
378 (cond ((eql m 0)
379 (setq m nx)
380 (setq i (+ i 1))))
382 tem))
384 (defmfun $rotation1 (phi th)
385 (let ((sinph (sin phi))
386 (cosph (cos phi))
387 (sinth (sin th))
388 (costh (cos th)))
389 `(($matrix simp)
390 ((mlist simp) ,(* cosph costh)
391 ,(* -1.0 cosph sinth)
392 ,sinph)
393 ((mlist simp) ,sinth ,costh 0.0)
394 ((mlist simp) ,(- (* sinph costh))
395 ,(* sinph sinth)
396 ,cosph))))
398 ;; pts is a vector of bts [x0,y0,z0,x1,y1,z1,...] and each tuple xi,yi,zi is rotated
399 #-abcl (defmfun $rotate_pts(pts rotation-matrix)
400 (or ($matrixp rotation-matrix) (merror (intl:gettext "rotate_pts: second argument must be a matrix.")))
401 (let* ((rot *rot*)
402 (l (length pts))
403 (x 0.0) (y 0.0) (z 0.0)
405 (declare (type flonum x y z))
406 (declare (type (cl:array flonum) rot))
407 ($copy_pts rotation-matrix *rot* 0)
409 (loop with j = 0
410 while (< j l)
412 (setq x (aref pts j))
413 (setq y (aref pts (+ j 1)))
414 (setq z (aref pts (+ j 2)))
415 (loop for i below 3 with a of-type flonum = 0.0
417 (setq a (* x (aref rot (+ (* 3 i) 0))))
418 (setq a (+ a (* y (aref rot (+ (* 3 i) 1)))))
419 (setq a (+ a (* z (aref rot (+ (* 3 i) 2)))))
420 (setf (aref pts (+ j i )) a))
421 (setf j (+ j 3)))))
423 (defmfun $rotate_list (x)
424 (cond ((and ($listp x) (not (mbagp (second x))))
425 ($list_matrix_entries (ncmul2 $rot x)))
426 ((mbagp x) (cons (car x) (mapcar '$rotate_list (cdr x))))))
428 (defmfun $get_range (pts k &aux (z 0.0) (max most-negative-flonum) (min most-positive-flonum))
429 (declare (type flonum z max min))
430 (declare (type (vector flonum) pts))
431 (loop for i from k below (length pts) by 3
432 do (setq z (aref pts i))
433 (cond ((< z min) (setq min z)))
434 (cond ((> z max) (setq max z))))
435 (list min max (- max min)))
437 (defmfun $polar_to_xy (pts &aux (r 0.0) (th 0.0))
438 (declare (type flonum r th))
439 (declare (type (cl:array t) pts))
440 (assert (typep pts '(vector t)))
441 (loop for i below (length pts) by 3
442 do (setq r (aref pts i))
443 (setq th (aref pts (+ i 1)))
444 (setf (aref pts i) (* r (cos th)))
445 (setf (aref pts (+ i 1)) (* r (sin th)))))
447 ;; Transformation from spherical coordinates to rectangular coordinates,
448 ;; to be used in plot3d. Example of its use:
449 ;; plot3d (expr, [th, 0, %pi], [ph, 0, 2*%pi], [transform_xy, spherical_to_xyz])
450 ;; where expr gives the value of r in terms of the inclination (th)
451 ;; and azimuth (ph).
453 (defmfun $spherical_to_xyz (pts &aux (r 0.0) (th 0.0) (ph 0.0))
454 (declare (type flonum r th ph))
455 (declare (type (cl:array t) pts))
456 (assert (typep pts '(vector t)))
457 (loop for i below (length pts) by 3
458 do (setq th (aref pts i))
459 (setq ph (aref pts (+ i 1)))
460 (setq r (aref pts (+ i 2)))
461 (setf (aref pts i) (* r (sin th) (cos ph)))
462 (setf (aref pts (+ i 1)) (* r (sin th) (sin ph)))
463 (setf (aref pts (+ i 2)) (* r (cos th)))))
466 ;; return a function suitable for the transform function in plot3d.
467 ;; FX, FY, and FZ are functions of three arguments.
468 (defmfun $make_transform (lvars fx fy fz)
469 (setq fx (coerce-float-fun fx lvars))
470 (setq fy (coerce-float-fun fy lvars))
471 (setq fz (coerce-float-fun fz lvars))
472 (let ((sym (gensym "transform")))
473 (setf (symbol-function sym)
474 #'(lambda (pts &aux (x1 0.0)(x2 0.0)(x3 0.0))
475 (declare (type flonum x1 x2 x3))
476 (declare (type (cl:array t) pts))
477 (loop for i below (length pts) by 3
479 (setq x1 (aref pts i))
480 (setq x2 (aref pts (+ i 1)))
481 (setq x3 (aref pts (+ i 2)))
482 (setf (aref pts i) (funcall fx x1 x2 x3))
483 (setf (aref pts (+ 1 i)) (funcall fy x1 x2 x3))
484 (setf (aref pts (+ 2 i)) (funcall fz x1 x2 x3)))))))
486 ;; Return value is a Lisp function which evaluates EXPR to a float.
487 ;; COERCE-FLOAT-FUN always returns a function and never returns a symbol,
488 ;; even if EXPR is a symbol.
490 ;; Following cases are recognized:
491 ;; EXPR is a symbol
492 ;; name of a Lisp function
493 ;; name of a Maxima function
494 ;; name of a DEFMSPEC function
495 ;; name of a Maxima macro
496 ;; a string which is the name of a Maxima operator (e.g., "!")
497 ;; name of a simplifying function
498 ;; EXPR is a Maxima lambda expression
499 ;; EXPR is a general Maxima expression
501 ;; %COERCE-FLOAT-FUN is the main internal routine for this.
502 ;; COERCE-FLOAT-FUN is the user interface for creating a function that
503 ;; returns floats. COERCE-BFLOAT-FUN is the same, except bfloats are
504 ;; returned.
505 (defun %coerce-float-fun (float-fun expr &optional lvars)
506 (cond ((and (consp expr) (functionp expr))
507 (let ((args (if lvars (cdr lvars) (list (gensym)))))
508 (coerce-lisp-function-or-lisp-lambda args expr :float-fun float-fun)))
509 ;; expr is a string which names an operator
510 ;; (e.g. "!" "+" or a user-defined operator)
511 ((and (stringp expr) (getopr0 expr))
512 (let ((a (if lvars lvars `((mlist) ,(gensym)))))
513 (%coerce-float-fun float-fun `(($apply) ,(getopr0 expr) ,a) a)))
514 ((and (symbolp expr) (not (member expr lvars)) (not ($constantp expr)))
515 (cond
516 ((fboundp expr)
517 (let ((args (if lvars (cdr lvars) (list (gensym)))))
518 (coerce-lisp-function-or-lisp-lambda args expr :float-fun float-fun)))
520 ;; expr is name of a Maxima function defined by := or
521 ;; define
522 ((mget expr 'mexpr)
523 (let*
524 ((mexpr (mget expr 'mexpr))
525 (args (cdr (second mexpr))))
526 (coerce-maxima-function-or-maxima-lambda args expr :float-fun float-fun)))
528 ((or
529 ;; expr is the name of a function defined by defmspec
530 (get expr 'mfexpr*)
531 ;; expr is the name of a Maxima macro defined by ::=
532 (mget expr 'mmacro)
533 ;; expr is the name of a simplifying function, and the
534 ;; simplification property is associated with the noun
535 ;; form
536 (get ($nounify expr) 'operators)
537 ;; expr is the name of a simplifying function, and the
538 ;; simplification property is associated with the verb
539 ;; form
540 (get ($verbify expr) 'operators))
541 (let ((a (if lvars lvars `((mlist) ,(gensym)))))
542 (%coerce-float-fun float-fun `(($apply) ,expr ,a) a)))
544 (merror (intl:gettext "COERCE-FLOAT-FUN: no such Lisp or Maxima function: ~M") expr))))
546 ((and (consp expr) (eq (caar expr) 'lambda))
547 (let ((args (cdr (second expr))))
548 (coerce-maxima-function-or-maxima-lambda args expr :float-fun float-fun)))
551 (let* ((vars (or lvars ($sort ($listofvars expr))))
552 (subscripted-vars ($sublist vars '((lambda) ((mlist) $x) ((mnot) (($atom) $x)))))
553 gensym-vars save-list-gensym subscripted-vars-save
554 subscripted-vars-mset subscripted-vars-restore)
556 ;; VARS and SUBSCRIPTED-VARS are Maxima lists. Other lists are
557 ;; Lisp lists.
558 (when (cdr subscripted-vars)
559 (setq gensym-vars (mapcar #'(lambda (ign) (declare (ignore ign)) (gensym))
560 (cdr subscripted-vars)))
561 (mapcar #'(lambda (a b) (setq vars (subst b a vars :test 'equal)))
562 (cdr subscripted-vars) gensym-vars)
564 ;; This stuff about saving and restoring array variables
565 ;; should go into MBINDING, and the lambda expression
566 ;; constructed below should call MBINDING. (At present
567 ;; MBINDING barfs on array variables.)
568 (setq save-list-gensym (gensym))
569 (setq subscripted-vars-save
570 (mapcar #'(lambda (a) `(push (meval ',a) ,save-list-gensym))
571 (cdr subscripted-vars)))
572 (setq subscripted-vars-mset
573 (mapcar #'(lambda (a b) `(mset ',a ,b))
574 (cdr subscripted-vars) gensym-vars))
575 (setq subscripted-vars-restore
576 (mapcar #'(lambda (a) `(mset ',a (pop ,save-list-gensym)))
577 (reverse (cdr subscripted-vars)))))
579 (coerce
580 `(lambda ,(cdr vars)
581 (declare (special ,@(cdr vars) errorsw))
583 ;; Nothing interpolated here when there are no subscripted
584 ;; variables.
585 ,@(if save-list-gensym `((declare (special ,save-list-gensym))))
587 ;; Nothing interpolated here when there are no subscripted
588 ;; variables.
589 ,@(if (cdr subscripted-vars)
590 `((progn (setq ,save-list-gensym nil)
591 ,@(append subscripted-vars-save subscripted-vars-mset))))
593 (let (($ratprint nil)
594 ;; We don't want to set $numer to T when coercing
595 ;; to a bigfloat. By doing so, things like
596 ;; log(400)^400 get converted to double-floats,
597 ;; which causes a double-float overflow. But the
598 ;; whole point of coercing to bfloat is to use
599 ;; bfloats, not doubles.
601 ;; Perhaps we don't even need to do this for
602 ;; double-floats? It would be nice to remove
603 ;; this. For backward compatibility, we bind
604 ;; numer to T if we're not trying to bfloat.
605 ($numer ,(not (eq float-fun '$bfloat)))
606 (*nounsflag* t)
607 (errorsw t)
608 (errcatch t))
609 (declare (special errcatch))
610 ;; Catch any errors from evaluating the
611 ;; function. We're assuming that if an error
612 ;; is caught, the result is not a number. We
613 ;; also assume that for such errors, it's
614 ;; because the function is not defined there,
615 ;; not because of some other maxima error.
617 ;; GCL 2.6.2 has handler-case but not quite ANSI yet.
618 (let ((result
619 #-gcl
620 (handler-case
621 (catch 'errorsw
622 (,float-fun (maybe-realpart (meval* ',expr))))
623 ;; Should we just catch all errors here? It is
624 ;; rather nice to only catch errors we care
625 ;; about and let other errors fall through so
626 ;; that we don't pretend to do something when
627 ;; it is better to let the error through.
628 (arithmetic-error () t)
629 (maxima-$error () t))
630 #+gcl
631 (handler-case
632 (catch 'errorsw
633 (,float-fun (maybe-realpart (meval* ',expr))))
634 (cl::error () t))
637 ;; Nothing interpolated here when there are no
638 ;; subscripted variables.
639 ,@(if (cdr subscripted-vars) `((progn ,@subscripted-vars-restore)))
641 result)))
642 'function)))))
644 (defun coerce-float-fun (expr &optional lvars)
645 (%coerce-float-fun '$float expr lvars))
647 (defun coerce-bfloat-fun (expr &optional lvars)
648 (%coerce-float-fun '$bfloat expr lvars))
650 (defun coerce-maxima-function-or-maxima-lambda (args expr &key (float-fun '$float))
651 (let ((gensym-args (loop for x in args collect (gensym))))
652 (coerce
653 `(lambda ,gensym-args (declare (special ,@gensym-args))
654 (let* (($ratprint nil)
655 ($numer t)
656 (*nounsflag* t)
657 (errorsw t)
658 (errcatch t))
659 (declare (special errcatch))
660 ;; Just always try to convert the result to a float,
661 ;; which handles things like $%pi. See also BUG
662 ;; https://sourceforge.net/p/maxima/bugs/1795/
664 ;; Should we use HANDLER-CASE like we do above in
665 ;; %coerce-float-fun? Seems not necessary for what we want
666 ;; to do.
667 (catch 'errorsw
668 (,float-fun
669 (maybe-realpart (mapply ',expr (list ,@gensym-args) t))))))
670 'function)))
672 ;; Same as above, but call APPLY instead of MAPPLY.
674 (defun coerce-lisp-function-or-lisp-lambda (args expr &key (float-fun '$float))
675 (let ((gensym-args (loop for x in args collect (gensym))))
676 (coerce
677 `(lambda ,gensym-args (declare (special ,@gensym-args))
678 (let* (($ratprint nil)
679 ($numer t)
680 (*nounsflag* t)
681 (result (maybe-realpart (apply ',expr (list ,@gensym-args)))))
682 ;; Always use $float. See comment for
683 ;; coerce-maxima-function-ormaxima-lambda above.
684 (,float-fun result)))
685 'function)))
687 (defmacro zval (points verts i) `(aref ,points (+ 2 (* 3 (aref ,verts ,i)))))
689 ;;sort the edges array so that drawing the edges will happen from the back towards
690 ;; the front. The if n==4 the edges array coming in looks like
691 ;; v1 v2 v3 v4 0 w1 w2 w3 w4 0 ...
692 ;; where vi,wi are indices pointint into the points array specifying a point
693 ;; in 3 space. After the sorting is done, the 0 is filled in with the vertex
694 ;; which is closer to us (ie highest z component after rotating towards the user)
695 ;; and this is then they are sorted in groups of 5.
696 (defun sort-ngons (points edges n &aux lis )
697 (declare (type (cl:array (flonum)) points)
698 (type (cl:array (mod #x80000000)) edges)
699 (fixnum n))
700 (let ((new (make-array (length edges) :element-type (array-element-type edges)))
701 (i 0)
702 (z 0.0)
703 (z1 0.0)
704 (n1 (- n 1))
705 (at 0)
706 (leng (length edges))
708 (declare (type (cl:array (mod #x80000000)) new)
709 (fixnum i leng n1 at )
711 (declare (type flonum z z1))
713 (setq lis
714 (loop for i0 below leng by (+ n 1)
716 (setq i i0)
717 (setq at 0)
718 (setq z (zval points edges i))
719 (setq i (+ i 1))
720 (loop for j below n1
721 do (if (> (setq z1 (zval points edges i)) z)
722 (setq z z1 at (aref edges i) ))
723 (setq i (+ i 1))
725 (setf (aref edges i) at)
726 collect (cons z i0)))
727 (setq lis (sort lis #'alphalessp :key #'car))
728 (setq i 0)
729 (loop for v in lis
731 (loop for j from (cdr v)
732 for k to n
733 do (setf (aref new i) (aref edges j))
734 (incf i))
736 (copy-array-portion edges new 0 0 (length edges))
739 (defun copy-array-portion (ar1 ar2 i1 i2 n1)
740 (declare (fixnum i1 i2 n1))
741 (loop while (>= (setq n1 (- n1 1)) 0)
742 do (setf (aref ar1 i1) (aref ar2 i2))
743 (setq i1 (+ i1 1))
744 (setq i2 (+ i2 1))))
747 (defmfun $concat_polygons (pl1 pl2 &aux tem new)
748 (setq new
749 (loop for v in pl1
750 for w in pl2
751 for l = (+ (length v) (length w))
752 do (setq tem (make-array l
753 :element-type (array-element-type v)
754 :fill-pointer l
757 collect tem))
758 (setq new (make-polygon (first new) (second new)) )
760 (copy-array-portion (polygon-pts pl1) (polygon-pts new)
761 0 0 (length (polygon-pts pl1)))
762 (copy-array-portion (polygon-pts pl2) (polygon-pts new)
763 (length (polygon-pts pl1))
764 0 (length (polygon-pts pl2)))
765 (copy-array-portion (polygon-edges pl1) (polygon-edges new)
766 0 0 (length (polygon-edges pl1)))
767 (loop for i from (length (polygon-edges pl1))
768 for j from 0 below (length (polygon-edges pl2))
769 with lpts1 = (length (polygon-pts pl1))
770 with ar2 = (polygon-edges pl2)
771 with arnew = (polygon-edges new)
772 do (setf (aref arnew i) (+ lpts1 (aref ar2 j)))))
774 (defmfun $copy_pts(lis vec start)
775 (declare (fixnum start))
776 (let ((tem vec))
777 (declare (type (cl:array flonum) tem))
778 (cond ((numberp lis)
779 (or (typep lis 'flonum) (setq lis (float lis)))
780 (setf (aref tem start) lis)
781 (1+ start))
782 ((typep lis 'cons)
783 ($copy_pts (cdr lis) vec ($copy_pts (car lis) vec start)))
784 ((symbolp lis) start)
785 (t (merror (intl:gettext "copy_pts: unrecognized first argument: ~M") lis)))))
787 ;; Implicit expressions of two variables, for instance, x and y,
788 ;; where expr is of the form f(x,y) = g(x,y).
789 ;; The result is a series of separated line segments.
791 (defun draw2d-implicit (expr options)
792 (let* ((xmin (first (getf options :x)))
793 (ymin (first (getf options :y)))
794 (xmax (second (getf options :x)))
795 (ymax (second (getf options :y)))
796 (gridx (or (first (getf options :sample)) 50))
797 (gridy (or (second (getf options :sample)) 50))
798 (eps (or (getf options :plotepsilon) 1e-6))
799 (f (make-array `(,(1+ gridx) ,(1+ gridy))))
800 vx vy dx dy fun faux fmax fmin levels values result results)
801 (setq dx (/ (- xmax xmin) gridx) dy (/ (- ymax ymin) gridy))
802 (setq vx (getf options :xvar) vy (getf options :yvar))
803 (if (getf options :contour)
804 (setq fun expr)
805 (setq fun (m- ($lhs expr) ($rhs expr))))
806 (setq fun (coerce-float-fun fun `((mlist) ,vx ,vy)))
807 ;; sets up array f with values of the function at corners of sample grid.
808 ;; finds maximum and minimum values in that array.
809 (dotimes (i (1+ gridx))
810 (dotimes (j (1+ gridy))
811 (setq faux (funcall fun (+ xmin (* i dx)) (+ ymin (* j dy))))
812 (setf (aref f i j) faux)
813 (when (and (numberp faux) (plusp i) (plusp j) (< i gridx) (< j gridy))
814 (if (numberp fmin)
815 (if (numberp fmax)
816 (progn
817 (when (< faux fmin) (setq fmin faux))
818 (when (> faux fmax) (setq fmax faux)))
819 (if (< faux fmin)
820 (setq fmax fmin fmin faux)
821 (setq fmax faux)))
822 (if (numberp fmax)
823 (if (> faux fmax)
824 (setq fmin fmax fmax faux)
825 (setq fmin faux))
826 (setq fmin faux))))))
827 ;; checks that the function has a minimum and a maximum
828 (when
830 (not (numberp fmin))
831 (not (numberp fmax)) (not (> fmax fmin)))
832 (merror (intl:gettext "plot2d: nothing to plot for ~M.~%") expr))
833 ;; sets up the levels for contour plots
834 (if (getf options :contour)
835 (if (setq levels (getf options :levels))
836 (unless (listp levels)
837 (setq levels (getlevels fmin fmax levels)))
838 (setq levels (getlevels fmin fmax 8)))
839 (setq levels (list 0.0)))
841 ;; Algorithm for implicit functions, by Jaime Villate. 2021
843 ;; The points at each rectangle in the sample grid are labeled as follows:
845 ;; ij+ ______ i+j+
846 ;; | |
847 ;; | | function fun has the following values at those points:
848 ;; | |
849 ;; ij |____| i+j fij, fi+j, fij+, fi+j+
851 (let (fij fi+j fij+ fi+j+ p1 p2 p3 p4 next)
852 (flet
853 ((interp+ (i j fi fi+ &aux x1 y1 x2 y2 (f1 fi) (f2 fi+) xp yp fp)
854 (if (minusp (* fi fi+))
855 (progn
856 (setq x1 (+ xmin (* dx i)))
857 (setq x2 (+ x1 dx))
858 (setq y1 (+ ymin (* dy j)))
859 (setq y2 (+ y1 dy))
860 (dotimes (n 2
861 (if (< (/ (+ (abs (- fi fp)) (abs (- fi+ fp)))
862 (abs (- fi fi+))) 1.5) (list xp yp) nil))
863 (setq xp (/ (+ x1 x2) 2.0))
864 (setq yp (/ (+ y1 y2) 2.0))
865 (setq fp (- (funcall fun xp yp) level))
866 (when (not (numberp fp)) (return nil))
867 (if (plusp (* f1 fp))
868 (setq x1 xp y1 yp f1 fp)
869 (setq x2 xp y2 yp f2 fp))
870 (setq xp (/ (- (* f1 x2) (* f2 x1)) (- f1 f2)))
871 (setq yp (/ (- (* f1 y2) (* f2 y1)) (- f1 f2)))
872 (setq fp (- (funcall fun xp yp) level))
873 (when (not (numberp fp)) (return nil))
874 (if (plusp (* f1 fp))
875 (setq x1 xp y1 yp f1 fp)
876 (setq x2 xp y2 yp f2 fp))))
877 nil))
878 (interp- (i j fi fi+ &aux x1 y1 x2 y2 (f1 fi) (f2 fi+) xp yp fp)
879 (if (minusp (* fi fi+))
880 (progn
881 (setq x1 (+ xmin (* dx i)))
882 (setq x2 (+ x1 dx))
883 (setq y1 (+ ymin (* dy j)))
884 (setq y2 (- y1 dy))
885 (dotimes (n 2
886 (if (< (/ (+ (abs (- fi fp)) (abs (- fi+ fp)))
887 (abs (- fi fi+))) 1.5) (list xp yp) nil))
888 (setq xp (/ (+ x1 x2) 2.0))
889 (setq yp (/ (+ y1 y2) 2.0))
890 (setq fp (- (funcall fun xp yp) level))
891 (when (not (numberp fp)) (return nil))
892 (if (plusp (* f1 fp))
893 (setq x1 xp y1 yp f1 fp)
894 (setq x2 xp y2 yp f2 fp))
895 (setq xp (/ (- (* f1 x2) (* f2 x1)) (- f1 f2)))
896 (setq yp (/ (- (* f1 y2) (* f2 y1)) (- f1 f2)))
897 (setq fp (- (funcall fun xp yp) level))
898 (when (not (numberp fp)) (return nil))
899 (if (plusp (* f1 fp))
900 (setq x1 xp y1 yp f1 fp)
901 (setq x2 xp y2 yp f2 fp))))
902 nil))
903 (interpx (i j fi fi+ &aux x1 x2 (f1 fi) (f2 fi+) xp yp fp)
904 (if (minusp (* fi fi+))
905 (progn
906 (setq x1 (+ xmin (* dx i)))
907 (setq x2 (+ x1 dx))
908 (setq yp (+ ymin (* dy j)))
909 (dotimes (n 2
910 (if (< (/ (+ (abs (- fi fp)) (abs (- fi+ fp)))
911 (abs (- fi fi+))) 1.5) (list xp yp) nil))
912 (setq xp (/ (+ x1 x2) 2.0))
913 (setq fp (- (funcall fun xp yp) level))
914 (when (not (numberp fp)) (return nil))
915 (if (plusp (* f1 fp))
916 (setq x1 xp f1 fp)
917 (setq x2 xp f2 fp))
918 (setq xp (/ (- (* f1 x2) (* f2 x1)) (- f1 f2)))
919 (setq fp (- (funcall fun xp yp) level))
920 (when (not (numberp fp)) (return nil))
921 (if (plusp (* f1 fp))
922 (setq x1 xp f1 fp)
923 (setq x2 xp f2 fp))))
924 nil))
925 (interpy (i j fj fj+ &aux y1 y2 (f1 fj) (f2 fj+) xp yp fp)
926 (if (minusp (* fj fj+))
927 (progn
928 (setq xp (+ xmin (* dx i)))
929 (setq y1 (+ ymin (* dy j)))
930 (setq y2 (+ y1 dy))
931 (dotimes (n 2
932 (if (< (/ (+ (abs (- fj fp)) (abs (- fj+ fp)))
933 (abs (- fj fj+))) 1.5) (list xp yp) nil))
934 (setq yp (/ (+ y1 y2) 2.0))
935 (setq fp (- (funcall fun xp yp) level))
936 (when (not (numberp fp)) (return nil))
937 (if (plusp (* f1 fp))
938 (setq y1 yp f1 fp)
939 (setq y2 yp f2 fp))
940 (setq yp (/ (- (* f1 y2) (* f2 y1)) (- f1 f2)))
941 (setq fp (- (funcall fun xp yp) level))
942 (when (not (numberp fp)) (return nil))
943 (if (plusp (* f1 fp))
944 (setq y1 yp f1 fp)
945 (setq y2 yp f2 fp))))
946 nil))
947 (coords (i j)
948 (list (+ xmin (* i dx)) (+ ymin (* j dy))))
949 (draw-line (p1 p2)
950 (push (first p1) result)
951 (push (second p1) result)
952 (push (first p2) result)
953 (push (second p2) result)
954 (push 'moveto result)
955 (push 'moveto result))
956 (draw-lines (p1 p2 p3)
957 (push (first p1) result)
958 (push (second p1) result)
959 (push (first p2) result)
960 (push (second p2) result)
961 (push (first p3) result)
962 (push (second p3) result)
963 (push 'moveto result)
964 (push 'moveto result)))
965 (dolist (level (reverse levels))
966 (dotimes (i gridx)
967 (dotimes (j gridy)
968 (setq fij (- (aref f i j) level))
969 (setq fij+ (- (aref f i (1+ j)) level))
970 (setq fi+j (- (aref f (1+ i) j) level))
971 (setq fi+j+ (- (aref f (1+ i) (1+ j)) level))
972 (setq next t)
973 ;; 1. undefined at ij
974 (when (not (numberp fij))
975 (setq next nil)
976 ;; if undefined also at i+j or ij+, continue to next rectangle
977 (when (and (numberp fi+j) (numberp fij+))
978 (if (< (abs fi+j) eps)
979 (if (< (abs fij+) eps)
980 ;; real and 0 at i+j and ij+
981 (draw-line (coords (1+ i) j) (coords i (1+ j)))
982 (when
983 (and
984 (numberp fi+j+)
985 (setq p2 (interpx i (1+ j) fij+ fi+j+)))
986 ;; real at i+j, ij+ and i+j+, 0 at i+j and segment
987 ;; ij+ i+j+
988 (draw-line (coords (1+ i) j) p2)))
989 (when (numberp fi+j+)
990 (if (< (abs fij+) eps)
991 (when (setq p2 (interpy (1+ i) j fi+j fi+j+))
992 ;; real at i+j, and i+j+, 0 at ij+ and segment
993 ;; i+j i+j+
994 (draw-line (coords i (1+ j)) p2))
995 (when
996 (and
997 (setq p1 (interpx i (1+ j) fij+ fi+j+))
998 (setq p2 (interpy (1+ i) j fi+j fi+j+)))
999 ;; real at i+j, ij+ and i+j+, 0 at segments
1000 ;; ij+ i+j+ and i+j i+j+
1001 (draw-line p1 p2)))))))
1002 ;; 2. real at ij and undefined at i+j
1003 (when (and next (not (numberp fi+j)))
1004 (setq next nil)
1005 ;; if undefined at ij+, continue to next rectangle
1006 (when (numberp fij+)
1007 (if (< (abs fij) eps)
1008 (if (< (abs fij+) eps)
1009 ;; zero at ij and ij+
1010 (draw-line (coords i j) (coords i (1+ j)))
1011 (when
1012 (and
1013 (numberp fi+j+)
1014 (setq p2 (interpx i (1+ j) fij+ fi+j+)))
1015 ;; real at ij+ and i+j+, 0 at ij and segment ij+ i+j+
1016 (draw-line (coords i j) p2)))
1017 (when
1018 (and
1019 (numberp fi+j+)
1020 (setq p1 (interpy i j fij fij+))
1021 (setq p2 (interpx i (1+ j) fij+ fi+j+)))
1022 ;; real at ij, ij+ and i+j+, 0 at segments ij ij+
1023 ;; and ij+ i+j+
1024 (draw-line p1 p2)))))
1025 ;; 3. real at fi+j and 0 at ij
1026 (when (and next (< (abs fij) eps))
1027 (setq next nil)
1028 (if (numberp fij+)
1029 (if (< (abs fij+) eps)
1030 ;; real at i+j, 0 at ij and ij+
1031 (draw-line (coords i j) (coords i (1+ j)))
1032 (when (setq p1 (interp- i (1+ j) fij+ fi+j))
1033 (if (numberp fi+j+)
1034 (if (< (abs fi+j+) eps)
1035 ;; real at i+j and ij, 0 at ij, i+j+ and
1036 ;; diagonal ij+ i+j
1037 (draw-lines (coords i j) p1
1038 (coords (1+ i) (1+ j)))
1039 (progn
1040 ;; real at i+j, ij+ and i+j+, 0 at ij,
1041 ;; diagonal ij+ i+j and segment ij+ i+j+
1042 (when (setq p2 (interpx i (1+ j) fij+ fi+j+))
1043 (draw-lines (coords i j) p1 p2))
1044 ;; real at i+j, ij+ and i+j+, 0 at ij,
1045 ;; diagonal ij+ i+j and segment i+j i+j+
1046 (when (setq p2 (interpy (1+ i) j fi+j fi+j+))
1047 (draw-lines (coords i j) p1 p2)))))))
1048 (if (numberp fi+j+)
1049 (if (< (abs fi+j) eps)
1050 ;; undefined at ij+, real at fi+j+, 0 at ij and i+j
1051 (draw-line (coords i j) (coords (1+ i) j))
1052 (when (setq p2 (interpy (1+ i) j fi+j fi+j+))
1053 ;; undefined at ij+, real at fi+j and fi+j+, 0 at
1054 ;; ij and segment i+j i+j+
1055 (draw-line (coords i j) p2)))
1056 (when (< (abs fi+j) eps)
1057 ;; undefined at ij+ and i+j+, 0 at ij and i+j
1058 (draw-line (coords i j) (coords (1+ i) j))))))
1059 ;; 4. real at ij and 0 at i+j
1060 (when (and next (< (abs fi+j) eps))
1061 (setq next nil)
1062 (if (numberp fij+)
1063 (if (numberp fi+j+)
1064 ;; if 0 at i+j but undefined at ij+ or there's no zero
1065 ;; in diagonal ij i+j+, continue to next rectangle
1066 (when (setq p1 (interp+ i j fij fi+j+))
1067 (if (< (abs fij+) eps)
1068 ;; 0 at i+j, ij+ and diagonal ij i+j+
1069 (draw-lines (coords (1+ i) j) p1 (coords i (1+ j)))
1070 (progn
1071 (when (setq p2 (interpy i j fij fij+))
1072 ;; 0 at i+j, diagonal ij i+j+ and segment
1073 ;; ij ij+
1074 (draw-lines (coords (1+ i) j) p1 p2))
1075 (when (setq p2 (interpx i (1+ j) fij+ fi+j+))
1076 ;; 0 at i+j, diagonal ij i+j+ and segment
1077 ;; ij+ i+j+
1078 (draw-lines (coords (1+ i) j) p1 p2)))))
1079 (when (setq p2 (interpy i j fij fij+))
1080 ;; undefined at i+j+, 0 at i+j and segment ij ij+
1081 (draw-line (coords (1+ i) j) p2)))))
1082 ;; 5. real at ij and i+j but undefined at ij+
1083 (when (and next (not (numberp fij+)))
1084 (setq next nil)
1085 (when
1086 (and
1087 (numberp fi+j+)
1088 (setq p1 (interpx i j fij fi+j))
1089 (setq p2 (interpy (1+ i) j fi+j fi+j+)))
1090 ;; 0 at segments ij i+j and i+j i+j+
1091 (draw-line p1 p2)))
1092 ;; 6. real at ij, i+j and ij+, but undefined at i+j+
1093 (when (and next (not (numberp fi+j+)))
1094 (setq next nil)
1095 (when
1096 (and
1097 (setq p1 (interpy i j fij fij+))
1098 (setq p2 (interpx i j fij fi+j)))
1099 ;; 0 at segments ij ij+ and ij i+j
1100 (draw-line p1 p2)))
1101 ;; 7. real at the four corners and 0 at ij+
1102 (when (and next (< (abs fij+) eps))
1103 (setq next nil)
1104 (when (setq p1 (interp+ i j fij fi+j+))
1105 (when (setq p2 (interpx i j fij fi+j))
1106 ;; 0 at diagonal ij i+j+ and segment ij i+j
1107 (draw-lines p2 p1 (coords i (1+ j))))
1108 (when (setq p2 (interpy (1+ i) j fi+j fi+j+))
1109 ;; 0 at diagonal ij i+j+ and segment i+j i+j+
1110 (draw-lines p2 p1 (coords i (1+ j))))))
1111 ;; 8. real at the four corners and 0 at i+j+
1112 (when (and next (< (abs fi+j+) eps))
1113 (setq next nil)
1114 (when (setq p1 (interp- i (1+ j) fij+ fi+j))
1115 (when (setq p2 (interpx i j fij fi+j))
1116 ;; 0 at diagonal ij+ i+j and segment ij i+j
1117 (draw-lines p2 p1 (coords (1+ i) (1+ j))))
1118 (when (setq p2 (interpy i j fij fij+))
1119 ;; 0 at diagonal ij+ i+j and segment ij ij+
1120 (draw-lines p2 p1 (coords (1+ i) (1+ j))))))
1121 ;; 9. real at the four corners and 0 at segment ij i+j
1122 (when (and next (setq p1 (interpx i j fij fi+j)))
1123 (setq next nil)
1124 (if (setq p2 (interpy i j fij fij+))
1125 (if (setq p3 (interpx i (1+ j) fij+ fi+j+))
1126 (when (setq p4 (interpy (1+ i) j fi+j fi+j+))
1127 ;; 0 at the four sides
1128 (draw-line p1 p3)
1129 (draw-line p2 p4))
1130 (when (setq p3 (interp+ i j fij fi+j+))
1131 ;; 0 at segments ij i+j, ij ij+ and diagonal ij i+j+
1132 (draw-lines p1 p3 p2)))
1133 (if (setq p4 (interpy (1+ i) j fi+j fi+j+))
1134 (when (setq p2 (interp- i (1+ j) fij+ fi+j))
1135 ;; 0 at segments ij i+j, i+j i+j+ and diagonal ij+ i+j
1136 (draw-lines p1 p2 p4))
1137 (when
1138 (and
1139 (setq p3 (interpx i (1+ j) fij+ fi+j+))
1140 (setq p2 (interp+ i j fij fi+j+)))
1141 ;; 0 at segments ij i+j, ij+ i+j+ and diagonal ij i+j+
1142 (draw-lines p1 p2 p3)))))
1143 ;; 10. real at the four corners, without zero in segment ij i+j
1144 (when next
1145 (if (setq p2 (interpy i j fij fij+))
1146 (if (setq p3 (interpx i (1+ j) fij+ fi+j+))
1147 (when (setq p4 (interp- i (1+ j) fij+ fi+j))
1148 ;; 0 at segments ij ij+ and ij+ i+j+ and diagonal
1149 ;; ij+ i+j
1150 (draw-lines p2 p4 p3))
1151 (when
1152 (and
1153 (setq p4 (interpy (1+ i) j fi+j fi+j+))
1154 (setq p3 (interp+ i j fij fi+j+)))
1155 ;; 0 at segments ij ij+ and i+j i+j+ and diagonal
1156 ;; ij i+j+
1157 (draw-lines p2 p3 p4)))
1158 (when
1159 (and
1160 (setq p3 (interpx i (1+ j) fij+ fi+j+))
1161 (setq p4 (interpy (1+ i) j fi+j fi+j+))
1162 (setq p1 (interp+ i j fij fi+j+)))
1163 ;; 0 at segments ij+ i+j+ and i+j i+j+ and diagonal
1164 ;; ij i+j+
1165 (draw-lines p4 p1 p3))))))
1166 (when (and (getf options :contour) result)
1167 (push (cons '(mlist) (reverse result)) results)
1168 (push level values)
1169 (setq result nil)))))
1170 ;; When called for a single implicit expression, returns a Maxima list
1171 ;; of points. When called for contours of an expression, returns a
1172 ;; Maxima list whose first element is another Maxima list with the values
1173 ;; of the contours, followed by Maxima lists of points for each contour.
1174 (if (getf options :contour)
1175 (cons '(mlist) (cons (cons '(mlist) values) results))
1176 (cons '(mlist) (reverse result)))))
1178 ;; parametric ; [parametric,xfun,yfun,[t,tlow,thigh],[nticks ..]]
1179 ;; the rest of the parametric list after the list will add to the plot options
1181 (defun draw2d-parametric-adaptive (param options &aux range)
1182 (or (= ($length param) 4)
1183 (merror (intl:gettext "plot2d: parametric plots must include two expressions and an interval")))
1184 (setq range (nth 4 param))
1185 (or (and ($listp range) (symbolp (second range)) (eql ($length range) 3))
1186 (merror (intl:gettext "plot2d: wrong interval for parametric plot: ~M") range))
1187 (setq range (check-range range))
1188 (let* ((nticks (getf options :nticks))
1189 (trange (cddr range))
1190 (tvar (second range))
1191 (xrange (or (getf options :x) (getf options :xbounds)))
1192 (yrange (or (getf options :y) (getf options :ybounds)))
1193 (tmin (coerce-float (first trange)))
1194 (tmax (coerce-float (second trange)))
1195 (xmin (coerce-float (first xrange)))
1196 (xmax (coerce-float (second xrange)))
1197 (ymin (coerce-float (first yrange)))
1198 (ymax (coerce-float (second yrange)))
1199 f1 f2)
1200 (declare (type flonum ymin ymax xmin xmax tmin tmax))
1201 (setq f1 (coerce-float-fun (third param) `((mlist), tvar)))
1202 (setq f2 (coerce-float-fun (fourth param) `((mlist), tvar)))
1204 (let ((n-clipped 0) (n-non-numeric 0)
1205 (t-step (/ (- tmax tmin) (coerce-float nticks) 2))
1206 t-samples x-samples y-samples result)
1207 ;; Divide the range into 2*NTICKS regions that we then
1208 ;; adaptively plot over.
1209 (dotimes (k (1+ (* 2 nticks)))
1210 (let ((tpar (+ tmin (* k t-step))))
1211 (push tpar t-samples)
1212 (push (funcall f1 tpar) x-samples)
1213 (push (funcall f2 tpar) y-samples)))
1214 (setf t-samples (nreverse t-samples))
1215 (setf x-samples (nreverse x-samples))
1216 (setf y-samples (nreverse y-samples))
1218 ;; Adaptively plot over each region
1219 (do ((t-start t-samples (cddr t-start))
1220 (t-mid (cdr t-samples) (cddr t-mid))
1221 (t-end (cddr t-samples) (cddr t-end))
1222 (x-start x-samples (cddr x-start))
1223 (x-mid (cdr x-samples) (cddr x-mid))
1224 (x-end (cddr x-samples) (cddr x-end))
1225 (y-start y-samples (cddr y-start))
1226 (y-mid (cdr y-samples) (cddr y-mid))
1227 (y-end (cddr y-samples) (cddr y-end)))
1228 ((null t-end))
1229 (setf result
1230 (if result
1231 (append result
1232 (cddr (adaptive-parametric-plot
1233 f1 f2
1234 (car t-start) (car t-mid) (car t-end)
1235 (car x-start) (car x-mid) (car x-end)
1236 (car y-start) (car y-mid) (car y-end)
1237 (getf options :adapt_depth)
1238 1e-5)))
1239 (adaptive-parametric-plot
1240 f1 f2
1241 (car t-start) (car t-mid) (car t-end)
1242 (car x-start) (car x-mid) (car x-end)
1243 (car y-start) (car y-mid) (car y-end)
1244 (getf options :adapt_depth)
1245 1e-5))))
1246 ;; Fix up out-of-range values and clobber non-numeric values.
1247 (do ((x result (cddr x))
1248 (y (cdr result) (cddr y)))
1249 ((null y))
1250 (if (and (numberp (car x)) (numberp (car y)))
1251 (unless (and (<= ymin (car y) ymax)
1252 (<= xmin (car x) xmax))
1253 (incf n-clipped)
1254 (setf (car x) 'moveto)
1255 (setf (car y) 'moveto))
1256 (progn
1257 (incf n-non-numeric)
1258 (setf (car x) 'moveto)
1259 (setf (car y) 'moveto))))
1260 ;; Filter out any MOVETO's which do not precede a number.
1261 ;; Code elsewhere in this file expects MOVETO's to
1262 ;; come in pairs, so leave two MOVETO's before a number.
1263 (let ((n (length result)))
1264 (dotimes (i n)
1265 (when
1266 (and
1267 (evenp i)
1268 (eq (nth i result) 'moveto)
1269 (eq (nth (1+ i) result) 'moveto)
1270 (or
1271 (eq i (- n 2))
1272 (eq (nth (+ i 2) result) 'moveto)))
1273 (setf (nth i result) nil)
1274 (setf (nth (1+ i) result) nil))))
1276 (let ((result-sans-nil (delete nil result)))
1277 (if (null result-sans-nil)
1278 (cond
1279 ((= n-non-numeric 0)
1280 (mtell (intl:gettext "plot2d: all values were clipped.~%")))
1281 ((= n-clipped 0)
1282 (mtell (intl:gettext
1283 "plot2d: expression evaluates to non-numeric value everywhere in plotting range.~%")))
1285 (mtell (intl:gettext
1286 "plot2d: all values are non-numeric, or clipped.~%"))))
1287 (progn
1288 (if (> n-non-numeric 0)
1289 (mtell (intl:gettext
1290 "plot2d: expression evaluates to non-numeric value somewhere in plotting range.~%")))
1291 (if (> n-clipped 0)
1292 (mtell (intl:gettext "plot2d: some values were clipped.~%")))))
1293 (cons '(mlist) result-sans-nil)))))
1295 ;; draw2d-discrete. Accepts [discrete,[x1,x2,...],[y1,y2,...]]
1296 ;; or [discrete,[[x1,y1]...] and returns [x1,y1,...] or nil, if
1297 ;; non of the points have real values.
1298 ;; Currently any options given are being ignored, because there
1299 ;; are no options specific to the generation of the points.
1300 (defun draw2d-discrete (f)
1301 (let ((x (third f)) (y (fourth f)) data gaps)
1302 (cond
1303 (($listp x) ; x is a list
1304 (cond
1305 (($listp (cadr x)) ; x1 is a list
1306 (cond
1307 ((= (length (cadr x)) 3) ; x1 is a 2D point
1308 (setq data (parse-points-xy x)))
1309 (t ; x1 is not a 2D point
1310 (merror (intl:gettext "draw2d-discrete: Expecting a point with 2 coordinates; found ~M~%") (cadr x)))))
1311 (t ; x1 is not a list
1312 (cond
1313 (($listp y) ; y is a list
1314 (cond
1315 ((symbolp (coerce-float (cadr y))); y is an option
1316 (setq data (parse-points-y x)))
1317 (t ; y is not an option
1318 (cond
1319 (($listp (cadr y)) ; y1 is a list
1320 (merror (intl:gettext "draw2d-discrete: Expecting a y coordinate; found ~M~%") (cadr y)))
1321 (t ; y1 not a list
1322 (cond
1323 ((= (length x) (length y)) ; case [x][y]
1324 (setq data (parse-points-x-y x y)))
1325 (t ; wrong
1326 (merror (intl:gettext "draw2d-discrete: The number of x and y coordinates do not match.~%")))))))))
1327 (t ; y is not a list
1328 (setq data (parse-points-y x)))))))
1329 (t ; x is not a list
1330 (merror (intl:gettext "draw2d-discrete: Expecting a list of x coordinates or points; found ~M~%") x)))
1332 ;; checks for non-real values
1333 (cond
1334 ((some #'realp data)
1335 (setq gaps (count-if #'(lambda (x) (eq x 'moveto)) data))
1336 (when (> gaps 0)
1337 ;; some points have non-real values
1338 (mtell (intl:gettext "Warning: excluding ~M points with non-numerical values.~%") (/ gaps 2))))
1340 ;; none of the points have real values
1341 (mtell (intl:gettext "Warning: none of the points have numerical values.~%"))
1342 (setq data nil)))
1343 data))
1345 ;; Two lists [x1...xn] and [y1...yn] are joined as
1346 ;; [x1 y1...xn yn], converting all expressions to real numbers.
1347 ;; If either xi or yi are not real, both are replaced by 'moveto
1348 (defun parse-points-x-y (x y)
1349 (do ((a (rest x) (cdr a))
1350 (b (rest y) (cdr b))
1351 c af bf)
1352 ((null b) (cons '(mlist) (reverse c)))
1353 (setq af (coerce-float (car a)))
1354 (setq bf (coerce-float (car b)))
1355 (cond
1356 ((or (not (realp af)) (not (realp bf)))
1357 (setq c (cons 'moveto (cons 'moveto c))))
1359 (setq c (cons bf (cons af c)))))))
1361 ;; One list [y1...yn] becomes the list [1 y1...n yn],
1362 ;; converting all expressions to real numbers.
1363 ;; If yi is not real, both i and yi are replaced by 'moveto
1364 (defun parse-points-y (y)
1365 (do ((a 1 (1+ a))
1366 (b (rest y) (cdr b))
1367 c bf)
1368 ((null b) (cons '(mlist) (reverse c)))
1369 (setq bf (coerce-float (car b)))
1370 (cond
1371 ((not (realp bf))
1372 (setq c (cons 'moveto (cons 'moveto c))))
1374 (setq c (cons bf (cons a c)))))))
1376 ;; List [[x1,y1]...[xn,yn]] is transformed into
1377 ;; [x1 y1...xn yn], converting all expressions to real numbers.
1378 ;; If either xi or yi are not real, both are replaced by 'moveto
1379 (defun parse-points-xy (xy)
1380 (do ((ab (rest xy) (cdr ab))
1381 c af bf)
1382 ((null ab) (cons '(mlist) (reverse c)))
1383 (setq af (coerce-float (cadar ab)))
1384 (setq bf (coerce-float (caddar ab)))
1385 (cond
1386 ((or (not (realp af)) (not (realp bf)))
1387 (setq c (cons 'moveto (cons 'moveto c))))
1389 (setq c (cons bf (cons af c)))))))
1391 ;;; Adaptive plotting, based on the adaptive plotting code from
1392 ;;; YACAS. See http://yacas.sourceforge.net/Algo.html#c3s1 for a
1393 ;;; description of the algorithm. More precise details can be found
1394 ;;; in the file yacas/scripts/plots.rep/plot2d.ys.
1397 ;; Determine if we have a slow oscillation of the function.
1398 ;; Basically, for each 3 consecutive function values, we check to see
1399 ;; if the function is monotonic or not. There are 3 such sets, and
1400 ;; the function is considered slowly oscillating if at most 2 of them
1401 ;; are not monotonic.
1402 (defun slow-oscillation-p (f0 f1 f2 f3 f4)
1403 (flet ((sign-change (x y z)
1404 (cond ((not (and (numberp x) (numberp y) (numberp z)))
1405 ;; Something is not a number. Assume the
1406 ;; oscillation is not slow.
1408 ((or (and (> y x) (> y z))
1409 (and (< y x) (< y z)))
1412 0))))
1413 (<= (+ (sign-change f0 f1 f2)
1414 (sign-change f1 f2 f3)
1415 (sign-change f2 f3 f4))
1416 2)))
1418 ;; Determine if the function values are smooth enough. This means
1419 ;; that integrals of the functions on the left part and the right part
1420 ;; of the range are approximately the same.
1423 (defun smooth-enough-p (f-a f-a1 f-b f-b1 f-c eps)
1424 (cond ((every #'numberp (list f-a f-a1 f-b f-b1 f-c))
1425 (let ((quad (/ (+ f-a
1426 (* -5 f-a1)
1427 (* 9 f-b)
1428 (* -7 f-b1)
1429 (* 2 f-c))
1430 24))
1431 (quad-b (/ (+ (* 5 f-b)
1432 (* 8 f-b1)
1433 (- f-c))
1434 12)))
1435 ;; According to the Yacas source code, quad is the Simpson
1436 ;; quadrature for the (fb,fb1) subinterval (using points b,b1,c),
1437 ;; subtracted from the 4-point Newton-Cotes quadrature for the
1438 ;; (fb,fb1) subinterval (using points a, a1, b, b1.
1440 ;; quad-b is the Simpson quadrature for the (fb,f1) subinterval.
1442 ;; This used to test for diff <= 0. But in some
1443 ;; situations, like plot2d(0.99,[x,0,5]), roundoff prevents
1444 ;; this from happening. So we do diff < delta instead, for
1445 ;; some value of delta.
1447 ;; XXX: What is the right value for delta? Does this break
1448 ;; other things? Simple tests thus far show that
1449 ;; 100*flonum-epsilon is ok.
1450 (let ((diff (- (abs quad)
1451 (* eps (- quad-b (min f-a f-a1 f-b f-b1 f-c)))))
1452 (delta (* 150 flonum-epsilon)))
1453 (<= diff delta))))
1455 ;; Something is not a number, so assume it's not smooth enough.
1456 nil)))
1458 (defun adaptive-plot (fcn a b c f-a f-b f-c depth eps)
1459 ;; Step 1: Split the interval [a, c] into 5 points
1460 (let* ((a1 (/ (+ a b) 2))
1461 (b1 (/ (+ b c) 2))
1462 (f-a1 (funcall fcn a1))
1463 (f-b1 (funcall fcn b1))
1465 (cond ((or (not (plusp depth))
1466 (and (slow-oscillation-p f-a f-a1 f-b f-b1 f-c)
1467 (smooth-enough-p f-a f-a1 f-b f-b1 f-c eps)))
1468 ;; Everything is nice and smooth so we're done. Don't
1469 ;; refine anymore.
1470 (list a f-a
1471 a1 f-a1
1472 b f-b
1473 b1 f-b1
1474 c f-c))
1475 ;; We are not plotting the real part of the function and the
1476 ;; function is undefined at all points - assume it has complex value
1477 ;; on [a,b]. Maybe we should refine it a couple of times just to make sure?
1478 ((and (null *plot-realpart*)
1479 (null f-a) (null f-a1) (null f-b) (null f-b1) (null f-c))
1480 (list a f-a
1481 a1 f-a1
1482 b f-b
1483 b1 f-b1
1484 c f-c))
1486 ;; Need to refine. Split the interval in half, and try to plot each half.
1487 (let ((left (adaptive-plot fcn a a1 b f-a f-a1 f-b (1- depth) (* 2 eps)))
1488 (right (adaptive-plot fcn b b1 c f-b f-b1 f-c (1- depth) (* 2 eps))))
1489 (append left (cddr right)))))))
1491 (defun adaptive-parametric-plot (x-fcn y-fcn a b c x-a x-b x-c y-a y-b y-c depth eps)
1492 ;; Step 1: Split the interval [a, c] into 5 points
1493 (let* ((a1 (/ (+ a b) 2))
1494 (b1 (/ (+ b c) 2))
1495 (x-a1 (funcall x-fcn a1))
1496 (x-b1 (funcall x-fcn b1))
1497 (y-a1 (funcall y-fcn a1))
1498 (y-b1 (funcall y-fcn b1)))
1499 (cond ((or (not (plusp depth))
1500 ;; Should we have a different algorithm to determine
1501 ;; slow oscillation and smooth-enough for parametric
1502 ;; plots?
1503 (and (slow-oscillation-p y-a y-a1 y-b y-b1 y-c)
1504 (slow-oscillation-p x-a x-a1 x-b x-b1 x-c)
1505 (smooth-enough-p y-a y-a1 y-b y-b1 y-c eps)
1506 (smooth-enough-p x-a x-a1 x-b x-b1 x-c eps)))
1507 ;; Everything is nice and smooth so we're done. Don't
1508 ;; refine anymore.
1509 (list x-a y-a
1510 x-a1 y-a1
1511 x-b y-b
1512 x-b1 y-b1
1513 x-c y-c))
1514 ;; We are not plotting the real part of the function and the
1515 ;; function is undefined at all points - assume it has complex value
1516 ;; on [a,b]. Maybe we should refine it a couple of times just to make sure?
1517 ((and (null *plot-realpart*)
1518 (null y-a) (null y-a1) (null y-b) (null y-b1) (null y-c)
1519 (null x-a) (null x-a1) (null x-b) (null x-b1) (null x-c))
1520 (list x-a y-a
1521 x-a1 y-a1
1522 x-b y-b
1523 x-b1 y-b1
1524 x-c y-c))
1526 ;; Need to refine. Split the interval in half, and try to plot each half.
1527 (let ((left (adaptive-parametric-plot x-fcn y-fcn
1528 a a1 b
1529 x-a x-a1 x-b
1530 y-a y-a1 y-b
1531 (1- depth) (* 2 eps)))
1532 (right (adaptive-parametric-plot x-fcn y-fcn
1533 b b1 c
1534 x-b x-b1 x-c
1535 y-b y-b1 y-c
1536 (1- depth) (* 2 eps))))
1537 ;; (cddr right) to skip over the point that is duplicated
1538 ;; between the right end-point of the left region and the
1539 ;; left end-point of the right
1540 (append left (cddr right)))))))
1542 (defun draw2d (fcn range plot-options)
1543 (if (and ($listp fcn) (equal '$parametric (cadr fcn)))
1544 (return-from draw2d
1545 (draw2d-parametric-adaptive fcn plot-options)))
1546 (if (and ($listp fcn) (equal '$discrete (cadr fcn)))
1547 (return-from draw2d (draw2d-discrete fcn)))
1548 (when (and ($listp fcn) (equal '$contour (cadr fcn)))
1549 (setf (getf plot-options :contour) t)
1550 (return-from draw2d (draw2d-implicit (caddr fcn) plot-options)))
1551 (when (and (listp fcn) (eq 'mequal (caar fcn)))
1552 (setf (getf plot-options :contour) nil)
1553 (return-from draw2d (draw2d-implicit fcn plot-options)))
1554 (let* ((nticks (getf plot-options :nticks))
1555 (yrange (getf plot-options :ybounds))
1556 (depth (getf plot-options :adapt_depth)))
1558 (setq fcn (coerce-float-fun fcn `((mlist), (second range))))
1560 (let* ((x-start (coerce-float (third range)))
1561 (xend (coerce-float (fourth range)))
1562 (x-step (/ (- xend x-start) (coerce-float nticks) 2))
1563 (ymin (coerce-float (first yrange)))
1564 (ymax (coerce-float (second yrange)))
1565 (n-clipped 0) (n-non-numeric 0)
1566 ;; What is a good EPS value for adaptive plotting?
1567 ;(eps 1e-5)
1568 x-samples y-samples result
1570 (declare (type flonum ymin ymax))
1571 ;; Divide the region into NTICKS regions. Each region has a
1572 ;; start, mid and endpoint. Then adaptively plot over each of
1573 ;; these regions. So it's probably a good idea not to make
1574 ;; NTICKS too big. Since adaptive plotting splits the sections
1575 ;; in half, it's also probably not a good idea to have NTICKS be
1576 ;; a power of two.
1577 (when (getf plot-options :logx)
1578 (setf x-start (log x-start))
1579 (setf xend (log xend))
1580 (setf x-step (/ (- xend x-start) (coerce-float nticks) 2)))
1582 (flet ((fun (x)
1583 (let ((y (if (getf plot-options :logx)
1584 (funcall fcn (exp x))
1585 (funcall fcn x))))
1586 (if (and (getf plot-options :logy)
1587 (numberp y))
1588 (if (> y 0) (log y) 'und)
1589 y))))
1591 (dotimes (k (1+ (* 2 nticks)))
1592 (let ((x (+ x-start (* k x-step))))
1593 (push x x-samples)
1594 (push (fun x) y-samples)))
1595 (setf x-samples (nreverse x-samples))
1596 (setf y-samples (nreverse y-samples))
1598 ;; For each region, adaptively plot it.
1599 (do ((x-start x-samples (cddr x-start))
1600 (x-mid (cdr x-samples) (cddr x-mid))
1601 (x-end (cddr x-samples) (cddr x-end))
1602 (y-start y-samples (cddr y-start))
1603 (y-mid (cdr y-samples) (cddr y-mid))
1604 (y-end (cddr y-samples) (cddr y-end)))
1605 ((null x-end))
1606 ;; The region is x-start to x-end, with mid-point x-mid.
1608 ;; The cddr is to remove the one extra sample (x and y value)
1609 ;; that adaptive plot returns. But on the first iteration,
1610 ;; result is empty, so we don't want the cddr because we want
1611 ;; all the samples returned from adaptive-plot. On subsequent
1612 ;; iterations, it's a duplicate of the last point of the
1613 ;; previous interval.
1614 (setf result
1615 (if result
1616 (append result
1617 (cddr
1618 (adaptive-plot #'fun (car x-start) (car x-mid) (car x-end)
1619 (car y-start) (car y-mid) (car y-end)
1620 depth 1e-5)))
1621 (adaptive-plot #'fun (car x-start) (car x-mid) (car x-end)
1622 (car y-start) (car y-mid) (car y-end)
1623 depth 1e-5))))
1625 ;; Fix up out-of-range values
1626 ;; and clobber non-numeric values.
1628 (do ((x result (cddr x))
1629 (y (cdr result) (cddr y)))
1630 ((null y))
1631 (if (numberp (car y))
1632 (unless (<= ymin (car y) ymax)
1633 (incf n-clipped)
1634 (setf (car x) 'moveto)
1635 (setf (car y) 'moveto))
1636 (progn
1637 (incf n-non-numeric)
1638 (setf (car x) 'moveto)
1639 (setf (car y) 'moveto)))
1640 (when (and (getf plot-options :logx)
1641 (numberp (car x)))
1642 (setf (car x) (exp (car x))))
1644 (when (and (getf plot-options :logy)
1645 (numberp (car y)))
1646 (setf (car y) (exp (car y)))))
1648 ;; Filter out any MOVETO's which do not precede a number.
1649 ;; Code elsewhere in this file expects MOVETO's to
1650 ;; come in pairs, so leave two MOVETO's before a number.
1651 (let ((n (length result)))
1652 (dotimes (i n)
1653 (when
1654 (and
1655 (evenp i)
1656 (eq (nth i result) 'moveto)
1657 (eq (nth (1+ i) result) 'moveto)
1658 (or
1659 (eq i (- n 2))
1660 (eq (nth (+ i 2) result) 'moveto)))
1661 (setf (nth i result) nil)
1662 (setf (nth (1+ i) result) nil))))
1664 (let ((result-sans-nil (delete nil result)))
1665 (if (null result-sans-nil)
1666 (cond
1667 ((= n-non-numeric 0)
1668 (mtell (intl:gettext "plot2d: all values were clipped.~%")))
1669 ((= n-clipped 0)
1670 (mtell (intl:gettext "plot2d: expression evaluates to non-numeric value everywhere in plotting range.~%")))
1672 (mtell (intl:gettext "plot2d: all values are non-numeric, or clipped.~%"))))
1673 (progn
1674 (if (> n-non-numeric 0)
1675 (mtell (intl:gettext "plot2d: expression evaluates to non-numeric value somewhere in plotting range.~%")))
1676 (if (> n-clipped 0)
1677 (mtell (intl:gettext "plot2d: some values were clipped.~%")))))
1678 (cons '(mlist) result-sans-nil))))))
1680 (defun get-range (lis)
1681 (let ((ymin most-positive-flonum)
1682 (ymax most-negative-flonum))
1683 (declare (type flonum ymin ymax))
1684 (do ((l lis (cddr l)))
1685 ((null l))
1686 (or (floatp (car l)) (setf (car l) (float (car l))))
1687 (cond ((< (car l) ymin)
1688 (setq ymin (car l))))
1689 (cond ((< ymax (car l))
1690 (setq ymax (car l)))))
1691 (list '(mlist) ymin ymax)))
1693 #+sbcl (defvar $gnuplot_view_args "-persist ~a")
1694 #-sbcl (defvar $gnuplot_view_args "-persist ~s")
1696 #+(or sbcl openmcl) (defvar $gnuplot_file_args "~a")
1697 #-(or sbcl openmcl) (defvar $gnuplot_file_args "~s")
1699 (defvar $mgnuplot_command "mgnuplot")
1700 (defvar $geomview_command "geomview")
1702 (defvar $xmaxima_plot_command "xmaxima")
1704 (defun plot-temp-file (file &optional (preserve-file nil))
1705 (let ((filename
1706 (if *maxima-tempdir*
1707 (format nil "~a/~a" *maxima-tempdir* file)
1708 file)))
1709 (declare (special *temp-files-list*))
1710 (unless preserve-file
1711 (setf (gethash filename *temp-files-list*) t))
1712 (format nil "~a" filename)
1715 ;; If no file path is given, uses temporary directory path
1716 (defun plot-file-path (file &optional (preserve-file nil))
1717 (if (pathname-directory file)
1718 file
1719 (plot-temp-file file preserve-file)))
1721 (defun gnuplot-process (plot-options &optional file out-file)
1722 (let ((gnuplot-term (getf plot-options :gnuplot_term))
1723 (run-viewer (getf plot-options :run_viewer))
1724 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
1725 (gnuplot-preamble
1726 (string-downcase (getf plot-options :gnuplot_preamble))))
1728 ;; creates the output file, when there is one to be created
1729 (when (and out-file (not (eq gnuplot-term '$default)))
1730 #+(or (and sbcl win32) (and sbcl win64) (and ccl windows))
1731 ($system $gnuplot_command (format nil $gnuplot_file_args file))
1732 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
1733 ($system (format nil "~a ~a" $gnuplot_command
1734 (format nil $gnuplot_file_args file))))
1736 ;; displays contents of the output file, when gnuplot-term is dumb,
1737 ;; or runs gnuplot when gnuplot-term is default
1738 (when run-viewer
1739 (case gnuplot-term
1740 ($default
1741 ;; the options given to gnuplot will be different when the user
1742 ;; redirects the output by using "set output" in the preamble
1743 #+(or (and sbcl win32) (and sbcl win64) (and ccl windows))
1744 ($system $gnuplot_command "-persist" (format nil $gnuplot_file_args file))
1745 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
1746 ($system
1747 (format nil "~a ~a" $gnuplot_command
1748 (format nil (if (search "set out" gnuplot-preamble)
1749 $gnuplot_file_args $gnuplot_view_args)
1750 file))))
1751 ($dumb
1752 (if out-file
1753 ($printfile (car out-file))
1754 (merror (intl:gettext "plotting: option 'gnuplot_out_file' not defined."))))))))
1756 ;; plot-options-parser puts the plot options given into a property list.
1757 ;; maxopts: a list (not a Maxima list!) with plot options.
1758 ;; options: a property list, or an empty list.
1759 ;; Example:
1760 ;; (plot-options-parser (list #$[x,-2,2]$ #$[nticks,30]$) '(:nticks 4))
1761 ;; returns:
1762 ;; (:XLABEL "x" :XMAX 2.0 :XMIN -2.0 :NTICKS 30)
1764 (defun plot-options-parser (maxopts options &aux name)
1765 (dolist (opt maxopts)
1766 (unless (or ($listp opt) (symbolp opt))
1767 (merror
1768 (intl:gettext
1769 "plot-options-parser: option \"~M\" should be a list or a symbol")
1770 opt))
1771 (cond
1772 (($listp opt)
1773 (unless ($symbolp (setq name (second opt)))
1774 (merror
1775 (intl:gettext
1776 "plot-options-parser: Expecting option name as a symbol, found: \"~M\"")
1777 opt))
1778 (case name
1779 ($adapt_depth
1780 (setf (getf options :adapt_depth)
1781 (check-option (cdr opt) #'naturalp "a natural number" 1)))
1782 ($axes (setf (getf options :axes)
1783 (check-option-b (cdr opt) #'axesoptionp "x, y, solid" 1)))
1784 ($azimuth (if (caddr opt)
1785 (setf (caddr opt) (parse-azimuth (caddr opt))))
1786 (setf (getf options :azimuth)
1787 (check-option (cdr opt) #'realp "a real number" 1)))
1788 ($box (setf (getf options :box)
1789 (check-option-boole (cdr opt))))
1790 ($color (setf (getf options :color)
1791 (check-option (cdr opt) #'plotcolorp "a color")))
1792 ($color_bar (setf (getf options :color_bar)
1793 (check-option-boole (cdr opt))))
1794 ($color_bar_tics
1795 (if (cddr opt)
1796 (setf (cddr opt) (mapcar #'coerce-float (cddr opt))))
1797 (setf (getf options :color_bar_tics)
1798 (check-option-b (cdr opt) #'realp "a real number" 3)))
1799 ($elevation (if (caddr opt)
1800 (setf (caddr opt) (parse-elevation (caddr opt))))
1801 (setf (getf options :elevation)
1802 (check-option (cdr opt) #'realp "a real number" 1)))
1803 ($grid (setf (getf options :grid)
1804 (check-option (cdr opt) #'naturalp "a natural number" 2)))
1805 ($grid2d (setf (getf options :grid2d)
1806 (check-option-boole (cdr opt))))
1807 ($iterations
1808 (setf (getf options :iterations)
1809 (check-option (cdr opt) #'naturalp "a natural number" 1)))
1810 ($label (setf (getf options :label)
1811 (check-option-label (cdr opt))))
1812 ($legend (setf (getf options :legend)
1813 (check-option-b (cdr opt) #'stringp "a string")))
1814 ($levels (setf (getf options :levels)
1815 (check-option-levels (cdr opt))))
1816 ($logx (setf (getf options :logx)
1817 (check-option-boole (cdr opt))))
1818 ($logy (setf (getf options :logy)
1819 (check-option-boole (cdr opt))))
1820 ($mesh_lines_color
1821 (setf (getf options :mesh_lines_color)
1822 (check-option-b (cdr opt) #'plotcolorp "a color" 1)))
1823 ($nticks (setf (getf options :nticks)
1824 (check-option (cdr opt) #'naturalp "a natural number" 1)))
1825 ($palette (setf (getf options :palette)
1826 (check-option-palette (cdr opt))))
1827 ($plotepsilon (setf (getf options :plotepsilon)
1828 (check-option (cdr opt) #'realp "a real number" 1)))
1829 ($plot_format (setf (getf options :plot_format)
1830 (check-option-format (cdr opt))))
1831 ($plot_realpart (setf (getf options :plot_realpart)
1832 (check-option-boole (cdr opt))))
1833 ($point_type (setf (getf options :point_type)
1834 (check-option (cdr opt) #'pointtypep "a point type")))
1835 ($pdf_file (setf (getf options :pdf_file)
1836 (check-option (cdr opt) #'stringp "a string" 1)))
1837 ($png_file (setf (getf options :png_file)
1838 (check-option (cdr opt) #'stringp "a string" 1)))
1839 ($ps_file (setf (getf options :ps_file)
1840 (check-option (cdr opt) #'stringp "a string" 1)))
1841 ($run_viewer (setf (getf options :run_viewer)
1842 (check-option-boole (cdr opt))))
1843 ($same_xy (setf (getf options :same_xy)
1844 (check-option-boole (cdr opt))))
1845 ($same_xyz (setf (getf options :same_xyz)
1846 (check-option-boole (cdr opt))))
1847 ($sample (setf (getf options :sample)
1848 (check-option (cdr opt) #'naturalp "a natural number" 2)))
1849 ($style (setf (getf options :style)
1850 (check-option-style (cdr opt))))
1851 ($svg_file (setf (getf options :svg_file)
1852 (check-option (cdr opt) #'stringp "a string" 1)))
1853 ($t (setf (getf options :t) (cddr (check-range opt))))
1854 ($title (setf (getf options :title)
1855 (check-option (cdr opt) #'stringp "a string" 1)))
1856 ($transform_xy (setf (getf options :transform_xy)
1857 (check-option-b (cdr opt) #'functionp "a function make_transform" 1)))
1858 ($x (setf (getf options :x) (cddr (check-range opt))))
1859 ($xbounds (setf (getf options :xbounds) (cddr (check-range opt))))
1860 ($xlabel (setf (getf options :xlabel)
1861 (check-option (cdr opt) #'string "a string" 1)))
1862 ($xtics
1863 (if (cddr opt)
1864 (setf (cddr opt) (mapcar #'coerce-float (cddr opt))))
1865 (setf (getf options :xtics)
1866 (check-option-b (cdr opt) #'realp "a real number" 3)))
1867 ($xy_scale
1868 (if (cddr opt)
1869 (setf (cddr opt) (mapcar #'coerce-float (cddr opt))))
1870 (setf (getf options :xy_scale)
1871 (check-option (cdr opt) #'realpositivep
1872 "a positive real number" 2)))
1873 ($y (setf (getf options :y) (cddr (check-range opt))))
1874 ($ybounds (setf (getf options :ybounds) (cddr (check-range opt))))
1875 ($ylabel (setf (getf options :ylabel)
1876 (check-option (cdr opt) #'string "a string" 1)))
1877 ($ytics
1878 (if (cddr opt)
1879 (setf (cddr opt) (mapcar #'coerce-float (cddr opt))))
1880 (setf (getf options :ytics)
1881 (check-option-b (cdr opt) #'realp "a real number" 3)))
1882 ($yx_ratio
1883 (if (caddr opt)
1884 (setf (caddr opt) (coerce-float (caddr opt))))
1885 (setf (getf options :yx_ratio)
1886 (check-option (cdr opt) #'realp "a real number" 1)))
1887 ($z (setf (getf options :z) (cddr (check-range opt))))
1888 ($zlabel (setf (getf options :zlabel)
1889 (check-option (cdr opt) #'string "a string" 1)))
1890 ($zmin
1891 (if (caddr opt)
1892 (setf (caddr opt) (coerce-float (caddr opt))))
1893 (setf (getf options :zmin)
1894 (check-option-b (cdr opt) #'realp "a real number" 1)))
1895 ($ztics
1896 (if (cddr opt)
1897 (setf (cddr opt) (mapcar #'coerce-float (cddr opt))))
1898 (setf (getf options :ztics)
1899 (check-option-b (cdr opt) #'realp "a real number" 3)))
1900 ($gnuplot_4_0 (setf (getf options :gnuplot_4_0)
1901 (check-option-boole (cdr opt))))
1902 ($gnuplot_curve_titles
1903 (setf (getf options :gnuplot_curve_titles)
1904 (check-option (cdr opt) #'stringp "a string")))
1905 ($gnuplot_curve_styles
1906 (setf (getf options :gnuplot_curve_styles)
1907 (check-option (cdr opt) #'stringp "a string")))
1908 ($gnuplot_default_term_command
1909 (setf (getf options :gnuplot_default_term_command)
1910 (check-option (cdr opt) #'stringp "a string" 1)))
1911 ($gnuplot_dumb_term_command
1912 (setf (getf options :gnuplot_dumb_term_command)
1913 (check-option (cdr opt) #'stringp "a string" 1)))
1914 ($gnuplot_out_file
1915 (setf (getf options :gnuplot_out_file)
1916 (check-option (cdr opt) #'stringp "a string" 1)))
1917 ($gnuplot_pm3d
1918 (setf (getf options :gnuplot_pm3d)
1919 (check-option-boole (cdr opt))))
1920 ($gnuplot_strings
1921 (setf (getf options :gnuplot_strings)
1922 (check-option-boole (cdr opt))))
1923 ($gnuplot_preamble
1924 (setf (getf options :gnuplot_preamble)
1925 (check-option (cdr opt) #'stringp "a string" 1)))
1926 ($gnuplot_postamble
1927 (setf (getf options :gnuplot_postamble)
1928 (check-option (cdr opt) #'stringp "a string" 1)))
1929 ($gnuplot_pdf_term_command
1930 (setf (getf options :gnuplot_pdf_term_command)
1931 (check-option (cdr opt) #'stringp "a string" 1)))
1932 ($gnuplot_png_term_command
1933 (setf (getf options :gnuplot_png_term_command)
1934 (check-option (cdr opt) #'stringp "a string" 1)))
1935 ($gnuplot_ps_term_command
1936 (setf (getf options :gnuplot_ps_term_command)
1937 (check-option (cdr opt) #'stringp "a string" 1)))
1938 ($gnuplot_svg_term_command
1939 (setf (getf options :gnuplot_svg_term_command)
1940 (check-option (cdr opt) #'stringp "a string" 1)))
1941 ;; gnuplot_term is a tricky one: when it is just default, dumb or
1942 ;; ps, we want it to be a symbol, but when it is more complicated,
1943 ;; i.e. "ps; size 16cm, 12cm", it must be a string and not a symbol
1944 ($gnuplot_term
1945 (let ((s (caddr opt)))
1946 (when (stringp s)
1947 (cond ((string= s "default") (setq s '$default))
1948 ((string= s "dumb") (setq s '$dumb))
1949 ((string= s "ps") (setq s '$ps))))
1950 (if (atom s)
1951 (setf (getf options :gnuplot_term) s)
1952 (merror
1953 (intl:gettext "Wrong argument for plot option \"gnuplot_term\". Expecting a string or a symbol but found \"~M\".") s))))
1955 (merror
1956 (intl:gettext "plot-options-parser: unknown plot option: ~M") opt))))
1957 ((symbolp opt)
1958 (case opt
1959 ($axes (setf (getf options :axes) t))
1960 ($box (setf (getf options :box) t))
1961 ($color_bar (setf (getf options :color_bar) t))
1962 ($color_bar_tics (remf options :color_bar_tics))
1963 ($grid2d (setf (getf options :grid2d) t))
1964 ($legend (remf options :legend))
1965 ($logx (setf (getf options :logx) t))
1966 ($logy (setf (getf options :logy) t))
1967 ($mesh_lines_color (remf options :mesh_lines_color))
1968 ($plot_realpart (setf (getf options :plot_realpart) t))
1969 ($run_viewer (setf (getf options :run_viewer) t))
1970 ($same_xy (setf (getf options :same_xy) t))
1971 ($same_xyz (setf (getf options :same_xyz) t))
1972 ($transform_xy (remf options :transform_xy))
1973 ($xtics (remf options :xtics))
1974 ($ytics (remf options :ytics))
1975 ($zmin (remf options :zmin))
1976 ($gnuplot_4_0 (setf (getf options :gnuplot_4_0) t))
1977 ($gnuplot_pm3d (setf (getf options :gnuplot_pm3d) t))
1978 ($gnuplot_strings (setf (getf options :gnuplot_strings) t))
1979 ($noaxes (setf (getf options :axes) nil))
1980 ($nobox (setf (getf options :box) nil))
1981 ($nocolor_bar (setf (getf options :color_bar) nil))
1982 ($nocolor_bat_tics (setf (getf options :color_bat_tics) nil))
1983 ($nogrid2d (setf (getf options :grid2d) nil))
1984 ($nolegend (setf (getf options :legend) nil))
1985 ($nologx (setf (getf options :logx) nil))
1986 ($nology (setf (getf options :logy) nil))
1987 ($nomesh_lines (setf (getf options :mesh_lines_color) nil))
1988 ($nopalette (setf (getf options :palette) nil))
1989 ($noplot_realpart (setf (getf options :plot_realpart) nil))
1990 ($norun_viewer (setf (getf options :run_viewer) nil))
1991 ($nosame_xy (setf (getf options :same_xy) nil))
1992 ($nosame_xyz (setf (getf options :same_xyz) nil))
1993 ($notransform_xy (setf (getf options :transform_xy) nil))
1994 ($noxtics (setf (getf options :xtics) nil))
1995 ($noytics (setf (getf options :ytics) nil))
1996 ($noztics (setf (getf options :ztics) nil))
1997 ($nozmin (setf (getf options :zmin) nil))
1998 ($nognuplot_4_0 (setf (getf options :gnuplot_4_0) nil))
1999 ($nognuplot_pm3d (setf (getf options :gnuplot_pm3d) nil))
2000 ($nognuplot_strings (setf (getf options :gnuplot_strings) nil))
2002 (merror (intl:gettext "Unknown plot option \"~M\".") opt))))))
2003 ;; plots that create a file work better in gnuplot than gnuplot_pipes
2004 (when (and (eq (getf options :plot_format) '$gnuplot_pipes)
2005 (or (eq (getf options :gnuplot_term) '$dumb)
2006 (getf options :pdf_file) (getf options :png_file)
2007 (getf options :ps_file) (getf options :svg_file)))
2008 (setf (getf options :plot_format) '$gnuplot))
2009 options)
2011 ;; natural numbers predicate
2012 (defun naturalp (n) (or (and (integerp n) (> n 0)) nil))
2014 ;; positive real numbers predicate
2015 (defun realpositivep (x) (or (and (realp x) (> x 0)) nil))
2017 ;; posible values for the axes option
2018 (defun axesoptionp (o) (if (member o '($x $y $solid)) t nil))
2020 ;; the 13 possibilities for the point types
2021 (defun pointtypep (p)
2022 (if (member p '($bullet $circle $plus $times $asterisk $box $square
2023 $triangle $delta $wedge $nabla $diamond $lozenge)) t nil))
2025 ;; Colors can only one of the named colors or a six-digit hexadecimal
2026 ;; number with a # suffix.
2027 (defun plotcolorp (color)
2028 (cond ((and (stringp color)
2029 (string= (subseq color 0 1) "#")
2030 (= (length color) 7)
2031 (ignore-errors (parse-integer (subseq color 1 6) :radix 16)))
2033 ((member color '($red $green $blue $magenta $cyan $yellow
2034 $orange $violet $brown $gray $black $white))
2036 (t nil)))
2038 ;; tries to convert az into a floating-point number between 0 and 360
2039 (defun parse-azimuth (az) (mod ($float (meval* az)) 360))
2041 ;; tries to convert el into a floating-poitn number between -180 and 180
2042 (defun parse-elevation (el) (- (mod (+ 180 ($float (meval* el))) 360) 180))
2044 ;; The following functions check the value of an option returning an atom
2045 ;; when there is only one argument or a list when there are several arguments
2048 ;; Checks for one or more items of the same type, using the test given
2049 (defun check-option (option test type &optional count)
2050 (when count
2051 (unless (= (1- (length option)) count)
2052 (merror
2053 (intl:gettext
2054 "Wrong number of arguments for plot option \"~M\". Expecting ~M but found ~M.")
2055 (car option) count (1- (length option)))))
2056 (dolist (item (cdr option))
2057 (when (not (funcall test item))
2058 (merror
2059 (intl:gettext "Wrong argument for plot option \"~M\". Expecting ~M but found \"~M\".") (car option) type item)))
2060 (if (= (length option) 2)
2061 (cadr option)
2062 (cdr option)))
2064 ;; Accepts one or more items of the same type or false.
2065 ;; When given, n is the maximum number of items.
2066 (defun check-option-b (option test type &optional count)
2067 (let ((n (- (length option) 1)))
2068 (when count
2069 (unless (< n (1+ count))
2070 (merror
2071 (intl:gettext
2072 "Plot option ~M must have ~M arguments, not ~M.")
2073 (car option) count (1- (length option)))))
2074 (cond
2075 ((= n 0)
2076 (merror
2077 (intl:gettext
2078 "Option ~M should be given arguments, or called by its name (no lists)")
2079 option))
2080 ((= n 1)
2081 (if (or (funcall test (cadr option)) (null (cadr option))
2082 (eq (cadr option) t))
2083 (cadr option)
2084 (merror
2085 (intl:gettext
2086 "Value of option ~M. should be ~M or false, not \"~M\".")
2087 (car option) type (cadr option))))
2088 ((> n 1)
2089 (dotimes (i n)
2090 (unless (funcall test (nth (+ i 1) option))
2091 (merror
2092 (intl:gettext
2093 "Value of option ~M should be ~M, not \"~M\".")
2094 (car option) type (nth (+ i 1) option))))
2095 (cdr option)))))
2097 ;; Boolean options can be [option], [option,true] or [option,false]
2098 (defun check-option-boole (option)
2099 (if (= 1 (length option))
2101 (if (and (= 2 (length option))
2102 (or (eq (cadr option) t) (null (cadr option))))
2103 (cadr option)
2104 (merror (intl:gettext "plot option ~M must be either true or false.")
2105 (car option)))))
2107 ;; label can be either [label, string, real, real] or
2108 ;; [label, [string_1, real, real],...,[string_n, real, real]]
2109 (defun check-option-label (option &aux opt)
2110 (if (not ($listp (cadr option)))
2111 (setq opt (list (cons '(mlist) (cdr option))))
2112 (setq opt (cdr option)))
2113 (dolist (item opt)
2114 (when (not (and ($listp item) (= 4 (length item)) (stringp (second item))
2115 (realp (setf (third item) (coerce-float (third item))))
2116 (realp (setf (fourth item) (coerce-float (fourth item))))))
2117 (merror
2118 (intl:gettext
2119 "Wrong argument ~M for option ~M. Must be either [label,\"text\",x,y] or [label, [\"text 1\",x1,y1],...,[\"text n\",xn,yn]]")
2120 item (car option))))
2121 opt)
2123 ;; one of the possible formats
2124 (defun check-option-format (option &aux formats)
2125 (setq formats '($geomview $gnuplot $gnuplot_pipes $mgnuplot $xmaxima))
2126 (unless (member (cadr option) formats)
2127 (merror
2128 (intl:gettext
2129 "Wrong argument ~M for option ~M. Must one of the following symbols: geomview, gnuplot, mgnuplot, xmaxima (or gnuplot_pipes in Unix)")
2130 (cadr option) (car option)))
2131 (cadr option))
2133 ; palette most be one or more Maxima lists starting with the name of one
2134 ;; of the 5 kinds: hue, saturation, value, gray or gradient.
2135 (defun check-option-palette (option)
2136 (if (and (= (length option) 2) (null (cadr option)))
2138 (progn
2139 (dolist (item (cdr option))
2140 (when (not (and ($listp item)
2141 (member (cadr item)
2142 '($hue $saturation $value $gray $gradient))))
2143 (merror
2144 (intl:gettext
2145 "Wrong argument ~M for option ~M. Not a valid palette.")
2146 item (car option))))
2147 (cdr option))))
2149 ;; style can be one or several of the names of the styles or one or several
2150 ;; Maxima lists starting with the name of one of the styles.
2151 (defun check-option-style (option)
2152 (if (and (= (length option) 2) (null (cadr option)))
2154 (progn
2155 (let (name parsed)
2156 (dolist (item (cdr option))
2157 (if ($listp item)
2158 (setq name (second item))
2159 (setq name item))
2160 (when (not (member name
2161 '($lines $points $linespoints $dots $impulses)))
2162 (merror
2163 (intl:gettext
2164 "Wrong argument ~M for option ~M. Not a valid style")
2165 name (car option)))
2166 (setq parsed (cons item parsed)))
2167 (reverse parsed)))))
2169 ;; Transform can be false or the name of a function for the transformation.
2170 (defun check-option-transform (option)
2171 (if (and (= (length option) 2)
2172 (or (atom (cadr option)) (null (cadr option))))
2173 (cadr option)
2174 (merror
2175 (intl:gettext
2176 "Wrong argument ~M for option ~M. Should be either false or the name of function for the transformation") option (car option))))
2178 ;; levels can be a single natural number (requested number of levels)
2179 ;; or two or more floating-point numbers.
2180 (defun check-option-levels (option)
2181 (cond
2182 ((< (length option) 3)
2183 (check-option option #'naturalp "a natural number" 1))
2185 (mapcar #'coerce-float (cdr option))
2186 (check-option option #'realp "a real number" (1- (length option))))))
2188 ;; Tries to get n numbers between fmin and fmax of the form d*10^e,
2189 ;; where d is either 1, 2 or 5.
2190 ;; It returns a list with n or less numbers
2191 ;; (adapted from procedure getticks of Xmaxima)
2193 (defun getlevels (fmin fmax n)
2194 (let ((len (- fmax fmin)) (best 0) levels val fac j1 j2 step ans)
2195 (dolist (v '(0.1 0.2 0.5))
2196 (setq val (ceiling (/ (log (/ len n v)) (log 10))))
2197 (setq fac (/ 1 v (expt 10 val)))
2198 (setq j1 (ceiling (* fmin fac)))
2199 (setq j2 (floor (* fmax fac)))
2200 (if (> j2 14)
2201 (setq step 5)
2202 (setq step 2))
2203 (setq levels nil)
2204 (do ((j j1 (1+ j))) ((> j j2))
2205 (push (/ j fac) levels))
2206 (when (> (length levels) best)
2207 (setq best (length levels))
2208 (setq ans (copy-list levels))))
2209 (reverse ans)))
2211 #| plot2d
2212 Examples:
2214 plot2d (sec(x), [x, -2, 2], [y, -20, 20]);
2216 plot2d (exp(3*s), [s, -2, 2], logy);
2218 plot2d ([parametric, cos(t), sin(t), [t, -%pi, %pi]], same_xy);
2220 xy:[[10,.6], [20,.9], [30,1.1], [40,1.3], [50,1.4]]$
2221 plot2d ( [ [discrete, xy], 2*%pi*sqrt(l/980) ], [l, 0, 50],
2222 [style, points, lines], [color, red, blue], [point_type, box],
2223 [legend, "experiment", "theory"],
2224 [xlabel, "pendulum's length (cm)"], [ylabel, "period (s)"]);
2226 plot2d ( x^2-1, [x, -3, 3], [y, -2, 10], nobox, [color, red],
2227 [ylabel, "x^2-1"], [plot_format, xmaxima]);
2229 plot2d ( x^2+y^2 = 1, [x, -2, 2], [y, -2 ,2]);
2231 (defmfun $plot2d
2232 (fun &optional xrange &rest extra-options
2233 &aux
2234 ($display2d nil) (*plot-realpart* *plot-realpart*)
2235 (options (copy-tree *plot-options*)) yrange output-file plot)
2236 ;; fun must be a maxima list with several objects: expressions (simple
2237 ;; functions), maxima lists (parametric or discrete cases).
2238 ;; A single parametric or discrete plot is placed inside a maxima list.
2239 (setf (getf options :type) "plot2d")
2240 (when (and (consp fun)
2241 (or (eq (second fun) '$parametric)
2242 (eq (second fun) '$contour)
2243 (eq (second fun) '$discrete)))
2244 (setq fun `((mlist) ,fun)))
2245 ;; If by now fun is not a maxima list, it is then a single expression
2246 (unless ($listp fun ) (setq fun `((mlist) ,fun)))
2247 ;; 2- Get names for the two axis and values for xmin and xmax if needed.
2248 ;; If any of the objects in the fun list is a simple function,
2249 ;; the xrange option is mandatory and will provide the name of
2250 ;; the horizontal axis and the values of xmin and xmax.
2251 (let ((xrange-required nil) (bounds-required nil) (yrange-required nil)
2252 small huge fpfun vars1 vars2 prange)
2253 #-clisp (setq small (- (/ most-positive-flonum 1024)))
2254 #+clisp (setq small (- (/ most-positive-double-float 1024.0)))
2255 #-clisp (setq huge (/ most-positive-flonum 1024))
2256 #+clisp (setq huge (/ most-positive-double-float 1024.0))
2257 (setf (getf options :ybounds) (list small huge))
2258 (dolist (f (rest fun))
2259 (if ($listp f)
2260 (progn
2261 (case ($first f)
2262 ($parametric
2263 (unless bounds-required
2264 (setq bounds-required t)
2265 ;; Default X and Y bound large so parametric plots don't get
2266 ;; prematurely clipped. Don't use most-positive-flonum
2267 ;; because draw2d will overflow.
2268 (setf (getf options :xbounds) (list small huge)))
2269 (setq prange (check-range ($fourth f)))
2270 ;; The two expressions can only depend on the parameter given
2271 (setq fpfun (coerce-float-fun ($second f) ($rest prange -2)))
2272 (setq vars1 ($listofvars (mfuncall fpfun ($first prange))))
2273 (setq fpfun (coerce-float-fun ($third f) ($rest prange -2)))
2274 (setq vars2 ($listofvars (mfuncall fpfun ($first prange))))
2275 (setq vars1 ($listofvars `((mlist) ,vars1 ,vars2)))
2276 (setq vars1 (delete ($first prange) vars1))
2277 (when (> ($length vars1) 0)
2278 (merror
2279 (intl:gettext
2280 "plot2d: parametric expressions ~M and ~M should depend only on ~M")
2281 ($second f) ($third f) ($first prange))))
2282 ($contour
2283 (setq xrange-required t)
2284 (setq fpfun (coerce-float-fun ($second f) ($rest xrange -2)))
2285 (setq vars1 ($listofvars (mfuncall fpfun ($first xrange))))
2286 (when (and (= ($length vars1) 2)
2287 (not (member ($first xrange) vars1)))
2288 (merror
2289 (intl:gettext "plot2d: ~M is not one of the variables in ~M")
2290 ($first xrange) f))
2291 (setq vars1 (delete ($first xrange) vars1))
2292 (if (< ($length vars1) 2)
2293 (progn
2294 (if yrange-required
2295 (unless (or (= ($length vars1) 0)
2296 (eq ($first yrange) ($first vars1)))
2297 (merror
2298 (intl:gettext
2299 "plot2d: ~M should only depend on ~M and ~M")
2300 f ($first xrange) ($first vars1)))
2301 (progn
2302 (setq yrange-required t)
2303 (if (null extra-options)
2304 (merror
2305 (intl:gettext
2306 "plot2d: Missing interval for variable 2."))
2307 (progn
2308 (setq yrange (pop extra-options))
2309 (setq vars1 (delete ($first yrange) vars1))
2310 (unless (= ($length vars1) 0)
2311 (merror
2312 (intl:gettext
2313 "plot2d: ~M should only depend on ~M and ~M")
2314 f ($first xrange) ($first yrange)))
2315 (setq yrange (check-range yrange))
2316 (setf (getf options :xvar) ($first xrange))
2317 (setf (getf options :yvar) ($first yrange))
2318 (setf (getf options :x) (cddr xrange))
2319 (setf (getf options :y) (cddr yrange)))))))
2320 (merror
2321 (intl:gettext "plot2d: ~M should only depend on 2 variables")
2322 ($second f))))
2323 ($discrete)
2325 (merror
2326 (intl:gettext
2327 "plot2d: a keyword 'parametric' or 'discrete' missing in ~M")
2328 f))))
2329 ;; The expression represents a function, explicit or implicit
2330 (progn
2331 (unless xrange-required
2332 (setq xrange-required t)
2333 (setq xrange (check-range xrange))
2334 (setq xrange-required t)
2335 (unless (getf options :xlabel)
2336 (setf (getf options :xlabel) (ensure-string (second xrange))))
2337 (setf (getf options :xvar) (cadr xrange))
2338 (setf (getf options :x) (cddr xrange)))
2339 (if (and (listp f) (eq 'mequal (caar f)))
2340 (progn
2341 ;; Implicit function
2342 (setq
2343 fpfun
2344 (coerce-float-fun (m- ($lhs f) ($rhs f)) ($rest xrange -2)))
2345 (setq vars1 ($listofvars (mfuncall fpfun ($first xrange))))
2346 (when
2347 (and
2348 (= ($length vars1) 2)
2349 (not (member ($first xrange) vars1)))
2350 (merror
2351 (intl:gettext
2352 "plot2d: ~M is not one of the variables in ~M")
2353 ($first xrange) f))
2354 (setq vars1 (delete ($first xrange) vars1))
2355 (if (< ($length vars1) 2)
2356 (progn
2357 (if yrange-required
2358 (unless
2359 (or (= ($length vars1) 0)
2360 (eq ($first yrange) ($first vars1)))
2361 (merror
2362 (intl:gettext
2363 "plot2d: ~M should only depend on ~M and ~M")
2364 f ($first xrange) ($first vars1)))
2365 (progn
2366 (setq yrange-required t)
2367 (if (null extra-options)
2368 (merror
2369 (intl:gettext
2370 "plot2d: Missing interval for variable 2."))
2371 (progn
2372 (setq yrange (pop extra-options))
2373 (setq vars1 (delete ($first yrange) vars1))
2374 (unless (= ($length vars1) 0)
2375 (merror
2376 (intl:gettext
2377 "plot2d: ~M should only depend on ~M and ~M")
2378 f ($first xrange) ($first yrange)))
2379 (setq yrange (check-range yrange))
2380 (setf (getf options :yvar) ($first yrange))
2381 (setf (getf options :y) (cddr yrange)))))))
2382 (merror
2383 (intl:gettext
2384 "plot2d: ~M should only depend on 2 variables")
2385 f)))
2386 (progn
2387 ;; Explicit function
2388 (setq fpfun (coerce-float-fun f ($rest xrange -2)))
2389 (setq vars1 ($listofvars (mfuncall fpfun ($first xrange))))
2390 (setq vars1 (delete ($first xrange) vars1))
2391 (when (> ($length vars1) 0)
2392 (merror
2393 (intl:gettext
2394 "plot2d: expression ~M~% should depend only on ~M, or be an expression of 2 variables~% equal another expression of the same variables.")
2395 f ($first xrange))))))))
2396 (when (not xrange-required)
2397 ;; Make the default ranges on X nd Y large so parametric plots
2398 ;; don't get prematurely clipped. Don't use most-positive-flonum
2399 ;; because draw2d will overflow.
2400 (setf (getf options :xbounds) (list small huge))
2401 (when xrange
2402 ;; second argument was really a plot option, not an xrange
2403 (setq extra-options (cons xrange extra-options)))))
2404 ;; If no global options xlabel or ylabel have been given, choose
2405 ;; a default value for them: the expressions given, converted
2406 ;; to Maxima strings, if their length is less than 50 characters,
2407 ;; or the default "x" and "y" otherwise.
2408 (when (= (length fun) 2)
2409 (let ((v (second fun)) xlabel ylabel)
2410 (cond ((atom v)
2411 (setq xlabel "x") (setq ylabel ($sconcat v)))
2412 ((eq (second v) '$parametric)
2413 (setq xlabel ($sconcat (third v)))
2414 (setq ylabel ($sconcat (fourth v))))
2415 ((eq (second v) '$discrete)
2416 (setq xlabel "x") (setq ylabel "y"))
2417 ((eq (second v) '$contour)
2418 (setq xlabel (ensure-string (getf options :xvar)))
2419 (setq ylabel (ensure-string (getf options :yvar))))
2421 (setq xlabel "x") (setq ylabel ($sconcat v))))
2422 (unless (getf options :xlabel)
2423 (if (< (length xlabel) 50) (setf (getf options :xlabel) xlabel)))
2424 (unless (getf options :ylabel)
2425 (if (< (length ylabel) 50) (setf (getf options :ylabel) ylabel)))))
2426 ;; For explicit functions, default ylabel is the name of the 2nd variable
2427 (when (getf options :yvar)
2428 (setf (getf options :ylabel) ($sconcat (getf options :yvar))))
2429 ;; Parse the given options into the options list
2430 (setq options (plot-options-parser extra-options options))
2431 (when (getf options :y) (setf (getf options :ybounds) (getf options :y)))
2432 ;; Remove axes labels when no box is used in gnuplot
2433 (when (and (member :box options) (not (getf options :box))
2434 (not (eq (getf options :plot_format) '$xmaxima)))
2435 (remf options :xlabel)
2436 (remf options :ylabel))
2437 ;; check options given
2438 (let ((xmin (first (getf options :x))) (xmax (second (getf options :x))))
2439 (when
2440 (and (getf options :logx) xmin xmax)
2441 (if (> xmax 0)
2442 (when (<= xmin 0)
2443 (let ((revised-xmin (/ xmax 1000)))
2444 (mtell
2445 (intl:gettext
2446 "plot2d: lower bound must be positive when using 'logx'.~%plot2d: assuming lower bound = ~M instead of ~M")
2447 revised-xmin xmin)
2448 (setf (getf options :x) (list revised-xmin xmax))
2449 (setq xrange `((mlist) ,(second xrange) ,revised-xmin ,xmax))))
2450 (merror
2451 (intl:gettext
2452 "plot2d: upper bound must be positive when using 'logx'; found: ~M")
2453 xmax))))
2454 (let ((ymin (first (getf options :y)))
2455 (ymax (second (getf options :y))))
2456 (when (and (getf options :logy) ymin ymax)
2457 (if (> ymax 0)
2458 (when (<= ymin 0)
2459 (let ((revised-ymin (/ ymax 1000)))
2460 (mtell
2461 (intl:gettext
2462 "plot2d: lower bound must be positive when using 'logy'.~%plot2d: assuming lower bound = ~M instead of ~M")
2463 revised-ymin ymin)
2464 (setf (getf options :y) (list revised-ymin ymax))))
2465 (merror
2466 (intl:gettext
2467 "plot2d: upper bound must be positive when using 'logy'; found: ~M")
2468 ymax))))
2469 (setq *plot-realpart* (getf options :plot_realpart))
2470 ;; Creates the object that will be passed to the external graphic program
2471 (case (getf options :plot_format)
2472 ($xmaxima
2473 (setq plot (make-instance 'xmaxima-plot)))
2474 ($gnuplot
2475 (setq plot (make-instance 'gnuplot-plot)))
2476 ($gnuplot_pipes
2477 (setq plot (make-instance 'gnuplot-plot))
2478 (setf (slot-value plot 'pipe) T))
2480 (merror (intl:gettext "plot2d: plot format ~M not supported")
2481 (getf options :plot_format))))
2482 ;; Parse plot object and pass it to the graphic program
2483 (setq output-file (plot-preamble plot options))
2484 (plot2d-command plot fun options xrange)
2485 (plot-shipout plot options output-file))
2487 (defun msymbolp (x)
2488 (and (symbolp x) (char= (char (symbol-value x) 0) #\$)))
2490 (defmfun $tcl_output (lis i &optional (skip 2))
2491 (when (not (typep i 'fixnum))
2492 (merror
2493 (intl:gettext "tcl_ouput: second argument must be an integer; found ~M")
2495 (when (not ($listp lis))
2496 (merror
2497 (intl:gettext "tcl_output: first argument must be a list; found ~M") lis))
2498 (format *standard-output* "~% {")
2499 (cond (($listp (second lis))
2500 (loop for v in lis
2502 (format *standard-output* "~,8,,,,,'eg " (nth i v))))
2504 (setq lis (nthcdr i lis))
2505 (loop with v = lis while v
2507 (format *standard-output* "~,8,,,,,'eg " (car v))
2508 (setq v (nthcdr skip v)))))
2509 (format *standard-output* "~% }"))
2510 (defun tcl-output-list ( st lis )
2511 (cond ((null lis) )
2512 ((atom (car lis))
2513 (princ " { " st)
2514 (loop for v in lis
2515 count t into n
2516 when (eql 0 (mod n 5))
2517 do (terpri st)
2519 (format st "~,8,,,,,'eg " v))
2520 (format st " }~%"))
2521 (t (tcl-output-list st (car lis))
2522 (tcl-output-list st (cdr lis)))))
2524 (defun check-range (range &aux tem a b)
2525 (or (and ($listp range)
2526 (setq tem (cdr range))
2527 (or (symbolp (car tem)) ($subvarp (car tem)))
2528 (numberp (setq a ($float (meval* (second tem)))))
2529 (numberp (setq b ($float (meval* (third tem)))))
2530 (< a b))
2531 (if range
2532 (merror
2533 (intl:gettext "plotting: range must be of the form [variable, min, max]; found: ~M")
2534 range)
2535 (merror
2536 (intl:gettext "plotting: no range given; must supply range of the form [variable, min, max]"))))
2537 `((mlist) ,(car tem) ,(float a) ,(float b)))
2539 (defmfun $zero_fun (x y) x y 0.0)
2541 (defun output-points (pl &optional m)
2542 "If m is supplied print blank line every m lines"
2543 (let ((j -1))
2544 (declare (fixnum j))
2545 (loop for i below (length (polygon-pts pl))
2546 with ar = (polygon-pts pl)
2547 do (print-pt (aref ar i))
2548 (setq i (+ i 1))
2549 (print-pt (aref ar i))
2550 (setq i (+ i 1))
2551 (print-pt (aref ar i))
2552 (terpri $pstream)
2553 (cond (m
2554 (setq j (+ j 1))
2555 (cond ((eql j (the fixnum m))
2556 (terpri $pstream)
2557 (setq j -1)))))
2560 (defun output-points-tcl (dest pl m)
2561 (format dest " {matrix_mesh ~%")
2562 ;; x y z are done separately:
2563 (loop for off from 0 to 2
2564 with ar = (polygon-pts pl)
2565 with i of-type fixnum = 0
2566 do (setq i off)
2567 (format dest "~%{")
2568 (loop
2569 while (< i (length ar))
2570 do (format dest "~% {")
2571 (loop for j to m
2572 do (print-pt (aref ar i))
2573 (setq i (+ i 3)))
2574 (format dest "}~%"))
2575 (format dest "}~%"))
2576 (format dest "}~%"))
2578 (defun show-open-plot (ans file)
2579 (cond ($show_openplot
2580 (with-open-file (st1 (plot-temp-file (format nil "maxout~d.xmaxima" (getpid))) :direction :output :if-exists :supersede)
2581 (princ ans st1))
2582 ($system (concatenate 'string *maxima-prefix*
2583 (if (string= *autoconf-windows* "true") "\\bin\\" "/bin/")
2584 $xmaxima_plot_command)
2585 #-(or (and sbcl win32) (and sbcl win64) (and ccl windows))
2586 (format nil " ~s &" file)
2587 #+(or (and sbcl win32) (and sbcl win64) (and ccl windows))
2588 file))
2589 (t (princ ans) "")))
2591 ;; contour_plot now punts to plot2d
2592 (defmfun $contour_plot (expr &rest optional-args)
2593 (let ((command "plot2d ([contour, "))
2594 (setq command ($sconcat command expr "]"))
2595 (when optional-args
2596 (dolist (arg optional-args)
2597 (setq command ($sconcat command ", " arg))))
2598 (setq command ($sconcat command ")"))
2599 (mtell (intl:gettext "contour_plot is now obsolete. Using plot2d instead:~%"))
2600 (mtell "~M~%" command)
2601 (apply #'$plot2d (cons `((mlist) $contour ,expr) optional-args))))
2603 #| plot3d
2604 Examples:
2606 plot3d (2^(-u^2 + v^2), [u, -3, 3], [v, -2, 2], [palette, false]);
2608 plot3d ( log ( x^2*y^2 ), [x, -2, 2], [y, -2, 2], [grid, 29, 29]);
2610 expr_1: cos(y)*(10.0+6*cos(x))$
2611 expr_2: sin(y)*(10.0+6*cos(x))$
2612 expr_3: -6*sin(x)$
2613 plot3d ([expr_1, expr_2, expr_3], [x, 0, 2*%pi], [y, 0, 2*%pi],
2614 ['grid, 40, 40], [z,-8,8]);
2616 plot3d (cos (-x^2 + y^3/4), [x, -4, 4], [y, -4, 4],
2617 [mesh_lines_color, false], [elevation, 0], [azimuth, 0], [grid, 150, 150]);
2619 spherical: make_transform ([th, phi,r], r*sin(phi)*cos(th),
2620 r*sin(phi)*sin(th), r*cos(phi))$
2621 plot3d ( 5, [th, 0, 2*%pi], [phi, 0, %pi], [transform_xy, spherical],
2622 [palette, [value, 0.65, 0.7, 0.1, 0.9]], [plot_format,xmaxima]);
2624 V: 1 / sqrt ( (x+1)^2+y^2 ) - 1 / sqrt( (x-1)^2+y^2 )$
2625 plot3d ( V, [x, -2, 2], [y, -2, 2], [z, -4, 4]);
2627 (defmfun $plot3d
2628 (fun &rest extra-options
2629 &aux
2630 lvars xrange yrange titles output-file functions exprn domain tem
2631 (options (copy-tree *plot-options*)) (*plot-realpart* *plot-realpart*)
2632 (usage (intl:gettext
2633 "plot3d: Usage.
2634 To plot a single function f of 2 variables v1 and v2:
2635 plot3d (f, [v1, min, max], [v2, min, max], options)
2636 A parametric representation of a surface with parameters v1 and v2:
2637 plot3d ([f1, f2, f3], [v1, min, max], [v2, min, max], options)
2638 Several functions depending on the two variables v1 and v2:
2639 plot3d ([f1, f2, ..., fn, [v1, min, max], [v2, min, max]], options)")))
2640 (setf (getf options :type) "plot3d")
2641 ;; Ensure that fun is a list of expressions and maxima lists, followed
2642 ;; by a domain definition
2643 (if ($listp fun)
2644 (if (= 1 (length (check-list-plot3d fun)))
2645 ;; fun consisted of a single parametric expression
2646 (setq fun `(,fun ,(pop extra-options) ,(pop extra-options)))
2647 ;; fun was a maxima list with several independent surfaces
2648 (pop fun))
2649 ;; fun consisted of a single expression
2650 (setq fun `(,fun ,(pop extra-options) ,(pop extra-options))))
2651 ;; go through all the independent surfaces creating the functions stack
2652 (loop
2653 (setq exprn (pop fun))
2654 (if ($listp exprn)
2655 (progn
2656 (setq domain (check-list-plot3d exprn))
2657 (case (length domain)
2659 ;; exprn is a parametric representation of a surface
2660 (let (vars1 vars2 vars3)
2661 ;; list fun should have two valid ranges after exprn
2662 (setq xrange (check-range (pop fun)))
2663 (setq yrange (check-range (pop fun)))
2664 ;; list of the two variables for the parametric equations
2665 (setq lvars `((mlist),(second xrange) ,(second yrange)))
2666 ;; make sure that the 3 parametric equations depend only
2667 ;; on the two variables in lvars
2668 (setq vars1
2669 ($listofvars (mfuncall
2670 (coerce-float-fun (second exprn) lvars)
2671 (second lvars) (third lvars))))
2672 (setq vars2
2673 ($listofvars (mfuncall
2674 (coerce-float-fun (third exprn) lvars)
2675 (second lvars) (third lvars))))
2676 (setq vars3
2677 ($listofvars (mfuncall
2678 (coerce-float-fun (fourth exprn) lvars)
2679 (second lvars) (third lvars))))
2680 (setq lvars ($listofvars `((mlist) ,vars1 ,vars2 ,vars3)))
2681 (if (<= ($length lvars) 2)
2682 ;; we do have a valid parametric set. Push it into
2683 ;; the functions stack, along with their domain
2684 (progn
2685 (push `(,exprn ,xrange ,yrange) functions)
2686 ;; add a title to the titles stack
2687 (push "Parametric function" titles)
2688 ;; unknown variables in the parametric equations
2689 ;; ----- GNUPLOT 4.0 WORK-AROUND -----
2690 (when (and ($constantp (fourth exprn))
2691 (getf options :gnuplot_4_0))
2692 (setf (getf options :const_expr)
2693 ($float (meval (fourth exprn))))))
2694 (merror
2695 (intl:gettext "plot3d: there must be at most two variables; found: ~M")
2696 lvars))))
2698 ;; expr is a simple function with its own domain. Push the
2699 ;; function and its domain into the functions stack
2700 (setq xrange (second domain))
2701 (setq yrange (third domain))
2702 (push `(,(second exprn) ,xrange ,yrange) functions)
2703 ;; push a title for this plot into the titles stack
2704 (if (< (length (ensure-string (second exprn))) 36)
2705 (push (ensure-string (second exprn)) titles)
2706 (push "Function" titles)))
2708 ;; syntax error. exprn does not have the expected form
2709 (merror
2710 (intl:gettext
2711 "plot3d: argument must be a list of three expressions; found: ~M")
2712 exprn))))
2713 (progn
2714 ;; exprn is a simple function, defined in the global domain.
2715 (if (and (getf options :xvar) (getf options :yvar))
2716 ;; the global domain has already been defined; use it.
2717 (progn
2718 (setq xrange `((mlist) ,(getf options :xvar)
2719 ,(first (getf options :x))
2720 ,(second (getf options :x))))
2721 (setq yrange `((mlist) ,(getf options :yvar)
2722 ,(first (getf options :y))
2723 ,(second (getf options :y)))))
2724 ;; the global domain should be defined by the last two lists
2725 ;; in fun. Extract it and check whether it is valid.
2726 (progn
2727 (setq
2728 domain
2729 (check-list-plot3d (append `((mlist) ,exprn) (last fun 2))))
2730 (setq fun (butlast fun 2))
2731 (if (= 3 (length domain))
2732 ;; domain is valid. Make it the global one.
2733 (progn
2734 (setq xrange (second domain))
2735 (setq yrange (third domain))
2736 (setf (getf options :xvar) (second xrange))
2737 (setf (getf options :x) (cddr xrange))
2738 (setf (getf options :yvar) (second yrange))
2739 (setf (getf options :y) (cddr yrange)))
2740 (merror usage))))
2741 ;; ----- GNUPLOT 4.0 WORK-AROUND -----
2742 (when (and ($constantp exprn) (getf options :$gnuplot_4_0))
2743 (setf (getf options :const_expr) ($float (meval exprn))))
2744 ;; push the function and its domain into the functions stack
2745 (push `(,exprn ,xrange ,yrange) functions)
2746 ;; push a title for this plot into the titles stack
2747 (if (< (length (ensure-string exprn)) 36)
2748 (push (ensure-string exprn) titles)
2749 (push "Function" titles))))
2750 (when (= 0 (length fun)) (return)))
2751 ;; recover the original ordering for the functions and titles stacks
2752 (setq functions (reverse functions))
2753 (setq titles (reverse titles))
2754 ;; parse the options given to plot3d
2755 (setq options (plot-options-parser extra-options options))
2756 (setq tem (getf options :transform_xy))
2757 (when (and (member :gnuplot_pm3d options) (null (getf options :gnuplot_pm3d)))
2758 (setf (getf options :palette) nil))
2759 (setq *plot-realpart* (getf options :plot_realpart))
2760 ;; set up the labels for the axes, unless no box is being shown
2761 (unless (and (member :box options) (not (getf options :box)))
2762 (if (and (getf options :xvar) (getf options :yvar) (null tem))
2763 (progn
2764 ;; Don't set xlabel (ylabel) if the user specified one.
2765 (unless (getf options :xlabel)
2766 (setf (getf options :xlabel) (ensure-string (getf options :xvar))))
2767 (unless (getf options :ylabel)
2768 (setf (getf options :ylabel) (ensure-string (getf options :yvar)))))
2769 (progn
2770 (setf (getf options :xlabel) "x")
2771 (setf (getf options :ylabel) "y")))
2772 (unless (getf options :zlabel) (setf (getf options :zlabel) "z")))
2773 ;; x and y should not be bound, when an xy transformation function is used
2774 (when tem (remf options :x) (remf options :y))
2775 ;; Set up the plot command
2776 (let (plot (legend (getf options :legend)))
2777 ;; titles will be a list. Titles given in the legend option prevail
2778 ;; over titles generated by plot3d. No legend if option [legend,false]
2779 (unless (listp legend) (setq legend (list legend)))
2780 (when (member :legend options)
2781 (if (first legend) (setq titles legend)) (setq titles nil))
2782 (case (getf options :plot_format)
2783 ($xmaxima
2784 (setq plot (make-instance 'xmaxima-plot)))
2785 ($gnuplot
2786 (setq plot (make-instance 'gnuplot-plot)))
2787 ($gnuplot_pipes
2788 (setq plot (make-instance 'gnuplot-plot))
2789 (setf (slot-value plot 'pipe) T))
2790 ($geomview
2791 (setq plot (make-instance 'geomview-plot)))
2793 (merror (intl:gettext "plot3d: plot format ~M not supported")
2794 (getf options :plot_format))))
2795 ;; Parse plot object and pass it to the graphic program
2796 (setq output-file (plot-preamble plot options))
2797 (plot3d-command plot functions options titles)
2798 (plot-shipout plot options output-file)))
2800 ;; Given a Maxima list with 3 elements, checks whether it represents a function
2801 ;; defined in a 2-dimensional domain or a parametric representation of a
2802 ;; 3-dimensional surface, depending on two parameters.
2803 ;; The return value will be a Maxima list if the test is succesfull or nil
2804 ;; otherwise.
2805 ;; In the case of a function and a 2D domain, it returns the domain, validated.
2806 ;; When it is a parametric representation it returns an empty Maxima list.
2808 (defun check-list-plot3d (lis)
2809 (let (xrange yrange)
2810 ;; Makes sure list has the form ((mlist) $atom item1 item2)
2811 (unless (and ($listp lis) (= 3 ($length lis)) (not ($listp (second lis))))
2812 (return-from check-list-plot3d nil))
2813 ;; we might have a function with domain or a parametric representation
2814 (if ($listp (third lis))
2815 ;; lis is probably a function with a valid domain
2816 (if ($listp (fourth lis))
2817 ;; we do have a function and a domain. Return the domain
2818 (progn
2819 (setq xrange (check-range (third lis)))
2820 (setq yrange (check-range (fourth lis)))
2821 (return-from check-list-plot3d `((mlist) ,xrange ,yrange)))
2822 ;; wrong syntax: [expr1, list, expr2]
2823 (return-from check-list-plot3d nil))
2824 ;; lis is probably a parametric representation
2825 (if ($listp (fourth lis))
2826 ;; wrong syntax: [expr1, expr2, list]
2827 (return-from check-list-plot3d nil)
2828 ;; we do have a parametric representation. Return an empty list
2829 (return-from check-list-plot3d '((mlist)))))))