Calling polymake from another program on a mac

Questions and problems about using polymake go here.
maclagan
Posts: 6
Joined: 04 Apr 2019, 21:25

Calling polymake from another program on a mac

Postby maclagan » 03 Jun 2019, 20:46

Hi,

We're trying to call polymake from inside Macaulay2. Unfortunately, code that works under linux doesn't seem to work on Macs - I'm hoping that there is a different way to do this that I don't know about. The issue is that we want to give polymake an input file from the command line, and have the output redirected to a file (since these M2 can interact with the outside world via reading and writing files, and running commands at the command line). Is there a way to do that that works on both systems?

I'm aware that this questions is a bit vague - I find the way polymake works on a Mac confusing.

Thanks,

Diane

paffenholz
Developer
Posts: 211
Joined: 24 Dec 2010, 13:47

Re: Calling polymake from another program on a mac

Postby paffenholz » 03 Jun 2019, 22:57

polymake works the same way on Mac and Linux.

The only thing that might make it look different is the binary version bundled as a Mac application. This is due to the way Apple has organized its applications. I can't change this. But apart from following apple's folder and file name requirements, the implied necessity to call polymake via a wrapper script to tell it where to find its files and the inclusion of all standard linux software apple fails to provide there is no difference between a polymake on a Mac and on Linux.

If you want to call the bundled polymake from a script, you should call

Code: Select all

<path>/polymake.app/Contents/MacOS/polymake.start
where ''<path>'' is the path you installed polymake into. This is usually "/Applications". But you can put it anywhere, so you should not hardcode "/Application" into your software. Also, people might have a self compiled version somewhere in the path.

Reading files and redirecting should then work as usual, e.g.

Code: Select all

<path>/polymake.app/Contens/MacOS/polymake.start infile > outfile


(I can't use the name "polymake" for the script because that script is what is called if you click on the polymake icon. In this case we have to open a terminal window first.)

Andreas

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

Re: Calling polymake from another program on a mac

Postby gawrilow » 03 Jun 2019, 23:08

Without seeing any error messages or code examples I can't give any concrete advice, just some general hints. There are several different ways how polymake can be integrated with other software, all of them are supposed to be generic enough to work under all UNIX-like systems, including Linux and MacOS.

One way is to use the callable library. There is a function shell_execute which takes a string of input (in perl/polymake language), executes it as if it were entered in the interactive shell, and returns the entire stdout, stderr and exceptions collected in three strings. If your software is written in C++, this would be the most direct and comfortable way of communication.

Another way is to run the polymake script in pipe mode, that is, with a '-' sign as the only argument, and in the same command redirect stdin, stdout and stderr with usual shell operators. This way is more convenient for software written in other languages where you don't want to mess with C/C++ wrappers.

There is a slight variation of the second method: instead of redirecting stdin/stdout/stderr, you can create a named pipe or socket file and let polymake connect to it. This is, however, a shady corner on Macs, in case you run polymake not natively in the host system but in a Linux VM, then the socket or pipe won't be visible within the VM.

maclagan
Posts: 6
Joined: 04 Apr 2019, 21:25

Re: Calling polymake from another program on a mac

Postby maclagan » 04 Jun 2019, 19:48

Thanks for the clarifications. Unfortunately I'm still having trouble. I put the following text in a file "polymaketestinput":

Code: Select all

use application 'tropical'; $H = new Hypersurface<Min>(POLYNOMIAL=>toTropicalPolynomial("min(a,b,c)")); print $H->VERTICES;
When I try

Code: Select all

maclagan% /Applications/polymake.app/Contents/MacOS/polymake.start polymaketestinput >polymakeoutput
I get the output

Code: Select all

polymake: ERROR: "/Users/maclagan/Dropbox/M2/LeipzigWorkshop/Workshop-2018-Leipzig/Tropical/polymaketestinput", line 2: reference to an undeclared variable $H
The code works correctly if I just type

Code: Select all

/Applications/polymake.app/Contents/MacOS/polymake.start
and cut and paste the three lines one by one. Is this polymake, MacOS, or something weirder about my set up?

Thanks,
Diane

User avatar
gawrilow
Main Author
Posts: 423
Joined: 25 Dec 2010, 17:40

Re: Calling polymake from another program on a mac

Postby gawrilow » 04 Jun 2019, 22:01

There is a subtle difference between scripts and interactive shell input. Scripts are evaluated more strictly, every variable must be declared. Interactive mode is more sloppy, it allows to introduce new variables just by assignment.
You are calling polymake in script mode, hence the error message.
If you want to stay with this, just prepend the assignment with 'my' or 'declare'.
Alternatively, you can switch to interactive (pipe) mode as explained in my previous reply, but then you must remove the word 'use' from the first line, otherwise it would stay without any effect.

maclagan
Posts: 6
Joined: 04 Apr 2019, 21:25

Re: Calling polymake from another program on a mac

Postby maclagan » 05 Jun 2019, 12:39

Thanks - that resolves this issue.
Diane


Return to “Helpdesk”