## pypolymake: A Sage Interface to polymake

General discussion on polymake goes here.

Moderator: Moderators

joswig
Main Author
Posts: 207
Joined: 24 Dec 2010, 11:10

### pypolymake: A Sage Interface to polymake

There is a new attempt to create an interface from Sage to polymake. This thread is meant to report on this development and to discuss how to proceed. To answer the obvious question of why this is not discussed in the developer's forum: the topic seems to be of interest to a wider audience, and the bulk of the development is outside polymake's code base.

Before I start I would like to express my sincere thanks to Burcin Erocal who took the lead to write this first interface. The code is available at https://bitbucket.org/burcin/pypolymake. The installation instructions below are also mostly due to him.

The setup is somewhat delicate but has been tried with sage 4.7.1 and polymake 2.9.10. While it does not work with our most recent release 2.10 Benjamin Lorenz was able to resolve this extremely subtle issue a few days ago, and the patch will appear in our next release (to be expected soon).

Here are my installation details on Ubuntu 11.04 which includes building sage. I used an additionally installed gcc 4.4 (from the Ubuntu package), since the default compiler from the 4.5 series is bogus. The newer versions of gcc 4.6.x would be another viable option.

Code: Select all

CC=gcc-4.4; CXX=g++-4.4 SAGE64=yes SAGE_FORTRAN=/usr/bin/gfortran-4.4 SAGE_FORTRAN_LIB=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.4/libgfortran.so 
In the sequel I assume that the sage source code has been extracted to /usr/local/sage-4.7.1. Building libatlas is very tedious and also unnecessary since it can be installed from a package.

Code: Select all

cd /usr/local/sage-4.7.1 touch spkg/installed/atlas-3.8.3.p16 make 
The following updates some sage packages to versions compatible with polymake. This includes a version of polymake 2.9.10 as a sage package.

Code: Select all

./sage -i http://sage.math.washington.edu/home/burcin/polymake/mpfi-1.5.spkg ./sage -i http://sage.math.washington.edu/home/burcin/polymake/mpfr-3.0.0.spkg ./sage -f spkg/standard/mpir-1.2.2.p2.spkg ./sage -i http://sage.math.washington.edu/home/burcin/polymake/polymake-2.9.10.p0.spkg ./sage -b 
For me the following trick was neceassary:

Code: Select all

cd local/lib ln -s libmpfr.so libmpfr.so.1 cd - 
You now should have custom made version of sage 4.7.1 up and running.

This is where the installation of pypolymake starts. The commands below require mercurial (with extension mq) to be installed. The patch itself is attached below. First we patch sage.

Code: Select all

cd local/bin hg qimport sage-env.patch hg qpush cd - 
Now comes the actual pypolymake:

Code: Select all

hg clone https://bitbucket.org/burcin/pypolymake ln -s /usr/local/sage-4.7.1/devel/sage/c_lib/include /usr/local/sage-4.7.1/local/include/sage ./sage -sh cd pypolymake python setup.py install 
That's it.

Here is a first demo (in a sage notebook):

Code: Select all

import polymake m = matrix(QQ,[[1, 3, 0, 0], [1, 0, 3, 0], [1, 1, 1, 1], [1, 0, 0, 3], [1, 0, 0, 0], [1, 2, 2, 2]]) p = polymake.Polytope('POINTS',m) p.is_simple() 
The above should return False. You can save objects to files which can then be processed further by polymake.

Code: Select all

p._save("p.poly") 
Visualization via jreality should work, too.

Code: Select all

p.visual() 
More fancy polymake functions can be called via passing the corresponding property as a string:

Code: Select all

p._get_integer_property("CUBICALITY") 
Please look into the pypolymake code for more examples.

The current pypolymake implementation has the following limitations:
• The only basic polymake types supported so far are: Bool, Int, Matrix<Rational>.
• The only big object types supported so far is: Polytope<Rational>.
Next Steps. Someone should take the lead to extend the pypolymake interface to add more basic types. Most importantly: Integer, Rational, Array<Int>, Set<Int>. Adding other big object types, such as SimplicialComplex and PolyhedralFan should be fairly easy but does not make much sense before more basic types are available. Whatever you do in this direction, please document it in this thread.

Happy pypolymaking!

-- Michael
Attachments
sage-env.patch

stumpc5
Posts: 6
Joined: 24 Nov 2011, 13:33

### Re: pypolymake: A Sage Interface to polymake

It took me a while to find your descriptions here...

I tried to install the polymake package for sage as described (thanks to Burcin for providing this first version!). I got into trouble to install polymake-2.9.10.p0.spkg, both with gcc-4.6 and with gcc-4.4. See the output below, I don't see what the problem might be...

I used Ubuntu 11.10 with also 64bit.

