Sapiens Search

How to run Mathematica functions on Python?

1-Create a script named runMath with the content:

    #!/usr/local/bin/MathematicaScript -script

    value=ToExpression[$ScriptCommandLine[[2]]];

    (*The next line prints the script name.*)
    (*Print[$ScriptCommandLine[[1]]];*)

    Print[value];

2-Give execution privilege to the file:
    sudo chmod +x runMath

3-Move the file to the execution path:

    sudo mv runMath /usr/bin/

4-Create a new script called run with the content:

    #!/usr/bin/python
    from subprocess import *
    from sys import *

    command='/usr/bin/runMath'
    parameter=argv[1]

    call([command,parameter])

5-Move to the execution path:

    sudo mv run /usr/bin

6-Finally, test it:

    $run Prime[100]
    541

    $run 'Sum[2x-1,{x,1,k}]'
    k^2

    $run Integrate[Log[x],x]
    -x + x*Log[x]

    $run 'Zeta[2]'
    Pi^2/6

You can use with ou without '. The ' are needed to commands with spaces.

    $run 'f[n_] := f[n] = f[n - 1] + f[n - 2]; f[1] = f[2] = 1; Table[f[n],{n,5}]'
    {1, 1, 2, 3, 5}

23 comments:

  1. I followed the procedure exactly , but when tried to run it, lets say 'run Prime[100]' ,do not know why it is showing :

    bash: /usr/bin/run: Permission denied

    any help ?

    ReplyDelete
  2. Hi string, your problem is simple, your script in /usr/bin/run isn't executable by other users. There are two ways, run as root ("sudo /usr/bin/run" ,for exemple or give it permissions, like "sudo chmod 777 /usr/bin/run", but this will give permissions to everyone execute). If you know your user you can do the following "sudo chown myUser:myUser /usr/bin/run && sudo chmod 711 /usr/bin/run". If you couldnt run it yet, please post a comment.

    ReplyDelete
  3. Thank you so much, it worked ! I appreciate your help! your blog is full of useful information. :-)

    ReplyDelete
  4. Instead of calling these functions and compute in my ubuntu terminal, is it possible to call the Mathematica functions directly to the Python shell (IDLE) and compute within it, in addition to some other parts of the code contains pure python syntex ?

    ReplyDelete
  5. Yes sure. I will write a project on github with a working code and the post here to you. Please wait a little =)

    ReplyDelete
  6. is it possible to call mathematica plot output in python. It would be great to do. Do you have any working solution of calling mathematica in Python.

    ReplyDelete
  7. I tried above procedure, did nt work in ubuntu, i do not see:

    /usr/local/bin/MathematicaScript

    ReplyDelete
    Replies
    1. Hey jr, look for path like this :

      /usr/local/Wolfram/Mathematica/8.0/SystemFiles/Kernel/Binaries/Linux/MathematicaScript

      Delete
    2. Take a look on a project called WITM (Web Interface to Mathematica) (using PHP) and MathKernel and MathematicaScript options. I don't remember now, but I think is possible, but maybe we need to do something WITM does but with Python. Take a look. =)

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. It has been what I am looking for. But still, I don't exactly follow the way to implement it. Could you explain in details?

    1) Is the runMath saved as .m format? Where should it be saved into?
    2) I can't run sudo chmod +x runMath, and sudo mv runMath /usr/bin/ in my Windows XP Command Prompt.
    3) Also, do we type runMath in verbatim? or should we modify into something like this:

    sudo chmod +x C:\Python27\runMath.m
    sudo mv C:\Python27\runMath.m /usr/bin/

    4) Is the script called 'run' also created using Mathematica or Python? If it is python, there is always a message error after entering 'parameter=argv[1]'.
    The message was

    Traceback (most recent call last):
    File "", line 1, in
    parameter=argv[1]
    IndexError: list index out of range

    5) Also, where should the run script be saved into?

    6) To run Mathematica functions in Python, do I have to initiate or import anything beforehand?

    Thank you!!

    ReplyDelete
    Replies
    1. Hi gratitude38, let answer your questions^^

      1 - runMath is a Mathematica script, but we don't need save it as .m. You can name it just with 'runMath' if you like.

      2 - chmod +x is a unix command. In fact, this solution holds just on linux/unix systems... Take a look on the observation at the end.

      3 - This commands will not work to you if you run on Windows. Maybe you should take a look on Linux. Take a look on the observation at the end.

      4 - run is a script created on python language.

      5 - Take a look at the observation at the end.

      6 - Python need to import some things with these couple of lines:
      from subprocess import *
      from sys import *

      Observation:

      The truth, you just need to create a script runMath (wrote in Mathematica with the same content that I already wrote) and a script run (wrote in Python) like I already created too. But the important thing is the line 'command='/usr/bin/runMath' should point to your runMath script (wherever it is). For example: 'command='C:\runMath' (if you put it into C:). I don't know if we need something more to run on Windows systems, but write run, runMath and put it on the same folder is all you need to put it to work. The other things is just to let the things a little elegant for unix users. Help it hopes.

      Delete
  10. Looking forward to reading more. Great blog post.Much thanks again. Great.
    Mathematica

    ReplyDelete
    Replies
    1. Wow. Long time with no answer. Sorry for no reply until now =/. Thx a lot for your comment.

      Delete
  11. Does it work on Mac OS X EI Capitan V10.11.5?
    I tried but I got

    from: can't read /var/mail/subprocess
    from: can't read /var/mail/sys
    /usr/bin/run: line 8: syntax error near unexpected token `[command,parameter]'
    /usr/bin/run: line 8: ` call([command,parameter])'

    So what should I do? Thanks.

    ReplyDelete
    Replies
    1. Hi, I'm not running Mac for now so I can't try it. But it should work on Mac without problems. The problem that you pointed happened because the imports could not be done by the python compiler. If you put "from subprocess import *" and "from sys import *" to work you're done. Maybe this happened because "#!/usr/bin/python" is not the correct location to your python executable. Inspecting at this point you should have success.

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I tried the above, wanting to call Mathematica from my Raspberry Pi 3. When I tried it from the command prompt, run showed as non-executable. So, I made it executable just like runMath. I also changed #!/usr/bin/python to #!/usr/bin/python3. Not sure if that harms anything. But when I try it again from the command prompt, I get errors that end with, No such field or directory: '/usr/bin/runMath' I did copy run and runMath to the directories you show. So I'm a little stumped.

    ReplyDelete
    Replies
    1. Hi, for testing purposes, we don't really need move the run and runMath to the bin folder, it's just a helper to use the command more easily. So I recommend you first give them execution permissions.

      sudo chmod 755 runMath

      then test inside the directory where runMath is int try

      ./runMath Prime[100]

      if works, now if you move them all to the bin folder should works.
      If the file is still not locate have sure if it is where it should be, in this case runMat

      cat /usr/bin/runMath

      Delete
  14. Hi,

    i followed your instructions here and on github https://github.com/Danilo-Araujo-Silva/Mathematica. I can run mathematica commands in terminal now. Can you tell me how i can use your work to run Mathematica commands from my ipython3 notebook? I tried using magic commands but i dont know how to catch the return of the mathematica function. Can you tell me how to do it?

    Thanks a lot, I really appreciate your work!

    ReplyDelete
  15. Hi, I'm not a user of IPython3 for now so maybe unfortunately I'll cannot help you so much. But if you would like to catch the return of a mathematica function and assign it to a variable you can do something like this:

    // test.py file

    from subprocess import *
    from sys import *

    command='echo'
    parameter=argv[1]

    result = check_output([command,parameter])

    print('This came from result: ' + result)

    // Tested with: python test.py $PATH

    That is, you could change the method 'call' (as in the repository) to 'check_output' and you're done.

    ReplyDelete
  16. Thank you! Thats exactly what i needed. The remaining problem will be converting the result to something i can use in python. I expect the result to be an mathematica array so I will try to convert it to a String and then to a python array. Do you happen to know if there is a nicer way to do that?

    ReplyDelete