# Math structures converters

Hi all,

I am a newbie here, and I am doing some experiments with SAGE. I am trying to tie up a kind of computing framework that could import data from some kind of sources, invoke generic algorhitms, and render output data.

SAGE and SAGENB are great works, thanks for developing them!

I see that SAGE can operate on many backends, but I can't figure out if it can convert data from one backend to another.

More explanation:

I have to take care mainly of Matlab syntax in this stage, but I'd like to save data in Python or a Scipy structure after import, and then perform conversion of data basing on the computing or rendering algorithm the user choose. Is it feasible with any library you developed or you know?

The "b plan" could be to use the Octave syntax which is 100% compatible. Am I right?

Respect to this question there are also performance,data size grow issues and maybe other to consider.

I hope this is the right place to post this question.

Thank you in advance Luca

edit retag close merge delete

This question is pretty vague and/or all-encompassing. Can you give a more explicit workflow of how you want to take data/algorithms around? You can save anything in Sage as Python, but certainly not all Matlab syntax would be "translatable" without human intervention into Sage; a lot of Sage stuff would translate back into Matlab, but probably not all of that, either (again, without human intervention).

let's say a simple example like: a = [ 1.1, 2.000002 ; 3, 4] , is there a way to say: sage_to_matlab(a) ? Of course I have already done this by hand with the help of str(), and " ".join() but what about number precision? and what about if I have a structure? like the one presented in Matlab "help save": s1.a = 12.7; s1.b = {'abc', [4 5; 6 7]}; s1.c = 'Hello!'; ? TIA

Sort by » oldest newest most voted

If you look at the source for the Matlab interface, you can see that it's still pretty rudimentary. In particular,

sage: M = matrix([[1.1,2.0000002],[3,4]])
sage: M._ma<tab>


doesn't reveal anything to convert it to a Matlab format. It turns out you can do

sage: matlab.sage2matlab_matrix_string(M)
'[1.10000000000000, 2.00000020000000; 3.00000000000000, 4.00000000000000]'


but I don't know if that's what you want.

It looks like this interface could use a lot of work. Perhaps you might contribute? Also, if you really just need an open source alternative, Octave is very good. Interestingly, there is

sage: M._octave_()


though of course one needs Octave installed and in the PATH to use it.

On a tangential note, I've opened Ticket 13291 to do some minor improvement to our internal testing stuff that I found while looking at this.

more

it is wonderful thank you. The thing that I still do not understand is how to revert the result to sage. In example I have a "data" variable in a data.mat file and I can do: matlab.eval("load('/tmp/data.mat')") ; x = matlab("data") ; but then I can't convert it to sage variable. I wrote x.sage(), but I get a "NotImplementedError: Unable to parse output: ??? Undefined function or variable 'sage10'"

thx to logix in the irc channel I found a workaround to do this: import scipy.io mat = scipy.io.loadmat('/tmp/test.mat') print mat['test_struct'] M =matrix(mat['test_struct']) matlab("%s'" % matlab.sage2matlab_matrix_string(M))

thx to logix in the irc channel I found a workaround to complete the circle, saved a struct to 'test.mat' file and then reload through scipy:

import scipy.io
print mat['test_struct']
M =matrix(mat['test_struct'])
matlab("%s'" %
matlab.sage2matlab_matrix_string(M))

more