login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for SandBoxMaxima revision 6 of 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Editor: page
Time: 2007/09/11 22:57:22 GMT-7


Testing the Maxima interface

Maxima commands are entered like this:


Each such pseudo-environment is saved in a temporary file and executed via the Maxima batch("filename") command.

First test:

(%i2)\begin{equation} 2+1 \end{equation}

(%o2)\begin{equation} \label{eq1} 3 \end{equation}
(%i3)\begin{equation} 3+2 \end{equation}

(%o3)\begin{equation} \label{eq2} 5 \end{equation}
(%i4)\begin{equation} \sqrt{2} \end{equation}

(%o4)\begin{equation} \label{eq3} \sqrt{2} \end{equation}

We can also print the root as float:

(%i6)\begin{equation} \mathrm{ev}\left(\sqrt{2}, \mathrm{numer}\right) \end{equation}

(%o6)\begin{equation} \label{eq4} 1.414213562373095 \end{equation}

We are able to separate out the LaTeX? code and display it nicely:

But separating input lines from output lines is rather hard.

Here is are some more sophisticated examples:

Math Teachers: Maxima Examples

Ordinary differential equations

(%i8)\begin{equation} \mathrm{depends}\left(y,x \right) \end{equation}

(%o8)\begin{equation*} \label{eq5} \left[ y\left(x\right) \right] \end{equation*}
(%i9)\begin{equation} \mathrm{diff}\left(y,x \right)={{4-2\x}\over{3\y^2-5}} \end{equation}

(%o9)\begin{equation} \label{eq6} {{d}\over{d\x}}\y={{4-2 \x}\over{3\y^2-5}} \end{equation}
(%i10)\begin{equation} \mathrm{ode2}\left( \mathrm{\%},y,x\right) \end{equation}

(%o10)\begin{equation} \label{eq7} -{{y^3-5\y}\over{2}}={{x ^2-4\x}\over{2}}+\mathrm{\%c} \end{equation}

Working with Matrices

(%i12)\begin{equation} a:\pmatrix{1&2\cr 3&4\cr } \end{equation}

(%o12)\begin{equation} \label{eq8} \pmatrix{1&2\cr 3&4\cr } \end{equation}
(%i13)\begin{equation} b:\pmatrix{2&2\cr 2&2\cr } \end{equation}

(%o13)\begin{equation} \label{eq9} \pmatrix{2&2\cr 2&2\cr } \end{equation}
(%i14)\begin{equation} a\cdot b \end{equation}

(%o14)\begin{equation} \label{eq10} \pmatrix{6&6\cr 14&14\cr } \end{equation}
(%i15)\begin{equation} a\b \end{equation*}

(%o15)\begin{equation} \label{eq11} \pmatrix{2&4\cr 6&8\cr } \end{equation}
(%i16)\begin{equation} h_{i,j}:={{1}\over{j+i}} \end{equation}

(%o16)\begin{equation} \label{eq12} h_{i,j}:={{1}\over{j+i}} \end{equation}
(%i17)\begin{equation} a:\mathrm{genmatrix} \left(h,3,3\right) \end{equation}

(%o17)\begin{equation} \label{eq13} \pmatrix{{{1}\over{2}}&{{ 1}\over{3}}&{{1}\over{4}}\cr {{1}\over{3}}&{{1}\over{4}}&{{1}\over{5 }}\cr {{1}\over{4}}&{{1}\over{5}}&{{1}\over{6}}\cr } \end{equation}
(%i18)\begin{equation} \mathrm{determinant} \left(a\right) \end{equation}

(%o18)\begin{equation} \label{eq14} {{1}\over{43200}} \end{equation}
(%i19)\begin{equation} b:\pmatrix{2&3\cr 5&6\cr } \end{equation}

(%o19)\begin{equation} \label{eq15} \pmatrix{2&3\cr 5&6\cr } \end{equation}
(%i20)\begin{equation} \mathrm{echelon}\left(b \right) \end{equation}

(%o20)\begin{equation} \label{eq16} \pmatrix{1&{{3}\over{2}} \cr 0&1\cr } \end{equation}
(%i21)\begin{equation} \mathrm{invert}\left(b \right) \end{equation}

(%o21)\begin{equation} \label{eq17} \pmatrix{-2&1\cr {{5 }\over{3}}&-{{2}\over{3}}\cr } \end{equation}
(%i22)\begin{equation} \mathrm{eigenvectors} \left(b\right) \end{equation}

