Add harmonic_number and harmonic_to_psi
[maxima.git] / src / displa.lisp
blob5b6fd958c2482d5d56201ab792c3ae43f5e94e86
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module displa)
15 ;; N.B. You must read the macro file before reading this file.
17 (load-macsyma-macros displm)
19 (defmvar $stardisp nil
20 "Causes factors of products to be separated by * when displayed.")
22 (defmvar $leftjust nil
23 "Causes equations to be drawn left justified rather than centered.
24 For slow consoles.")
26 (defmvar $noundisp nil)
28 (defmvar displayp nil "Is T when inside of `displa'")
30 ;; Parameters which control how boxes, absolute value signs,
31 ;; evaluation-at-a-point bars, and matrices are drawn.
33 (defmvar $boxchar "\"" "Character used for drawing boxes.")
34 (defmvar $absboxchar "!" "Character used for drawing absolute value signs and 'evaluation at' signs.")
35 (defmvar $lmxchar "[" "Character used for drawing the left edge of a matrix.")
36 (defmvar $rmxchar "]" "Character used for drawing the right edge of a matrix.")
38 (defvar linearray (make-array 80. :initial-element nil))
40 (defun maxima-display (form &key (stream *standard-output*) )
41 (let ((*standard-output* stream))
42 (displa form)))
44 (defvar *alt-display2d* nil)
45 (defvar *alt-display1d* nil)
47 (defun displa (form)
48 (if (not $ttyoff)
49 (cond ($display2d
50 (cond (*alt-display2d* (apply *alt-display2d* form ()))
52 (let ((displayp t)
53 (linearray (if displayp
54 (make-array 80. :initial-element nil)
55 linearray))
56 (mratp (checkrat form))
57 (maxht 1) (maxdp 0) (width 0)
58 (height 0) (depth 0) (level 0) (size 2)
59 (break 0) (right 0) (lines 1) bkpt
60 (bkptwd 0) (bkptht 1) (bkptdp 0) (bkptout 0)
61 (bkptlevel 0) in-p)
62 (unwind-protect
63 (progn
64 (setq form (dimension form nil 'mparen 'mparen 0 0))
65 (checkbreak form width)
66 (output form (if (and (not $leftjust) (= 2 lines))
67 (- $linel (- width bkptout))
68 0)))
69 ;; make sure the linearray gets cleared out.
70 (fill linearray nil))))))
72 (if *alt-display1d*
73 (apply *alt-display1d* form ())
74 (linear-displa form))))))
76 (defmspec $with_default_2d_display (expr)
77 (let ((*alt-display2d* nil)
78 ($display2d t)
79 (mexpr (cons '(mprog) (cdr expr))))
80 (meval mexpr)))
82 ;;; (LINEAR-DISPLA <thing-to-display>)
83 ;;;
84 ;;; Display text linearly. This function should be usable in any case
85 ;;; DISPLA is usable and will attempt to do something reasonable with
86 ;;; its input.
88 (defun linear-displa (form)
89 (fresh-line *standard-output*)
90 (mgrind form *standard-output*)
91 (mterpri))
93 (defmvar $display_format_internal nil
94 "Setting this TRUE can help give the user a greater understanding
95 of the behavior of maxima on certain of his problems,
96 especially those involving roots and quotients")
98 (defun nformat-check (form)
99 (if (and $display_format_internal
100 (not (or (atom form) (atom (car form)) (specrepp form))))
101 form
102 (nformat form)))
104 (defun dimension (form result lop rop w right)
105 (let ((level (1+ level))
106 (break (if (and w break) (+ w break))))
107 (setq form (nformat-check form))
108 (cond ((atom form)
109 (dimension-atom form result))
110 ((and (atom (car form)) (setq form (cons '(mprogn) form)) nil))
111 ((or (<= (lbp (caar form)) (rbp lop)) (>= (lbp rop) (rbp (caar form))))
112 (dimension-paren form result))
113 ((member 'array (car form) :test #'eq) (dimension-array form result))
114 ((safe-get (caar form) 'dimension)
115 (funcall (get (caar form) 'dimension) form result))
116 (t (dimension-function form result)))))
118 (defvar atom-context 'dimension-list)
119 ;; bound by DIMENSION-ARRAY and DIMENSION-FUNCTION.
120 ;; This ATOM-CONTEXT put in by GJC so that MCW could have a clean
121 ;; hook by which to write his extensions for vector-underbars.
123 ;; Referenced externally by RAT;FLOAT.
125 (defun dimension-atom (form result)
126 (cond ((and (symbolp form) (get form atom-context))
127 (funcall (get form atom-context) form result))
128 ((stringp form) (dimension-string (makestring form) result))
129 ((member (marray-type form) '(array hash-table $functional))
130 (dimension-array-object form result))
131 (t (dimension-string (makestring form) result))))
133 ;; Referenced externally by anyone who wants to display something as
134 ;; a funny looking atom, e.g. Trace, Mformat.
136 (defun dimension-string (dummy result &aux crp)
137 (setq width 0 height 1 depth 0)
138 (do ((l dummy (cdr l)))
139 ((null l))
140 (incf width)
141 (if (char= (car l) #\newline) (setq crp t)))
142 (cond ((or (and (checkfit width) (not crp)) (not break))
143 (nreconc dummy result))
144 (t (setq width 0)
145 (do ((l dummy) (w (- $linel (- break bkptout))))
146 ((null l) (checkbreak result width) result)
147 (setq dummy l l (cdr l))
148 (cond ((char= (car dummy) #\newline)
149 (forcebreak result width)
150 (setq result nil w (+ $linel width)))
151 (t (incf width)
152 (when (and (= w width) l)
153 (forcebreak (cons #\\ result) width)
154 (setq result nil w (+ $linel width))
155 (incf width))
156 (setq result (rplacd dummy result))))))))
158 (defun makestring (atom)
159 (let (dummy)
160 (cond ((numberp atom) (exploden atom))
161 ((stringp atom)
162 (setq dummy (coerce atom 'list))
163 (if $stringdisp
164 (cons #\" (nconc dummy (list #\")))
165 dummy))
166 ((not (symbolp atom)) (exploden atom))
167 ((and (setq dummy (get atom 'reversealias))
168 (not (and (member atom $aliases :test #'eq) (get atom 'noun))))
169 (exploden (stripdollar dummy)))
170 ((not (eq (getop atom) atom))
171 (makestring (getop atom)))
172 (t (setq dummy (exploden atom))
173 (cond
174 ((null dummy) nil)
175 ((char= #\$ (car dummy)) (cdr dummy))
176 ((char= #\% (car dummy)) (cdr dummy))
177 ($lispdisp (cons #\? dummy))
178 (t dummy))))))
180 (defun dimension-paren (form result)
181 (setq result (cons #\) (dimension form (cons #\( result) 'mparen 'mparen 1 (1+ right))))
182 (incf width 2)
183 result)
185 (defun dimension-array (x result)
186 (prog (dummy bas (w-base 0) (h-base 0) (d-base 0))
187 (if (eq (caar x) 'mqapply)
188 (setq dummy (cadr x) x (cdr x))
189 (setq dummy (caar x)))
190 (cond ((or (not $noundisp) (not (symbolp (caar x)))))
191 ((and (get (caar x) 'verb) (get (caar x) 'alias))
192 (push-string "''" result) (setq w-base 2))
193 ((and (get (caar x) 'noun) (not (member (caar x) (cdr $aliases) :test #'eq))
194 (not (get (caar x) 'reversealias)))
195 (setq result (cons #\' result) w-base 1)))
196 (setq
197 bas (if (and (not (atom dummy)) (member 'array (car dummy) :test #'eq))
198 (let ((break nil) (right 0)) (dimension-paren dummy nil))
199 (let ((atom-context 'dimension-array))
200 (dimension dummy nil lop 'mfunction nil 0)))
201 w-base (+ w-base width)
202 h-base height
203 d-base depth)
204 (multiple-value-bind (w00 w01 w10 w11 h00 h01 h10 h11 d00 d01 d10 d11 pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output)
205 (dimension-indices dummy (cdr x))
206 (declare (special pre-subscripts pre-superscripts post-subscripts post-superscripts))
207 (cond
208 ((not (checkfit (setq width (+ w-base (max w00 w01) (max w10 w11))))) ;; ?? !!
209 (return (dimension-function (cons '(subscript) (cons dummy (cdr x))) result)))
210 ((and (atom (car bas)) (char= #\ (car bas))) ; Adding the test (atom (car bas))
211 (setq result (cons (cons 0 (cons (- h-base) post-subscripts)) bas) depth (max (+ h-base d-base) depth))) ;; ?? !!
213 (setq result
214 (append
215 (if (and (> w11 0) (> w10 w11)) (list (list (- w10 w11) 0)))
216 (remove-if #'(lambda (l) (= (length l) 2))
217 (list
218 (append (list (- w10) (+ h-base d11)) post-superscripts-output)
219 (append (list 0 (- h10)) post-subscripts-output)
220 (append (list 0 0) bas)
221 (append (list (if (> w00 0) (- w01) 0) (+ h-base d01)) pre-superscripts-output)
222 (append (list (if (> w01 w00) (- w01 w00) 0) (- h00)) pre-subscripts-output)))
223 result)
224 height (+ h-base (max (+ d01 h01) (+ d11 h11)))
225 depth (+ d-base (max (+ d00 h00) (+ d10 h10))))))
226 (update-heights height depth)
227 (return result))))
229 (defun dimension-indices (base-symbol indices)
230 (let
231 ((display-indices (safe-mget base-symbol 'display-indices))
232 (separator (let ((x (safe-$get base-symbol '$display_index_separator)))
233 (if (or (null x) (stringp x)) x (coerce (mstring x) 'string)))))
234 (if (and (> (length display-indices) 0) (not (= (length display-indices) (length indices))))
235 ;; Ignore DISPLAY-INDICES if it's nonempty and not the same size as INDICES.
236 (setq display-indices nil))
237 (let (pre-subscripts pre-superscripts post-subscripts post-superscripts)
238 (declare (special pre-subscripts pre-superscripts post-subscripts post-superscripts))
239 (if display-indices
240 (setq pre-subscripts (extract-indices indices display-indices '$presubscript)
241 pre-superscripts (extract-indices indices display-indices '$presuperscript)
242 post-subscripts (extract-indices indices display-indices '$postsubscript)
243 post-superscripts (extract-indices indices display-indices '$postsuperscript))
244 (setq pre-subscripts nil
245 pre-superscripts nil
246 post-subscripts indices
247 post-superscripts nil))
248 (let (pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output
249 (w00 0) (w01 0) (w10 0) (w11 0) (h00 0) (h01 0) (h10 0) (h11 0) (d00 0) (d01 0) (d10 0) (d11 0))
250 (if pre-subscripts
251 (setq pre-subscripts-output
252 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
253 (dimension-list (cons '(mlist) pre-subscripts) nil separator))
254 w00 width
255 h00 height
256 d00 depth))
257 (if pre-superscripts
258 (setq pre-superscripts-output
259 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
260 (dimension-list (cons '(mlist) pre-superscripts) nil separator))
261 w01 width
262 h01 height
263 d01 depth))
264 (if post-subscripts
265 (setq post-subscripts-output
266 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
267 (dimension-list (cons '(mlist) post-subscripts) nil separator))
268 w10 width
269 h10 height
270 d10 depth))
271 (if post-superscripts
272 (setq post-superscripts-output
273 (let ((lop 'mparen) (rop 'mparen) (break nil) (size 1))
274 (dimension-list (cons '(mlist) post-superscripts) nil separator))
275 w11 width
276 h11 height
277 d11 depth))
278 (values w00 w01 w10 w11 h00 h01 h10 h11 d00 d01 d10 d11 pre-subscripts-output pre-superscripts-output post-subscripts-output post-superscripts-output)))))
280 (defun extract-indices (indices display-indices index-flag)
281 (remove nil
282 (loop for i from 0 to (length display-indices)
283 collect (if (eq (nth i display-indices) index-flag) (nth i indices)))))
285 (defun safe-$get (x y)
286 (and (symbolp x) ($get x y)))
288 (defun dimension-function (x result)
289 (prog (fun (w 0) (h 0) (d 0))
290 (cond ((or (not $noundisp) (not (symbolp (caar x)))))
291 ((and (get (caar x) 'verb) (get (caar x) 'alias))
292 (push-string "''" result) (setq w 2))
293 ((and (get (caar x) 'noun) (not (member (caar x) (cdr $aliases) :test #'eq))
294 (not (get (caar x) 'reversealias)))
295 (setq result (cons #\' result) w 1)))
296 (if (eq (caar x) 'mqapply) (setq fun (cadr x) x (cdr x)) (setq fun (caar x)))
297 (setq result (let ((atom-context 'dimension-function))
298 (dimension fun result lop 'mparen 0 1))
299 w (+ w width) h height d depth)
300 (cond ((null (cdr x))
301 (setq result (list* #\) #\( result)
302 width (+ 2 w)))
303 (t (setq result (let ((lop 'mparen)
304 (rop 'mparen)
305 (break (if break (+ 1 w break))))
306 (cons #\) (dimension-list x (cons #\( result))))
307 width (+ 2 w width) height (max h height) depth (max d depth))))
308 (return result)))
310 (defun dimension-prefix (form result)
311 (prog (dissym (symlength 0))
312 (setq dissym (safe-get (caar form) 'dissym)
313 symlength (length dissym))
314 (setq result (dimension (cadr form) (revappend dissym result) (caar form) rop symlength right)
315 width (+ symlength width))
316 (return result)))
318 (defun dimension-list (form result &optional separator)
319 (prog ((w 0) (h 0) (d 0))
320 (setq result (dimension (cadr form) result lop 'mcomma 0 right)
321 w width h height d depth)
322 (do ((l (cddr form) (cdr l)))
323 ((null l))
324 (push-string (or separator ", ") result)
325 (incf w (if separator (length separator) 2))
326 (checkbreak result w)
327 (setq result (dimension (car l) result 'mcomma 'mcomma w right)
328 w (+ w width) h (max h height) d (max d depth)))
329 (setq width w height h depth d)
330 (return result)))
332 (defun dimension-infix (form result)
333 (unless (= (length (cdr form)) 2)
334 (return-from dimension-infix (dimension-function form result)))
335 (prog (dissym (symlength 0) (w 0) (h 0) (d 0))
336 (setq dissym (safe-get (caar form) 'dissym)
337 symlength (length dissym)
338 result (dimension (cadr form) result lop (caar form) 0 symlength)
339 w width
340 h height
341 d depth)
342 (setq result (revappend dissym result))
343 (checkbreak result (+ symlength w))
344 (setq result (dimension (caddr form) result (caar form) rop (+ symlength w) right)
345 width (+ w symlength width)
346 height (max h height)
347 depth (max d depth))
348 (return result)))
350 (defun dimension-nary (form result)
351 ;; If only 0 or 1 arguments, then print "*"() or "*"(A)
352 (cond ((null (cddr form))
353 (dimension-function form result))
355 (prog (dissym (symlength 0) (w 0) (h 0) (d 0) helper)
356 (setq dissym (safe-get (caar form) 'dissym)
357 symlength (length dissym)
359 ;; Look for a helper function. Fall back on default if none found.
360 helper (or (safe-get (caar form) 'dimension-nary-helper) 'dimnary)
362 result (funcall helper (cadr form) result lop (caar form) (caar form) 0)
363 w width
364 h height
365 d depth
367 (do ((l (cddr form) (cdr l)))
368 (nil)
369 (checkbreak result w)
370 (setq result (revappend dissym result) w (+ symlength w))
371 (cond ((null (cdr l))
372 (setq result (funcall helper (car l) result (caar form) (caar form) rop w)
373 width (+ w width)
374 height (max h height)
375 depth (max d depth))
376 (return t))
378 (setq result (funcall helper (car l) result (caar form) (caar form) (caar form) w)
379 w (+ w width)
380 h (max h height)
381 d (max d depth)))))
382 (return result)))))
384 ;; Output for MTEXT, generated for example by print(...).
385 ;; Suppress parentheses which would be generated by DIMNARY.
387 (defun dimnary-mtext (form result lop op rop w)
388 (declare (ignore op))
389 (dimension form result lop rop w right))
391 ;; Output for general n-ary operators.
392 ;; Heuristic: if some argument is displayed as an infix or n-ary operator,
393 ;; then put parentheses around that argument (even if parentheses are not
394 ;; necessary, according to operator precedence, to disambiguate the output).
396 ;; Examples:
398 ;; a * b * c; --> a b c
399 ;; a . b . c; --> a . b . c
400 ;; a * b . c; --> a (b . c)
401 ;; (a * b) . c; --> (a b) . c
402 ;; a . b * c; --> (a . b) c
403 ;; a . (b * c); --> a . (b c)
404 ;; a . b * c . d; --> (a . b) (c . d)
405 ;; a * b . c * d; --> a (b . c) d
406 ;; (a * b) . (c * d); --> (a b) . (c d)
407 ;; infix ("@@");
408 ;; a @@ b * c @@ d; --> (a @@ b) (c @@ d)
409 ;; a @@ b . c @@ d; --> (a @@ b) . (c @@ d)
410 ;; a * b @@ c . d; --> a ((b @@ c) . d)
411 ;; (a * b) @@ (c . d); --> (a b) @@ (c . d)
413 (defun dimnary (form result lop op rop w)
414 (declare (ignore op))
415 (if (and (consp form)
416 (member (safe-get (caar form) 'dimension)
417 '(dimension-infix dimension-nary)))
418 (progn
419 (setq result
420 (cons #\)
421 (dimension form
422 (cons #\( result)
423 'mparen 'mparen (if w (1+ w)) (1+ right))))
424 (incf width 2)
425 result)
426 (dimension form result lop rop w right)))
428 ;; Output for Boolean n-ary operators.
429 ;; Heuristic: if some argument is displayed as an infix or n-ary operator,
430 ;; or the operator of the argument is MNOT, then put parentheses around that argument
431 ;; (even if parentheses are not necessary, according to operator precedence,
432 ;; to disambiguate the output).
434 ;; Examples:
436 ;; a and b and c; --> a and b and c
437 ;; a or b or c; --> a or b or c
438 ;; a or b and c; --> a or (b and c)
439 ;; (a or b) and c; --> (a or b) and c
440 ;; a and b or c; --> (a and b) or c
441 ;; a and (b or c); --> a and (b or c)
442 ;; a and b or c and d; --> (a and b) or (c and d)
443 ;; a or b and c or d; --> a or (b and c) or d
444 ;; (a or b) and (c or d); --> (a or b) and (c or d)
445 ;; a < 0 and b < 0 or c < 0; --> ((a < 0) and (b < 0)) or (c < 0)
446 ;; a < 0 and not foo; --> (a < 0) and (not foo)
447 ;; a < 0 or not foo; --> (a < 0) or (not foo)
448 ;; infix ("=>");
449 ;; a => b and a => c; --> (a => b) and (a => c)
450 ;; a => b or not b => c; --> (a => b) or (not b => c)
451 ;; a and b => c and d; --> a and (b => c) and d
452 ;; a or b => c and d; --> a or ((b => c) and d)
453 ;; (a and b) => (c and d); --> (a and b) => (c and d)
454 ;; if a and b then 1 else 0; --> if a and b then 1 else 0
456 (defun dimnary-boolean (form result lop op rop w)
457 (declare (ignore op))
458 (if (and (consp form)
459 (or (member (safe-get (caar form) 'dimension) '(dimension-infix dimension-nary))
460 (eq (caar form) 'mnot)))
461 (dimension-paren form result)
462 (dimension form result lop rop w right)))
464 (defun dimension-postfix (form result)
465 (prog (dissym (symlength 0))
466 (setq dissym (safe-get (caar form) 'dissym)
467 symlength (length dissym))
468 (setq result (dimension (cadr form) result lop (caar form) 0 (+ symlength right))
469 width (+ symlength width))
470 (return (revappend dissym result))))
472 (defun dimension-nofix (form result)
473 (setq form (safe-get (caar form) 'dissym)
474 width (length form))
475 (revappend form result))
477 (defun dimension-match (form result)
478 (prog (dissym (symlength 0))
479 (setq dissym (safe-get (caar form) 'dissym)
480 symlength (length (car dissym)))
481 (cond ((null (cdr form))
482 (setq width (+ symlength (length (cdr dissym)))
483 height 1
484 depth 0)
485 (return (revappend (cdr dissym) (revappend (car dissym) result))))
486 (t (setq result (let ((lop 'mparen)
487 (rop 'mparen)
488 (break (if break (+ symlength break)))
489 (right (+ symlength right)))
490 (dimension-list form (revappend (car dissym) result))))
491 (setq width (+ (length (cdr dissym)) symlength width))
492 (return (revappend (cdr dissym) result))))))
494 (defun dimension-superscript (form result)
495 (prog (exp (w 0) (h 0) (d 0) bas)
496 (setq exp (let ((size 1))
497 (dimension (caddr form) nil 'mparen 'mparen nil 0))
498 w width
499 h height
500 d depth)
501 (cond ((and (not (atom (cadr form))) (member 'array (cdaadr form) :test #'eq))
502 (if (safe-mget (caaadr form) 'display-indices)
503 (return (dimension-superscript `(,(first form) ((mprogn) ,(second form)) ,(third form)) result))
504 (progn (prog (sub (w2 0) (h2 0) (d2 0))
505 (setq sub (if (eq 'mqapply (caaadr form))
506 (cdadr form) (cadr form)))
507 (setq sub (let ((lop 'mparen) (break nil) (size 1))
508 (dimension-list sub nil))
509 w2 width h2 height d2 depth)
510 (setq bas (dimension (mop (cadr form)) result lop 'mexpt nil 0))
511 (when (not (checkfit (+ width (max w w2))))
512 (setq result (dimension-function (cons '($expt) (cdr form)) result))
513 (return result))
514 (setq result (cons (cons 0 (cons (+ height d) exp)) bas))
515 (setq result (cons (cons (- w) (cons (- (+ depth h2)) sub)) result))
516 (setq result (cons (list (- (max w w2) w2) 0) result)
517 width (+ width (max w w2)) height (+ h d height) depth (+ d2 h2 depth)))
518 (update-heights height depth)
519 (return result))))
520 ((and (atom (caddr form))
521 (not (atom (cadr form)))
522 (not (safe-get (caaadr form) 'dimension))
523 (prog2 (setq bas (nformat-check (cadr form)))
524 (not (safe-get (caar bas) 'dimension))))
525 (return (dimension-function
526 (list* '(mqapply) (list '(mexpt) (mop bas) (caddr form)) (margs bas))
527 result)))
528 (t (setq bas (dimension (cadr form) result lop 'mexpt nil 0) width (+ w width))
529 (if (not (checkfit width))
530 (return (dimension-function (cons '($expt) (cdr form)) result)))
531 (if (eql #\) (car bas))
532 (setq result (cons (list* 0 (1+ d) exp) bas) height (max (+ 1 h d) height))
533 (setq result (cons (list* 0 (+ height d) exp) bas) height (+ h d height)))
534 (update-heights height depth)
535 (return result)))))
537 (defun dsumprod (form result d-form sw sh sd)
538 (prog (str to dummy (w 0) (h 0) (d 0) dummy2 (lsum (eq (caar form) '%lsum)))
539 (setq dummy2 (dimension (caddr form) nil 'mparen 'mequal nil 0)
540 w width
541 h height
542 d depth)
543 (if lsum
544 (setq str " in " to "")
545 (setq str " = " to (cadr (cdddr form))))
546 (push-string str dummy2)
547 (setq dummy2 (dimension (cadddr form) dummy2 'mequal 'mparen nil 0)
548 w (+ (length str) w width)
549 h (max h height)
550 d (max d depth))
551 (setq dummy (dimension to nil 'mparen 'mparen nil 0))
552 (unless (checkfit (max w width))
553 (return (dimension-function form result)))
554 (setq dummy2 (cons (cons (- sw) (cons (- (+ sd h)) dummy2)) (cons d-form result)))
555 (cond ((> width sw)
556 (setq sw 0))
558 (setq sw (truncate (- sw width) 2)
559 width (+ sw width))))
560 (setq dummy (cons (cons (- sw w) (cons (+ sh depth) dummy)) dummy2)
561 w (max w width)
562 d (+ sd h d)
563 h (+ sh height depth))
564 (update-heights h d)
565 (setq dummy (dimension (cadr form) (cons (list (1+ (- w width)) 0) dummy)
566 (caar form) rop w right)
567 width (+ 1 w width)
568 height (max h height)
569 depth (max d depth))
570 (return dummy)))
572 (displa-def bigfloat dim-bigfloat)
573 (displa-def %bigfloat dim-bigfloat)
574 (displa-def mquote dimension-prefix "'")
575 (displa-def %mquote dimension-prefix "'")
576 (displa-def msetq dimension-infix " : ")
577 (displa-def %msetq dimension-infix " : ")
578 (displa-def mset dimension-infix " :: ")
579 (displa-def %mset dimension-infix " :: ")
580 (displa-def mdefine dim-mdefine " := ")
581 (displa-def %mdefine dim-mdefine " := ")
582 (displa-def mdefmacro dim-mdefine " ::= ")
583 (displa-def %mdefmacro dim-mdefine " ::= ")
585 (defun dim-mdefine (form result)
586 (let (($noundisp t)
587 ($stringdisp t))
588 (dimension-infix (if (cdddr form)
589 (list (car form) (cadr form) (cons '(mprogn) (cddr form)))
590 form)
591 result)))
593 (displa-def mfactorial dimension-postfix "!")
594 (displa-def %mfactorial dimension-postfix "!")
595 (displa-def mexpt dimension-superscript)
596 (displa-def %mexpt dimension-superscript)
597 (displa-def mncexpt dim-mncexpt "^^")
598 (displa-def %mncexpt dim-mncexpt "^^")
600 (defun dim-mncexpt (form result)
601 (dimension-superscript (list '(mncexpt) (cadr form) (cons '(mangle) (cddr form)))
602 result))
604 (displa-def mnctimes dimension-nary " . ")
605 (displa-def %mnctimes dimension-nary " . ")
607 (displa-def %product dim-%product 115.)
609 (defun dim-%product (form result)
610 (dsumprod form result '(d-prodsign) 5 3 1))
612 (displa-def rat dim-rat "/")
613 (displa-def %rat dim-rat "/")
615 (defun dim-rat (form result)
616 (if $pfeformat
617 (dimension-nary form result)
618 (dim-mquotient form result)))
620 (displa-def mquotient dim-mquotient "/")
621 (displa-def %mquotient dim-mquotient "/")
623 (defun dim-mquotient (form result)
624 (unless (= (length (cdr form)) 2)
625 (return-from dim-mquotient (dimension-function form result)))
626 (prog (num (w 0) (h 0) (d 0) den)
627 (when (and (= 1 size) (atom (cadr form)) (atom (caddr form)))
628 (return (dimension-nary form result)))
629 (setq num (dimension (cadr form) nil 'mparen 'mparen nil right)
630 w width
631 h height
632 d depth)
633 (unless (checkfit w)
634 (return (dimension-nary form result)))
635 (setq den (dimension (caddr form) nil 'mparen 'mparen nil right))
636 (unless (checkfit width)
637 (return (dimension-nary form result)))
638 (return (dratio result num w h d den width height depth))))
640 ;; <-- W1 -->
641 ;; ------------------
642 ;; | ^ |
643 ;; <- X1 -> | | H1 |
644 ;; | | D1 |
645 ;; | v |
646 ;; ------------------
647 ;; ----------------------------------
648 ;; (Likewise for X2, H2, D2, W2 in the denominator)
650 (defun dratio (result num w1 h1 d1 den w2 h2 d2)
651 (setq width (max w1 w2)
652 height (+ 1 h1 d1)
653 depth (+ h2 d2))
654 (setq h1 (truncate (- width w1) 2)
655 d2 (truncate (- width w2) 2))
656 (update-heights height depth)
657 (push `(,h1 ,(1+ d1) . ,num) result)
658 (push `(,(- d2 (+ h1 w1)) ,(- h2) . ,den) result)
659 (push `(,(- 0 d2 w2) 0) result)
660 (push `(d-hbar ,width) result)
661 result)
663 (displa-def mtimes dimension-nary " ")
664 (displa-def %mtimes dimension-nary " ")
666 ;; This code gets run when STARDISP is assigned a value.
668 (defprop $stardisp stardisp assign)
670 (defun stardisp (symbol val)
671 (declare (ignore symbol))
672 (putprop 'mtimes (if val '(#\*) '(#\space)) 'dissym))
674 (displa-def %integrate dim-%integrate 115.)
676 (defun dim-%integrate (form result)
677 (prog (dummy (w 0)(h 0)(d 0) dummy2)
678 (cond ((not (or (= (length (cdr form)) 2) (= (length (cdr form)) 4)))
679 (return-from dim-%integrate (dimension-function form result)))
680 ((null (cdddr form))
681 (setq dummy `(#\space (d-integralsign) . ,result) w 2 h 3 d 2))
682 (t (setq dummy (dimension (cadr (cdddr form)) nil 'mparen 'mparen nil 0)
683 w width h height d depth)
684 (setq dummy2 (dimension (cadddr form) nil 'mparen 'mparen nil 0))
685 (if (not (checkfit (+ 2 (max w width))))
686 (return (dimension-function form result)))
687 (setq dummy `((0 ,(+ 3 d) . ,dummy) (d-integralsign) . ,result))
688 (setq dummy (cons (cons (- w) (cons (- (+ 2 height)) dummy2)) dummy)
689 w (+ 2 (max w width)) h (+ 3 h d) d (+ 2 height depth)
690 dummy (cons (list (- w 1 width) 0) dummy))))
691 (update-heights h d)
692 (setq dummy (dimension (cadr form) dummy '%integrate 'mparen w 2)
693 w (+ w width) h (max h height) d (max d depth))
694 (push-string " d" dummy)
695 (setq dummy (dimension (caddr form) dummy 'mparen rop (+ 2 w) right)
696 width (+ 2 w width) height (max h height) depth (max d depth))
697 (return dummy)))
699 (displa-def %derivative dim-%derivative 125.)
701 (defun dim-%derivative (form result)
702 (prog ()
703 (cond ((null (cddr form))
704 (return (dimension-function (cons '(%diff) (cdr form)) result))))
705 (cond ((null (cdddr form)) (setq form (append form '(1)))))
706 (cond ((and $derivabbrev
707 (do ((l (cddr form) (cddr l))) ((null l) t)
708 (cond ((and (atom (car l)) (integerp (cadr l)) (> (cadr l) 0)))
709 (t (return nil)))))
710 (return (dmderivabbrev form result)))
711 ((or (> (rbp lop) 130.) (> (lbp rop) 130.)
712 (and (not (atom (cadr form))) (or (> (rbp lop) 110.) (> (lbp rop) 110.))))
713 (return (dimension-paren form result)))
714 (t (return (dmderivlong form result))))))
716 (defun dmderivabbrev (form result)
717 (prog (dummy (w 0))
718 (do ((l (cddr form) (cddr l)) (var))
719 ((null l) (setq dummy (cdr dummy) w (1- w)))
720 (setq var (dimension (car l) nil 'mparen 'mparen nil 0))
721 (do ((i (cadr l) (1- i)))
722 ((= 1 i))
723 (setq dummy (cons #\space (append var dummy))))
724 (setq dummy (cons #\space (nconc var dummy)) w (+ w (cadr l) (* (cadr l) width))))
725 (setq result (dimension (cadr form) result lop '%deriv 0 right))
726 (setq result (cons (cons 0 (cons (- 0 depth 1) dummy)) result)
727 width (+ w width) depth (max 1 (1+ depth)))
728 (update-heights height depth)
729 (return result)))
731 (defun dmderivlong (form result)
732 (prog (num (w1 0) (h1 0) (d1 0) den (w2 0)( h2 0) (d2 0))
733 (setq num (list (cadddr form))
734 den (cond ((equal 1 (cadddr form))
735 (dimension (caddr form)
736 (list #\d) 'mparen 'mparen nil 0))
737 (t (dimension-superscript
738 (cons '(diff)(cddr form)) (list #\d))))
739 w2 (1+ width) h2 height d2 depth)
740 (do ((l (cddddr form) (cddr l))) ((null l))
741 (setq num (cons (cadr l) num)
742 den (cond ((equal 1 (cadr l))
743 (dimension (car l) (cons #\d (cons #\space den))
744 'mparen 'mparen nil 0))
745 (t (dimension-superscript
746 (cons '(diff) l) (cons #\d (cons #\space den)))))
747 w2 (+ 2 w2 width) h2 (max h2 height) d2 (+ d2 depth)))
748 (setq num (nformat-check (addn num t)))
749 (cond ((equal 1 num) (setq num (list #\d) w1 1 h1 1 d1 0))
750 (t (setq num (dimension-superscript (list '(diff) #\d num) nil)
751 w1 width h1 height d1 depth)))
752 (cond ((atom (setq form (nformat-check (cadr form))))
753 (setq num (dimension form num '%deriv 'mparen nil 0) w1 (+ w1 width))
754 (return (dratio result num w1 h1 d1 den w2 h2 d2)))
755 (t (setq result (dratio result num w1 h1 d1 den w2 h2 d2) w1 width h1 height d1 depth)
756 (setq result (dimension form (cons #\space result) '%deriv rop w1 right)
757 width (+ 1 w1 width) height (max h1 height) depth (max d1 depth))
758 (update-heights height depth)
759 (return result)))))
761 (displa-def %at dim-%at 105. 105.)
763 (defun dim-%at (form result)
764 (prog (exp eqs (w 0) (h 0) (d 0))
765 (unless (= (length (cdr form)) 2)
766 (return-from dim-%at (dimension-function form result)))
767 (setq exp (dimension (cadr form) result lop '%at nil 0)
768 w width
769 h height
770 d depth)
771 (setq eqs (dimension (cond ((not (eq 'mlist (caar (caddr form)))) (caddr form))
772 ((null (cddr (caddr form))) (cadr (caddr form)))
773 (t (cons '(mcomma) (cdaddr form))))
774 nil 'mparen 'mparen nil 0))
775 (unless (checkfit (+ 1 w width))
776 (return (dimension-function form result)))
777 (setq result (cons (cons 0 (cons (- 0 1 d) eqs))
778 (cons `(d-vbar ,(1+ h) ,(1+ d) ,(car (coerce $absboxchar 'list))) exp))
779 width (+ 1 w width)
780 height (1+ h)
781 depth (+ 1 d depth))
782 (update-heights height depth)
783 (return result)))
785 (displa-def mplus dim-mplus)
786 (displa-def %mplus dim-mplus)
787 (defprop munaryplus (#\+ #\space) dissym)
789 (defun dim-mplus (form result)
790 ;; If only 0 or 1 arguments, then print "+"() or +A
791 (cond ((and (null (cddr form))
792 (not (member (cadar form) '(trunc exact) :test #'eq)))
793 (if (null (cdr form))
794 (dimension-function form result)
795 (dimension-prefix (cons '(munaryplus) (cdr form)) result)))
796 (t (setq result (dimension (cadr form) result lop 'mplus 0 0))
797 (checkbreak result width)
798 (do ((l (cddr form) (cdr l))
799 (w width) (h height) (d depth)
800 (trunc (member 'trunc (cdar form) :test #'eq)) (dissym))
801 ((null l) (cond (trunc
802 (setq width (+ 8 w) height h depth d)
803 (push-string " + . . ." result)))
804 result)
805 (if (mmminusp (car l))
806 (setq dissym '(#\space #\- #\space) form (cadar l))
807 (setq dissym '(#\space #\+ #\space) form (car l)))
808 (cond ((and (not trunc) (null (cdr l)))
809 (setq result (dimension form (append dissym result)
810 'mplus rop (+ 3 w) right)
811 width (+ 3 w width)
812 height (max h height)
813 depth (max d depth))
814 (return result))
815 (t (setq result
816 (dimension form (append dissym result)
817 'mplus 'mplus (+ 3 w) 0)
818 w (+ 3 w width)
819 h (max h height)
820 d (max d depth))
821 (checkbreak result w)))))))
823 (displa-def mminus dim-mminus)
824 (displa-def %mminus dim-mminus)
825 (defprop munaryminus (#\- #\space) dissym)
826 (def-rbp munaryminus 134)
827 (def-rbp munaryminus 100)
829 (defun dim-mminus (form result)
830 (cond ((and (null (cddr form))
831 (not (member (cadar form) '(trunc exact) :test #'eq)))
832 (if (null (cdr form))
833 (dimension-function form result)
834 (dimension-prefix (cons '(munaryminus) (cdr form)) result)))
836 (setq result (dimension (cadr form) result lop 'mminus 0 0))
837 (checkbreak result width)
838 (do ((l (cddr form) (cdr l))
839 (w width)
840 (h height)
841 (d depth)
842 (trunc (member 'trunc (cdar form) :test #'eq))
843 (dissym))
844 ((null l)
845 (cond (trunc
846 (setq width (+ 8 w)
847 height h
848 depth d)
849 (push-string " + . . ." result)))
850 result)
851 (if (mmminusp (car l))
852 (setq dissym '(#\space #\+ #\space) form (cadar l))
853 (setq dissym '(#\space #\- #\space) form (car l)))
854 (cond ((and (not trunc) (null (cdr l)))
855 (setq result (dimension form (append dissym result)
856 'mminus rop (+ 3 w) right)
857 width (+ 3 w width)
858 height (max h height)
859 depth (max d depth))
860 (return result))
862 (setq result
863 (dimension form (append dissym result)
864 'mminus 'mminus (+ 3 w) 0)
865 w (+ 3 w width)
866 h (max h height)
867 d (max d depth))
868 (checkbreak result w)))))))
870 (displa-def %sum dim-%sum 110.)
871 (displa-def %limit dim-%limit 110. 110.)
872 (displa-def %lsum dim-%lsum 110.)
874 (defun dim-%lsum (form result)
875 (dsumprod form result '(d-sumsign) 4 3 2))
877 (defun dim-%sum (form result)
878 (dsumprod form result '(d-sumsign) 4 3 2))
880 (defun dim-%limit (form result)
881 (prog ((w 0) (h 0) (d 0) dummy)
882 (unless (or (= (length (cdr form)) 3) (= (length (cdr form)) 4))
883 (return-from dim-%limit (dimension-function form result)))
884 (setq dummy (dimension (third form) nil 'mparen 'mparen nil 0)
885 w width h height d depth)
886 (push-string " -> " dummy)
887 (setq dummy (dimension (fourth form) dummy 'mparen 'mparen nil 0)
888 w (+ 4 w width)
889 h (max h height)
890 d (max d depth))
891 (cond ((null (cddddr form)))
892 ((eq '$plus (fifth form))
893 (push #\+ dummy)
894 (incf w))
895 (t (push #\- dummy)
896 (incf w)))
897 (push-string "limit" result)
898 (setq dummy (cons (list* -5 (- h) dummy) result)
899 d (+ h d))
900 (update-heights 1 d)
901 (setq dummy (dimension (cadr form) (cons '(1 0) dummy) '%limit rop (1+ w) right))
902 (setq width (+ 1 w width)
903 depth (max d depth))
904 (return dummy)))
906 ;; Some scheme needs to be worked out to allow use of mathematical character
907 ;; sets on consoles which have them.
909 (displa-def marrow dimension-infix " -> " 80. 80.)
910 (displa-def %marrow dimension-infix " -> " 80. 80.)
911 (displa-def mgreaterp dimension-infix " > ")
912 (displa-def %mgreaterp dimension-infix " > ")
913 (displa-def mgeqp dimension-infix " >= ")
914 (displa-def %mgeqp dimension-infix " >= ")
915 (displa-def mequal dimension-infix " = ")
916 (displa-def %mequal dimension-infix " = ")
917 (displa-def mnotequal dimension-infix " # ")
918 (displa-def %mnotequal dimension-infix " # ")
919 (displa-def mleqp dimension-infix " <= ")
920 (displa-def %mleqp dimension-infix " <= ")
921 (displa-def mlessp dimension-infix " < ")
922 (displa-def %mlessp dimension-infix " < ")
923 (displa-def mnot dimension-prefix "not ")
924 (displa-def %mnot dimension-prefix "not ")
925 (displa-def mand dimension-nary " and ")
926 (displa-def %mand dimension-nary " and ")
927 (defprop mand dimnary-boolean dimension-nary-helper)
928 (displa-def mor dimension-nary " or ")
929 (displa-def %mor dimension-nary " or ")
930 (defprop mor dimnary-boolean dimension-nary-helper)
931 (displa-def mcond dim-mcond)
932 (displa-def %mcond dim-mcond)
934 ;; MCOND or %MCOND always has an even number of arguments.
935 ;; The first two arguments are the foo and bar in 'if foo then bar .
936 ;; Of the remaining pairs of arguments,
937 ;; the first is MAYBE-ELSEIF and the second is ELSE-OR-THEN in the code below.
938 ;; MAYBE-ELSEIF is T if the construct is 'if foo then bar else quux ,
939 ;; otherwise the construct is 'if foo then bar elseif baz then quux
940 ;; where baz is the value of MAYBE-ELSEIF.
941 ;; If ELSE-OR-THEN is NIL, just omit the final "else".
943 ;; The parser appends (T NIL) to any if-then which lacks an else.
944 ;; DIM-MCOND renders both '((%MCOND) $A $B) and '((%MCOND) $A $B T NIL) as "if a then b".
946 ;; Examples. The "<==>" here means that the stuff on the right parses as the stuff on the left,
947 ;; and the stuff on the left displays as the stuff on the right.
949 ;; ((%mcond) $a $b t nil) <==> 'if a then b
950 ;; ((%mcond) $a $b t $d) <==> 'if a then b else d
951 ;; ((%mcond) $a $b $c nil t nil) <==> 'if a then b elseif c then false
952 ;; ((%mcond) $a $b $c $d t nil) <==> 'if a then b elseif c then d
953 ;; ((%mcond) $a $b $c $d t $f) <==> 'if a then b elseif c then d else f
954 ;; ((%mcond) $a $b $c $d $e nil t nil) <==> 'if a then b elseif c then d elseif e then false
955 ;; ((%mcond) $a $b $c $d $e $f t nil) <==> 'if a then b elseif c then d elseif e then f
956 ;; ((%mcond) $a $b $c $d $e $f t $h) <==> 'if a then b elseif c then d elseif e then f else h
957 ;; ((%mcond) $a $b $c $d $e $f $g nil t nil) <==> 'if a then b elseif c then d elseif e then f elseif g then false
958 ;; ((%mcond) $a $b $c $d $e $f $g $h) <==> 'if a then b elseif c then d elseif e then f elseif g then h
960 (defun dim-mcond (form result)
961 (prog ((w 0) (h 0) (d 0))
962 (push-string "if " result)
963 (setq result (dimension (cadr form) result 'mcond 'mparen 3 0)
964 w (+ 3 width)
965 h height
966 d depth)
967 (checkbreak result w)
968 (push-string " then " result)
969 (setq result (dimension (caddr form) result 'mcond 'mparen (+ 6 w) 0)
970 w (+ 6 w width)
971 h (max h height)
972 d (max d depth))
973 (let ((args (cdddr form)))
974 (loop while (>= (length args) 2) do
975 (let ((maybe-elseif (car args)) (else-or-then (cadr args)))
976 (cond
977 ((and (eq maybe-elseif t) (= (length args) 2))
978 (unless (or (eq '$false else-or-then) (eq nil else-or-then))
979 (checkbreak result w)
980 (push-string " else " result)
981 (setq result (dimension else-or-then result 'mcond rop (+ 6 w) right)
982 w (+ 6 w width)
983 h (max h height)
984 d (max d depth))))
986 (checkbreak result w)
987 (push-string " elseif " result)
988 (setq result (dimension maybe-elseif result 'mcond rop (+ 8 w) right)
989 w (+ 8 w width)
990 h (max h height)
991 d (max d depth))
992 (checkbreak result w)
993 (push-string " then " result)
994 (setq result (dimension else-or-then result 'mcond rop (+ 6 w) right)
995 w (+ 6 w width)
996 h (max h height)
997 d (max d depth)))))
998 (setq args (cddr args))))
999 (setq width w height h depth d)
1000 (return result)))
1003 (displa-def mdo dim-mdo)
1004 (displa-def %mdo dim-mdo)
1006 (defun dim-mdo (form result)
1007 (prog ((w 0) (h 0) (d 0) brkflag)
1008 (cond ((not (null (cadr form)))
1009 (push-string "for " result)
1010 (setq result (cons #\space (dimension (cadr form) result 'mdo 'mparen 4 right))
1011 w (+ 4 width) h height d depth brkflag t)))
1012 (cond ((or (null (caddr form)) (equal 1 (caddr form))))
1013 (t (push-string "from " result)
1014 (setq result (cons #\space (dimension (caddr form) result 'mdo 'mparen (+ 6 w) 0))
1015 w (+ 6 w width) h (max h height) d (max d depth))))
1016 (setq form (cdddr form))
1017 (cond ((equal 1 (car form)))
1018 ((not (null (car form)))
1019 (push-string "step " result)
1020 (setq result (cons #\space (dimension (car form) result 'mdo 'mparen (+ 6 w) 0))
1021 w (+ 6 w width) h (max h height) d (max d depth)))
1022 ((not (null (cadr form)))
1023 (push-string "next " result)
1024 (setq result (cons #\space (dimension (cadr form) result 'mdo 'mparen (+ 6 w) 0))
1025 w (+ 6 w width) h (max h height) d (max d depth))))
1026 (cond ((not (null (caddr form)))
1027 (push-string "thru " result)
1028 (setq result (cons #\space (dimension (caddr form) result 'mdo 'mparen (+ 6 w) 0))
1029 w (+ 6 w width) h (max h height) d (max d depth) brkflag t)))
1030 (cond ((not (null (cadddr form)))
1031 (cond ((and (not (atom (cadddr form))) (eq (caar (cadddr form)) 'mnot))
1032 (push-string "while " result)
1033 (setq result (cons #\space (dimension (cadr (cadddr form)) result 'mdo 'mparen (+ 7 w) 0))
1034 w (+ 7 w width) h (max h height) d (max d depth)))
1035 (t (push-string "unless " result)
1036 (setq result
1037 (cons #\space (dimension (cadddr form) result 'mdo 'mparen (+ 8 w) 0))
1038 w (+ 8 w width) h (max h height) d (max d depth))))))
1039 (if brkflag (checkbreak result w))
1040 (push-string "do " result)
1041 (setq result (dimension (car (cddddr form)) result 'mdo rop (+ 4 w) right)
1042 width (+ 4 w width) height (max h height) depth (max d depth))
1043 (return result)))
1046 (displa-def mdoin dim-mdoin)
1047 (displa-def %mdoin dim-mdoin)
1049 (defun dim-mdoin (form result)
1050 (prog ((w 0) (h 0) ( d 0))
1051 (push-string "for " result)
1052 (setq result (dimension (cadr form) result 'mdo 'mparen 4 0)
1053 w (+ 4 width) h height d depth)
1054 (push-string " in " result)
1055 (setq result (dimension (caddr form) result 'mdo 'mparen (+ 4 w) 0)
1056 w (+ 4 w width) h (max h height) d (max d depth))
1057 (setq form (cdr (cddddr form)))
1058 (cond ((not (null (car form)))
1059 (push-string " thru " result)
1060 (setq result (dimension (car form) result 'mdo 'mparen (+ 6 w) 0)
1061 w (+ 6 w width) h (max h height) d (max d depth))))
1062 (cond ((not (null (cadr form)))
1063 (push-string " unless " result)
1064 (setq result (dimension (cadr form) result 'mdo 'mparen (+ 8 w) 0)
1065 w (+ 8 w width) h (max h height) d (max d depth))))
1066 (push-string " do " result)
1067 (setq result (dimension (caddr form) result 'mdo rop (+ 4 w) right)
1068 width (+ 4 w width) height (max h height) depth (max d depth))
1069 (return result)))
1071 (displa-def mprogn dimension-match "(" ")")
1072 (displa-def %mprogn dimension-match "(" ")")
1073 (displa-def mlist dimension-match "[" "]")
1074 (displa-def %mlist dimension-match "[" "]")
1075 (displa-def mangle dimension-match "<" ">")
1076 (displa-def %mangle dimension-match "<" ">")
1077 (displa-def mcomma dimension-nary ", " 10. 10.)
1078 (displa-def %mcomma dimension-nary ", " 10. 10.)
1079 (displa-def mabs dim-mabs)
1080 (displa-def %mabs dim-mabs)
1082 (defun dim-mabs (form result &aux arg bar)
1083 (setq arg (dimension (cadr form) nil 'mparen 'mparen nil 0))
1084 (cond ((or (> (+ 2 width) $linel) (and (= 1 height) (= 0 depth)))
1085 (dimension-function form result))
1086 (t (setq width (+ 2 width))
1087 (update-heights height depth)
1088 (setq bar `(d-vbar ,height ,depth ,(car (coerce $absboxchar 'list))))
1089 (cons bar (nconc arg (cons bar result))))))
1091 (displa-def $matrix dim-$matrix)
1092 (displa-def %matrix dim-$matrix)
1094 (defun dim-$matrix (form result)
1095 (prog (dmstr rstr cstr consp cols)
1096 (setq cols (if ($listp (cadr form)) (length (cadr form)) 0))
1097 (if (or (null (cdr form))
1098 (memalike '((mlist simp)) (cdr form))
1099 ;; Check if the matrix has lists as rows with a equal number of
1100 ;; columns.
1101 (dolist (row (cdr form))
1102 (if (or (not ($listp row))
1103 (not (eql cols (length row))))
1104 (return t))))
1105 ;; The matrix is not well formed. Display the matrix in linear mode.
1106 (return (dimension-function form result)))
1107 (do ((l (cdadr form) (cdr l))) ((null l))
1108 (setq dmstr (cons nil dmstr) cstr (cons 0 cstr)))
1109 (do ((r (cdr form) (cdr r)) (h1 0) (d1 0))
1110 ((or consp (null r))
1111 (setq width 0)
1112 (do ((cs cstr (cdr cs))) ((null cs)) (setq width (+ 2 (car cs) width)))
1113 (setq h1 (1- (+ h1 d1)) depth (truncate h1 2) height (- h1 depth)))
1114 (do ((c (cdar r) (cdr c))
1115 (nc dmstr (cdr nc))
1116 (cs cstr (cdr cs)) (dummy) (h2 0) (d2 0))
1117 ((null c) (setq d1 (+ d1 h1 h2) h1 (1+ d2)))
1118 (setq dummy (dimension (car c) nil 'mparen 'mparen nil 0)
1119 h2 (max h2 height) d2 (max d2 depth))
1120 (cond ((not (checkfit (+ 14. width))) (setq consp t) (return nil))
1121 (t (rplaca nc (cons (list* width height depth dummy) (car nc)))
1122 (rplaca cs (max width (car cs))))))
1123 (setq rstr (cons d1 rstr)))
1124 (if (> (+ height depth) (length linearray))
1125 (setq consp t))
1126 (return
1127 (cond ((and (not consp) (checkfit (+ 2 width)))
1128 (matout dmstr cstr rstr result))
1129 ((and (not consp) (<= level 2)) (colout dmstr cstr result))
1130 (t (dimension-function form result))))))
1132 (defun matout (dmstr cstr rstr result)
1133 (push `(d-matrix left ,height ,depth) result)
1134 (push #\space result)
1135 (do ((d dmstr (cdr d)) (c cstr (cdr c)) (w 0 0))
1136 ((null d))
1137 (do ((d (car d) (cdr d)) (r rstr (cdr r))) ((null d))
1138 (rplaca (cddar d) (- height (car r)))
1139 (rplaca (cdar d) (- (truncate (- (car c) (caar d)) 2) w))
1140 (setq w (truncate (+ (car c) (caar d)) 2))
1141 (rplaca d (cdar d)))
1142 (setq result (cons (list (+ 2 (- (car c) w)) 0) (nreconc (car d) result))))
1143 (setq width (+ 2 width))
1144 (update-heights height depth)
1145 (rplaca (car result) (1- (caar result)))
1146 (push `(d-matrix right ,height ,depth) result)
1147 result)
1149 (defun colout (dmstr cstr result)
1150 (setq width 0 height 1 depth 0)
1151 (do ((r dmstr (cdr r)) (c cstr (cdr c)) (col 1 (1+ col)) (w 0 0) (h -1 -1) (d 0))
1152 ((null r))
1153 (push-string " Col " result)
1154 (setq result (nreconc (exploden col) result))
1155 (push-string " = " result)
1156 (setq width (+ 8 (flatc col) width))
1157 (do ((r (car r) (cdr r))) ((null r))
1158 (setq h (+ 1 h (cadar r) (caddar r)))
1159 (rplaca (cddar r) (- h (cadar r)))
1160 (rplaca (cdar r) (- (truncate (- (car c) (caar r)) 2) w))
1161 (setq w (truncate (+ (car c) (caar r)) 2))
1162 (rplaca r (cdar r)))
1163 (setq d (truncate h 2) h (- h d))
1164 (push `(d-matrix left ,h ,d) result)
1165 (push #\space result)
1166 (push `(0 ,(- d) . ,(nreverse (car r))) result)
1167 (push `(,(1+ (- (car c) w)) 0) result)
1168 (push `(d-matrix right ,h ,d) result)
1169 (setq width (+ 4 (car c) width) height (max h height) depth (max d depth))
1170 (update-heights h d)
1171 (checkbreak result width))
1172 result)
1174 (displa-def mbox dim-mbox)
1175 (displa-def %mbox dim-mbox)
1177 (defun dim-mbox (form result &aux dummy)
1178 (setq dummy (dimension (cadr form) nil 'mparen 'mparen nil 0))
1179 (cond ((not (checkfit (+ 2 width)))
1180 (dimension-function (cons '($box) (cdr form)) result))
1181 (t (push `(d-box ,height ,depth ,width ,(nreverse dummy)) result)
1182 (setq width (+ 2 width) height (1+ height) depth (1+ depth))
1183 (update-heights height depth)
1184 result)))
1186 (displa-def mlabox dim-mlabox)
1187 (displa-def %mlabox dim-mlabox)
1189 (defun dim-mlabox (form result)
1190 (prog (dummy ch)
1191 (setq dummy (dimension (cadr form) nil 'mparen 'mparen nil 0))
1192 (cond ((not (checkfit (+ 2 width)))
1193 (return (dimension-function (cons '($box) (cdr form)) result))))
1194 (setq width (+ 2 width) height (1+ height) depth (1+ depth))
1195 (setq ch (car (coerce $boxchar 'list)))
1196 (setq result
1197 (cons (do ((l (mapcar #'(lambda (l) (char (symbol-name l) 0))
1198 (makstring (caddr form))) (cdr l))
1199 (w 0) (nl))
1200 ((or (null l) (= width w))
1201 (cons 0 (cons (1- height)
1202 (cond ((< w width)
1203 (cons `(d-hbar ,(- width w) ,ch) nl))
1204 (t nl)))))
1205 (setq nl (cons (car l) nl) w (1+ w)))
1206 result))
1207 (setq result (nconc dummy (list* `(d-vbar ,(1- height) ,(1- depth) ,ch)
1208 (list (- width) 0) result)))
1209 (setq result (cons (list (- 1 width) (- depth) `(d-hbar ,width ,ch)) result))
1210 (setq result (list* `(d-vbar ,(1- height) ,(1- depth) ,ch) '(-1 0) result))
1211 (update-heights height depth)
1212 (return result)))
1214 (displa-def mtext dim-mtext 1 1)
1215 (displa-def %mtext dim-mtext 1 1)
1216 (defprop mtext dimnary-mtext dimension-nary-helper)
1218 (defun dim-mtext (form result)
1219 (if (null (cddr form)) (dimension (cadr form) result lop rop 0 0)
1220 (dimension-nary form result)))
1222 (displa-def mlabel dim-mlabel 0 0)
1223 (displa-def %mlabel dim-mlabel 0 0)
1224 (setf (get 'mlabel 'wxxml) 'wxxml-mlable) ;; backwards-compatibility for wxMaxima
1226 (defun dim-mlabel (form result)
1227 (prog (dummy (w 0) (h 0) (d 0))
1228 (cond ((eq nil (cadr form)) (setq w 0 h 0 d 0))
1229 (mratp (setq result (append mratp (if *display-labels-p*
1230 (dimension-paren (cadr form) result)))
1231 w (+ 4 width) h height d depth))
1232 (t (setq result (cons #\space (if *display-labels-p*
1233 (dimension-paren (cadr form) result)))
1234 w (1+ width) h height d depth)))
1235 (let ((level $linel)) (checkbreak result w))
1236 (setq dummy (list 0 0))
1237 (setq result (dimension (caddr form) (cons dummy result) 'mlabel rop w right))
1238 (cond ((and (not $leftjust) (= 0 bkptout))
1239 (rplaca dummy (max 0 (- (truncate (- $linel width) 2) w)))
1240 (setq width (+ (car dummy) width))))
1241 (setq width (+ w width) height (max h height) depth (max d depth))
1242 (return result)))
1244 (defprop mparen -1. lbp)
1245 (defprop mparen -1. rbp)
1247 (defun checkrat (form)
1248 (cond ((atom form) nil)
1249 ((and (not (atom (car form))) (eq (caar form) 'mrat))
1250 (if (member 'trunc (cdar form) :test #'eq)
1251 '(#\space #\/ #\T #\/)
1252 '(#\space #\/ #\R #\/)))
1253 ((and (not (atom (car form))) (eq (caar form) 'mpois))
1254 '(#\space #\/ #\P #\/))
1256 (do ((l (cdr form) (cdr l)))
1257 ((null l))
1258 (cond ((atom l)
1259 (merror (intl:gettext "display: not a well-formed Maxima expression: ~S") form))
1260 ((setq form (checkrat (car l)))
1261 (return form)))))))
1263 (defun checkfit (w)
1264 (or (not break) (<= (- (+ w break right 1) bkptwd) $linel)))
1266 (defun checkbreak (result w)
1267 (cond ((not break))
1268 ((> (- (setq w (+ w break)) bkptout) $linel)
1269 (if (or (null bkpt) (eq result bkpt))
1270 (merror (intl:gettext "display: failed to break up a long expression.~%display: change '$linel' slightly and try again.")))
1271 (do ((l result (cdr l)))
1272 ;; THE NEED FOR EQUAL HERE IS PROBABLY THE SYMPTOM OF A BUG IN ECL !!
1273 ;; PROBABLY RELATED TO SIDE-EFFECTS OF NRECONC, RPLACD, ETC !!
1274 ((#+ecl equal #-ecl eq bkpt (cdr l)) (rplacd l nil))
1275 (if (null l)
1276 ;; MEANING OF FOLLOWING MESSAGE IS OBSCURE.
1277 (merror (intl:gettext "display: 'checkbreak' not found."))))
1278 (output bkpt 0)
1279 (setq lines (1+ lines)
1280 bkpt result bkptout bkptwd bkptwd w
1281 bkptht maxht bkptdp maxdp bkptlevel level maxht 1 maxdp 0))
1282 ((or (null bkpt) (<= level bkptlevel) (> (truncate $linel 2) (- bkptwd bkptout)))
1283 (setq bkpt result bkptwd w bkptlevel level
1284 bkptht (max maxht bkptht) bkptdp (max maxdp bkptdp) maxht 1 maxdp 0))))
1286 (defun forcebreak (result w)
1287 (output result 0)
1288 (setq lines (+ 2 lines)
1289 bkpt nil
1290 bkptout (+ w break)
1291 maxht 1 maxdp 0))
1293 (defun update-heights (ht* dp*)
1294 (if break
1295 (setq maxht (max maxht ht*)
1296 maxdp (max maxdp dp*))))
1298 ;;; BKPT dimension structure for last breakpoint saved
1299 ;;; BKPTWD width at last bkpt
1300 ;;; BKPTHT height of current line to last bkpt
1301 ;;; BKPTDP depth of current line to last bkpt
1302 ;;; BKPTOUT width of stuff already output
1304 ;;; MAXHT height from last bkpt saved to current point
1305 ;;; MAXDP depth from last bkpt saved to current point
1307 ;;; BREAK width up to last call to DIMENSION
1308 ;;; RESULT dimension structure to current point minus output
1309 ;;; W width from last call to DIMENSION to current point
1311 ;; Code above this point deals with dimensioning and constructing
1312 ;; up dimension strings. Code past this point deals with printing
1313 ;; them.
1315 ;; <dimension string> ::= () | (<string element> . <dimension string>)
1316 ;; <string element> ::= character |
1317 ;; (<column-relative> <row-relative> . <dimension string>) |
1318 ;; (<drawing function> . args)
1319 ;; <column-relative> ::= <fixnum>
1320 ;; <row-relative> ::= <fixnum>
1321 ;; <drawing function> ::= D-HBAR | D-VBAR | D-INTEGRALSIGN | ...
1323 ;; When a character appears in a dimension string, it is printed and
1324 ;; the cursor moves forward a single position. (The variable OLDCOL is
1325 ;; incremented) When a form with a fixnum car is encountered, the
1326 ;; first two elements of the form are taken to be relative displacements
1327 ;; for OLDCOL and OLDROW. *** NOTE *** After drawing the cddr of the form,
1328 ;; OLDROW is reset to its original value, but OLDCOL is left in the new
1329 ;; position. Why this is done is beyond me. It only appears to complicate
1330 ;; things.
1332 ;; When a <drawing function> is invoked, the first argument passed to it is a
1333 ;; flag which is T for linear output and NIL for 2D output. A
1334 ;; <drawing function> is also expected to return the new column position.
1336 (defun output (result w)
1337 ;; The following is a hack to attempt to determine if we're on an
1338 ;; interactive terminal, in which case the user's hitting of the ENTER
1339 ;; key will have caused a newline to be displayed already.
1340 #+(or gcl clisp) (cond ((not (equal *query-io* *standard-input*)) (fresh-line)))
1341 #-(or gcl clisp) (cond ((not (interactive-stream-p *standard-input*)) (fresh-line)))
1342 (cond
1343 ;; If console output is disabled, don't output anything.
1344 ($ttyoff)
1345 ((> (+ bkptht bkptdp) (length linearray))
1346 ;; I suppose we could reallocate LINEARRAY to some larger size and keep going here ...
1347 (merror (intl:gettext "display: expression is too tall to be displayed.")))
1349 (output-linear (nreverse result) w))))
1351 ;; Output function for terminals without cursor positioning capability.
1352 ;; Characters are drawn into LINEARRAY instead. Each element of LINEARRAY is a
1353 ;; list -- the car is how many spaces to indent; the cdr is a list of
1354 ;; characters to draw. After drawing into this array, lines are printed one at
1355 ;; a time. This is used for printing terminals and when writing to files.
1356 ;; Block mode i/o isn't needed since PRINC is used instead of WRITE-CHAR and
1357 ;; CURSORPOS.
1359 (defun output-linear (result w)
1360 (draw-linear result bkptdp w)
1361 (do ((i (1- (+ bkptht bkptdp)) (1- i)))
1362 ((< i 0))
1363 (cond ((null (aref linearray i)))
1364 (t (output-linear-one-line i)))))
1366 (defun output-linear-one-line (i)
1367 (prog (line (n 0))
1368 (setq line (aref linearray i)
1369 line (nreverse (cdr line))
1370 n (car line))
1371 (setf (aref linearray i) nil)
1372 (tyotbsp n)
1373 (loop for v in (cdr line) do (write-char v #+(or sbcl cmu) *standard-output*))
1374 (mterpri)))
1376 ;; Move the cursor over N spaces to the left by outputting spaces.
1377 ;; This function assumes that the cursor is in the left margin when
1378 ;; it is called. This is only called from OUTPUT-LINEAR, so it is
1379 ;; used only for printing terminals or for file output.
1381 (defun tyotbsp (n)
1382 (do ()
1383 ((< n 1))
1384 (write-char #\space #+(or sbcl cmu) *standard-output*)
1385 (decf n)))
1387 (defun draw-linear (dmstr oldrow oldcol)
1388 "This puts the LINE lists into LINEARRAY ready to be drawn.
1389 Each LINE consists of first an initial number of columns to space
1390 and then the characters to be printed.
1391 oldrow and oldcol are the starting points for the the (dx,dy) offsets
1392 given in the dimension string DMSTR. It does not check that oldrow
1393 is big enough for possible negative y offsets in DMSTR, but BKPTDP is the
1394 right global to use for oldrow."
1395 (do ((line))
1396 ((null dmstr))
1397 (cond ((atom (car dmstr))
1398 (setq line (aref linearray oldrow))
1399 (cond ((null line) (setq line (list oldcol)))
1400 (t (prog (n)
1401 (setq n (car line) line (cdr line))
1402 (do ()
1403 ((<= oldcol n))
1404 (push #\space line)
1405 (incf n)))))
1406 (do ()
1407 ((or (null dmstr) (not (atom (car dmstr))))
1408 (setf (aref linearray oldrow) (cons oldcol line)))
1409 (incf oldcol)
1410 (push (car dmstr) line)
1411 (pop dmstr)))
1412 ((integerp (caar dmstr))
1413 ;; Why update OLDCOL and not OLDROW? Should either update both
1414 ;; (requiring multiple value return) or neither (analogous to lambda
1415 ;; binding).
1416 (setq oldcol (draw-linear (reverse (cddar dmstr))
1417 (+ oldrow (cadar dmstr))
1418 (+ oldcol (caar dmstr))))
1419 (pop dmstr))
1420 (t (setq oldcol (apply (caar dmstr) t (cdar dmstr)))
1421 (pop dmstr))))
1422 ;; Be sure to return this.
1423 oldcol)
1425 ;; Special symbol drawing functions -- lines, boxes, summation signs, etc.
1426 ;; Every drawing function must take at least one argument. The first
1427 ;; argument is T if equations must be printed line-by-line. Otherwise,
1428 ;; draw them using cursor movement, character graphics, or line graphics
1429 ;; if possible.
1431 ;; Most of these functions just invoke DRAW-XXX on some constant
1432 ;; list structure, so be careful about NREVERSEing. In other cases,
1433 ;; stuff is consed only for the linear case, but direct calls are used
1434 ;; in the 2D case. This should work for both cases. (See end of
1435 ;; program.)
1437 (defun d-hbar (linear? w &optional (char #\-) &aux nl)
1438 (declare (ignore linear?))
1439 (dotimes (i w)
1440 (push char nl))
1441 (draw-linear nl oldrow oldcol))
1443 ;; Notice that in all of the height computations, an offset of 2 is added or
1444 ;; subtracted to the y-dimension. This is to get the lines to fit within the
1445 ;; character cell precisely and not get clipped when moving things around in
1446 ;; the equation editor.
1448 (defun d-vbar (linear? h d &optional (char #\|))
1449 (declare (ignore linear?))
1450 (setq d (- d))
1451 (do ((i (- h 2) (1- i))
1452 (nl `((0 ,(1- h) ,char))))
1453 ((< i d) (draw-linear (nreverse nl) oldrow oldcol))
1454 (push `(-1 ,i ,char) nl)))
1456 (defun d-integralsign (linear? &aux dmstr)
1457 (declare (ignore linear?))
1458 (setq dmstr `((0 2 #\/) (-1 1 #\[) (-1 0 #\I) (-1 -1 #\]) (-1 -2 #\/)))
1459 (draw-linear dmstr oldrow oldcol))
1461 (defun d-prodsign (linear? &aux dmstr)
1462 (declare (ignore linear?))
1463 (setq dmstr '((0 2 #\\ (d-hbar 3 #\=) #\/) (-4 0) (d-vbar 2 1 #\!) #\space (d-vbar 2 1 #\!) (1 0)))
1464 (draw-linear dmstr oldrow oldcol))
1466 (defun d-sumsign (linear? &aux dmstr)
1467 (declare (ignore linear?))
1468 (setq dmstr '((0 2 (d-hbar 4 #\=)) (-4 1 #\\) #\> (-2 -1 #\/) (-1 -2 (d-hbar 4 #\=))))
1469 (draw-linear dmstr oldrow oldcol))
1471 ;; Notice how this calls D-VBAR in the non-graphic case. The entire output
1472 ;; side should be structured this way, with no consing of intermediate
1473 ;; dimension strings.
1475 (defun d-matrix (linear? direction h d)
1476 (d-vbar linear? h d (car (coerce (if (eq direction 'right)
1477 $rmxchar
1478 $lmxchar) 'list))))
1480 ;; There is wired knowledge of character offsets here.
1482 (defun d-box (linear? h d w body &aux (char 0) dmstr) ;char a char?
1483 (declare (ignore linear?))
1484 (setq char (car (coerce $boxchar 'list)))
1485 (setq dmstr `((0 ,h (d-hbar ,(+ 2 w) ,char))
1486 (,(- (+ w 2)) 0)
1487 (d-vbar ,h ,d ,char)
1488 ,@body
1489 (,(- (1+ w)) ,(- (1+ d)) (d-hbar ,(+ w 2) ,char))
1490 (-1 0)
1491 (d-vbar ,h ,d ,char)))
1492 (draw-linear dmstr oldrow oldcol))