0

I interact with the Gnu Octave's inferior mode and everything seems to be working fine there:

octave> pkg load symbolic
octave> syms x
g = (x+1)/sqrt(x^2 - 1)
simplify(diff(g,x))
octave> g = (sym)

     x + 1   
  ───────────
     ________
    ╱  2     
  ╲╱  x  - 1 

octave> ans = (sym)

          -1         
  ───────────────────
             ________
            ╱  2     
  (x - 1)⋅╲╱  x  - 1 

Now I do the same using org mode and babel where I get an error:

#+begin_src octave :session :eval never-export :results verbatim
pkg load symbolic
syms x
g = (x+1)/sqrt(x^2 - 1)
ans = simplify(diff(g,x))
#+end_src

And when I evaluate the above code, I see the following error in the inferior mode's octave session:

octave> > > error: fprintf: wrong type argument 'class'
error: called from
    dlmwrite at line 195 column 7

Any idea on what I'm doing wrong ?

NickD
  • 27,023
  • 3
  • 23
  • 42
Sibi
  • 3,603
  • 2
  • 22
  • 35

2 Answers2

1

The Error

octave> > > error: fprintf: wrong type argument 'class'
error: called from
    dlmwrite at line 195 column 7

This is because you assigned a value of type sym to the variable ans. fprintf(), which is a part of wrapping code while Org Babel is evaluating it, is trying to write the given ans but because the value of ans isn't class char and it complains about it. See org-babel-octave-wrapper-method in ob-octave.el for the wrapping code.

The Org Manual 9.4 (info "(org) Results of Evaluation") says:

Collection

Collection options specify the results. Choose one of the options; they are mutually exclusive.

value

Default for most Babel libraries(1). Functional mode. Org gets the value by wrapping the code in a function definition in the language of the source block. That is why when using :results value, code should execute like a function and return a value. For languages like Python, an explicit return statement is mandatory when using :results value. Result is the value returned by the last statement in the code block.

When evaluating the code block in a session (see *note Environment of a Code Block::), Org passes the code to an interpreter running as an interactive Emacs inferior process. Org gets the value from the source code interpreter’s last statement output. Org has to use language-specific methods to obtain the value. For example, from the variable _ in Ruby, and the value of .Last.value in R.

output

Scripting mode. Org passes the code to an external process running the interpreter. Org returns the contents of the standard output stream as text results.

When using a session, Org passes the code to the interpreter running as an interactive Emacs inferior process. Org concatenates any text output from the interpreter and returns the collection as a result.

For Octave, with value, you either

  • a) assign the value you want to return to Org buffer to the variable ans, or
  • b) evaluate without assigning it to any variable so that the interpreter assign your value to the automatic variable ans.

Unfortunately, the current ob-octave doesn't parse class sym well. This leaves you to use output instead.

With output, you just get everything back from the interpreter.

Getting a Result

Thus, with the following code block:

#+begin_src octave :session :results output
  pkg load symbolic
  syms x
  g = (x+1)/sqrt(x^2 - 1)
  simplify(diff(g,x))
#+end_src

You get this

#+RESULTS:
#+begin_example

octave> g = (sym)

     x + 1   
  ───────────
     ________
    ╱  2     
  ╲╱  x  - 1
ans = (sym)

          -1         
  ───────────────────
             ________
            ╱  2     
  (x - 1)⋅╲╱  x  - 1
#+end_example

Result Types

Unfortunately, the current Babel Octave doesn't support result types. Thus, verbatim, or any other type, doesn't work at all.

Remarks

These might change in the future if someone fixed them.

This result was produced with

(org-version nil t)
"Org mode version 9.4.6 (release_9.4.6-628-g366444 @ /home/yashi/src/org-mode/lisp/)"
Yasushi Shoji
  • 2,151
  • 15
  • 35
0

So I have found an workaround by playing around with different header arguments. This works for me:

#+begin_src octave :session :eval never-export :results value verbatim output replace
pkg load symbolic
syms x
g = (x+1)/sqrt(x^2 - 1)
ans = simplify(diff(g,x))
#+end_src

#+RESULTS:
#+begin_example

octave> g = (sym)

     x + 1   
  ───────────
     ________
    ╱  2     
  ╲╱  x  - 1
ans = (sym)

          -1         
  ───────────────────
             ________
            ╱  2     
  (x - 1)⋅╲╱  x  - 1
#+end_example
Sibi
  • 3,603
  • 2
  • 22
  • 35
  • `:results value ... output ...` is (or should be) the same as `:results ... output ...` - the manual says `value` and `output` are mutually exclusive, but practically the last one wins. – NickD Jan 30 '21 at 12:42