Rcpp Version 1.0.9
RcppSimpleExample.r
Go to the documentation of this file.
1 #!/usr/bin/env r
2 
3 
4 suppressMessages(library(Rcpp))
5 suppressMessages(library(inline))
6 
7 
8 foo <- '
9  int i, j, na, nb, nab;
10  double *xa, *xb, *xab;
11  SEXP ab;
12 
13  PROTECT(a = AS_NUMERIC(a));
14  PROTECT(b = AS_NUMERIC(b));
15  na = LENGTH(a); nb = LENGTH(b); nab = na + nb - 1;
16  PROTECT(ab = NEW_NUMERIC(nab));
17  xa = NUMERIC_POINTER(a); xb = NUMERIC_POINTER(b);
18  xab = NUMERIC_POINTER(ab);
19  for(i = 0; i < nab; i++) xab[i] = 0.0;
20  for(i = 0; i < na; i++)
21  for(j = 0; j < nb; j++) xab[i + j] += xa[i] * xb[j];
22  UNPROTECT(3);
23  return(ab);
24 '
25 
26 funx <- cfunction(signature(a="numeric",b="numeric"), foo, Rcpp=FALSE, verbose=FALSE)
27 funx(a=1:20, b=2:11)