Ask Your Question
1

How do I save a rational matrix for Mathematica and Matlab to read?

asked 2019-01-22 22:19:21 -0600

raykan gravatar image

Hi,

I create a rational matrix (rather large, say 1000x1000), and I want to save it in a file that can be read by Mathematica or MATLAB (with symbolic toolbox). Is there a simple way to do that?

Right now, I use

o = open('file.txt','w')

o.write(str(A))

o.close()

where A is the matrix. However, it has these "[" and "]" at the beginning of line that makes it difficult for Mathetmatica and MATLAB to read. Any suggestions would be appreciated.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2019-01-23 01:57:58 -0600

Sébastien gravatar image

updated 2019-01-23 06:29:25 -0600

You want to use the method _mathematica_init_(). For matlab, I haven't seen _matlab_init_() method but there is a _octave_init_() method and according to Octave website "The Octave syntax is largely compatible with Matlab" :

sage: A = sin(x)
sage: A._mathematica_init_()
'Sin[x]'
sage: A._octave_init_()
'sin(x)'

sage: B = matrix(3, range(9))
sage: B._mathematica_init_()
'{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}'
sage: B._octave_init_()
'[0 1 2]\n[3 4 5]\n[6 7 8]'

You have a bunch of those methods:

sage: [method for method in dir(A) if method.endswith('_init_')]
['_axiom_init_',
 '_fricas_init_',
 '_gap_init_',
 '_giac_init_',
 '_gp_init_',
 '_interface_init_',
 '_kash_init_',
 '_macaulay2_init_',
 '_magma_init_',
 '_maple_init_',
 '_mathematica_init_',
 '_maxima_init_',
 '_maxima_lib_init_',
 '_octave_init_',
 '_pari_init_',
 '_polymake_init_',
 '_r_init_',
 '_singular_init_']

If you have mathematica installed, you may use it directly from sage by doing things like below. For me it does not work, because I do not have mathematica installed:

sage: Bm = mathematica(B)
...
TypeError: unable to start mathematica: End Of File (EOF). Exception style platform.
sage: Bm.Inverse() # or something like this

Conversely, with sage 8.6 because of ticket #25501 you may do:

┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 8.6, Release Date: 2019-01-15                     │
│ Using Python 2.7.15. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: from sage.symbolic.integration.external import symbolic_expression_from_mathematica_string
sage: symbolic_expression_from_mathematica_string('Sin[x]')
sin(x)
sage: symbolic_expression_from_mathematica_string('{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}')
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
edit flag offensive delete link more

Comments

If you initialize a matrix in Sage with sparse=True then _mathematica_init_() and _octave_init_() still return a dense representation. Not very practical for large matrices.

rburing gravatar imagerburing ( 2019-01-23 11:29:14 -0600 )edit
0

answered 2019-01-24 14:46:38 -0600

raykan gravatar image

updated 2019-01-26 18:06:48 -0600

Thanks so much. Just to add this from my experience. In Sage, I do this (say A is the rational matrix):

o = open('file.txt','w'); o.write(str(A._mathematica_init())); o.close();

Then in Mathematica, I do this

A = ReadList("file.txt")[[1]]

I need the [[1]] to make it as a matrix. Otherwise, it is a 3-dimensional matrix.

For MATLAB, octave_init does not work. Any suggestions would be appreciated.

I also never get Mathematica interface working in Sage (8.5 Windows 64bit version) :(

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: 2019-01-22 22:01:12 -0600

Seen: 177 times

Last updated: Jan 26