# Converting from Mathematica output

Hi!

I am trying to run a Mathematica program from within Sage and would like to convert the output of the program to Sage objects, so that I can keep manipulating them. The output are typically lists. However, I am having trouble with the conversion; see the Sage session below. Does someone know what is wrong or how to do it correctly (my reference is link:here ).

Thank you!

Anne

::

sage: m = mathematica
sage: slist = [[1,2,3],1]
sage: mlist = m(slist)
sage: mlist
{{1, 2, 3}, 1}
sage: type(mlist)
<class 'sage.interfaces.mathematica.MathematicaElement'>
sage: mlist.sage()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)

/Users/anne/Documents/Mathematica/<ipython console> in <module>()

/Applications/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc in sage(self)
1732             Rational Field
1733         """
-> 1734         return self._sage_()
1735
1736     def __repr__(self):

/Applications/sage/local/lib/python2.6/site-packages/sage/interfaces  /mathematica.pyc in _sage_(self)
581             return SR(result)
582         except TypeError:
--> 583             raise NotImplementedError, "Unable to parse Mathematica     output: %s" % result
584
585     def __str__(self):

NotImplementedError: Unable to parse Mathematica output:                        {{1, 2, 3}, 1}

edit retag close merge delete

Sort by Â» oldest newest most voted

Well, this was implemented by Wilfried Huss in order to improve our handling of sums. But, as the code for ._sage_() says:

This currently does not implement a parser for the Mathematica output language, therefore only very simple expressions will convert successfully.

The current thing just result = str(result).lower().replace('[', '(').replace(']', ')') parses the output and tries to 'Sageify' things like Sin[x] to sin(x).

So it sounds like a case of "implement it"! I'm sorry there isn't a better answer.

On the plus side, parsing lists like this probably isn't too bad, compared to other output types. If the thing you get doesn't live in SR, then one could probably very easily just do str(result).replace('{','[') or something like that. Does that seem plausible? I know very little about Mathematica's type systems.

more

str() gives funny results for mathematica objects (IIRC it gives you ascii art, where x^2 is represented by a 2 on one line and an x on the line below it). Better to use repr() for this purpose. And the .replace()ing can be done by a generic function ._sage_repr(), defined in sage/interfaces/expect.py

( 2011-02-17 18:52:37 +0200 )edit

There is a patch available that fixes this behaviour. If you want to apply the patch to get this working on your version of Sage, instructions are available.

Someone (other than me) needs to review the patch before it can be included in Sage by default.

In the meantime, if you don't want to go to the trouble of applying the patch, a quick and dirty solution would be to use sage_eval(mlist._sage_repr()):

sage: mlist = mathematica([[1,2,3],1])
sage: mlist
{{1, 2, 3}, 1}
sage: mlist._sage_repr()
'[[1, 2, 3], 1]'
sage: slist2 = sage_eval(mlist._sage_repr())
sage: slist2
[[1, 2, 3], 1]
sage: type(slist2)
<type 'list'>
sage: type(slist2[1])
<type 'sage.rings.integer.Integer'>


This approach will not work for symbolics - if you need to import things with symbolic variables, then it gets a little more complicated and you'd be better off using the patch linked above.

more

I didn't realize that patch was the one responsible for this problem - I knew of the ticket. I was partly responsible for merging the previous one, so I apologize. I don't have Mma, so I can't review this, but looks like a great idea!

( 2011-02-17 20:52:36 +0200 )edit