Question about converting integers in PPL

hello,
I have (simple) a question about using PPL 0.4:
I cannot find how to convert a coefficient (of a constraint or generator) to the normal integer type. In my code I do the following; c1 is a constraint which is defined earlier in the code. Now I try to convert a coefficient with
c1_int = mpz_get_si(c1.coefficient(Variable(0)))
but this doesn't work, probably because the coefficient is not of the regular gmp format (c1_int is a standard integer)
How can I convert coefficients to 'normal' integers (or double integers)?
Thank you for your help!
Martin Rohde
Delft University of Technology Kramers Laboratorium voor Fysische Technologie Tel 0031 +152787084 Mail martin@klftfs01.tn.tudelft.nl

Dear Martin,
the coefficients of the PPL are objects of class Integer, which is defined as a shorthand for
namespace Parma_Polyhedra_Library { typedef mpz_class Integer; }
"mpz_class" is the GMP wrapper class for the type mpz_t (see, e.g., http://www.swox.com/gmp/manual/C---Class-Interface.html#C++%20Class%20Interf...).
Thus, the conversion can be done by writing something like
Integer c = c1.coefficient(Variable(0)); long c_long = c.get_si();
I am now wondering if it is worth to enrich the PPL user interface with some methods to perform this kind of conversions directly, perhaps throwing an exception when the conversion cannot be done safely. However, I must reckon that I am not an expert as far as GMP-related issues are concerned ... it is maybe better to wait for Roberto's advice.
Ciao, Enea.
Martin Rohde wrote:
hello,
I have (simple) a question about using PPL 0.4:
I cannot find how to convert a coefficient (of a constraint or generator) to the normal integer type. In my code I do the following; c1 is a constraint which is defined earlier in the code. Now I try to convert a coefficient with
c1_int = mpz_get_si(c1.coefficient(Variable(0)))
but this doesn't work, probably because the coefficient is not of the regular gmp format (c1_int is a standard integer)
How can I convert coefficients to 'normal' integers (or double integers)?
Thank you for your help!
Martin Rohde
Delft University of Technology Kramers Laboratorium voor Fysische Technologie Tel 0031 +152787084 Mail martin@klftfs01.tn.tudelft.nl
PPL-devel mailing list PPL-devel@cs.unipr.it http://www.cs.unipr.it/mailman/listinfo/ppl-devel

Enea Zaffanella wrote: [...]
Integer c = c1.coefficient(Variable(0)); long c_long = c.get_si();
Actually, there is no need to copy the coefficient before converting it (the above code was just an explanation). For efficiency, use an Integer& or write something like:
long c_long = c1.coefficient(Variable(0)).get_si();
Ciao, Enea.

Martin Rohde wrote:
I have (simple) a question about using PPL 0.4:
I cannot find how to convert a coefficient (of a constraint or generator) to the normal integer type. In my code I do the following; c1 is a constraint which is defined earlier in the code. Now I try to convert a coefficient with
c1_int = mpz_get_si(c1.coefficient(Variable(0)))
but this doesn't work, probably because the coefficient is not of the regular gmp format (c1_int is a standard integer)
How can I convert coefficients to 'normal' integers (or double integers)?
Dear Martin,
if you want to go the safe way (something I would highly recommend), you should use a fragment like the following (perhaps incapsulating this functionality into a convenience function):
const Integer& c1_coeff_0 = c1.coefficient(Variable(0)); if (c1_coeff_0.fits_sint_p()) { long c1_int = c1_coeff_0.get_si(); // Use c1_int. } else { // c1_coeff_0 exceeds the range of signed integers. // Do something else. }
The chapter of GMP's manual "C++ Class Interface" will tell you more about the operations supported by mpz_class (which is the same as Parma_Polyhedra_Library::Integer).
By the way, thanks to your message we have discovered that the user's manual does not give any indication that Parma_Polyhedra_Library::Integer is the same as GMP's mpz_class. This will be fixed in future releases. Thanks!
Roberto
participants (3)
-
Enea Zaffanella
-
Martin Rohde
-
Roberto Bagnara