0

I have the following C library: http://www.irisa.fr/polylib/

I would like to run its Domain union and Domain intersection functions in R.

Can someone show me the code to write in R or C to get it done fast?

The code source of the first function I'd like to use is:

/*
  * Return the intersection of two polyhedral domains 'Pol1' and 'Pol2'. 
* The maximum allowed rays in the new polyhedron generated is 'NbMaxRays'. 
*/
  Polyhedron *DomainIntersection(Polyhedron *Pol1,Polyhedron *Pol2,unsigned NbMaxRays) {

    Polyhedron *p1, *p2, *p3, *d;

    if (!Pol1 || !Pol2) return (Polyhedron*) 0;
    if (Pol1->Dimension != Pol2->Dimension) {
      errormsg1( "DomainIntersection", "diffdim",
                 "operation on different dimensions");
      return (Polyhedron*) 0;
    }

    /* For every polyhedron pair (p1,p2) where p1 belongs to domain Pol1 and */
      /* p2 belongs to domain Pol2, compute the intersection and add it to the */
      /* new domain 'd'.                                                       */
      d = (Polyhedron *)0;
      for (p1=Pol1; p1; p1=p1->next) {
        for (p2=Pol2; p2; p2=p2->next) {
          p3 = AddConstraints(p2->Constraint[0],
                              p2->NbConstraints, p1, NbMaxRays);      
          d = AddPolyToDomain(p3,d);
        }
      }
      if (!d)
        return Empty_Polyhedron(Pol1->Dimension);
      else
        return d;

  } /* DomainIntersection */

Is there a way to create an R function similarly to the following?

add <- cfunction(c(a = "integer", b = "integer"), "
  SEXP result = PROTECT(allocVector(REALSXP, 1));
  REAL(result)[0] = asReal(a) + asReal(b);
  UNPROTECT(1);

  return result;
")
 add(1, 5) 
Simon
  • 349
  • 2
  • 12
  • Asking for book/tutorial recommendations is off-topic on Stack Overflow. This might help: https://adv-r.hadley.nz/, or, better yet, http://adv-r.had.co.nz/C-interface.html – John Coleman Dec 22 '19 at 12:47
  • Thanks. I hope I can find a turnkey solution, since it is quite a standard procedure and I have never used C. – Simon Dec 22 '19 at 12:49
  • 2
    To call a C function directly is non-trivial since you need to properly translate between R data structures and C. You could of course call a C program from the shell and get its response, but that would incur the overhead of translating the input to strings, invoking the function, getting its response, and parsing it. – John Coleman Dec 22 '19 at 12:53
  • OK so I assume there is no convenient way to use those functions in my R code, even though my input data would be simple matrices? – Simon Dec 22 '19 at 12:54
  • There is a tremendous amount of material online if you search for "Calling C function from R" for example https://www.r-bloggers.com/three-ways-to-call-cc-from-r/ . Maybe some of those ways are convenient for you, maybe not. – John Coleman Dec 22 '19 at 12:56
  • For what I've just seen, you would need to adapt the C functions to R's calling and return conventions. The easiest way would be to use the [`.C` interface](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Interface-functions-_002eC-and-_002eFortran). And change the calls to `malloc` to R's [memory management API](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Memory-allocation). – Rui Barradas Dec 22 '19 at 13:02
  • I have updated my question so that it is more specific – Simon Dec 22 '19 at 13:33
  • 2
    You could use the Rcpp package to write converters between the C data structures and corresponding R data structures, c.f. https://stackoverflow.com/q/51110244/8416610. – Ralf Stubner Dec 23 '19 at 16:15

0 Answers0