1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Purpose: Generate Presentation MathML code from MAXIMA
;;; File: PrMathML.lsp
;;; Author: Paul S. Wang
;;; Date: March 1999
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;=============================================================================
; (c) copyright 1999 Kent State University
; all rights reserved.
;=============================================================================
(in-package :maxima)
(macsyma-module mathml)
;; special variables used in TeXetting
(declaim (special *row* *indent* ccol mPrport $mPrautolabel $mPrworksheet $lamPrworksheet
$mPrlabelleft $lamPrautolabel $mPrdisplaytype $mPrevaluate
macmPr-lib lop rop $labels casep))
;;****************************************************************************
;; Program : prmathml
;;****************************************************************************
;;Generatig MathML presenation codes for the expr
;;This is a maxima top-level function used the form
;; prmathml(expr, [,filename[,t (d)]]) on the C-line
(defmfun $prmathml (&rest margs)
(prog (ccol *row* *indent* filename mexplabel mexpress mPrport x y)
(setq mexpress (car margs))
(setq ccol 1 *indent* 0 *row* t)
(cond
((null mexpress) (princ " NO EXPRESSION GIVEN ")
(return nil))
((null (cdr margs)) (setq filename nil) (setq mPrport t))
((null (cddr margs)) (setq filename (cadr margs))
(setq mPrport
(open (fullstrip1 (cadr margs))
:direction :output
:if-exists :append
:if-does-not-exist :create)))
(t (princ " wrong No. of Arguments given ")))
(cond
((member mexpress $labels :test #'eq)
(setq mexplabel
(intern (concatenate 'string "("
(princ-to-string (fullstrip1 mexpress))
")")))
(setq mexpress (eval mexpress)))
(t (setq mexplabel nil)
(when $mPrevaluate (setq mexpress (meval mexpress)))))
(when $mPrautolabel (setq mexplabel (updateautolabel)))
(when (symbolp (setq x mexpress))
(setq x ($verbify x))
(cond
((setq y (mget x 'mexprer))
(setq mexpress
(list '(mdefine) (cons (list x) (cdadr y))
(caddr y))))
((setq y (mget x 'mmacro))
(setq mexpress
(list '(mdefmacro) (cons (list x) (cdadr y))
(caddr y))))
((setq y (mget x 'aexpr))
(setq mexpress
(list '(mdefine)
(cons (list x 'array)
(cdadr y))
(caddr y))))))
(when (and (consp mexpress) (consp (car mexpress))
(eq 'mlable (caar mexpress)))
(setq mexpress (cadr mexpress)))
(cond
((and $lamPrworksheet
(when mexplabel
(member 'c (explode mexplabel) :test #'eq)))
(format mPrport "\\begin{verbatim}~%~a " mexplabel)
(mgrind mexpress mPrport)
(format mPrport ";~%\\end{verbatim}~%"))
((and $mPrworksheet
(when mexplabel
(member 'c (explode mexplabel) :test
#'eq)))
(format mPrport "|~a " mexplabel)
(mgrind mexpress mPrport) (format mPrport ";|~%"))
(t (cond
($lamPrautolabel
(format mPrport "\\begin{equation}~%"))
($mPrdisplaytype
(tprinc "<math xmlns='http://www.w3.org/1998/Math/MathML'>") )
(t (tprinc "<math xmlns='http://www.w3.org/1998/Math/MathML'>")))
(mPr_engine mexpress 'mparen 'mparen)
(cond
($lamPrautolabel
(format mPrport "~%\\end{equation}~%"))
($mPrdisplaytype
(when mexplabel
(if $mPrlabelleft
(format mPrport "\\leqno{\\tt ~a}" mexplabel)
(format mPrport "\\eqno{\\tt ~a}" mexplabel)))
(tprinc "</math>") (myterpri))
(t (tprinc "</math>")))))
(when filename (terpri mPrport) (close mPrport))
(return 'done)))
;; mPr_engine is a kernel fuction for this program. It checks whether
;;an argument "mexpress" is an atom or expression. Then it will assign
;;a proper function to the expression or just print if it is an atom.
;;This is an applied object-oriented programming technique.
;; arg: mexpress - macsyma internal representaton
;; lop , rop - left and right handside operator of mexpress
;;special check if expression is an array
;;check whether or not to put parenthesis
;;if not a keyword,it is a function
;;;;;; This is the work house routine ;;;;;;;;
(defun mPr_engine (mexpress lop rop)
(setq mexpress (nformat mexpress))
(if (atom mexpress) (mPr-atom mexpress)
(when (listp (car mexpress))
(cond
((member 'array (car mexpress) :test #'eq)
(mPr-array mexpress))
((or (<= (mPr-lbp (caar mexpress)) (mPr-rbp lop))
(> (mPr-lbp rop) (mPr-rbp (caar mexpress))))
(mPr-paren mexpress))
(t (if (get (caar mexpress) 'mPrprocess)
(funcall (get (caar mexpress) 'mPrprocess) mexpress)
(mPr-function mexpress)))))))
;*************************************************************************
;;
;; Utilities Section
;;
;*************************************************************************
;;; tprinc is an intelligent low level printing routine. it keeps track of
;;; the size of the output for purposes of allowing the TeX file to
;;; have a reasonable line-length. tprinc will break it at a space
;;; once it crosses a threshold.
;;; this has nothign to do with breaking the resulting equations.
;- arg: chstr - string or number to princ
;- scheme: This function keeps track of the current location
;- on the line of the cursor and makes sure
;- that a value is all printed on one line (and not divided
;- by the crazy top level os routines)
(defun row-begin(str)
(myterpri)
(princ str mPrport)
(incf *indent*)
(setq *row* t))
(defun myindent()
(do ((i *indent*))
((equal i 0) nil)
(princ " " mPrport)
(decf i)))
(defun row-end(str)
(decf *indent*)
(myterpri)
(princ str mPrport)
(setq *row* t))
(defun tpchar (c)
(incf ccol)
(princ c mPrport))
;would have exceeded the line length
; lead off with a space for safety
;so we split it up.
(defun tprinc (chstr)
(prog (chlst linebreak)
(cond ((> (+ (length (setq chlst (exploden chstr))) ccol) 80)
(setq linebreak t)))
(cond (*row* (setq *row* nil) (myterpri))) ;; *row* calls for new row
(do ((ch chlst (cdr ch)) (colc ccol (1+ colc)))
((null ch) (setq ccol colc))
(write-char (car ch) mPrport)
(if (and linebreak (eq (car ch) '>)) ;; line break only after >
(myterpri))
)))
;; myterpri is terpri with port and indent control
(defun myterpri ()
(if mPrport (terpri mPrport) (mterpri))
(setq ccol 1)
(myindent)
)
;; lastlementp is a predicate function to check a list l
;;that is there only one element left in the list.
(defun lastelementp (l) (equal (length l) 1))
;; getsymbol is a function tool. It'll get the information
;;from the database which is a symbol for an argument (atom)
(defun getsymbol (atom) (get atom 'chchr))
;; get_process is a function tool. It'll get the information
;;from the database about the process to handle the operator (atom)
;; (defun get_process (atom) (get atom 'mPrprocess))
;; setup is a function to build the database (put properties) for
;;each key word
; check if property exists already
(defun setup (arg)
(mapc #'(lambda (ls) (setf (get (car arg) (car ls)) (cadr ls)))
(cdr arg)))
;; mPr-lbp and mPr-rbp are the functions to get information
;; about size of the particular operator
;; this is from the latex version of this prog
;; not sure how well it works for MathML
(defun mPr-lbp (x) (cond ((get x 'mPr-lbp)) (t (lbp x))))
(defun mPr-rbp (x) (cond ((get x 'mPr-rbp)) (t (rbp x))))
;; reduce lbp and rbp value for mtimes to get less parentesis
(defun $lessparen ()
(setf (get 'mtimes 'mPr-lbp) '110)
(setf (get 'mtimes 'mPr-rbp) '110)
'$done)
;; get back to normal case for paren
(defun $parenback ()
(setf (get 'mtimes 'mPr-lbp) '120)
(setf (get 'mtimes 'mPr-rbp) '120)
'$done)
;; mPr-abs is a function to handle abs()
(defun mPr-abs (mexpress)
(tprinc "<mo>|</mo>")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(tprinc "<mo>|</mo>"))
;; a[1]^2 or a[x,y]^z
(defun mPr-arr-power(b e)
(tprinc "<msubsup>")
(mPr_engine (caar b) lop 'mfunction)
(cond
((equal (length b) 2)
(mPr_engine (cadr b) lop rop)
)
(t (row-begin "<mrow>") ;;(tprinc "<mrow>")
(do ((l (cdr b) (cdr l))) ((null l)(row-end "</mrow>"))
(mPr_engine (car l) lop rop)
(when (not (lastelementp l)) (tprinc "<mo>,</mo>")))
))
(mPr_engine e 'mparen 'mparen) (tprinc "</msubsup>")
)
;; when the operator is array ,this function will be called
;; ex. a[x1,..] is a top level representation
(defun mPr-array (mexpress)
(tprinc "<msub>")
(mPr_engine (caar mexpress) lop 'mfunction)
(row-begin "<mrow>")
(do ((l (cdr mexpress) (cdr l))) ((null l) (row-end "</mrow>")(tprinc "</msub>"))
(mPr_engine (car l) lop rop)
(when (not (lastelementp l)) (tprinc "<mo>,</mo>"))))
;; mPr-at is a function to handel at(..) function
;;
(defun mPr-at (mexpress)
(row-begin "<mrow>") ;;(tprinc "<mrow>")
(mPr_engine (cadr mexpress) lop rop)
(tprinc "<msub><mo>|</mo>")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(tprinc "</msub>") (row-end "</mrow>")
)
;; in mPr_engine ,whennever mexpress is an atom this function taking care
;;of it by getting a TeX symbol if it exsits. Also it handles some word wich
;;has a reserved character for TeX
;; prints instead of returning value now
(defun mPr-atom (chr)
(cond
((numberp chr) (mPr-num chr))
;; pwang 1/2005
;; ((atom chr) (tprinc "<mi>") (tprinc (fullstrip1 chr)) (tprinc "</mi>"))
((safe-get chr 'chchr) (tprinc "<mi>")
(tprinc (safe-get chr 'chchr)) (tprinc "</mi>"))
(t
(let ((my-atom (if (symbolp chr) (print-invert-case (stripdollar chr)) chr)))
(tprinc "<mi>")
(tprinc (coerce (mapcar #'handle_rsw (exploden my-atom)) 'string))
(tprinc "</mi>")))))
(defun rm (a list)
(do ((l list (cdr l)) (l2 nil)) ((null l) (reverse l2))
(when (not (equal a (car l))) (setq l2 (cons (car l) l2)))))
;; this fn is called by mPr-atom ,it checks for a reserved char.
(defun handle_rsw (c)
(cond
((equal c #\<) "<")
((equal c #\>) ">")
((equal c #\&) "&")
(t c)))
;; mPr-binomial :-
;; top level: binomail(x,y);
(defun mPr-binomial (mexpress)
(row-begin "<mrow>") (tprinc "<mo>(</mo><mfrac linethickness=\"0\">")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(tprinc " ")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(tprinc "</mfrac><mo>)</mo>") (row-end "</mrow>"))
;; mPr-det is a function to handle determinant()
(defun mPr-det (mexpress)
(let ((operand (cadr mexpress)))
(tprinc "<mi>det</mi>")
(mPr_engine operand 'mparen 'mparen)))
;; mPr-dif is a function to handle diferentiation function.
;;It calls to subfunctions powerof_d and denopart.
;;
(defun mPr-diff (mexpress)
(cond
((powerof_d (cddr mexpress)) (denopart (cddr mexpress))
(tprinc "</mfrac><mo>⁢</mo>")
(mPr_engine (cadr mexpress) 'mtimes rop)
(row-end "</mrow>"))
(t (mPr_engine (cadr mexpress) lop rop))))
;;if there is no repeating differentiation
;; just diff(exp,x)
;; if diff(exp,x,no,..)
(defun powerof_d (l)
(cond
((lastelementp l)
(row-begin "<mrow>")(tprinc "<mfrac><mo> ⅆ </mo>") t)
(t (do ((l1 l (cddr l1)) (l2 nil (cons (cadr l1) l2))
(power_of_d nil))
((null l1) (setq power_of_d (addn l2 nil))
(cond
((numberp power_of_d)
(cond
((equal 0 power_of_d) nil)
((equal 1 power_of_d)(row-begin "<mrow>")(tprinc "<mfrac><mo> ⅆ </mo>") t)
(t (row-begin "<mrow>")(tprinc "<mfrac><msup><mo> ⅆ</mo><mn>")
(tprinc power_of_d) (tprinc "</mn></msup>") t)))
(t (row-begin "<mrow>")(tprinc "<mfrac><msup><mo> ⅆ</mo>")
(mPr_engine power_of_d 'mparen 'mparen)
(tprinc "</msup>") t))))))
)
;;if just diff(exp,x)
;;if diff(exp,x,nox,y,noy,...)
(defun denopart (l)
(prog (result)
(cond
((lastelementp l) (row-begin "<mrow>") (tprinc "<mo> ⅆ </mo>")
(p-op (getsymbol 'mtimes)) (mPr_engine (car l) 'mtimes rop)
(row-end "</mrow>"))
(t (do ((l1 l (cddr l1)) (l2 nil)) ((null l1) (setq result l2))
(setq l2
(cons (append '((mexpt)) (list (car l1))
(list (cadr l1)))
l2)))
(setq result (muln result nil))
(cond
((atom result) (row-begin "<mrow>")(tprinc "<mo> ⅆ </mo>")
(p-op (getsymbol 'mtimes))
(mPr_engine result 'mparen 'mparen) (row-end "</mrow>"))
((listp result)
(cond
((eq (caar result) 'mexpt)
(row-begin "<mrow>")(tprinc "<mo> ⅆ </mo>")
(p-op (getsymbol 'mtimes))
(mPr_engine result 'mtimes 'mparen) (row-end "</mrow>"))
(t (row-begin "<mrow>")
(do ((l1 (cdr result) (cdr l1)) (l2 nil)
(power_of_d nil))
((null l1) (row-end "</mrow>"))
(tprinc "<mo> ⅆ </mo>")
(p-op (getsymbol 'mtimes))
(mPr_engine (car l1) 'mtimes 'mtimes)
(when (not (lastelementp l1)) (tprinc "<mo>,</mo>"))
)
))))))))
;; this fuction is adopted the main idea form macTeX from Prof. Richard
;; Fateman in the mPr-mexpt
;;
;; insert left-angle-brackets for mncexpt. a^<n> is how a^^n looks.
;; here is where we have to check for f(x)^b to be displayed
;; as f^b(x), as is the case for sin(x)^2 .
;; which should be sin^2 x rather than (sin x)^2 or (sin(x))^2.
;; yet we must not display (a+b)^2 as +^2(a,b)...
;; or (sin(x))^(-1) as sin^(-1)x, which would be arcsine x
; this is f(x)
; this is f [or nil]
;this is (x) [maybe (x,y..), or nil]
;; this is the exponent
; there is such a function
;; insist it is a % or $ function
; x
;;this case like sin(x)^x --> sin x
;; if for example exp = (x+2)^4
;; in case x^^y
(defun mPr-expt (mexpress)
(cond
((eq (caar mexpress) 'mexpt)
(if (and (not (atom (cadr mexpress)))
(member 'array (caadr mexpress) :test #'eq) ;; array
)
(mPr-arr-power (cadr mexpress) (caddr mexpress))
(let* ((fx (cadr mexpress))
(f (and (not (atom fx)) (atom (caar fx)) (caar fx)))
(bascdr (and f (cdr fx))) (expon (caddr mexpress))
(doit (and f (member (char (string f) 0) (list #\% #\$) :test #'eq)
(not (member f '(%sum %product) :test #'eq)))))
(cond
(doit (cond ;;;; sin^2 x case
((atom expon)
(row-begin "<mrow>")(tprinc "<msup>") (mPr-fname f)
(mPr-atom expon)(tprinc "</msup>")
(tprinc "<mo>⁡</mo>")
(if (cdr bascdr) (mPr-listparen bascdr)
(mPr_engine (car bascdr) 'mtimes 'mtimes))
(row-end "</mrow>")
)
(t (tprinc "<msup>")
(mPr_engine fx 'mparen 'mparen)
(mPr_engine expon 'mparen 'mparen)
(tprinc "</msup>"))))
(t (tprinc "<msup>")
(mPr_engine (cadr mexpress) lop (caar mexpress))
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(tprinc "</msup>")))))
)
(t (tprinc "<msup>") ;;; mnexpt case
(mPr_engine (cadr mexpress) lop (caar mexpress))
(row-begin "<mrow>")(tprinc "<mo>⟨</mo>")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(tprinc "<mo>⟩</mo>")(row-end "</mrow>")
(tprinc "</msup>")))
)
;; this function will check that whether or not an arg has a symbol
;;in data base or not, if not it 'll be treated to be function which 'll
;;be printed in rm font
(defun mPr-fname (f)
(tprinc "<mi>")
(tprinc (if (getsymbol f) (getsymbol f) f))
(tprinc "</mi>")
)
;; to handle if an operator is a function which will be printed
(defun mPr-function (mexpress)
(mPr_engine (caar mexpress) 'mparen 'mparen)
(tprinc "<mo>⁡</mo>")
(mPr-listparen (cdr mexpress)))
;; for infix operator , and also handle when there is a truncation
;;in macsyma expression (see mPr-infix1)
;; mPr-infix calling
;; 1)mPr-infix1 calling
;; 1.1) p-op-oprd
;; 2)p-op-oprd
;;
;if -x or +x so call mPr-function
(defun mPr-infix (mexpress)
(let ((moperator (car mexpress)) (moperands (cdr mexpress)))
(cond
((equal (length moperands) 1) (mPr-function mexpress))
(t (row-begin "<mrow>")
(mPr_engine (car moperands) lop (car moperator))
(p-op-oprd moperator (cadr moperands))
(mPr-infix1 moperator (cddr moperands))
(row-end "</mrow>")
))))
(defun mPr-infix1 (moperator moperands)
(cond
((null moperands)
(when (member 'trunc moperator :test #'equal)
(p-op (getsymbol (car moperator)))
(tprinc "<mo>⃛</mo>")))
(t (p-op-oprd moperator (car moperands))
(mPr-infix1 moperator (cdr moperands)))))
;; p-op-oprd is a function printing operator and operand consecutively
;; ex + x when + is a infix op and x is oprd
(defun p-op-oprd (moperator moperand)
(let ((op (car moperator)))
(cond
((equal op 'mplus)
(cond
((listp moperand)
(cond
((equal (caar moperand) 'mminus) (tprinc "<mo>-</mo>")
(mPr_engine (cadr moperand) 'mminus rop))
(t (tprinc "<mo>+</mo>") (mPr_engine moperand 'mplus rop))))
(t (tprinc "<mo>+</mo>") (mPr-atom moperand))))
(t (p-op (getsymbol op))
(mPr_engine moperand op op))))
)
(defun p-op(symbol)
(tprinc "<mo>")
(tprinc symbol)
(tprinc "</mo>")
)
;; mPr-intgrate handles an integration expression
;; It will detect that integrate function is called in short form
;; or long form example: integrate(x^4,x,0,inf) is a long form.
;;short form
;;long form
(defun mPr-integrate (mexpress)
(setq mexpress (meval
(list '($substitute) '((mminus) $inf) '$minf mexpress)))
(cond
((equal (length mexpress) 3)
(row-begin "<mrow>")(tprinc "<mo>∫</mo>"))
((equal (length mexpress) 5)
(row-begin "<mrow>")(tprinc "<msubsup><mo>∫</mo>")
(mPr_engine (cadddr mexpress) 'mparen 'mparen) (tprinc " ")
(mPr_engine (car (cddddr mexpress)) 'mparen 'mparen)
(tprinc "</msubsup>"))
(t (merror "Wrong NO. of Arguments")))
(row-begin "<mrow>")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(row-end "</mrow>")(tprinc "<mo>⁢</mo><mo> ⅆ </mo>")
(mPr_engine (caddr mexpress) 'mparen rop)
(row-end "</mrow>"))
;; mPr-limit takes care the "limit(exp,var,val,dir)"
(defun mPr-limit (mexpress)
(setq mexpress (meval
(list '($substitute) '((mminus) $inf) '$minf mexpress)))
(row-begin "<mrow>")(tprinc "<munder><mo>lim</mo>")(row-begin "<mrow>")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(tprinc "<mo>→</mo>")
(mPr_engine (cadddr mexpress) 'mparen 'mapren)
(when (car (cddddr mexpress))
(if (member (car (cddddr mexpress)) '($minus $plus) :test #'equal)
(p-op (getsymbol (car (cddddr mexpress))))
(merror "THE 4TH ARG MUST BE PLUS OR MINUS")))
(row-end "</mrow>")(tprinc "</munder>")
;; (tprinc "<mo>⁢</mo>")
(mPr_engine (cadr mexpress) 'mparen rop)
(row-end "</mrow>"))
;; This function handles a macsyma list expression
;;
(defun mPr-list (mexpress)
(tprinc "<mo>[</mo>")
(do ((l (cdr mexpress) (cdr l))) ((null l) (tprinc "<mo>]</mo>"))
(mPr_engine (car l) 'mparen 'mparen)
(when (not (lastelementp l)) (tprinc "<mo>,</mo>"))))
;; This function is a subfunction of mPr-expt , mPr-function and
;; mPr-mqapply
(defun mPr-listparen (mexpress)
(row-begin "<mrow>")(tprinc "<mo>(</mo>")
(do ((l mexpress (cdr l))) ((null l) (tprinc "<mo>)</mo>") (row-end "</mrow>"))
(mPr_engine (car l) 'mparen 'mparen)
(when (not (lastelementp l)) (tprinc "<mo>,</mo>"))))
;; mPr-matrix handles matrix function
(defun mPr-matrix (mexpress)
(row-begin "<mfenced open='(' close=')'><mtable>")
(mapc #'(lambda (arg)
(row-begin "<mtr>")
(do ((l (cdr arg) (cdr l))) ((null l) (row-end "</mtr>"))
(row-begin "<mtd>")
(mPr_engine (car l) 'mparen 'mparen)
(row-end "</mtd>")
))
(cdr mexpress))
(row-end "</mtable></mfenced>")
)
(defun mPr-mqapply (mexpress)
(mPr_engine (cadr mexpress) lop 'mfunction)
(mPr-listparen (cddr mexpress)))
;; this function handles the floating point number.
;; convert 1.2e20 to 1.2 \\cdot 10^{20}
;; is it ddd.ddde+EE
;; it is not. go with it as given
(defun mPr-num (atom)
(let (r firstpart exponent)
(cond
((integerp atom) (tprinc "<mn>") (tprinc atom) (tprinc "</mn>"))
(t (setq r (explode atom))
(setq exponent (member 'e r :test #'eq))
(cond
((null exponent) (tprinc "<mn>") (tprinc atom) (tprinc "</mn>"))
(t (setq firstpart
(nreverse (cdr (member 'e (reverse r) :test #'eq))))
(tprinc "<mn>")
(mapc #'tpchar firstpart) (tprinc "</mn>")
(tprinc "<mo>·</mo><msup><mn>10</mn> <mn>")
(mapc #'tpchar (cdr exponent))
(tprinc "</mn></msup>")))))))
;; this function puts parenthesis for the expression
(defun mPr-paren (mexpress)
(row-begin "<mrow>")(tprinc "<mo>(</mo>")
(mPr_engine mexpress 'mparen 'mparen)
(tprinc "<mo>)</mo>") (row-end "</mrow>"))
;; this function handles "+" operator which is infix form
;;
;if -x or +x so call mPr-function
(defun mPr-plus (mexpress)
(let ((moperands (cdr mexpress))
(flag_trunc (member 'trunc (car mexpress) :test #'eq)))
(cond
((equal (length moperands) 1) (mPr-prefix mexpress))
(t (row-begin "<mrow>")
(mPr_engine (car moperands) lop 'mplus)
(print_op_oprd (cadr moperands))
(mPr-plus1 (cddr moperands) flag_trunc)
(row-end "</mrow>")
)
)))
(defun mPr-plus1 (moperands flag_trunc)
(cond
((null moperands) (when flag_trunc (tprinc "<mo>+</mo><mo>⃛</mo>")))
(t (print_op_oprd (car moperands))
(mPr-plus1 (cdr moperands) flag_trunc))))
(defun print_op_oprd (moperand)
(cond
((listp moperand)
(cond
((equal (caar moperand) 'mminus) (tprinc "<mo>-</mo>")
(mPr_engine (cadr moperand) 'mplus rop))
(t (tprinc "<mo>+</mo>") (mPr_engine moperand 'mplus 'mparen))))
(t (tprinc "<mo>+</mo>") (mPr-atom moperand))))
;; mPr-postfix handles for postfix notation expression like factorial
;;
(defun mPr-postfix (mexpress)
(row-begin "<mrow>")
(mPr_engine (cadr mexpress) lop (caar mexpress))
(row-end "</mrow>")
(p-op (getsymbol (caar mexpress))))
;; mPr-prefix is a function to handle a prefix notation form
;;
(defun mPr-prefix (mexpress)
(let ((op (caar mexpress)) (oprnd (cadr mexpress)))
(row-begin "<mrow>")
(p-op (getsymbol op))
(mPr_engine oprnd op rop)
(row-end "</mrow>")))
;; this function takes care the quotient function or "/" sign
;;
(defun mPr-quotient (mexpress)
(row-begin "<mfrac><mrow>")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(row-end "</mrow>")
(row-begin "<mrow>")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(row-end "</mrow></mfrac>"))
(defun mPr-rat (mexpress) (mPr-quotient mexpress))
;; this function handles binomial coefficients
;;
(defun mPr-binomial(mexpress)
(row-begin "<mrow><mfenced open='(' close=')'><mfrac linethickness='0'><mrow>")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(tprinc "</mrow><mrow>")
(mPr_engine (caddr mexpress) 'mparen 'mparen)
(row-end "</mrow></mfrac></mfenced></mrow>")
)
;; this function handles sqrt
;;
(defun mPr-sqrt (mexpress)
(tprinc "<msqrt>")
(mPr_engine (cadr mexpress) 'mparen 'mparen)
(tprinc "</msqrt>"))
;; This function taks care both sum(exp,ind,lo,hi) and
;; product(exp,ind,lo,hi)
;;ind
;;low
;; hi
;;exp
(defun mPr-sumprod (mexpress)
(row-begin "<mrow>")(tprinc "<munderover>")
(p-op (getsymbol (caar mexpress)))
(row-begin "<mrow>")
(mPr_engine (caddr mexpress) 'mparen 'mequal)
(tprinc "<mo>=</mo>")
(mPr_engine (meval
(list '($substitute) '((mminus) $inf) '$minf (cadddr mexpress)))
'mequal 'mparen)
(row-end "</mrow>")
(mPr_engine (meval
(list '($substitute) '((mminus) $inf) '$minf (car (cddddr mexpress))))
'mparen 'mparen)
(tprinc "</munderover>")
(mPr_engine (cadr mexpress) 'mparen rop)
(row-end "</mrow>"))
;; mPr-times a function handle multiplication
(defun mPr-times (mexpress)
(let ((lop 'mtimes) (rop 'mtimes)) (mPr-infix mexpress)))
;;;;;;; Operators
(setup '(mlist (mPrprocess mPr-list)))
(setup '(mplus (mPrprocess mPr-plus) (mPr-lbp 100) (mPr-rbp 100)
(chchr "+")))
(setup '(mminus (mPrprocess mPr-prefix) (mPr-lbp 100) (mPr-rbp 100)
(chchr "-")))
(setup '(mquote (mPrprocess mPr-prefix) (mPr-rbp 201) (chchr "'")))
(setup '(mand (mPrprocess mPr-infix) (mPr-lbp 60) (mPr-rbp 60)
(chchr "and")))
(setup '(mor (mPrprocess mPr-infix) (mPr-lbp 50) (mPr-rbp 50)
(chchr "or")))
(setup '(mnot (mPrprocess mPr-prefix) (mPr-rbp 70) (chchr "¬")))
(setup '(mgreaterp (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr ">")))
(setup '(mgeqp (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr "≥")))
(setup '(mnotequal (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr "≠")))
(setup '(mleqp (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr "≤")))
(setup '(mlessp (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr "<")))
(setup '(msetq (mPrprocess mPr-infix) (mPr-lbp 180) (mPr-rbp 20)
(chchr "≔")))
(setup '(mset (mPrprocess mPr-infix) (mPr-lbp 180) (mPr-rbp 20)
(chchr "≔"))) ;;; This is not math
(setup '(mdefine (mPrprocess mPr-infix) (mPr-lbp 180) (mPr-rbp 20)
(chchr ":=")))
(setup '(mfactorial (mPrprocess mPr-postfix) (mPr-lbp 160) (chchr "!")))
(setup '(mabs (mPrprocess mPr-abs)))
(setup '(%abs (mPrprocess mPr-abs)))
(setup '(mnctimes (mPrprocess mPr-infix) (mPr-lbp 110) (mPr-rbp 109)
(chchr "·")))
(setup '(marrow (mPrprocess mPr-infix) (mPr-lbp 180) (mPr-rbp 20)
(chchr "→")))
(setup '(mrarrow (mPrprocess mPr-prefix) (mPr-lbp 180) (mPr-rbp 20)
(chchr "→")))
(setup '(mdif (mPrprocess mPr-infix) (mPr-lbp 100) (mPr-rbp 100)
(chchr "-")))
(setup '(mtimes (mPrprocess mPr-times) (mPr-lbp 120) (mPr-rbp 120)
(chchr "⁢")))
(setup '(mdottimes (mPrprocess mPr-infix) (mPr-lbp 120) (mPr-rbp 120)
(chchr "·")))
(setup '(mexpt (mPrprocess mPr-expt) (mPr-lbp 140) (mPr-rbp 139)))
(setup '(mncexpt (mPrprocess mPr-expt) (mPr-lbp 135) (mPr-rbp 134)))
(setup '(%at (mPrprocess mPr-at)))
(setup '($at (mPrprocess mPr-at)))
(setup '($det (mPrprocess mPr-det)))
(setup '(%determinant (mPrprocess mPr-det)))
(setup '($binomial (mPrprocess mPr-binomial)))
(setup '(%binomial (mPrprocess mPr-binomial)))
(setup '(%sum (mPrprocess mPr-sumprod) (chchr "∑")))
(setup '($sum (mPrprocess mPr-sumprod) (chchr "∑")))
(setup '($product (mPrprocess mPr-sumprod) (chchr "&Prod;")))
(setup '(%product (mPrprocess mPr-sumprod) (chchr "&Prod;")))
(setup '($integrate (mPrprocess mPr-integrate) (chchr "∫")))
(setup '(%integrate (mPrprocess mPr-integrate) (chchr "∫")))
(setup '($diff (mPrprocess mPr-diff) (chchr "<mo>ⅆ</mo>")))
(setup '(%derivative (mPrprocess mPr-diff) (chchr "<mo>ⅆ</mo>")))
(setup '($limit (mPrprocess mPr-limit)))
(setup '(%limit (mPrprocess mPr-limit)))
(setup '($sqrt (mPrprocess mPr-sqrt) (chchr "√")))
(setup '(%sqrt (mPrprocess mPr-sqrt) (chchr "√")))
(setup '(%binomial (mPrprocess mPr-binomial)))
(setup '(mquotient (mPrprocess mPr-quotient) (mPr-lbp 122)
(mPr-rbp 123) (chchr "<mo>/</mo>")))
(setup '(rat (mPrprocess mPr-rat) (mPr-lbp 120) (mPr-rbp 121)))
(setup '(mconc (mPrprocess mPr-infix) (chchr " ")))
(setup '(mparen (chchr " ")))
(setup '(mbrak (chchr " ")))
(setup '(mequal (mPrprocess mPr-infix) (mPr-lbp 80) (mPr-rbp 80)
(chchr "=")))
;;(setup '(mmsubs (mPrprocess mPr-mmsubs) (chchr "&")))
(setup '(mqapply (mPrprocess mPr-mqapply)))
(setup '(mmfunct (mPrprocess mPr-funct)))
(setup '($matrix (mPrprocess mPr-matrix)))
(setup '($%pi (chchr "π")))
(setup '($%e (chchr "ⅇ")))
(setup '($%gamma (chchr "γ")))
(setup '($%phi (chchr "φ")))
(setup '(& (chchr "&")))
(setup '(% (chchr "%")))
(setup '($ (chchr "$")))
(setup '(_ (chchr "_")))
(setup '($minus (chchr "-")))
(setup '($plus (chchr "+")))
;;
(setup '(mprog (chchr "block")))
(setup '($$block (chchr "block")))
(setup '($$boldif (chchr "if")))
(setup '($$boldthen (chchr "then")))
(setup '($$boldelse (chchr "else")))
;;;; routines to access these fields
;; The following are databases for special characters
(setf (get '$inf 'chchr) '"∞")
;;;(setf (get '$minf 'chchr) '"<mo>-</mo>∞")
;; lower case greek database
(setf (get '$alpha 'chchr) '"α")
(setf (get '%alpha 'chchr) '"α")
(setf (get '$beta 'chchr) '"β")
(setf (get '$gamma 'chchr) '"γ")
(setf (get '%gamma 'chchr) '"γ")
(setf (get '$delta 'chchr) '"δ")
(setf (get '$epsilon 'chchr) '"ε")
(setf (get '$varepsilon 'chchr) '"ϵ")
(setf (get '$zeta 'chchr) '"ζ")
(setf (get '$eta 'chchr) '"η")
(setf (get '$theta 'chchr) '"θ")
(setf (get '$vartheta 'chchr) '"ϑ")
(setf (get '$iota 'chchr) '"ι")
(setf (get '$kappa 'chchr) '"κ")
(setf (get '$lambda 'chchr) '"λ")
(setf (get 'lambda 'chchr) '"λ")
(setf (get '$mu 'chchr) '"μ")
(setf (get '$nu 'chchr) '"ν")
(setf (get '$xi 'chchr) '"ξ")
(setf (get '$pi 'chchr) '"π")
(setf (get '$varpi 'chchr) '"ϖ")
(setf (get '$rho 'chchr) '"ρ")
(setf (get '$varrho 'chchr) '"ϱ")
(setf (get '$sigma 'chchr) '"σ")
(setf (get '$varsigma 'chchr) '"ς")
(setf (get '$tau 'chchr) '"τ")
(setf (get '$upsilon 'chchr) '"υ")
(setf (get '$phi 'chchr) '"φ")
(setf (get '$varphi 'chchr) '"ϕ")
(setf (get '$chi 'chchr) '"χ")
(setf (get '$psi 'chchr) '"ψ")
(setf (get '$omega 'chchr) '"ω")
;; Greek Upper Case Database
(setf (get '|$Alpha| 'chchr) '"Α")
(setf (get '|$Gamma| 'chchr) '"Γ")
(setf (get '|$Delta| 'chchr) '"Δ")
(setf (get '|$Theta| 'chchr) '"Θ")
(setf (get '|$Lambda| 'chchr) '"Λ")
(setf (get '|$Xi| 'chchr) '"Ξ")
(setf (get '|$Pi| 'chchr) '"Π")
(setf (get '|$Sigma| 'chchr) '"Σ")
(setf (get '|$Upsilon| 'chchr) '"Υ")
(setf (get '|$Phi| 'chchr) '"Φ")
(setf (get '|$Psi| 'chchr) '"Ψ")
(setf (get '|$Omega| 'chchr) '"Ω")
(setf (get '|$Re| 'chchr) '"ℜ")
(setf (get '|$Im| 'chchr) '"ℑ")
;; Miscellaneous symbols
(setf (get '$aleph 'chchr) '"ℵ")
(setf (get '$hbar 'chchr) '"ℏ")
(setf (get '$%i 'chchr) '"ⅈ")
(setf (get '$%j 'chchr) '"&ij")
(setf (get '$ell 'chchr) '"ℓ")
(setf (get '$wp 'chchr) '"℘")
(setf (get '$mho 'chchr) '"℧")
(setf (get '$infty 'chchr) '"&infty;")
(setf (get '$nabla 'chchr) '"∇")
(setf (get '$partial 'chchr) '"∂")
(setf (get '$triangle 'chchr) '"▵")
(setup '(%sin (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sin")))
(setup '(%cos (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cos")))
(setup '(%tan (mPrprocess mPr-function) (mPr-rbp 110) (chchr"tan")))
(setup '(%cot (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cot")))
(setup '(%sec (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sec")))
(setup '(%csc (mPrprocess mPr-function) (mPr-rbp 110) (chchr"csc")))
(setup '(%asin (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arcsin")))
(setup '(%acos (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arccos")))
(setup '(%atan (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arctan")))
(setup '(%acot (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acot")))
(setup '(%asec (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asec")))
(setup '(%acsc (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acsc")))
(setup '(%sinh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sinh")))
(setup '(%cosh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cosh")))
(setup '(%tanh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"tanh")))
(setup '(%coth (mPrprocess mPr-function) (mPr-rbp 110) (chchr"coth")))
(setup '(%sech (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sec")))
(setup '(%csch (mPrprocess mPr-function) (mPr-rbp 110) (chchr"csch")))
(setup '(%asinh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asinh")))
(setup '(%acosh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acosh")))
(setup '(%atanh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"atanh")))
(setup '(%acoth (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acoth")))
(setup '(%asech (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asec")))
(setup '(%acsch (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acsch")))
(setup '(%ln (mPrprocess mPr-function) (mPr-rbp 110) (chchr"ln")))
(setup '(%log (mPrprocess mPr-function) (mPr-rbp 110) (chchr"log")))
(setup '($sin (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sin")))
(setup '($cos (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cos")))
(setup '($tan (mPrprocess mPr-function) (mPr-rbp 110) (chchr"tan")))
(setup '($cot (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cot")))
(setup '($sec (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sec")))
(setup '($csc (mPrprocess mPr-function) (mPr-rbp 110) (chchr"csc")))
(setup '($asin (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arcsin")))
(setup '($acos (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arccos")))
(setup '($atan (mPrprocess mPr-function) (mPr-rbp 110) (chchr"arctan")))
(setup '($acot (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acot")))
(setup '($asec (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asec")))
(setup '($acsc (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acsc")))
(setup '($sinh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sinh")))
(setup '($cosh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"cosh")))
(setup '($tanh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"tanh")))
(setup '($coth (mPrprocess mPr-function) (mPr-rbp 110) (chchr"coth")))
(setup '($sech (mPrprocess mPr-function) (mPr-rbp 110) (chchr"sec")))
(setup '($csch (mPrprocess mPr-function) (mPr-rbp 110) (chchr"csch")))
(setup '($asinh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asinh")))
(setup '($acosh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acosh")))
(setup '($atanh (mPrprocess mPr-function) (mPr-rbp 110) (chchr"atanh")))
(setup '($acoth (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acoth")))
(setup '($asech (mPrprocess mPr-function) (mPr-rbp 110) (chchr"asec")))
(setup '($acsch (mPrprocess mPr-function) (mPr-rbp 110) (chchr"acsch")))
(setup '($ln (mPrprocess mPr-function) (mPr-rbp 110) (chchr"ln")))
(setup '($log (mPrprocess mPr-function) (mPr-rbp 110) (chchr"log")))
;;
;;
;; set the preference feature
;;
($lessparen)
(setq casep nil) ;set to distinguish a capital or lower case
(setq $mPrworksheet nil) ;set TeX worksheet mode false
(setq $lamPrworksheet nil) ;set LaTeX worksheet mode false
(setq $mPrlabelleft nil) ;set Tex or LaTeX left Labeling mode false
(setq $mPrdisplaytype t) ;set default for TeX or LaTeX in display type
(setq $mPrevaluate t) ;set default for evaluating macsyma expression
(setq $mPrautolabel nil) ;set autolabel mode off, can be set to be integer
(setq $lamPrautolabel nil) ;set LaTeX autolabel mode false
|