EVOLUTION-MANAGER
Edit File: cfunction.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Inline C, C++, Fortran function calls from R</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for cfunction {inline}"><tr><td>cfunction {inline}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Inline C, C++, Fortran function calls from R </h2> <h3>Description</h3> <p>Functionality to dynamically define R functions and S4 methods with in-lined C, C++ or Fortran code supporting .C and .Call calling conventions. </p> <h3>Usage</h3> <pre> cfunction(sig=character(), body=character(), includes=character(), otherdefs=character(), language=c("C++", "C", "Fortran", "F95", "ObjectiveC", "ObjectiveC++"), verbose=FALSE, convention=c(".Call", ".C", ".Fortran"), Rcpp=FALSE, cppargs=character(), cxxargs=character(), libargs=character(), dim=NULL, implicit=NULL, module=NULL) ## S4 methods for signatures # f='character', sig='list', body='list' # f='character', sig='character', body='character' setCMethod(f, sig, body, ...) ## Further arguments: # setCMethod(f, sig, body, includes="", otherdefs="", cpp=TRUE, # verbose=FALSE, where=topenv(.GlobalEnv), ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>f</code></td> <td> <p>A single character value if <code>sig</code> and <code>body</code> are character vectors or a character vector of the same length and the length of <code>sig</code> or <code>body</code> with the name(s) of methods to create.</p> </td></tr> <tr valign="top"><td><code>sig</code></td> <td> <p>A match of formal argument names for the function with the character-string names of corresponding classes. Alternatively, a list of such character vectors. </p> </td></tr> <tr valign="top"><td><code>body</code></td> <td> <p> A character vector with C, C++ or Fortran code omitting function declaration (only the body, i.e. in case of C starting after the function opening curly bracket and ending before the closing curly bracket, brackets excluded). In case of <code>setCMethod</code> with signature <code>list</code> – a list of such character vectors. </p> </td></tr> <tr valign="top"><td><code>includes</code></td> <td> <p> A character vector of additional includes and preprocessor statements etc that will be put between the R includes and the user function(s).</p> </td></tr> <tr valign="top"><td><code>otherdefs</code></td> <td> <p> A characted vector with the code for any further definitions of functions, classes, types, forward declarations, namespace usage clauses etc which is inserted between the includes and the declarations of the functions defined in <code>sig</code>.</p> </td></tr> <tr valign="top"><td><code>language</code></td> <td> <p> A character value that specifies the source language of the inline code. The possible values for <code>language</code> include all those supported by <code>R CMD SHLIB</code> on any platform, which are currently C, C++, Fortran, F95, ObjectiveC and ObjectiveC++; they may not all be supported on your platform. One can specify the language either in full as above, or using any of the following case insensitive shortened forms: <code>c, cpp, c++, f, f95, objc, objcpp, objc++</code>. Defaults to <code>C++</code>.</p> </td></tr> <tr valign="top"><td><code>verbose</code></td> <td> <p> If <code>TRUE</code> prints the compilation output, the source code of the resulting program and the definitions of all declared methods. If <code>FALSE</code>, the function is silent, but it prints compiler warning and error messages and the source code if compilation fails. </p> </td></tr> <tr valign="top"><td><code>convention</code></td> <td> <p> Which calling convention to use? See the Details section.</p> </td></tr> <tr valign="top"><td><code>Rcpp</code></td> <td> <p>If <code>TRUE</code> adds inclusion of <code>Rcpp.h</code> to <code>includes</code>, also queries the <code>Rcpp</code> package about the location of header and library files and sets environment variables <code>PKG_CXXFLAGS</code> and <code>PKG_LIBS</code> accordingly so that the R / C++ interface provided by the <code>Rcpp</code> package can be used. Default value is <code>FALSE</code>.</p> </td></tr> <tr valign="top"><td><code>cppargs</code></td> <td> <p>Optional character vector of tokens to be passed to the compiler via the <code>PKG_CPPFLAGS</code> environment variable. Elements should be fully formed as for example <code>c("-I/usr/local/lib/foo", "-DDEBUG")</code> and are passed along verbatim.</p> </td></tr> <tr valign="top"><td><code>cxxargs</code></td> <td> <p>Optional character vector of tokens to be passed to the compiler via the <code>PKG_CXXFLAGS</code> environment variable. Elements should be fully formed as for example <code>c("-I/usr/local/lib/foo", "-DDEBUG")</code> and are passed along verbatim.</p> </td></tr> <tr valign="top"><td><code>libargs</code></td> <td> <p>Optional character vector of tokens to be passed to the compiler via the <code>PKG_LIBS</code> environment variable. Elements should be fully formed as for example <code>c("-L/usr/local/lib/foo -lfoo", "--lpthread")</code> and are passed along verbatim.</p> </td></tr> <tr valign="top"><td><code>dim</code></td> <td> <p>Optional character vector defining the dimensionality of the function arguments. Of same length as <code>sig</code>. Fortran or F95 only.</p> </td></tr> <tr valign="top"><td><code>implicit</code></td> <td> <p>A character vector defining the implicit declaration in Fortran or F95; the default is to use the implicit typing rules for Fortran, which is <code>integer</code> for names starting with the letters <code>I</code> through <code>N</code>, and <code>real</code> for names beginning with any other letter. As <code>R</code> passes double precision, this is not the best choice. Safest is to choose <code>implicit = "none"</code> which will require all names in the subroutine to be explicitly declared.</p> </td></tr> <tr valign="top"><td><code>module</code></td> <td> <p>Name(s) of any modules to be used in the <code>Fortran</code> or <code>F95</code> subroutine.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p> Reserved.</p> </td></tr> </table> <h3>Details</h3> <p>To declare multiple functions in the same library one can use <code>setCMethod</code> supplying lists of signatures and implementations. In this case, provide as many method names in <code>f</code> as you define methods. Avoid clashes when selecting names of the methods to declare, i.e. if you provide the same name several times you must ensure that signatures are different but can share the same generic! </p> <p>The source code in the <code>body</code> should not include the header or "front-matter" of the function or the close, e.g. in C or C++ it must start after the C-function opening curly bracket and end before the C-function closing curly bracket, brackets should not be included. The header will be automatically generated from the R-<code>signature</code> argument. Arguments will will carry the same name as used in the signature, so avoid variable names that are not legal in the target language (e.g. names with dots). </p> <p>C/C++: If <code>convention == ".Call"</code> (the default), the <code><a href="../../base/html/CallExternal.html">.Call</a></code> mechanism is used and its result is returned directly as the result of the call of the generated function. As the last line of the generated C/C++ code a <code>return R_NilValue;</code> is added in this case and a warning is generated in case the user has forgotten to provide a return value. To suppress the warning and still return NULL, add <code>return R_NilValue;</code> explicitly. </p> <p>Special care is needed with types, memory allocation and protection – exactly the same as if the code was not inline: see the Writing R Extension manual for information on <code><a href="../../base/html/CallExternal.html">.Call</a></code>. </p> <p>If <code>convention == ".C"</code> or <code>convention == ".Fortran"</code>, the <code><a href="../../base/html/Foreign.html">.C</a></code> or <code><a href="../../base/html/Foreign.html">.Fortran</a></code> mechanism respectively is used, and the return value is a list containing all arguments. </p> <p>Attached R includes include <code>R.h</code> for <code>".C"</code>, and additionally <code>Rdefines.h</code> and <code>R_ext\Error.h</code> for <code>".Call"</code>. </p> <h3>Value</h3> <p>If <code>sig</code> is a single character vector, <code>cfunction</code> returns a single <code><a href="../../base/html/function.html">function</a></code>; if it is a list, it returns a list of functions. </p> <p><code>setCMethod</code> declares new methods with given names and signatures and returns invisible <code>NULL</code>. </p> <h3>Author(s)</h3> <p>Oleg Sklyar, Duncan Murdoch, Mike Smith, Dirk Eddelbuettel </p> <h3>See Also</h3> <p><code> <a href="../../base/html/Foreign.html">Foreign</a> Function Interface </code></p> <h3>Examples</h3> <pre> x <- as.numeric(1:10) n <- as.integer(10) ## Not run: ## A simple Fortran example - n and x: assumed-size vector code <- " integer i do 1 i=1, n(1) 1 x(i) = x(i)**3 " cubefn <- cfunction(signature(n="integer", x="numeric"), code, convention=".Fortran") print(cubefn) cubefn(n, x)$x ## Same Fortran example - now n is one number code2 <- " integer i do 1 i=1, n 1 x(i) = x(i)**3 " cubefn2 <- cfunction(signature(n="integer", x="numeric"), implicit = "none", dim = c("", "(*)"), code2, convention=".Fortran") cubefn2(n, x)$x ## Same in F95, now x is fixed-size vector (length = n) code3 <- "x = x*x*x" cubefn3 <- cfunction(sig = signature(n="integer", x="numeric"), implicit = "none", dim = c("", "(n)"), code3, language="F95") cubefn3(20, 1:20) print(cubefn3) ## Same example in C code4 <- " int i; for (i = 0; i < *n; i++) x[i] = x[i]*x[i]*x[i]; " cubefn4 <- cfunction(signature(n="integer", x="numeric"), code4, language = "C", convention = ".C") cubefn4(20, 1:20) ## End(Not run) ## use of a module in F95 modct <- "module modcts double precision, parameter :: pi = 3.14159265358979 double precision, parameter :: e = 2.71828182845905 end" getconstants <- "x(1) = pi x(2) = e" cgetcts <- cfunction(getconstants, module = "modcts", implicit = "none", includes = modct, sig = c(x = "double"), dim = c("(2)"), language = "F95") cgetcts(x = 1:2) print(cgetcts) ## Use of .C convention with C code ## Defining two functions, one of which calls the other sigSq <- signature(n="integer", x="numeric") codeSq <- " for (int i=0; i < *n; i++) { x[i] = x[i]*x[i]; }" sigQd <- signature(n="integer", x="numeric") codeQd <- " squarefn(n, x); squarefn(n, x); " fns <- cfunction( list(squarefn=sigSq, quadfn=sigQd), list(codeSq, codeQd), convention=".C") squarefn <- fns[["squarefn"]] quadfn <- fns[["quadfn"]] squarefn(n, x)$x quadfn(n, x)$x ## Alternative declaration using 'setCMethod' setCMethod(c("squarefn", "quadfn"), list(sigSq, sigQd), list(codeSq, codeQd), convention=".C") squarefn(n, x)$x quadfn(n, x)$x ## Use of .Call convention with C code ## Multyplying each image in a stack with a 2D Gaussian at a given position code <- " SEXP res; int nprotect = 0, nx, ny, nz, x, y; PROTECT(res = Rf_duplicate(a)); nprotect++; nx = INTEGER(GET_DIM(a))[0]; ny = INTEGER(GET_DIM(a))[1]; nz = INTEGER(GET_DIM(a))[2]; double sigma2 = REAL(s)[0] * REAL(s)[0], d2 ; double cx = REAL(centre)[0], cy = REAL(centre)[1], *data, *rdata; for (int im = 0; im < nz; im++) { data = &(REAL(a)[im*nx*ny]); rdata = &(REAL(res)[im*nx*ny]); for (x = 0; x < nx; x++) for (y = 0; y < ny; y++) { d2 = (x-cx)*(x-cx) + (y-cy)*(y-cy); rdata[x + y*nx] = data[x + y*nx] * exp(-d2/sigma2); } } UNPROTECT(nprotect); return res; " funx <- cfunction(signature(a="array", s="numeric", centre="numeric"), code) x <- array(runif(50*50), c(50,50,1)) res <- funx(a=x, s=10, centre=c(25,15)) if (interactive()) image(res[,,1]) ## Same but done by registering an S4 method setCMethod("funy", signature(a="array", s="numeric", centre="numeric"), code, verbose=TRUE) res <- funy(x, 10, c(35,35)) if (interactive()) { x11(); image(res[,,1]) } </pre> <hr /><div style="text-align: center;">[Package <em>inline</em> version 0.3.15 <a href="00Index.html">Index</a>]</div> </body></html>