Ask Your Question
1

Converting Octave vector to Sage list

asked 2013-05-27 15:32:02 -0600

jaia gravatar image

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 flag offensive close merge delete

Comments

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

kcrisman gravatar imagekcrisman ( 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.

jaia gravatar imagejaia ( 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]
FrédéricC gravatar imageFrédéricC ( 2015-09-18 02:06:05 -0600 )edit

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

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

But this works

sage: matrix(ZZ,O)
[1 2 3]
[5 6 7]
[3 2 1]
FrédéricC gravatar imageFrédéricC ( 2015-09-24 14:55:27 -0600 )edit

3 answers

Sort by » oldest newest most voted
1

answered 2015-09-24 15:02:06 -0600

FrédéricC gravatar image

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]
edit flag offensive delete link more
1

answered 2013-05-28 05:10:48 -0600

kcrisman gravatar image

updated 2013-05-28 05:11:04 -0600

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.

edit flag offensive delete link more
1

answered 2013-05-27 20:20:27 -0600

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-05-27 15:32:02 -0600

Seen: 428 times

Last updated: Sep 24 '15