Using Mathematica within Sagemath

Note: The information and work-around below relate to our older Kubuntu TDE 12.04 desktop installation, which had Sagemath 6 and Mathematica {8, 9, 10}. Our newer Kubuntu TDE 16.04 installation, which have Sagemath 7.2 and Mathematica {9, 10.4}, do not exhibit the problem for which the work-around below was necessary. This webpage will be removed once we no longer have systems to which it applies.


Examples of calling Mathematica from within Sagemath are provided at doc.sagemath.org. However, those examples seem to work only when Sagemath talks to Mathematica 8. Although our systems still have Mathematica 8 available, the default behavior is to invoke a newer version of Mathematica, and Sagemath itself does not let you specify which Mathematica to invoke. Here is how you can trick Sagemath into invoking the older version.

One-time setup:

  • Open a Terminal window. (Do not connect to chaos.)
  • Copy and paste the following commands:
    pushd $HOME
    mkdir -p sagewithmathematica8
    ln -s /opt/bin/math8 sagewithmathematica8/math
    mkdir -p bin
    cat <<"EOF" >bin/sagewithmathematica
    #!/bin/sh
    export PATH=$HOME/sagewithmathematica8:$PATH
    sage "$@"
    EOF
    chmod +x bin/sagewithmathematica
    mkdir -p .local/share/applications
    cd .local/share/applications
    cp /usr/share/applications/lsumath-sage6.desktop sagewithmathematica.desktop
    perl -pi -e 's/^Name=.*$/Name=Sagemath 6 with Mathematica 8/' sagewithmathematica.desktop
    perl -pi -e 's/^Exec=.*$/Exec=sagewithmathematica -notebook/' sagewithmathematica.desktop
    popd
    
  • Close the Terminal.
  • Log out and log back in.

For the curious: the commands create a script (and a corresponding GUI Desktop Start Menu entry) that on-the-fly modifies PATH for the duration of a single Sagemath session. Sagemath is hardcoded to invoke 'math'. In the modified path, 'math' points to 'math8'.

Two ways to launch the modified Sagemath:

  • Use your desktop start menu and look under Science & Math Computation.
  • At a command prompt the command is sagewithmathematica (for example, this is what you would type when connected to chaos).

Either way, the examples given in the link at the top of this page should now work.