\mbox{\verb|C|}\mbox{\verb|o|}\mbox{\verb|u|}\mbox{\verb|l|} \mbox{\verb|d|}\mbox{\verb| |}\mbox{\verb|n|}\mbox{\verb|o|} \mbox{\verb|t|}\mbox{\verb| |}\mbox{\verb|f|}\mbox{\verb|i|} \mbox{\verb|n|}\mbox{\verb|d|}\mbox{\verb| |}\mbox{\verb|`|} \mathrm{EIGEN}\mbox{\verb|'|}\mbox{\verb| |}\mbox{\verb|u|} \mbox{\verb|s|}\mbox{\verb|i|}\mbox{\verb|n|}\mbox{\verb|g|} \mbox{\verb| |}\mbox{\verb|p|}\mbox{\verb|a|}\mbox{\verb|t|} \mbox{\verb|h|}\mbox{\verb|s|}\mbox{\verb| |}\mbox{\verb|i|} \mbox{\verb|n|}\mbox{\verb| |}\mbox{\verb|f|}\mbox{\verb|i|} \mbox{\verb|l|}\mbox{\verb|e|}\mbox{\verb|_|}\mbox{\verb|s|} \mbox{\verb|e|}\mbox{\verb|a|}\mbox{\verb|r|}\mbox{\verb|c|} \mbox{\verb|h|}\mbox{\verb|_|}\mbox{\verb|m|}\mbox{\verb|a|} \mbox{\verb|x|}\mbox{\verb|i|}\mbox{\verb|m|}\mbox{\verb|a|} \mbox{\verb|,|}\mbox{\verb|s|}\mbox{\verb|y|}\mbox{\verb|s|} \mbox{\verb|t|}\mbox{\verb|e|}\mbox{\verb|m|}\mbox{\verb| |} \mbox{\verb|(|}\mbox{\verb|c|}\mbox{\verb|o|}\mbox{\verb|m|} \mbox{\verb|b|}\mbox{\verb|i|}\mbox{\verb|n|}\mbox{\verb|e|} \mbox{\verb|d|}\mbox{\verb| |}\mbox{\verb|v|}\mbox{\verb|a|} \mbox{\verb|l|}\mbox{\verb|u|}\mbox{\verb|e|}\mbox{\verb|s|} \mbox{\verb|:|}\mbox{\verb| |}\left[ \mbox{{}/home/page/.maxima/###.{mac,mc}{}}, \mbox{{}/usr/share/maxima/5.9.2/share/###.{mac,mc}{}}, \mbox{{}/usr/share/maxima/5.9.2/share/{affine,algebra,calculus,combinatorics,contrib,contrib/nset,contrib/pdiff,contrib/numericalio,contrib/descriptive,contrib/distrib,linearalgebra,diffequations,graphics,integequations,integration,macro,matrix,misc,numeric,physics,simplification,specfunctions,sym,tensor,trigonometry,utils,vector}/###.{mac,mc}{}} , \mbox{{}/usr/lib/gcl-2.6.7/unixport/../{src,share,share1,sharem}/foo.{mc,mac}{}} \right] \mbox{\verb| |}\mbox{\verb|)|} #0: eigenvectors(?_l=[matrix([2,3]?,[5,6]?)]) -- an error. Quitting. To debug this try debugmode(true);


(%i24)\begin{equation} s:0 \end{equation}

(%o24)\begin{equation} \label{eq18} 0 \end{equation}
(%i25)\begin{equation} \mathbf{for}\;i\; \mathbf{while}\;i\leq 10\;\mathbf{do}\;s:i+s \end{equation}

(%o25)\begin{equation} \label{eq19} \mathrm{done} \end{equation}
(%i26)\begin{equation} \mathrm{done} \end{equation}

(%o26)\begin{equation} \label{eq20} \mathrm{done} \end{equation}
(%i27)\begin{equation} s \end{equation}

(%o27)\begin{equation} \label{eq21} 55 \end{equation}
(%i28)\begin{equation} \mathrm{fib}_{0}:0 \end{equation}

(%o28)\begin{equation} \label{eq22} 0 \end{equation}
(%i29)\begin{equation} \mathrm{fib}_{1}:1 \end{equation}

(%o29)\begin{equation} \label{eq23} 1 \end{equation}
(%i30)\begin{equation} \mathrm{fib}_{n}:= \mathrm{fib}_{n-2}+\mathrm{fib}_{n-1} \end{equation}

(%o30)\begin{equation} \label{eq24} \mathrm{fib}_{n}:= \mathrm{fib}_{n-2}+\mathrm{fib}_{n-1} \end{equation}
(%i31)\begin{equation} \mathrm{fib}_{20} \end{equation}

(%o31)\begin{equation} \label{eq25} 6765 \end{equation}

For more examples, see SandBoxMaxima2?.

See also:



How to properly separate Maxima input and output --billpage, Wed, 09 Aug 2006 11:51:17 -0500 reply
I would like to be able to distinquish Maxima input (%i ) lines from output (%o ) lines, but Maxima insists on horizonatally centering the line identifier with the 2d ascii artwork. This makes find the start of an input or output line nearly impossible.

I know almost nothing about Maxima...

I have read about the Maxima external interface but I do not seem to be able to get Maxima to behave this way:


but I do not understand how to get it to work. For example entering the following commands at the console:

  (setf *prompt-prefix* "prefix: ")
  (setf *prompt-suffix* " :suffix")

seems to have no effect. We are running here Maxima 5.9.3.

Any hints or pointers to some more useful documentation from the Maxima gurus would be greatly appreciated!

Try this --Bob McElrath?, Wed, 09 Aug 2006 13:01:46 -0500 reply
Use the texmacs scripts directly, and modify them...

echo "integrate(x^2,x);" | maxima -p /usr/share/texmacs/TeXmacs?/plugins/maxima/lisp/texmacs-maxima-5.9.2.lisp > integral.txt

Note that the texmacs inserts a lot of control characters that will make your xterm go goofy, so above I sent the output to a file and you can look at it with a text editor.

We really don't want to parse maxima's ascii-art... ;)

Good work!

-- Bob

Re: Try this --Bill Page, Wed, 09 Aug 2006 15:32:25 -0500 reply
Damn good idea! Thanks.

I pulled just the texmacs-maxima-5.9.2.lisp from the TeXmacs? CVS and it seems to work fine. I think I will mode it a little to make it more friendly to Python. This is going to make things a lot easier.

maxima script for mathaction --billpage, Thu, 10 Aug 2006 02:32:14 -0500 reply

Based on a suggestion by Bob McElrath?, I modified the lisp code of the texmacs-maxima interface to produce something much easier to handle than raw Maxima console interface using only Python. This version also avoids the funny binary delimiter characters used by TeXmacs?. The result bears some resemblence to an XML-style protocol.

This works really great as you can see from the examples above.

The original version for TeXmacs? was written by James Amundson and modified for recent versions of TeXmacs? and Maxima by A. G. Grozin ARXiv:html/cs.SC/0504039

(in-package :maxima) #+clisp (defvar *old-suppress-check-redefinition* custom:*suppress-check-redefinition*) #+clisp (setf custom:*suppress-check-redefinition* t) (setf *alt-display2d* 'texmacs) (setf *prompt-prefix* "<prompt>") (setf *prompt-suffix* "</prompt>") ; (setf *general-display-prefix* "<verbatim>") (setf *maxima-prolog* "<maxima>") (setf *maxima-epilog* "</maxima>") #-gcl(setf *debug-io* (make-two-way-stream *standard-input* *standard-output*)) #+(or cmu sbcl scl) (setf *terminal-io* (make-two-way-stream *standard-input* *standard-output*)) (defun latex (x) (let ((ccol 1)) (mapc #'myprinc (if (and (listp x) (cdr x) (equal (cadr x) "Is ")) (tex x '("<display>") '("</display>") 'mparen 'mparen) (tex x '("<latex>") '("</latex>") 'mparen 'mparen))))) (setf *alt-display2d* 'latex) ;; Small changes to mactex.lisp for interfacing with TeXmacs ;; Andrey Grozin, 2001-2005 (defun main-prompt () (format () "~A(~A~D) ~A" *prompt-prefix* (tex-stripdollar $inchar) $linenum *prompt-suffix*)) (declare-top (special lop rop ccol $gcprint $inchar) (*expr tex-lbp tex-rbp)) (defconstant texport *standard-output*) (defun tex-stripdollar (sym) (or (symbolp sym) (return-from tex-stripdollar sym)) (let* ((name (quote-% (print-invert-case sym))) (name1 (if (memq (elt name 0) '($ &)) (subseq name 1) name)) (l (length name1))) (if (eql l 1) name1 (concatenate 'string "\\mathrm{" name1 "}")))) (defprop mtimes "\\*" texsym) (defun texmacs (x) (let ((ccol 1)) (mapc #'myprinc (tex x '("latex:$\\displaystyle ") '("$ ") 'mparen 'mparen)))) ;; In order to allow cut-and-paste from output to input, ;; we should output \sin(x), not \sin x. (map 'list #'(lambda (f) (remprop f 'tex) (remprop f 'tex-rbp)) '(%sin %cos %tan %cot %sec %csc %sinh %cosh %tanh %coth %asin %acos %atan %exp %log)) (remprop '$pi 'texword) (remprop '$gamma 'texword) (setf (get '$%i 'texword) "\\mathi") (setf (get '$%e 'texword) "\\mathe") ;; Also, we should output f(x)^2, not f^2(x) (defun tex-mexpt (x l r) (let((nc (eq (caar x) 'mncexpt))) ; true if a^^b rather than a^b (setq l (if (and (numberp (cadr x)) (numneedsparen (cadr x))) (tex (cadr x) (cons "\\left(" l) '("\\right)") lop (caar x)) (tex (cadr x) l nil lop (caar x))) r (if (mmminusp (setq x (nformat (caddr x)))) ;; the change in base-line makes parens unnecessary (if nc (tex (cadr x) '("^ {-\\langle ")(cons "\\rangle }" r) 'mparen 'mparen) (tex (cadr x) '("^ {- ")(cons " }" r) 'mminus 'mparen)) (if nc (tex x (list "^{\\langle ")(cons "\\rangle}" r) 'mparen 'mparen) (if (and (integerp x) (< x 10)) (tex x (list "^")(cons "" r) 'mparen 'mparen) (tex x (list "^{")(cons "}" r) 'mparen 'mparen))))) (append l r))) ;; binomial coefficients (defun tex-choose (x l r) `(,@l "\\binom{" ,@(tex (cadr x) nil nil 'mparen 'mparen) "}{" ,@(tex (caddr x) nil nil 'mparen 'mparen) "}" ,@r)) ;; Integrals, sums, products (defun tex-int (x l r) (let ((s1 (tex (cadr x) nil nil 'mparen 'mparen)) ;;integrand delims / & d (var (tex (caddr x) nil nil 'mparen rop))) ;; variable (cond((= (length x) 3) (append l `("\\int {" ,@s1 "}{\\;\\mathd\\;" ,@var "}\\big.") r)) (t ;; presumably length 5 (let ((low (tex (nth 3 x) nil nil 'mparen 'mparen)) ;; 1st item is 0 (hi (tex (nth 4 x) nil nil 'mparen 'mparen))) (append l `("\\int_{" ,@low "}^{" ,@hi "}{" ,@s1 "\\;\\mathd\\;" ,@var "}\\big.") r)))))) (defun tex-sum(x l r) (let ((op (cond ((eq (caar x) '%sum) "\\sum_{") ((eq (caar x) '%product) "\\prod_{") ;; extend here )) ;; gotta be one of those above (s1 (tex (cadr x) nil nil 'mparen rop)) ;; summand (index ;; "index = lowerlimit" (tex `((mequal simp) ,(caddr x),(cadddr x)) nil nil 'mparen 'mparen)) (toplim (tex (car(cddddr x)) nil nil 'mparen 'mparen))) (append l `( ,op ,@index "}^{" ,@toplim "}{" ,@s1 "}\\big.") r))) (defun tex-lsum(x l r) (let ((op (cond ((eq (caar x) '%lsum) "\\sum_{") ;; extend here )) ;; gotta be one of those above (s1 (tex (cadr x) nil nil 'mparen rop)) ;; summand (index ;; "index = lowerlimit" (tex `((min simp) , (caddr x), (cadddr x)) nil nil 'mparen 'mparen))) (append l `( ,op ,@index "}}{" ,@s1 "}\\big.") r))) ;; This is a hack for math input of integrals, sums, products (defmfun $tmint (a b f x) ($integrate f x a b)) (defmspec $tmsum (l) (setq l (cdr l)) (if (= (length l) 3) (dosum (caddr l) (cadar l) (meval (caddar l)) (meval (cadr l)) t) (wna-err '$tmsum))) (defmspec $tmlsum (l) (setq l (cdr l)) (or (= (length l) 2) (wna-err '$tmlsum)) (let ((form (cadr l)) (ind (cadar l)) (lis (meval (caddar l))) (ans 0)) (or (symbolp ind) (merror "Second argument not a variable ~M" ind)) (cond (($listp lis) (loop for v in (cdr lis) with lind = (cons ind nil) for w = (cons v nil) do (setq ans (add* ans (mbinding (lind w) (meval form))))) ans) (t `((%lsum) ,form ,ind ,lis))))) (defmspec $tmprod (l) (setq l (cdr l)) (if (= (length l) 3) (dosum (caddr l) (cadar l) (meval (caddar l)) (meval (cadr l)) nil) (wna-err '$tmprod))) #+clisp (setf custom:*suppress-check-redefinition* *old-suppress-check-redefinition*)
Compiling /var/zope/var/LatexWiki/3232980969489245406-25px001.lisp.
; (DEFUN LATEX ...) is being compiled.
;; Warning: The variable CCOL is not used.
; (DEFUN MAIN-PROMPT ...) is being compiled.
;; The variable *PROMPT-PREFIX* is undefined.
;; The compiler will assume this variable is a global.
;; The variable $INCHAR is undefined.
;; The compiler will assume this variable is a global.
;; The variable $LINENUM is undefined.
;; The compiler will assume this variable is a global.
;; The variable *PROMPT-SUFFIX* is undefined.
;; The compiler will assume this variable is a global.
; (DEFUN TEXMACS ...) is being compiled.
;; Warning: The variable CCOL is not used.
; (DEFUN TEX-MEXPT ...) is being compiled.
;; The variable LOP is undefined.
;; The compiler will assume this variable is a global.
; (DEFUN TEX-INT ...) is being compiled.
;; The variable ROP is undefined.
;; The compiler will assume this variable is a global.
End of Pass 1.  
End of Pass 2.  
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
Finished compiling /var/zope/var/LatexWiki/3232980969489245406-25px001.lisp.
   >> System error:
   The function DECLARE-TOP is undefined.

Some or all expressions may not have rendered properly, because Latex returned the following error:
This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
entering extended mode
LaTeX2e <2003/12/01>
Babel <v3.8d> and hyphenation patterns for american, french, german, ngerman, b
ahasa, basque, bulgarian, catalan, croatian, czech, danish, dutch, esperanto, e
stonian, finnish, greek, icelandic, irish, italian, latin, magyar, norsk, polis
h, portuges, romanian, russian, serbian, slovak, slovene, spanish, swedish, tur
kish, ukrainian, nohyphenation, loaded.
Document Class: article 2004/02/16 v1.4f Standard LaTeX document class
For additional information on amsmath, use the `?' option.
(/usr/share/texmf-tetex/tex/generic/xypic/xy.tex Bootstrap'ing: catcodes,
docmode, (/usr/share/texmf-tetex/tex/generic/xypic/xyrecat.tex)

Xy-pic version 3.7 <1999/02/16> Copyright (c) 1991-1998 by Kristoffer H. Rose <krisrose@ens-lyon.fr> Xy-pic is free software: see the User's Guide for details.

Loading kernel: messages; fonts; allocations: state, direction, utility macros; pictures: \xy, positions, objects, decorations; kernel objects: directionals, circles, text; options; algorithms: directions, edges, connections; Xy-pic loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xyall.tex Xy-pic option: All features v.3.3 (/usr/share/texmf-tetex/tex/generic/xypic/xycurve.tex Xy-pic option: Curve and Spline extension v.3.7 curve, circles, loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xyframe.tex Xy-pic option: Frame and Bracket extension v.3.7 loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xycmtip.tex Xy-pic option: Computer Modern tip extension v.3.3 (/usr/share/texmf-tetex/tex/generic/xypic/xytips.tex Xy-pic option: More Tips extension v.3.3 loaded) loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xyline.tex Xy-pic option: Line styles extension v.3.6 loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xyrotate.tex Xy-pic option: Rotate and Scale extension v.3.3 loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xycolor.tex Xy-pic option: Colour extension v.3.3 loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xymatrix.tex Xy-pic option: Matrix feature v.3.4 loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xyarrow.tex Xy-pic option: Arrow and Path feature v.3.5 path, \ar, loaded) (/usr/share/texmf-tetex/tex/generic/xypic/xygraph.tex Xy-pic option: Graph feature v.3.7 loaded) loaded)) (/usr/share/texmf-tetex/tex/latex/graphics/graphicx.sty (/usr/share/texmf-tetex/tex/latex/graphics/keyval.sty) (/usr/share/texmf-tetex/tex/latex/graphics/graphics.sty (/usr/share/texmf-tetex/tex/latex/graphics/trig.sty) (/usr/share/texmf-tetex/tex/latex/graphics/graphics.cfg) (/usr/share/texmf-tetex/tex/latex/graphics/dvips.def))) (/usr/share/texmf-tetex/tex/latex/tools/verbatim.sty)

LaTeX Error: File `graphviz.sty' not found.

Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty)

Enter file name: Emergency stop. <read *>

l.14 \usepackage {sagetex}^^M No pages of output. Transcript written on 3628371614119465452-18px.log.