Ask Your Question
0

Math structures converters

asked 2012-07-22 04:51:01 +0100

fero gravatar image

updated 2012-07-26 00:14:45 +0100

kcrisman gravatar image

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

Comments

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).

kcrisman gravatar imagekcrisman ( 2012-07-23 10:59:12 +0100 )edit

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

fero gravatar imagefero ( 2012-07-25 09:09:25 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-07-25 13:10:58 +0100

kcrisman gravatar image

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.

edit flag offensive delete link more

Comments

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'"

fero gravatar imagefero ( 2012-08-03 03:23:09 +0100 )edit

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))

fero gravatar imagefero ( 2012-08-03 12:00:35 +0100 )edit
1

answered 2012-08-03 12:03:25 +0100

fero gravatar image

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 
mat = scipy.io.loadmat('test.mat') 
print mat['test_struct'] 
M =matrix(mat['test_struct']) 
matlab("%s'" % 
matlab.sage2matlab_matrix_string(M))
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

Stats

Asked: 2012-07-22 04:51:01 +0100

Seen: 681 times

Last updated: Aug 03 '12