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?
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?
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.
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]
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.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2013-05-27 22:32:02 +0100
Seen: 1,352 times
Last updated: Sep 24 '15
Sage Notebook: how to set the path for Octave?
What's the vector equivalent to Pythons list addition? `[1,2]+[3,4]=[1,2,3,4]`
Can I define a function from a list of values?
Running octave from the sage notebook
How do I change the default latex output of a vector?
Does [the Sage-Octave interface page](http://www.sagemath.org/doc/reference/interfaces/sage/interfaces/octave.html) help?
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.
For square matrices, there is the following bad behaviour:
Yikes! Please open a ticket. Probably that isn't supported, but then it should be checked...
But this works