[PPL-devel] C interface

Roberto Bagnara bagnara at cs.unipr.it
Fri Feb 10 08:26:27 CET 2006


Lambert Simon wrote:
> I use the C interface of the Parma Polyhedra Library but I have some
> problems to build a linear expression. I want to build the linear expression
> "x_1 + 5x_3 - 100 >= 0" for example but I do not arrive.

Dear Simon,

I guess you meant `I want to build the linear expression
"x_1 + 5x_3 - 100"'.

> I do "ppl_new_Linear_Expression _with_dimension(&ple, (ppl_dimension_type)
> 4);" and after I want to add the values to the variables by using the
> "ppl_Linear_Expression_add_to_coefficient(ple, coefficient_index[i],
> ppl_coeff);" function but that doesn't go.
> 
> I don't understand how we can build a linear expression just using this 2
> functions.

Why 2?  You are overlooking several functions that allow building linear
expressions: you will find them all in the user's manual (pages 34 and 35
of ppl-user-0.8.pdf).

> Can you help me by giving me the code to build this linear expression with
> the C interface and to explain it ?

I attach under the signature some example code doing what you want.
Use it as follows:

$ gcc sample.c -lppl_c -lppl -lgmpxx -lgmp
$ ./a.out
x_1 + 5*x_3 - 100

If, after reading the manual, you need further advice or explanations,
please come back to us.
All the best,

     Roberto

-- 
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara at cs.unipr.it

#include <ppl_c.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

static mpz_t tmp_z;

static void
init_globals() {
   mpz_init(tmp_z);
}

static void
deinit_globals() {
   mpz_clear(tmp_z);
}

static int
assign_Coefficient_from_int(ppl_Coefficient_t dst, int src) {
   mpz_set_si(tmp_z, src);
   return ppl_assign_Coefficient_from_mpz_t(dst, tmp_z);
}

static const char*
my_variable_output_function(ppl_dimension_type d) {
   static char buffer[20];
   int r = sprintf(buffer, "x_%d", d+1);
   if (r < 0)
     return 0;
   else if (r >= 19) {
     errno = ERANGE;
     return 0;
   }
   return buffer;
}

int
main() {
   int x_1 = 0;
   /* int x_2 = 1; */
   int x_3 = 2;
   /* int x_4 = 3; */
   ppl_Coefficient_t coeff;
   ppl_Linear_Expression_t le;

   ppl_initialize();
   init_globals();
   ppl_new_Coefficient(&coeff);
   ppl_new_Linear_Expression_with_dimension(&le, 4);

   /* le += 1*x_1 */
   assign_Coefficient_from_int(coeff, 1);
   ppl_Linear_Expression_add_to_coefficient(le, x_1, coeff);
   /* le += 5*x_3 */
   assign_Coefficient_from_int(coeff, 5);
   ppl_Linear_Expression_add_to_coefficient(le, x_3, coeff);
   /* le += -100 */
   assign_Coefficient_from_int(coeff, -100);
   ppl_Linear_Expression_add_to_inhomogeneous(le, coeff);

   if (ppl_io_set_variable_output_function(my_variable_output_function) < 0) {
     fprintf(stderr, "cannot install the custom variable output function\n");
     exit(1);
   }

   ppl_io_print_Linear_Expression(le);

   ppl_delete_Linear_Expression(le);
   ppl_delete_Coefficient(coeff);
   deinit_globals();
   ppl_finalize();
   return 0;
}



More information about the PPL-devel mailing list