|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
;; This file:
;; https://anggtwu.net/MAXIMA/edrxbox-examples.lisp.html
;; https://anggtwu.net/MAXIMA/edrxbox-examples.lisp
;; (find-angg "MAXIMA/edrxbox-examples.lisp")
;; Author: Eduardo Ochs <eduardoochs@gmail.com>
;; See: https://anggtwu.net/maxima-edrxbox.html
;; https://anggtwu.net/MAXIMA/edrxbox.lisp.html
;; https://anggtwu.net/MAXIMA/edrxbox.lisp
;; Version: 2025dec12
;; License: GPL v2
;;
;; This file contains several examples of `dim-*' functions
;; implemented with edrxbox. See the links above!
;;
;; Some edrx-isms:
;; (defun o () (interactive) (find-angg "MAXIMA/edrxbox.lisp"))
;; (defun e () (interactive) (find-angg "MAXIMA/edrxbox-examples.lisp"))
;; (defun oe () (interactive) (find-2a '(o) '(e)))
;; Index:
;; «.verbatimbox» (to "verbatimbox")
;; «.verbatimbox-tests» (to "verbatimbox-tests")
;; «.hdw» (to "hdw")
;; «.hdw-tests» (to "hdw-tests")
;; «.underbrace» (to "underbrace")
;; «.underbrace-tests» (to "underbrace-tests")
;; «.antideriv» (to "antideriv")
;; «.antideriv-tests» (to "antideriv-tests")
;; «.dim-edrxbox» (to "dim-edrxbox")
;; «.dim-edrxbox-tests» (to "dim-edrxbox-tests")
;;; __ __ _ _ _ _
;;; \ \ / /__ _ __| |__ __ _| |_(_)_ __ ___ | |__ _____ __
;;; \ \ / / _ \ '__| '_ \ / _` | __| | '_ ` _ \| '_ \ / _ \ \/ /
;;; \ V / __/ | | |_) | (_| | |_| | | | | | | |_) | (_) > <
;;; \_/ \___|_| |_.__/ \__,_|\__|_|_| |_| |_|_.__/ \___/_/\_\
;;;
;; «verbatimbox» (to ".verbatimbox")
(setf (get '$verbatimbox 'dimension)
'dim-$verbatimbox)
(defun verbatimbox-core (strings)
(let* ((nstrings (length strings)))
(loop for string in strings
for k from 0 to (- nstrings 1)
do (edrxboxvars-push-x-y-string 0 (- k) string))
(edrxboxvars-push-x-y-end)))
(defun verbatimbox-edrxbox (strings)
(run-in-edrxbox
(verbatimbox-core strings)))
(defun verbatimbox-edrxbox-centered (strings)
(edrxbox-vcenter
(verbatimbox-edrxbox strings)))
(defun dim-$verbatimbox (form result)
(let* ((strings (rest form)))
(run-in-edrxbox-dim
(edrxboxvars-from-edrxbox
(verbatimbox-edrxbox-centered strings)))))
;; «verbatimbox-tests» (to ".verbatimbox-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
o1 : verbatimbox("abcd", "ef", "g");
o2 : verbatimbox("abcdoo", "ef", "g", "h", "i", "j");
[box(o1)];
[box(o2)];
to_lisp();
(load "edrxbox-examples.lisp")
(verbatimbox-edrxbox '("a" "b" "c" "d" "e"))
(verbatimbox-edrxbox-centered '("a" "b" "c" "d" "e"))
(to-maxima)
|#
;;; _ _ ______ __
;;; | | | | _ \ \ / /
;;; | |_| | | | \ \ /\ / /
;;; | _ | |_| |\ V V /
;;; |_| |_|____/ \_/\_/
;;;
;; «hdw» (to ".hdw")
;; Construct a box of a certain width, depth, and height.
;; The resulting box looks like this, but without the "|" and "-"s:
;;
;; h
;; |
;; o-------w
;; |
;; |
;; d
;;
;; These hdw-boxes are useful to debug other `dim-*' functions -
;; like antideriv, below.
;;
(setf (get '$hdw 'dimension)
'dim-$hdw)
(defun dim-hdw-core (h d w)
(edrxboxvars-push-x-y-string 0 0 "+")
(if (> h 1) (edrxboxvars-push-x-y-string 0 (- h 1) "h"))
(if (> d 0) (edrxboxvars-push-x-y-string 0 (- d) "d"))
(if (> w 1) (edrxboxvars-push-x-y-string (- w 1) 0 "w"))
(edrxboxvars-push-x-y-end))
(defun dim-$hdw (form result)
(let* ((args (rest form))
(h (first args))
(d (second args))
(w (third args)))
(if (not (and (eq (length args) 3)
(integerp h) (>= h 1)
(integerp d) (>= d 0)
(integerp w) (>= w 1)))
(dimension-function form result)
(run-in-edrxbox-dim
(dim-hdw-core h d w)))))
;; «hdw-tests» (to ".hdw-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
box(hdw());
box(hdw(a,b,c));
box(hdw(1,0,1));
[box(hdw(2,1,3))];
[box(hdw(3,4,6))];
|#
;;; _ _ _ _
;;; __ _ _ __ | |_(_) __| | ___ _ __(_)_ __
;;; / _` | '_ \| __| |/ _` |/ _ \ '__| \ \ / /
;;; | (_| | | | | |_| | (_| | __/ | | |\ V /
;;; \__,_|_| |_|\__|_|\__,_|\___|_| |_| \_/
;;;
;; «antideriv» (to ".antideriv")
;; Supersedes: (find-angg "MAXIMA/dim-antideriv.lisp")
(setf (get '$antideriv 'dimension)
'dim-antideriv)
(defun dim-antideriv-core (a b c)
(let* ((a-box (edrxbox-from-form a))
(b-box (edrxbox-from-form b))
(c-box (edrxbox-from-form c))
(a-height (getf a-box :height))
(b-height (getf b-box :height))
(c-height (getf c-box :height))
(a-depth (getf a-box :depth))
(b-depth (getf b-box :depth))
(c-depth (getf c-box :depth))
(abc-height (max a-height (+ b-height b-depth 1)))
(abc-depth (max a-depth (+ c-height c-depth)))
(b-y (- abc-height b-height))
(c-y (- c-depth abc-depth))
(vbar-x (getf a-box :width))
(bc-x (+ vbar-x 1))
(vbar-box (list :width 1 :height abc-height :depth abc-depth
:result `((d-vbar ,abc-height ,abc-depth)))))
(edrxboxvars-push-x-y-edrxbox 0 0 a-box)
(edrxboxvars-push-x-y-edrxbox vbar-x 0 vbar-box)
(edrxboxvars-push-x-y-edrxbox bc-x b-y b-box)
(edrxboxvars-push-x-y-edrxbox bc-x c-y c-box)
(edrxboxvars-push-x-y-end)))
(defun dim-antideriv (form result)
(let* ((args (rest form))
(a (first args))
(b (second args))
(c (third args)))
(if (not (= (length args) 3))
(dimension-function form result)
(run-in-edrxbox-dim
(dim-antideriv-core a b c)))))
;; «antideriv-tests» (to ".antideriv-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
to_lisp();
(load "edrxbox-examples.lisp")
(to-maxima)
antideriv(a,b,c);
antideriv(hdw(5,3,1), hdw(2,1,1), hdw(2,1,1));
antideriv(hdw(2,1,3), hdw(2,1,1), hdw(2,1,1));
antideriv(x^2, x=4, x=3);
|#
;;; _ _ _ _
;;; | | | |_ __ __| | ___ _ __| |__ _ __ __ _ ___ ___
;;; | | | | '_ \ / _` |/ _ \ '__| '_ \| '__/ _` |/ __/ _ \
;;; | |_| | | | | (_| | __/ | | |_) | | | (_| | (_| __/
;;; \___/|_| |_|\__,_|\___|_| |_.__/|_| \__,_|\___\___|
;;;
;; «underbrace» (to ".underbrace")
;; Supersedes: (find-angg "MAXIMA/dim-underbrace.lisp")
(setf (get '$underbrace 'dimension)
'dim-$underbrace)
(defun dim-underbrace-core (a b)
(let* ((a-box (edrxbox-from-form a))
(b-box (edrxbox-from-form b))
(max-width (max (getf a-box :width) (getf b-box :width)))
(a-box-centered (edrxbox-hcenter max-width a-box))
(b-box-centered (edrxbox-hcenter max-width b-box))
(bar-box (list :width max-width :height 1 :depth 0
:result `((d-hbar ,max-width #\:))))
(bar-y (+ (- (getf a-box :depth)) -1))
(b-y (- bar-y (getf b-box :height))))
(edrxboxvars-push-x-y-edrxbox 0 0 a-box-centered)
(edrxboxvars-push-x-y-edrxbox 0 bar-y bar-box)
(edrxboxvars-push-x-y-edrxbox 0 b-y b-box-centered)
(edrxboxvars-push-x-y-end)))
(defun dim-$underbrace (form result)
(let* ((args (rest form))
(a (first args)) ; above the \underbrace
(b (second args))) ; below the \underbrace
(if (not (eq (length args) 2))
(dimension-function form result)
(run-in-edrxbox-dim
(dim-underbrace-core a b)))))
;; «underbrace-tests» (to ".underbrace-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
o : underbrace(bc^de, f[ghijk]);
o : underbrace(bc^de12345, f[ghijk]);
[box(o)];
|#
;;; _ _ _ _
;;; __| (_)_ __ ___ ___ __| |_ ____ _| |__ _____ __
;;; / _` | | '_ ` _ \ _____ / _ \/ _` | '__\ \/ / '_ \ / _ \ \/ /
;;; | (_| | | | | | | |_____| __/ (_| | | > <| |_) | (_) > <
;;; \__,_|_|_| |_| |_| \___|\__,_|_| /_/\_\_.__/ \___/_/\_\
;;;
;; «dim-edrxbox» (to ".dim-edrxbox")
;; This is a trick to convert edrxboxes to objects that can be
;; manipulated, and displayed, from Maxima; I learned the core
;; idea by asking questions about "taylor" in the mailing list.
;; If you run this,
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
ta : taylor (exp(x), x, 0, 3);
to_lisp();
#$ta$
(car #$ta$)
(loop for flag in (car #$ta$)
do (format t "~S~%" flag))
(to-maxima)
|#
;; ...you will see that several of the flags in "ta" are lists:
;;
;; MRAT
;; SIMP
;; (((MEXPT SIMP) $%E $X) $X)
;; (#:G494 #:G495)
;; (($X ((3 . 1)) 0 NIL #:G495 . 2))
;; TRUNC
;;
;; Maxima ignores the flags that it doesn't understand, and only
;; the functions related to "taylor" and "mrat" pay attention to
;; the "list flags" above. In the diagram below
;;
;; (:WIDTH 2 :HEIGHT 1 :DEPTH 0 :RESULT (#\b #\a))
;; -> (($EDRXBOX SIMP (EDRXBOX (:WIDTH 2 :HEIGHT 1 :DEPTH 0 :RESULT (#\b #\a)))))
;; -> (:WIDTH 2 :HEIGHT 1 :DEPTH 0 :RESULT (#\b #\a))
;;
;; ...the first arrow shows what `edrxbox-to-$edrxbox' does;
;; the second arrow shows what `edrxbox-from-$edrxbox' does.
;; Run the tests below to understand more.
(setf (get '$edrxbox 'dimension)
'dim-edrxbox)
(defun edrxbox-to-$edrxbox (edrxbox)
`(($edrxbox simp (edrxbox ,edrxbox))))
(defun edrxbox-from-$edrxbox (form)
(let* ((flags (cdar form)))
(loop for flag in flags
if (and (consp flag) (eq (car flag) 'edrxbox))
return (cadr flag))))
(defun dim-edrxbox (form result)
(let* ((edrxbox (edrxbox-from-$edrxbox form)))
(if (not edrxbox)
(dimension-function form result)
(run-in-edrxbox-dim
(edrxboxvars-from-edrxbox edrxbox)))))
(defun edrxbox-display (edrxbox)
"Display `edrxbox' from Lisp."
(let* ((form (edrxbox-to-$edrxbox edrxbox))
(formboxed `((MLIST SIMP) ((MBOX SIMP) ,form)))) ; [box(form)]
(maxima-display formboxed)))
;; «dim-edrxbox-tests» (to ".dim-edrxbox-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
edrxbox(42);
to_lisp();
(edrxbox-to-$edrxbox (edrxbox-from-string "ab"))
(defun foo (&optional add-end)
(edrxboxvars-push-x-y-string 0 0 "abcd")
(edrxboxvars-push-x-y-string 0 -1 "de")
(edrxboxvars-push-x-y-string 0 -2 "f")
(if add-end
(edrxboxvars-push-x-y-end)))
(defvar b-ok)
(defvar b-bug)
(setq b-ok (run-in-edrxbox (foo 'add-end)))
(setq b-bug (run-in-edrxbox (foo)))
(edrxbox-display b-ok)
(edrxbox-display b-bug)
(defvar $o_ok)
(defvar $o_bug)
(setq $o_ok (edrxbox-to-$edrxbox b-ok))
(setq $o_bug (edrxbox-to-$edrxbox b-bug))
(edrxbox-from-form $o_ok)
(edrxbox-from-form $o_bug)
(to-maxima)
[o_ok, box(o_ok), box(o_bug), o_bug];
|#
;;; ____ _
;;; / ___| __ ___ _____ __| |
;;; \___ \ / _` \ \ / / _ \/ _` |
;;; ___) | (_| |\ V / __/ (_| |
;;; |____/ \__,_| \_/ \___|\__,_|
;;;
;; «saved-edrxbox» (to ".saved-edrxbox")
;; WORK IN PROGRESS!!! UNFINISHED!!! UNDOCUMENTED!!! HARD TO EXPLAIN!!!
(defvar saved-edrxbox)
(defmacro save-edrxbox-run (&rest code)
`(setq saved-edrxbox (run-in-edrxbox ,@code)))
(setf (get '$saved_edrxbox 'dimension)
'dim-saved-edrxbox)
(defun dim-saved-edrxbox (form result)
(declare (ignorable form result))
(run-in-edrxbox-dim
(edrxboxvars-from-edrxbox saved-edrxbox)))
;; «saved-edrxbox-tests» (to ".saved-edrxbox-tests")
#|
* (eepitch-maxima)
* (eepitch-kill)
* (eepitch-maxima)
load("edrxbox.lisp");
load("edrxbox-examples.lisp");
to_lisp();
(load "edrxbox.lisp")
(setq saved-edrxbox (run-in-edrxbox (edrxboxvars-push-x-y-string 1 1 "@")))
(setq saved-edrxbox (run-in-edrxbox (edrxboxvars-push-x-y-string 2 -1 "!")))
(to-maxima)
:lisp (save-edrxbox-run (edrxboxvars-push-x-y-string 1 1 "@"))
[box(saved_edrxbox())];
:lisp (save-edrxbox-run (edrxboxvars-push-x-y-string 2 -1 "@"))
[box(saved_edrxbox())];
|#
;; Local Variables:
;; coding: utf-8-unix
;; End: