ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 17 Feb 2011 20:52:36 +0100Converting from Mathematica outputhttps://ask.sagemath.org/question/7942/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](http://www.sagemath.org/doc/reference/sage/interfaces/mathematica.html) ).
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}
Mon, 14 Feb 2011 17:50:44 +0100https://ask.sagemath.org/question/7942/converting-from-mathematica-output/Answer by Felix Lawrence for <p>Hi!</p>
<p>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:<a href="http://www.sagemath.org/doc/reference/sage/interfaces/mathematica.html">here</a> ).</p>
<p>Thank you!</p>
<p>Anne</p>
<p>::</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?answer=12122#post-id-12122There is a [patch](http://trac.sagemath.org/sage_trac/ticket/8495) available that fixes this behaviour. If you want to apply the patch to get this working on your version of Sage, [instructions are available](http://sagemath.org/doc/developer/walk_through.html#reviewing-a-patch).
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.Thu, 17 Feb 2011 18:50:45 +0100https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?answer=12122#post-id-12122Comment by kcrisman for <p>There is a <a href="http://trac.sagemath.org/sage_trac/ticket/8495">patch</a> available that fixes this behaviour. If you want to apply the patch to get this working on your version of Sage, <a href="http://sagemath.org/doc/developer/walk_through.html#reviewing-a-patch">instructions are available</a>.</p>
<p>Someone (other than me) needs to review the patch before it can be included in Sage by default.</p>
<p>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 <code>sage_eval(mlist._sage_repr())</code>:</p>
<pre><code>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'>
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?comment=22092#post-id-22092I 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!Thu, 17 Feb 2011 20:52:36 +0100https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?comment=22092#post-id-22092Answer by kcrisman for <p>Hi!</p>
<p>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:<a href="http://www.sagemath.org/doc/reference/sage/interfaces/mathematica.html">here</a> ).</p>
<p>Thank you!</p>
<p>Anne</p>
<p>::</p>
<pre><code>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}
</code></pre>
https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?answer=12103#post-id-12103Well, 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.Mon, 14 Feb 2011 21:04:40 +0100https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?answer=12103#post-id-12103Comment by Felix Lawrence for <p>Well, this was implemented by Wilfried Huss in order to improve our handling of sums. But, as the code for <code>._sage_()</code> says:</p>
<blockquote>
<p>This currently does not implement a
parser for the Mathematica output
language, therefore only very simple
expressions will convert successfully.</p>
</blockquote>
<p>The current thing just <code>result = str(result).lower().replace('[', '(').replace(']', ')')</code> parses the output and tries to 'Sageify' things like <code>Sin[x]</code> to <code>sin(x)</code>. </p>
<p>So it sounds like a case of "implement it"! I'm sorry there isn't a better answer. </p>
<p>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 <code>str(result).replace('{','[')</code> or something like that. Does that seem plausible? I know very little about Mathematica's type systems.</p>
https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?comment=22093#post-id-22093str() 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.pyThu, 17 Feb 2011 18:52:37 +0100https://ask.sagemath.org/question/7942/converting-from-mathematica-output/?comment=22093#post-id-22093