Best, Christian

Code: Select all

Linux stumpc5 3.0.0-13-generic #22-Ubuntu SMP Wed Nov 2 13:27:26 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux **************************************************** **************************************************** CC Version gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) **************************************************** Starting build... Running build_polymake... Use of inherited AUTOLOAD for non-method Term::ReadLine::Gnu::ornaments() is deprecated at /usr/lib/perl5/Term/ReadLine/Gnu.pm line 250. /usr/bin/java seems to belong to a pure Java runtime installation: JNI headers (most prominently jni.h) were not found. If they happen to reside at a different location, please specify it using --with-jni-headers option (but double-check upfront whether they really suit the chosen Java environment). Otherwise, please install a complete JDK or disable building Java native libraries by passing --without-java to the configure script. In the latter case interfaces to JavaView and JReality will be deactivated, but the users will be able to reconfigure them on individual basis. Failed to configure. 
ps: I also see that installing Ubuntu 11.10 corrupted my polymake. I thought that I try the same version as Michael did, but I cannot download polymake-2.9.10.tar.bz2.

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

### Re: pypolymake: A Sage Interface to polymake

ps: I also see that installing Ubuntu 11.10 corrupted my polymake. I thought that I try the same version as Michael did, but I cannot download polymake-2.9.10.tar.bz2.

blorenz
Developer
Posts: 108
Joined: 10 Jan 2011, 17:21

### Re: pypolymake: A Sage Interface to polymake

Code: Select all

/usr/bin/java seems to belong to a pure Java runtime installation: JNI headers (most prominently jni.h) were not found. If they happen to reside at a different location, please specify it using --with-jni-headers option (but double-check upfront whether they really suit the chosen Java environment). Otherwise, please install a complete JDK or disable building Java native libraries by passing --without-java to the configure script. In the latter case interfaces to JavaView and JReality will be deactivated, but the users will be able to reconfigure them on individual basis. Failed to configure. 
For this spkg you either need to install a jdk (default-jdk package should work) or edit the configure call in the spkg-install script inside the polymake-...spkg (which is just a .tar.bz2 file) as follows, replace:

Code: Select all

./configure --with-gmp=$SAGE_LOCAL --prefix=$SAGE_LOCAL 
by

Code: Select all

./configure --with-gmp=$SAGE_LOCAL --prefix=$SAGE_LOCAL --without-java 
And you should use CC=gcc44 CXX=g++44 or you will run into more errors.

stumpc5
Posts: 6
Joined: 24 Nov 2011, 13:33

### Re: pypolymake: A Sage Interface to polymake

I now got it installed, thanks for your comments. Only the visualization doesn't yet work:

Code: Select all

sage: p.visual() polymake: WARNING: stored preference statements for label jreality are not in effect - probably excluded by auto-configuration sage: ERROR: ld.so: object '/home/stumpc5/progs/sage-4.7.1/local/lib/libpolymake.so' from LD_PRELOAD cannot be preloaded: ignored. evince: error while loading shared libraries: libz.so.1: failed to map segment from shared object: Permission denied 
Beside that, here are two minor comments on Michael's installation description:

There is a typo in

Code: Select all

SAGE_FORTRAN_LIB=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.4/libgfortran.so 
which should be

Code: Select all

SAGE_FORTRAN_LIB=/usr/lib/gcc/x86_64-linux-gnu/4.4/libgfortran.so 
Then I would maybe set also

Code: Select all

SAGE_ROOT=/your/path/to/sage-4.7.1/ 
and use it everywhere, including at the places where you used

Code: Select all

cd - 
I will now start looking into the patch and see if I can start working on that a little...

joswig
Main Author
Posts: 207
Joined: 24 Dec 2010, 11:10

### Re: pypolymake: A Sage Interface to polymake

I now got it installed, thanks for your comments. Only the visualization doesn't yet work:

Code: Select all

sage: p.visual() polymake: WARNING: stored preference statements for label jreality are not in effect - probably excluded by auto-configuration sage: ERROR: ld.so: object '/home/stumpc5/progs/sage-4.7.1/local/lib/libpolymake.so' from LD_PRELOAD cannot be preloaded: ignored. evince: error while loading shared libraries: libz.so.1: failed to map segment from shared object: Permission denied 
Actually, at least the evince problem does not look like something polymake could possibly be responsible for. It rather suggests that your shared library setup is messed up somehow. Please, check ld.so.conf and LD_LIBRARY_PATH. If need be explanations can be found via "man ld". Or permissions are not set correctly.

jplabbe
Posts: 1
Joined: 23 Jan 2012, 10:51

### Re: pypolymake: A Sage Interface to polymake

Hi everyone,

I use mainly MacOSX until the next LTS version of ubuntu...

