Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#16703 closed enhancement (fixed)

Fix Mathematica 10 interface

Reported by: etn40ff Owned by:
Priority: major Milestone: sage-6.4
Component: scripts Keywords: Mathematica, Interfaces
Cc: jakobkroeker, dimpase Merged in:
Authors: Salvatore Stella Reviewers: Nathan Dunfield
Report Upstream: N/A Work issues:
Branch: c07475f (Commits) Commit:
Dependencies: Stopgaps:

Description

The interface with Mathematica has been broken since Mathematica 9 came out. The problem is that the program math does not behave nicely with expect. This fix uses a wrapper, as suggested in http://code.activestate.com/recipes/363500-mathematica-with-a-gnu-readline-input/, to make math readline-aware.

Change History (22)

comment:1 Changed 5 years ago by etn40ff

  • Branch set to u/etn40ff/mathematica_10
  • Commit set to a15573f863f1133cecafe46e14282d3835ef072a
  • Status changed from new to needs_review

New commits:

a15573fFix Mathematica interface by passing through readline

comment:2 Changed 5 years ago by git

  • Commit changed from a15573f863f1133cecafe46e14282d3835ef072a to 8ac00fceb5cfd764bb0fdbe41bbb269576c06822

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

8ac00fcFix Mathematica interface by passing through readline

comment:3 Changed 5 years ago by git

  • Commit changed from 8ac00fceb5cfd764bb0fdbe41bbb269576c06822 to 3fe1373cec8e956430521b17ce3996a44283aaba

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

3fe1373Fix Mathematica interface by passing through readline

comment:4 Changed 5 years ago by git

  • Commit changed from 3fe1373cec8e956430521b17ce3996a44283aaba to 43d3ff01c97b841e54f9fff604edd519b711dd04

Branch pushed to git repo; I updated commit sha1. New commits:

43d3ff0Removed duplicate code

comment:5 Changed 5 years ago by git

  • Commit changed from 43d3ff01c97b841e54f9fff604edd519b711dd04 to c07475f78e4434cfd4424b3c09a502e3db22b323

Branch pushed to git repo; I updated commit sha1. New commits:

c07475fRemoved non needed argument from raw_input()

comment:6 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:7 Changed 4 years ago by jakobkroeker

  • Cc jakobkroeker added

comment:8 Changed 4 years ago by drkirkby

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 MathLink?

http://www.wolfram.com/learningcenter/tutorialcollection/MathLinkDevelopmentInC/

like "jmath" uses

http://robotics.caltech.edu/~radford/jmath/

See the

The total source code for that is under 1000 lines, and makes use of functions like MLInitialize, MLActivate etc.

Sure it will take a bit more time to get it right, but it seems to me a better long-term approach.

Dave

comment:9 Changed 4 years ago by etn40ff

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 MathLink? is a better idea but I am not sure there is interest enough in this feature to justify the effort.

S.

comment:10 Changed 4 years ago by drkirkby

Robert Jacobson indicated he wanted to do this last month

https://groups.google.com/forum/#!searchin/sage-devel/Mathematica$20interface|sort:relevance/sage-devel/N4K6lUkUwe0/pKlGj4AfdDcJ

I was the only one to make any comments. Perhaps he might want to complete what he started.

Dave

comment:11 Changed 4 years ago by dunfield

  • Reviewers set to Nathan Dunfield
  • Status changed from needs_review to positive_review

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...

comment:12 Changed 4 years ago by vbraun

  • Branch changed from u/etn40ff/mathematica_10 to c07475f78e4434cfd4424b3c09a502e3db22b323
  • Resolution set to fixed
  • Status changed from positive_review to closed

comment:13 follow-up: Changed 4 years ago by slabbe

  • Commit c07475f78e4434cfd4424b3c09a502e3db22b323 deleted

I get failing mathematica doctests (with version 10.0). I created #18888. Do you also get the same failing tests as me?

comment:14 Changed 4 years ago by etn40ff

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.

It looks like the errors are not related to mathematica (at least some of them are not):

$ 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

Last edited 4 years ago by etn40ff (previous) (diff)

comment:15 in reply to: ↑ 13 Changed 4 years ago by dunfield

Replying to slabbe:

I get failing mathematica doctests (with version 10.0). I created #18888. Do you also get the same failing tests as me?

Basically, yes. See my comment on #18888 for more.

comment:16 follow-up: Changed 4 years ago by ncohen

  • Cc dimpase added

In an attempt to run the '#optional - mathematica' doctests automatically (see #18904), we noticed that running 'mathematica(1)' hangs forever when mathematica is not installed. Dima created a ticket for this (#18908), and I post here because it is where math-readline was apparently created.

Unless the bug comes from the modification made in #16077.

Nathann

comment:17 in reply to: ↑ 16 Changed 4 years ago by dimpase

Replying to ncohen:

In an attempt to run the '#optional - mathematica' doctests automatically (see #18904), we noticed that running 'mathematica(1)' hangs forever when mathematica is not installed. Dima created a ticket for this (#18908), and I post here because it is where math-readline was apparently created.

Unless the bug comes from the modification made in #16077

no, that wrapper (original or updated) does not check whether math can be called. And it uses obsolete os.popen stuff, too...

comment:18 Changed 4 years ago by etn40ff

This is a quick fix. We should probably use the occasion to rewrite the wrapper without os.popen

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:

comment:19 follow-up: Changed 4 years ago by etn40ff

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 math via rlfe, which is part of the standard GNU readline implementation, simply as

rfle math

Alternatively there are plenty more, maybe too fancy for our purpose, wrappers like rlwrap.

comment:20 in reply to: ↑ 19 ; follow-up: Changed 4 years ago by dunfield

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 math via rlfe, which is part of the standard GNU readline implementation

I don't thinkrlfe 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.

comment:21 in reply to: ↑ 20 Changed 4 years ago by etn40ff

Replying to dunfield:

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 math via rlfe, which is part of the standard GNU readline implementation

I don't thinkrlfe 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.

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.

comment:22 Changed 4 years ago by dimpase

let's move this over to #18908. This ticket is closed, and should stay so.

Note: See TracTickets for help on using tickets.