[-- Attachment #1: Type: text/plain, Size: 83 bytes --] Please consider a patch to construct a table from the output of maxima code block. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: 0001-ob-maxima-Construct-a-table-from-the-output-of-the-c.patch --] [-- Type: text/x-diff, Size: 7408 bytes --] From a0305117f4e793c93d7d10bc7aab04f96bd62e9c Mon Sep 17 00:00:00 2001 From: Litvinov Sergey <slitvinov@gmail.com> Date: Sat, 1 Oct 2011 22:29:18 +0200 Subject: [PATCH] [ob-maxima] Construct a table from the output of the code block. Add ert tests. --- lisp/ob-maxima.el | 8 +++- testing/README.org | 1 + testing/examples/ob-maxima-test.org | 64 +++++++++++++++++++-------- testing/lisp/test-ob-maxima.el | 82 +++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 19 deletions(-) create mode 100644 testing/lisp/test-ob-maxima.el diff --git a/lisp/ob-maxima.el b/lisp/ob-maxima.el index 3b4f6a0..9c39a5f 100644 --- a/lisp/ob-maxima.el +++ b/lisp/ob-maxima.el @@ -84,7 +84,13 @@ called by `org-babel-execute-src-block'." (org-babel-eval cmd ""))))) (if (org-babel-maxima-graphical-output-file params) nil - result))) + (if (or (member "scalar" result-params) + (member "verbatim" result-params) + (member "output" result-params)) + result + (let ((tmp-file (org-babel-temp-file "maxima-res-"))) + (with-temp-file tmp-file (insert result)) + (org-babel-import-elisp-from-file tmp-file)))))) (defun org-babel-prep-session:maxima (session params) diff --git a/testing/README.org b/testing/README.org index 2f16a55..eba147f 100644 --- a/testing/README.org +++ b/testing/README.org @@ -80,6 +80,7 @@ First tangle this file out to your desktop. (concat org-dir "/testing/examples/normal.org") (concat org-dir "/testing/examples/ob-awk-test.org") (concat org-dir "/testing/examples/ob-fortran-test.org") + (concat org-dir "/testing/examples/ob-maxima-test.org") (concat org-dir "/testing/examples/link-in-heading.org") (concat org-dir "/testing/examples/links.org"))) diff --git a/testing/examples/ob-maxima-test.org b/testing/examples/ob-maxima-test.org index 23c76e7..ad5fc80 100644 --- a/testing/examples/ob-maxima-test.org +++ b/testing/examples/ob-maxima-test.org @@ -1,13 +1,20 @@ -* Test org maxima file +#+Title: a collection of examples for ob-maxima tests +#+OPTIONS: ^:nil + +* Simple tests + :PROPERTIES: + :ID: b5842ed4-8e8b-4b18-a1c9-cef006b6a6c8 + :END: #+begin_src maxima :var s=4 :results silent print(s); #+end_src Pass a string #+begin_src maxima :var fun="sin(x)" :var q=2 :results silent -print(diff(fun, x, q)); +print(diff(fun, x, q))$ #+end_src +* Graphic output Graphic output #+begin_src maxima :var a=0.5 :results graphics :file maxima-test-sin.png plot2d(sin(a*x), [x, 0, 2*%pi])$ @@ -16,21 +23,28 @@ plot2d(sin(a*x), [x, 0, 2*%pi])$ #+begin_src maxima :results graphics :file maxima-test-3d.png plot3d (2^(-u^2 + v^2), [u, -3, 3], [v, -2, 2])$ #+end_src - +* Output to a file Output to a file #+begin_src maxima :file maxima-test-ouput.out for i:1 thru 10 do print(i)$ #+end_src - -List as input -#+begin_src maxima :var a=(list 1 2 3) -print(a+1); +* List input + :PROPERTIES: + :ID: b5561c6a-73cd-453a-ba5e-62ad84844de6 + :END: +Simple list as an input +#+begin_src maxima :var a=(list 1 2 3) :results silent :results verbatim +print(a)$ #+end_src -#+begin_src maxima :var a=(list 1 (list 1 2) 3) +#+begin_src maxima :var a=(list 1 (list 1 2) 3) :results silent :results verbatim print(a+1); #+end_src +* Table input + :PROPERTIES: + :ID: 400ee228-6b12-44fd-8097-7986f0f0db43 + :END: #+tblname: test_tbl_col | 1.0 | | 2.0 | @@ -38,26 +52,40 @@ print(a+1); #+tblname: test_tbl_row | 1.0 | 2.0 | -Extra bracket? TODO: -#+begin_src maxima :var s=test_tbl_col +#+begin_src maxima :var s=test_tbl_col :results silent :results verbatim print(s+1.0); #+end_src -#+begin_src maxima :var s=test_tbl_row +#+begin_src maxima :var s=test_tbl_row :results silent :results verbatim print(s+1.0); #+end_src Matrix -#+tblname: test_tbl_mtr +#+tblname: test_tbl_mtr | 1.0 | 1.0 | -| 0.0 | 4.0 | -#+begin_src maxima :var s=test_tbl_mtr +#+begin_src maxima :var s=test_tbl_mtr :results silent :results verbatim ms: apply(matrix, s); -print(ms^^2); +print(ms); #+end_src -#+begin_src maxima :var s=test_tbl_mtr -ms: apply(matrix, s); -print(ms^^2); +* Construct a table from the output + :PROPERTIES: + :ID: cc158527-b867-4b1d-8ae0-b8c713a90fd7 + :END: +#+begin_src maxima :var s=test_tbl_mtr :results silent +m: genmatrix (lambda([i,j], i+j-1), 3, 3)$ +write_data(m, "/dev/stdout")$ #+end_src + +* Latex output +#+begin_src maxima :exports both :results latex :results verbatim +assume(x>0); +tex(ratsimp(diff(%e^(a*x), x))); +#+end_src + +#+results: +#+BEGIN_LaTeX +$$a\,e^{a\,x}$$ +#+END_LaTeX + diff --git a/testing/lisp/test-ob-maxima.el b/testing/lisp/test-ob-maxima.el new file mode 100644 index 0000000..dff4033 --- /dev/null +++ b/testing/lisp/test-ob-maxima.el @@ -0,0 +1,82 @@ +;;; test-ob-maxima.el --- tests for ob-maxima.el + +;; Copyright (c) 2010 Sergey Litvinov +;; Authors: Sergey Litvinov + +;; Released under the GNU General Public License version 3 +;; see: http://www.gnu.org/licenses/gpl-3.0.html + +(org-test-for-executable "maxima") + +(let ((load-path (cons (expand-file-name + ".." (file-name-directory + (or load-file-name buffer-file-name))) + load-path))) + (require 'org-test) + (require 'org-test-ob-consts)) + +(let ((load-path (cons (expand-file-name + "langs" + (expand-file-name + "babel" + (expand-file-name + "contrib" + (expand-file-name + ".." + (expand-file-name + ".." + (file-name-directory + (or load-file-name buffer-file-name))))))) + load-path))) + + (require 'ob-maxima)) + +(ert-deftest ob-maxima/assert () + (should t)) + +(ert-deftest ob-maxima/integer-input () + "Test of integer input" + (org-test-at-id "b5842ed4-8e8b-4b18-a1c9-cef006b6a6c8" + (org-babel-next-src-block) + (should (equal 4 (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/string-input () + "Test of string input" + (org-test-at-id "b5842ed4-8e8b-4b18-a1c9-cef006b6a6c8" + (org-babel-next-src-block 2) + (should (equal "- sin(x)" (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/simple-list-input () + "Test of flat list input" + (org-test-at-id "b5561c6a-73cd-453a-ba5e-62ad84844de6" + (org-babel-next-src-block) + (should (equal "[1, 2, 3] " (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/list-input () + "Test of list input" + (org-test-at-id "b5561c6a-73cd-453a-ba5e-62ad84844de6" + (org-babel-next-src-block 2) + (should (equal "[2, [2, 3], 4] " (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/table-input1 () + "Test of table input" + (org-test-at-id "400ee228-6b12-44fd-8097-7986f0f0db43" + (org-babel-next-src-block) + (should (equal "[[2.0], [3.0]] " (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/table-input2 () + "Test of table input" + (org-test-at-id "400ee228-6b12-44fd-8097-7986f0f0db43" + (org-babel-next-src-block 2) + (should (equal "[[2.0, 3.0]] " (org-babel-execute-src-block))))) + +(ert-deftest ob-maxima/matrix-output () + "Test of table output" + (org-test-at-id "cc158527-b867-4b1d-8ae0-b8c713a90fd7" + (org-babel-next-src-block) + (should (equal '((1 2 3) (2 3 4) (3 4 5)) (org-babel-execute-src-block))))) + +(provide 'test-ob-maxima) + +;;; test-ob-maxima.el ends here + -- 1.7.4.1
Litvinov Sergey <slitvinov@gmail.com> writes:
> Please consider a patch to construct a table from the output of maxima
> code block.
I am interested in trying this patch out but it would appear that it has
been generated relative to a branch other than the master at origin! I
cannot apply the patch as a result. I do seem to recall that you
proposed a set of changes a while back but they obviously have not been
incorporated into the standard branch of org. Is there any reason for
this as I seem to recall that your additions were useful!
In any case, can you please post a patch against the standard version?
Or, maybe easier, simply post ob-maxima.el as the other changes are not
necessary for me to test your patch.
thanks,
eric
--
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.50.1
: using Org-mode version 7.7 (release_7.7.346.g4e3cc)
[-- Attachment #1: Type: text/plain, Size: 444 bytes --] > In any case, can you please post a patch against the standard > version? Or, maybe easier, simply post ob-maxima.el as the other > changes are not necessary for me to test your patch. I though my previous patch was applied. It is still under review due to copyright papers. I am attaching a patch against master branch and ob-maxima.el file. The commit message is ob-maxima.el: add input variables, png graphic output, table as an output [-- Attachment #2: 0001-ob-maxima.el-add-input-variables-graphic-output-to-p.patch --] [-- Type: text/x-patch, Size: 4651 bytes --] From d9cfa76e83c10e86ffacca3da7cbc83702268cc2 Mon Sep 17 00:00:00 2001 From: Litvinov Sergey <slitvinov@gmail.com> Date: Tue, 4 Oct 2011 19:48:14 +0200 Subject: [PATCH] ob-maxima.el: add input variables, graphic output to png file, table as an output --- lisp/ob-maxima.el | 97 ++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 73 insertions(+), 24 deletions(-) diff --git a/lisp/ob-maxima.el b/lisp/ob-maxima.el index 085609c..9c39a5f 100644 --- a/lisp/ob-maxima.el +++ b/lisp/ob-maxima.el @@ -30,47 +30,96 @@ ;; ;; 1) there is no such thing as a "session" in maxima ;; -;; 2) we are generally only going to return output from maxima -;; -;; 3) we are adding the "cmdline" header argument -;; -;; 4) there are no variables +;; 2) we are adding the "cmdline" header argument ;;; Code: (require 'ob) +(defvar org-babel-tangle-lang-exts) +(add-to-list 'org-babel-tangle-lang-exts '("maxima" . "max")) + (defvar org-babel-default-header-args:maxima '()) (defun org-babel-maxima-expand (body params) "Expand a block of Maxima code according to its header arguments." - body) + (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) + (mapconcat 'identity + (list + ;; graphic output + (let ((graphic-file (org-babel-maxima-graphical-output-file params))) + (if graphic-file + (format + "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);" + graphic-file) + "")) + ;; variables + (mapconcat 'org-babel-maxima-var-to-maxima vars "\n") + ;; body + body + "gnuplot_close ()$") + "\n"))) (defun org-babel-execute:maxima (body params) "Execute a block of Maxima entries with org-babel. This function is called by `org-babel-execute-src-block'." (message "executing Maxima source code block") - (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) - (cmdline (cdr (assoc :cmdline params))) - (in-file (org-babel-temp-file "maxima-")) - (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s" - in-file cmdline))) - (with-temp-file in-file (insert body)) - (message cmd) - ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' " - (mapconcat - #'identity - (delq nil - (mapcar (lambda (line) - (unless (or (string-match "batch" line) - (string-match "^rat: replaced .*$" line) - (= 0 (length line))) - line)) - (split-string raw "[\r\n]"))) "\n")) - (org-babel-eval cmd "")))) + (let ((result + (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) + (cmdline (cdr (assoc :cmdline params))) + (in-file (org-babel-temp-file "maxima-" ".max")) + (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s" + in-file cmdline))) + (with-temp-file in-file (insert (org-babel-maxima-expand body params))) + (message cmd) + ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' " + (mapconcat + #'identity + (delq nil + (mapcar (lambda (line) + (unless (or (string-match "batch" line) + (string-match "^rat: replaced .*$" line) + (= 0 (length line))) + line)) + (split-string raw "[\r\n]"))) "\n")) + (org-babel-eval cmd ""))))) + (if (org-babel-maxima-graphical-output-file params) + nil + (if (or (member "scalar" result-params) + (member "verbatim" result-params) + (member "output" result-params)) + result + (let ((tmp-file (org-babel-temp-file "maxima-res-"))) + (with-temp-file tmp-file (insert result)) + (org-babel-import-elisp-from-file tmp-file)))))) + (defun org-babel-prep-session:maxima (session params) (error "Maxima does not support sessions")) +(defun org-babel-maxima-var-to-maxima (pair) + "Convert an elisp val into a string of maxima code specifying a var +of the same value." + (let ((var (car pair)) + (val (cdr pair))) + (when (symbolp val) + (setq val (symbol-name val)) + (when (= (length val) 1) + (setq val (string-to-char val)))) + (format "%S: %s$" var + (org-babel-maxima-elisp-to-maxima val)))) + +(defun org-babel-maxima-graphical-output-file (params) + "Name of file to which maxima should send graphical output." + (and (member "graphics" (cdr (assq :result-params params))) + (cdr (assq :file params)))) + +(defun org-babel-maxima-elisp-to-maxima (val) + "Return a string of maxima code which evaluates to VAL." + (if (listp val) + (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]") + (format "%s" val))) + + (provide 'ob-maxima) -- 1.7.4.1 [-- Attachment #3: ob-maxima.el --] [-- Type: text/x-emacs-lisp, Size: 4242 bytes --] ;;; ob-maxima.el --- org-babel functions for maxima evaluation ;; Copyright (C) 2009-2011 Free Software Foundation, Inc. ;; Author: Eric S Fraga ;; Eric Schulte ;; Keywords: literate programming, reproducible research, maxima ;; Homepage: http://orgmode.org ;; This file is part of GNU Emacs. ;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. ;;; Commentary: ;; Org-Babel support for evaluating maxima entries. ;; ;; This differs from most standard languages in that ;; ;; 1) there is no such thing as a "session" in maxima ;; ;; 2) we are adding the "cmdline" header argument ;;; Code: (require 'ob) (defvar org-babel-tangle-lang-exts) (add-to-list 'org-babel-tangle-lang-exts '("maxima" . "max")) (defvar org-babel-default-header-args:maxima '()) (defun org-babel-maxima-expand (body params) "Expand a block of Maxima code according to its header arguments." (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) (mapconcat 'identity (list ;; graphic output (let ((graphic-file (org-babel-maxima-graphical-output-file params))) (if graphic-file (format "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);" graphic-file) "")) ;; variables (mapconcat 'org-babel-maxima-var-to-maxima vars "\n") ;; body body "gnuplot_close ()$") "\n"))) (defun org-babel-execute:maxima (body params) "Execute a block of Maxima entries with org-babel. This function is called by `org-babel-execute-src-block'." (message "executing Maxima source code block") (let ((result (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) (cmdline (cdr (assoc :cmdline params))) (in-file (org-babel-temp-file "maxima-" ".max")) (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s" in-file cmdline))) (with-temp-file in-file (insert (org-babel-maxima-expand body params))) (message cmd) ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' " (mapconcat #'identity (delq nil (mapcar (lambda (line) (unless (or (string-match "batch" line) (string-match "^rat: replaced .*$" line) (= 0 (length line))) line)) (split-string raw "[\r\n]"))) "\n")) (org-babel-eval cmd ""))))) (if (org-babel-maxima-graphical-output-file params) nil (if (or (member "scalar" result-params) (member "verbatim" result-params) (member "output" result-params)) result (let ((tmp-file (org-babel-temp-file "maxima-res-"))) (with-temp-file tmp-file (insert result)) (org-babel-import-elisp-from-file tmp-file)))))) (defun org-babel-prep-session:maxima (session params) (error "Maxima does not support sessions")) (defun org-babel-maxima-var-to-maxima (pair) "Convert an elisp val into a string of maxima code specifying a var of the same value." (let ((var (car pair)) (val (cdr pair))) (when (symbolp val) (setq val (symbol-name val)) (when (= (length val) 1) (setq val (string-to-char val)))) (format "%S: %s$" var (org-babel-maxima-elisp-to-maxima val)))) (defun org-babel-maxima-graphical-output-file (params) "Name of file to which maxima should send graphical output." (and (member "graphics" (cdr (assq :result-params params))) (cdr (assq :file params)))) (defun org-babel-maxima-elisp-to-maxima (val) "Return a string of maxima code which evaluates to VAL." (if (listp val) (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]") (format "%s" val))) (provide 'ob-maxima) ;;; ob-maxima.el ends here
Sergey Litvinov <slitvinov@gmail.com> writes: >> In any case, can you please post a patch against the standard >> version? Or, maybe easier, simply post ob-maxima.el as the other >> changes are not necessary for me to test your patch. > > I though my previous patch was applied. It is still under review due to > copyright papers. I am attaching a patch against master branch and > ob-maxima.el file. > > The commit message is > ob-maxima.el: add input variables, png graphic > output, table as an output > Yes, there are a number of very nice patches from Sergey which are still in holding pending completion of the FSF copyright papers. See [1] for a list of pending patches -- the relevant ones are tagged "under review". Best -- Eric > > Footnotes: [1] http://patchwork.newartisans.com/project/org-mode/list/ -- Eric Schulte http://cs.unm.edu/~eschulte/
Sergey Litvinov <slitvinov@gmail.com> writes:
>> In any case, can you please post a patch against the standard
>> version? Or, maybe easier, simply post ob-maxima.el as the other
>> changes are not necessary for me to test your patch.
>
> I though my previous patch was applied. It is still under review due to
> copyright papers. I am attaching a patch against master branch and
> ob-maxima.el file.
Seems fine from minimal testing on my part. Nice extra functionality!
I hope the FSF papers come through soon as I would like to see this in
the master branch.
--
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.90.1
: using Org-mode version 7.7 (release_7.7.377.gbbf83.dirty)