So I thought I'd try to see how it goes on the mac for pypolymake.

I definitely had a different setup than the tutorial, but it was worth to make a first try!

-I got a version 4.7.2 of Sage running.
-Mac OSX 10.6.8

I proceeded as written and then proceeded to install the package mpfi and mpfr, but has the mpfr library changes the name of the dynamic library file "libmpfr.1.dylib" to "libmpfr.4.dylib", it creates a error when starting sage:

Code: Select all

ImportError: dlopen( SAGE_PATH/local/lib/python2.6/site-packages/sage/rings/real_mpfi.so, 2): Library not loaded: /usr/local/lib/libmpfr.4.dylib Referenced from: SAGE_PATH/local/lib//libmpfi.0.dylib Reason: Incompatible library version: libmpfi.0.dylib requires version 6.0.0 or later, but libmpfr.4.dylib provides version 5.0.0 Error importing ipy_profile_sage - perhaps you should run %upgrade? WARNING: Loading of ipy_profile_sage failed. 
I found a page with a related problem.

I also saw that sage plans on updating the version of mpfr.

I still proceeded to install mpir and polymake2.9.10 (and installed the necessaries for polymake2.9.10 which Fink is one of them).

And then build sage with ./sage -b

and when I start sage I get a similar error as above (and then even the simplest computations fail, like 1+1...).

Since the version was now broken, I decided to compile fresh version of sage... and then the make process stops because sage detected Fink in the path and that it breaks the building process (thus needing to change the path during the build...) Perhaps, one need to change the path-name to Fink before building sage after installing polymake2.9.10.spkg?

Well, that's the information I got so far!

Do you have any ideas how we should deal with this?

tom111
Posts: 3
Joined: 29 May 2012, 19:08

### Re: pypolymake: A Sage Interface to polymake

Hi everyone. What is the latest on pypolymake? I have sage-5.0 and polymake-2.12 installed (with libpolymake available, etc.). In my dreams I'm passing a sage Polyhedron object and a property like 'H_STAR_VECTOR' to some (imaginary) function 'callpolymake' and then it returns the result of the corresponding polymake computation. I think until the libpolymake/cython interface is usable (and stays so for more than one release of polymake/sage) it would be very cool to have a POpen wrapper that is simple and just works (even if polymake is upgraded by the package manager of my distribution...)
Has anyone worked in this direction? Otherwise I'll just start to implement this by editing Will Stein's 2006 version of polymake.py: https://github.com/sagemath/sagelib/blo ... olytope.py

Cheers,
Thomas

tom111
Posts: 3
Joined: 29 May 2012, 19:08

### Re: pypolymake: A Sage Interface to polymake

Hi again. While this is being actively worked on, I wrote a small hack that solved my problem for now. The following is a sage function that calls polymake to compute a number of properties and then write the output to a given filename. Note that it is not very flexible, for instance I just pass the H-representation without any choice, so it may be computed once in sage, and then again in polymake.

If p is a sage polyhedron then calling

writePolyhedronWithPolymake (p, "/home/tom/mypolyhedron.poly", ["F_VECTOR", "H_STAR_VECTOR"])

will run polymake to compute the F_VECTOR and H_STAR_VECTOR, and then write the polyhedron to the given filename.

Here's the function:

import subprocess
import tempfile
def writePolyhedronWithPolymake (P, filename, toCompute=[]):
"""
Write a polymake file with a sage polyhedron.
With the toCompute option a list of polymake property to precompute can be given.
"""
polyscript = tempfile.NamedTemporaryFile(mode="wr") # Automatically deleted later
polyscript.write ("use application \"polytope\";\n")
polyscript.write ("my $p = new Polytope<Rational> (INEQUALITIES=>[") polyscript.write(",".join([str(i) for i in P.inequalities()])) polyscript.write("], EQUATIONS=>[") polyscript.write(",".join([str(i) for i in P.equations()])) polyscript.write("]);\n") for p in toCompute: polyscript.write("$p->"+p+";\n")
polyscript.write("save(\$p,'" + filename + "');")
polyscript.flush()
polyscript.seek(0)
for line in polyscript.file:
print line
print polyscript.name
print "calling: " + " ".join(["polymake", "--script", polyscript.name])
subprocess.call(["polymake", "--script", polyscript.name])

tom111
Posts: 3
Joined: 29 May 2012, 19:08

### Re: pypolymake: A Sage Interface to polymake

Here's another update on this. After consulting with Burcin it turned out pretty easy to install pypolymake under Gentoo Linux. I have installed sage via the package manager and after that Burcin's ebuild from the lmona.de tree just worked. I had to set two paths for the installation
export SAGE_ROOT="/usr/share/sage"
export SAGE_LOCAL="/usr"