# Converting Octave vector to Sage list

I have a Sage notebook that combines Sage and Octave code. How can I make the output of the Octave code (a vector) available to Sage as a list or vector?

edit retag close merge delete

Does [the Sage-Octave interface page](http://www.sagemath.org/doc/reference/interfaces/sage/interfaces/octave.html) help?

( 2013-05-27 15:50:05 -0600 )edit

No, it doesn't. It describes a function for going from a Sage list to an Octave matrix, but I need to go the other way.

( 2013-05-27 17:06:51 -0600 )edit

For square matrices, there is the following bad behaviour:

sage: O = octave('[1, 2, 3; 5, 6, 7; 3,2,1]')
sage: matrix(ZZ,3,3,O)
[123567321         0         0]
[        0 123567321         0]
[        0         0 123567321]

( 2015-09-18 02:06:05 -0600 )edit

Yikes! Please open a ticket. Probably that isn't supported, but then it should be checked...

( 2015-09-18 17:10:15 -0600 )edit

But this works

sage: matrix(ZZ,O)
[1 2 3]
[5 6 7]
[3 2 1]

( 2015-09-24 14:55:27 -0600 )edit

Sort by » oldest newest most voted

So one can do that at least:

sage: octave = Octave()
sage: w = octave([1, 2, 3])
sage: Matrix(ZZ, w)
[1 2 3]
sage: w = octave([pi, 5.7, 3])
sage: Matrix(RR, w)
[3.14159000000000 5.70000000000000 3.00000000000000]

more

For completeness:

In general, you should be able to access variables from other programs using the methods in this question. Unfortunately, as vdelecroix points out, Sage's reverse interface to Octave is too rudimentary.

sage: O = octave([1,2,3])
sage: O._sage_()
Traceback (click to the left of this block for traceback)
...
NotImplementedError: Unable to parse output:    1 2 3


But maybe someone will write a better parser for various Octave objects... and this will work for very simple things like integers.

more

Hi,

I did not find anything in the documentation. But you can at least parse the ouptut of octave as a string:

sage: v = octave('[1,5,7]')
sage: str(v)
'\n\n 1 5 7\n\n'
sage: vector(map(Integer,str(v).split()))
(1, 5, 7)


The function map juste evaluate the function Integer on each element of str(v).split() which is the list ['1','5','7']. The method split of strings is very useful: it splits a string at each space character.

more