Testing the Maxima interface
Maxima commands are entered like this:
\begin{maxima}
command;
\end{maxima}
Each such pseudo-environment is saved in a temporary file and
executed via the Maxima batch("filename")
command.
First test:
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o3}) \black}5</latex>
(%i4) sqrt(2)
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o4}) \black}\sqrt{2}</latex>
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o4}) \black}
\mbox{{}/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/7560538117633067118-25px.001.max{}}
</latex>
We can also print the root as float:
maxima
(%i6) ev(sqrt(2),numer)
We are able to separate out the LaTeX code and display it
nicely:
But separating input lines from output lines is rather hard.
http://maxima.sourceforge.net/docs/tutorial/en/gaertner-tutorial-revision/Contents.htm
Here is are some more sophisticated examples:
Ordinary differential equations
maxima
(%i9) diff(y,x) = (4-2*x)/(3*y^2-5)
maxima
<latex>\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|1|}\mbox{\verb|:|}\mbox{\verb| |}\mbox{{}ode2.mac{}}
\mbox{\verb| |}\mbox{\verb|n|}\mbox{\verb|o|}\mbox{\verb|t|}
\mbox{\verb| |}\mbox{\verb|f|}\mbox{\verb|o|}\mbox{\verb|u|}
\mbox{\verb|n|}\mbox{\verb|d|}\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|.|}</latex>
#0: ode2(?_l=['diff(y,x,1) = (4-2*x)/(3*y^2-5),y,x])
-- an error. To debug this try: debugmode(true);
Working with Matrices
maxima
(%i12) a:matrix([1,2],[3,4])
maxima
(%i13) b:matrix([2,2],[2,2])
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o14}) \black}\pmatrix{6&6\cr 14&14\cr }
</latex>
(%i15) a*b
maxima
(%i16) h[i,j]:=1/(j+i)
maxima
(%i17) a:genmatrix(h,3,3)
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o17}) \black}\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 }</latex>
(%i18) determinant(a)
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o18}) \black}{{1}\over{43200}}</latex>
(%i19) b:matrix([2,3],[5,6])
maxima
<latex>\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|1|}\mbox{\verb|:|}\mbox{\verb| |}
\mbox{{}linearalgebra{}}\mbox{\verb| |}\mbox{\verb|n|}
\mbox{\verb|o|}\mbox{\verb|t|}\mbox{\verb| |}\mbox{\verb|f|}
\mbox{\verb|o|}\mbox{\verb|u|}\mbox{\verb|n|}\mbox{\verb|d|}
\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|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|l|}
\mbox{\verb|i|}\mbox{\verb|s|}\mbox{\verb|p|}\mbox{\verb|.|}</latex>
-- an error. To debug this try: debugmode(true);
Programming
maxima
(%i24) for i while i <= 10 do s:i+s
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o24}) \black}\mathbf{done}</latex>
(%i25) s
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o25}) \black}55</latex>
(%i26) fib[0]:0
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o26}) \black}0</latex>
(%i27) fib[1]:1
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o27}) \black}1</latex>
(%i28) fib[n]:=fib[n-2]+fib[n-1]
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o28}) \black}\mathrm{fib}_{n}:=\mathrm{fib}
_{n-2}+\mathrm{fib}_{n-1}</latex>
(%i29) fib[20]
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o29}) \black}6765</latex>
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o29}) \black}
\mbox{{}/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/3252853603640219661-25px.005.max{}}
</latex>
maxima
(%i31) assume(0 < x and x < 1)
(%i32) assume(-1 < x and x < 0)
(%i33) floor(x)
maxima
<latex>
\mbox{\tt\red(\mathrm{\%o33}) \black}
\mbox{{}/var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/5871252602191436931-25px.006.max{}}
</latex>
maxima
(%i35) build_info()
For more examples, see SandBoxMaxima2.
See also:
WebSite:maxima.sourceforge.net:/docs/maximabook
WebSite:/melusine.eu.org:/syracuse/maxima/
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:
WebSite:/web.mit.edu:/maxima_v5.9.1/share/maxima/5.9.1/doc/implementation/external-interface.txt
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!
Use the texmacs scripts directly, and modify them...
echo "integrate(x^2,x);" | maxima -p /usr/share/texmacs/OldTeXmacs/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
Damn good idea! Thanks.
I pulled just the texmacs-maxima-5.9.2.lisp from the OldTeXmacs
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.
mathaction-maxima-5.9.3.lisp
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 OldTeXmacs. 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 OldTeXmacs was written by James Amundson
and modified for recent versions of OldTeXmacs and Maxima by
A. G. Grozin ARXiv:html/cs.SC/0504039
fricas
(1) -> <lisp>
(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 OldTeXmacs
;; 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*)</lisp>
Your user access level is compiler and this command is therefore not
available. See the )set userlevel command for more information.