Ask Your Question

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

asked 2019-01-23 05:19:21 +0100

raykan gravatar image


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



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

answered 2019-01-23 08:57:58 +0100

Sébastien gravatar image

updated 2019-01-23 13:29:25 +0100

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_()
sage: A._octave_init_()

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_')]

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


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 18:29:14 +0100 )edit

answered 2019-01-24 21:46:38 +0100

raykan gravatar image

updated 2019-01-27 01:06:48 +0100

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


Asked: 2019-01-23 05:01:12 +0100

Seen: 833 times

Last updated: Jan 27 '19