Sage: Ticket #16703: Fix Mathematica 10 interface
https://trac.sagemath.org/ticket/16703
<p>
The interface with Mathematica has been broken since Mathematica 9 came out.
The problem is that the program <code>math</code> does not behave nicely with expect.
This fix uses a wrapper, as suggested in <a class="ext-link" href="http://code.activestate.com/recipes/363500-mathematica-with-a-gnu-readline-input/"><span class="icon"></span>http://code.activestate.com/recipes/363500-mathematica-with-a-gnu-readline-input/</a>, to make <code>math</code> readline-aware.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/16703
Trac 1.1.6etn40ffTue, 22 Jul 2014 20:19:10 GMTstatus changed; commit, branch set
https://trac.sagemath.org/ticket/16703#comment:1
https://trac.sagemath.org/ticket/16703#comment:1
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>commit</strong>
set to <em>a15573f863f1133cecafe46e14282d3835ef072a</em>
</li>
<li><strong>branch</strong>
set to <em>u/etn40ff/mathematica_10</em>
</li>
</ul>
<p>
New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="http://git.sagemath.org/sage.git/commit/?id=a15573f863f1133cecafe46e14282d3835ef072a"><span class="icon"></span>a15573f</a></td><td><code>Fix Mathematica interface by passing through readline</code>
</td></tr></table>
TicketgitTue, 22 Jul 2014 20:21:18 GMTcommit changed
https://trac.sagemath.org/ticket/16703#comment:2
https://trac.sagemath.org/ticket/16703#comment:2
<ul>
<li><strong>commit</strong>
changed from <em>a15573f863f1133cecafe46e14282d3835ef072a</em> to <em>8ac00fceb5cfd764bb0fdbe41bbb269576c06822</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="http://git.sagemath.org/sage.git/commit/?id=8ac00fceb5cfd764bb0fdbe41bbb269576c06822"><span class="icon"></span>8ac00fc</a></td><td><code>Fix Mathematica interface by passing through readline</code>
</td></tr></table>
TicketgitTue, 22 Jul 2014 20:28:34 GMTcommit changed
https://trac.sagemath.org/ticket/16703#comment:3
https://trac.sagemath.org/ticket/16703#comment:3
<ul>
<li><strong>commit</strong>
changed from <em>8ac00fceb5cfd764bb0fdbe41bbb269576c06822</em> to <em>3fe1373cec8e956430521b17ce3996a44283aaba</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="http://git.sagemath.org/sage.git/commit/?id=3fe1373cec8e956430521b17ce3996a44283aaba"><span class="icon"></span>3fe1373</a></td><td><code>Fix Mathematica interface by passing through readline</code>
</td></tr></table>
TicketgitWed, 23 Jul 2014 19:45:39 GMTcommit changed
https://trac.sagemath.org/ticket/16703#comment:4
https://trac.sagemath.org/ticket/16703#comment:4
<ul>
<li><strong>commit</strong>
changed from <em>3fe1373cec8e956430521b17ce3996a44283aaba</em> to <em>43d3ff01c97b841e54f9fff604edd519b711dd04</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="http://git.sagemath.org/sage.git/commit/?id=43d3ff01c97b841e54f9fff604edd519b711dd04"><span class="icon"></span>43d3ff0</a></td><td><code>Removed duplicate code</code>
</td></tr></table>
TicketgitWed, 23 Jul 2014 21:20:18 GMTcommit changed
https://trac.sagemath.org/ticket/16703#comment:5
https://trac.sagemath.org/ticket/16703#comment:5
<ul>
<li><strong>commit</strong>
changed from <em>43d3ff01c97b841e54f9fff604edd519b711dd04</em> to <em>c07475f78e4434cfd4424b3c09a502e3db22b323</em>
</li>
</ul>
<p>
Branch pushed to git repo; I updated commit sha1. New commits:
</p>
<table class="wiki">
<tr><td><a class="ext-link" href="http://git.sagemath.org/sage.git/commit/?id=c07475f78e4434cfd4424b3c09a502e3db22b323"><span class="icon"></span>c07475f</a></td><td><code>Removed non needed argument from raw_input()</code>
</td></tr></table>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/16703#comment:6
https://trac.sagemath.org/ticket/16703#comment:6
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketjakobkroekerFri, 30 Jan 2015 17:36:55 GMTcc set
https://trac.sagemath.org/ticket/16703#comment:7
https://trac.sagemath.org/ticket/16703#comment:7
<ul>
<li><strong>cc</strong>
<em>jakobkroeker</em> added
</li>
</ul>
TicketdrkirkbySat, 31 Jan 2015 10:51:15 GMT
https://trac.sagemath.org/ticket/16703#comment:8
https://trac.sagemath.org/ticket/16703#comment:8
<p>
It seems to me that until the interface is designed properly, it will always have these issues. What is probably the most sensible approach is to use the C interface to Mathematica's published API, called <a class="missing wiki">MathLink?</a>
</p>
<p>
<a class="ext-link" href="http://www.wolfram.com/learningcenter/tutorialcollection/MathLinkDevelopmentInC/"><span class="icon"></span>http://www.wolfram.com/learningcenter/tutorialcollection/MathLinkDevelopmentInC/</a>
</p>
<p>
like "jmath" uses
</p>
<p>
<a class="ext-link" href="http://robotics.caltech.edu/~radford/jmath/"><span class="icon"></span>http://robotics.caltech.edu/~radford/jmath/</a>
</p>
<p>
See the
</p>
<p>
The total source code for that is under 1000 lines, and makes use of functions like MLInitialize, MLActivate etc.
</p>
<p>
Sure it will take a bit more time to get it right, but it seems to me a better long-term approach.
</p>
<p>
Dave
</p>
Ticketetn40ffSat, 31 Jan 2015 15:01:57 GMT
https://trac.sagemath.org/ticket/16703#comment:9
https://trac.sagemath.org/ticket/16703#comment:9
<p>
I agree that this is not a good solution: at the time I needed something quick and this was the fastest way I could get it to work. In the long term using <a class="missing wiki">MathLink?</a> is a better idea but I am not sure there is interest enough in this feature to justify the effort.
</p>
<p>
S.
</p>
TicketdrkirkbySat, 31 Jan 2015 21:03:04 GMT
https://trac.sagemath.org/ticket/16703#comment:10
https://trac.sagemath.org/ticket/16703#comment:10
<p>
Robert Jacobson indicated he wanted to do this last month
</p>
<p>
<a class="ext-link" href="https://groups.google.com/forum/#!searchin/sage-devel/Mathematica$20interface|sort:relevance/sage-devel/N4K6lUkUwe0/pKlGj4AfdDcJ"><span class="icon"></span>https://groups.google.com/forum/#!searchin/sage-devel/Mathematica$20interface|sort:relevance/sage-devel/N4K6lUkUwe0/pKlGj4AfdDcJ</a>
</p>
<p>
I was the only one to make any comments. Perhaps he might want to complete what he started.
</p>
<p>
Dave
</p>
TicketdunfieldMon, 02 Feb 2015 17:47:31 GMTstatus changed; reviewer set
https://trac.sagemath.org/ticket/16703#comment:11
https://trac.sagemath.org/ticket/16703#comment:11
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>Nathan Dunfield</em>
</li>
</ul>
<p>
I tried out this patch with Mathematica 8, 9, and 10. It worked fine with all of them, so moving to positive review. A more robust interface would of course be better, but at least this will mean that the Mathematica interface isn't broken "out of the box" as it has been for several years now...
</p>
TicketvbraunTue, 17 Feb 2015 20:50:31 GMTstatus, branch changed; resolution set
https://trac.sagemath.org/ticket/16703#comment:12
https://trac.sagemath.org/ticket/16703#comment:12
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>branch</strong>
changed from <em>u/etn40ff/mathematica_10</em> to <em>c07475f78e4434cfd4424b3c09a502e3db22b323</em>
</li>
</ul>
TicketslabbeMon, 13 Jul 2015 12:41:17 GMTcommit deleted
https://trac.sagemath.org/ticket/16703#comment:13
https://trac.sagemath.org/ticket/16703#comment:13
<ul>
<li><strong>commit</strong>
<em>c07475f78e4434cfd4424b3c09a502e3db22b323</em> deleted
</li>
</ul>
<p>
I get failing mathematica doctests (with version 10.0). I created <a class="closed ticket" href="https://trac.sagemath.org/ticket/18888" title="defect: failing optional mathematica tests, due to bugs in N() and n() (closed: fixed)">#18888</a>. Do you also get the same failing tests as me?
</p>
Ticketetn40ffMon, 13 Jul 2015 14:19:05 GMT
https://trac.sagemath.org/ticket/16703#comment:14
https://trac.sagemath.org/ticket/16703#comment:14
<p>
Edit:: I did not realize before posting but I had some issues on my Sage install so my reply was completey wrong; please dirsegard the rest of this comment.
</p>
<p>
It looks like the errors are not related to mathematica (at least some of them are not):
</p>
<pre class="wiki">$ which math
math not found
$ sage -t --warn-long 2.3 src/sage/symbolic/expression.pyx
Running doctests with ID 2015-07-13-16-14-33-bf78acfd.
Git branch: develop
Using --optional=python2,sage,scons
Doctesting 1 file.
sage -t --warn-long 2.3 src/sage/symbolic/expression.pyx
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 1061, in sage.symbolic.expression.Expression.__int__
Warning, slow doctest:
int(log(8)/log(2))
Test ran for 3.46 s
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 1063, in sage.symbolic.expression.Expression.__int__
Warning, slow doctest:
int(-log(8)/log(2))
Test ran for 2.46 s
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 2385, in sage.symbolic.expression.Expression.__nonzero__
Failed example:
bool(log(2)*Infinity == Infinity)
Expected:
True
Got:
False
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 4071, in sage.symbolic.expression.Expression.expand
Failed example:
(c/2*(5*(3*a*b*x*y*p*q)^2)^(7/2*c)).expand()
Expected:
1/2*45^(7/2*c)*(a^2*b^2*x^2*y^2)^(7/2*c)*c*p^(7*c)*q^(7*c)
Got:
1/2*(45*a^2*b^2*p^2*q^2*x^2*y^2)^(7/2*c)*c
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 4073, in sage.symbolic.expression.Expression.expand
Failed example:
((-(-a*x*p)^3*(b*y*p)^3)^(c/2)).expand()
Expected:
(a^3*b^3*x^3*y^3)^(1/2*c)*p^(3*c)
Got:
(a^3*b^3*p^6*x^3*y^3)^(1/2*c)
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 5155, in sage.symbolic.expression.Expression._numerical_approx
Warning, slow doctest:
all(len(str(e.n(digits=k)))-1 >= k for k in ks)
Test ran for 4.45 s
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 10884, in sage.symbolic.expression.Expression._plot_fast_callable
Warning, slow doctest:
plot(s)
Test ran for 2.43 s
**********************************************************************
2 items had failures:
1 of 70 in sage.symbolic.expression.Expression.__nonzero__
2 of 22 in sage.symbolic.expression.Expression.expand
[2464 tests, 3 failures, 49.88 s]
----------------------------------------------------------------------
sage -t --warn-long 2.3 src/sage/symbolic/expression.pyx # 3 doctests failed
----------------------------------------------------------------------
Total time for all tests: 50.8 seconds
cpu time: 48.0 seconds
cumulative wall time: 49.9 seconds
</pre>
TicketdunfieldMon, 13 Jul 2015 17:31:59 GMT
https://trac.sagemath.org/ticket/16703#comment:15
https://trac.sagemath.org/ticket/16703#comment:15
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/16703#comment:13" title="Comment 13">slabbe</a>:
</p>
<blockquote class="citation">
<p>
I get failing mathematica doctests (with version 10.0). I created <a class="closed ticket" href="https://trac.sagemath.org/ticket/18888" title="defect: failing optional mathematica tests, due to bugs in N() and n() (closed: fixed)">#18888</a>. Do you also get the same failing tests as me?
</p>
</blockquote>
<p>
Basically, yes. See my comment on <a class="closed ticket" href="https://trac.sagemath.org/ticket/18888" title="defect: failing optional mathematica tests, due to bugs in N() and n() (closed: fixed)">#18888</a> for more.
</p>
TicketncohenWed, 15 Jul 2015 15:35:32 GMTcc changed
https://trac.sagemath.org/ticket/16703#comment:16
https://trac.sagemath.org/ticket/16703#comment:16
<ul>
<li><strong>cc</strong>
<em>dimpase</em> added
</li>
</ul>
<p>
In an attempt to run the '#optional - mathematica' doctests automatically (see <a class="closed ticket" href="https://trac.sagemath.org/ticket/18904" title="enhancement: Automatically test optional non-packages (CPLEX, Gurobi, Maple?, ..) (closed: fixed)">#18904</a>), we noticed that running 'mathematica(1)' hangs forever when mathematica is not installed. Dima created a ticket for this (<a class="closed ticket" href="https://trac.sagemath.org/ticket/18908" title="defect: Fix math-readline script (closed: fixed)">#18908</a>), and I post here because it is where <code>math-readline</code> was apparently created.
</p>
<p>
Unless the bug comes from the modification made in <a class="closed ticket" href="https://trac.sagemath.org/ticket/16077" title="enhancement: Python 3 preparation: Handle changes to "raw_input()" and "input()" (closed: fixed)">#16077</a>.
</p>
<p>
Nathann
</p>
TicketdimpaseWed, 15 Jul 2015 15:53:25 GMT
https://trac.sagemath.org/ticket/16703#comment:17
https://trac.sagemath.org/ticket/16703#comment:17
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/16703#comment:16" title="Comment 16">ncohen</a>:
</p>
<blockquote class="citation">
<p>
In an attempt to run the '#optional - mathematica' doctests automatically (see <a class="closed ticket" href="https://trac.sagemath.org/ticket/18904" title="enhancement: Automatically test optional non-packages (CPLEX, Gurobi, Maple?, ..) (closed: fixed)">#18904</a>), we noticed that running 'mathematica(1)' hangs forever when mathematica is not installed. Dima created a ticket for this (<a class="closed ticket" href="https://trac.sagemath.org/ticket/18908" title="defect: Fix math-readline script (closed: fixed)">#18908</a>), and I post here because it is where <code>math-readline</code> was apparently created.
</p>
<p>
Unless the bug comes from the modification made in <a class="closed ticket" href="https://trac.sagemath.org/ticket/16077" title="enhancement: Python 3 preparation: Handle changes to "raw_input()" and "input()" (closed: fixed)">#16077</a>
</p>
</blockquote>
<p>
no, that wrapper (original or updated) does not check whether <code>math</code> can be called.
And it uses obsolete <code>os.popen</code> stuff, too...
</p>
Ticketetn40ffWed, 15 Jul 2015 16:01:08 GMT
https://trac.sagemath.org/ticket/16703#comment:18
https://trac.sagemath.org/ticket/16703#comment:18
<p>
This is a quick fix. We should probably use the occasion to rewrite the wrapper without os.popen
</p>
<pre class="wiki">diff --git a/src/bin/math-readline b/src/bin/math-readline
index 49a58a1..8175c77 100755
--- a/src/bin/math-readline
+++ b/src/bin/math-readline
@@ -6,6 +6,19 @@
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/363500
import os, sys
+
+installed = False
+
+for path in os.environ["PATH"].split(os.pathsep):
+ path = path.strip('"')
+ exe_file = os.path.join(path, 'math')
+ if os.path.isfile(exe_file) and os.access(exe_file, os.X_OK):
+ installed = True
+ break
+
+if not installed:
+ raise RuntimeError("Could not find math in the system path.")
+
f1 = os.popen('math ', 'w')
f1.flush()
try:
</pre>
Ticketetn40ffWed, 15 Jul 2015 16:35:14 GMT
https://trac.sagemath.org/ticket/16703#comment:19
https://trac.sagemath.org/ticket/16703#comment:19
<p>
I did a little research and it looks like this wrapper is duplicated effort and we should get rid of it. We should be able to call <code>math</code> via <code>rlfe</code>, which is part of the standard GNU readline implementation, simply as
</p>
<pre class="wiki">rfle math
</pre><p>
Alternatively there are plenty more, maybe too fancy for our purpose, wrappers like <code>rlwrap</code>.
</p>
TicketdunfieldWed, 15 Jul 2015 16:53:41 GMT
https://trac.sagemath.org/ticket/16703#comment:20
https://trac.sagemath.org/ticket/16703#comment:20
<blockquote class="citation">
<p>
I did a little research and it looks like this wrapper is duplicated effort and we should get rid of it. We should be able to call <code>math</code> via <code>rlfe</code>, which is part of the standard GNU readline implementation
</p>
</blockquote>
<p>
I don't think<code>rlfe</code> is part of Sage currently, and it's not installed by default on Ubuntu or OS X. While there are many tools available that do the work of the current wrapper, it does have the advantage of being a tiny script with no dependencies other than Python itself.
</p>
Ticketetn40ffWed, 15 Jul 2015 16:59:19 GMT
https://trac.sagemath.org/ticket/16703#comment:21
https://trac.sagemath.org/ticket/16703#comment:21
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/16703#comment:20" title="Comment 20">dunfield</a>:
</p>
<blockquote class="citation">
<blockquote class="citation">
<p>
I did a little research and it looks like this wrapper is duplicated effort and we should get rid of it. We should be able to call <code>math</code> via <code>rlfe</code>, which is part of the standard GNU readline implementation
</p>
</blockquote>
<p>
I don't think<code>rlfe</code> is part of Sage currently, and it's not installed by default on Ubuntu or OS X. While there are many tools available that do the work of the current wrapper, it does have the advantage of being a tiny script with no dependencies other than Python itself.
</p>
</blockquote>
<p>
Ok, I guess sticking to the python script is the better option here. Should I write a more modern version or should we use the patch from comment 18? Either option works for me.
</p>
TicketdimpaseThu, 16 Jul 2015 09:01:49 GMT
https://trac.sagemath.org/ticket/16703#comment:22
https://trac.sagemath.org/ticket/16703#comment:22
<p>
let's move this over to <a class="closed ticket" href="https://trac.sagemath.org/ticket/18908" title="defect: Fix math-readline script (closed: fixed)">#18908</a>. This ticket is closed, and should stay so.
</p>
Ticket