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