Ask Your Question
1

Converting Octave vector to Sage list

asked 2013-05-27 22:32:02 +0100

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 22:50:05 +0100 )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-28 00:06:51 +0100 )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 09:06:05 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2015-09-19 00:10:15 +0100 )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 21:55:27 +0100 )edit

3 Answers

Sort by » oldest newest most voted
1

answered 2013-05-28 12:10:48 +0100

kcrisman gravatar image

updated 2013-05-28 12:11:04 +0100

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 2015-09-24 22:02:06 +0100

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 03:20:27 +0100

vdelecroix gravatar image

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 22:32:02 +0100

Seen: 1,352 times

Last updated: Sep 24 '15