[Rcpp-commits] r3929 - in pkg/Rcpp: . inst/examples inst/examples/Attributes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Nov 10 20:33:54 CET 2012
Author: jjallaire
Date: 2012-11-10 20:33:54 +0100 (Sat, 10 Nov 2012)
New Revision: 3929
Added:
pkg/Rcpp/inst/examples/Attributes/
pkg/Rcpp/inst/examples/Attributes/Depends.cpp
pkg/Rcpp/inst/examples/Attributes/Export.cpp
pkg/Rcpp/inst/examples/Attributes/cppFunction.R
pkg/Rcpp/inst/examples/Attributes/sourceCpp.R
Modified:
pkg/Rcpp/ChangeLog
Log:
add some simple examples for attributes and related functions
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2012-11-10 15:31:53 UTC (rev 3928)
+++ pkg/Rcpp/ChangeLog 2012-11-10 19:33:54 UTC (rev 3929)
@@ -1,6 +1,10 @@
2012-11-10 JJ Allaire <jj at rstudio.org>
* src/Attributes.cpp: add optional hook for inclusion of package types
+ * examples/Attributes/Export.cpp: example use of Rcpp::export
+ * examples/Attributes/Depends.cpp: example use of Rcpp::depends
+ * examples/Attributes/sourceCpp.R: example use of sourceCpp()
+ * examples/Attributes/cppFunction.R: example use of cppFunction()
2012-11-09 Romain Francois <romain at r-enthusiasts.com>
Added: pkg/Rcpp/inst/examples/Attributes/Depends.cpp
===================================================================
--- pkg/Rcpp/inst/examples/Attributes/Depends.cpp (rev 0)
+++ pkg/Rcpp/inst/examples/Attributes/Depends.cpp 2012-11-10 19:33:54 UTC (rev 3929)
@@ -0,0 +1,28 @@
+
+// [[Rcpp::depends(RcppArmadillo)]]
+
+#include <RcppArmadillo.h>
+
+using namespace Rcpp;
+
+// [[Rcpp::export]]
+List fastLm(NumericVector yr, NumericMatrix Xr) {
+
+ int n = Xr.nrow(), k = Xr.ncol();
+
+ arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
+ arma::colvec y(yr.begin(), yr.size(), false);
+
+ arma::colvec coef = arma::solve(X, y); // fit model y ~ X
+ arma::colvec resid = y - X*coef; // residuals
+
+ double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
+ // std.error of estimate
+ arma::colvec stderrest = arma::sqrt(
+ sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
+
+ return List::create(Named("coefficients") = coef,
+ Named("stderr") = stderrest
+ );
+}
+
Added: pkg/Rcpp/inst/examples/Attributes/Export.cpp
===================================================================
--- pkg/Rcpp/inst/examples/Attributes/Export.cpp (rev 0)
+++ pkg/Rcpp/inst/examples/Attributes/Export.cpp 2012-11-10 19:33:54 UTC (rev 3929)
@@ -0,0 +1,40 @@
+
+#include <Rcpp.h>
+
+using namespace Rcpp;
+
+// [[Rcpp::export]]
+int fibonacci(const int x) {
+
+ if (x == 0) return(0);
+ if (x == 1) return(1);
+
+ return (fibonacci(x - 1)) + fibonacci(x - 2);
+}
+
+
+// [[Rcpp::export("convolveCpp")]]
+NumericVector convolve(NumericVector a, NumericVector b) {
+
+ int na = a.size(), nb = b.size();
+ int nab = na + nb - 1;
+ NumericVector xab(nab);
+
+ for (int i = 0; i < na; i++)
+ for (int j = 0; j < nb; j++)
+ xab[i + j] += a[i] * b[j];
+
+ return xab;
+}
+
+
+// [[Rcpp::export]]
+List lapplyCpp(List input, Function f) {
+
+ List output(input.size());
+
+ std::transform(input.begin(), input.end(), output.begin(), f);
+ output.names() = input.names();
+
+ return output;
+}
\ No newline at end of file
Added: pkg/Rcpp/inst/examples/Attributes/cppFunction.R
===================================================================
--- pkg/Rcpp/inst/examples/Attributes/cppFunction.R (rev 0)
+++ pkg/Rcpp/inst/examples/Attributes/cppFunction.R 2012-11-10 19:33:54 UTC (rev 3929)
@@ -0,0 +1,43 @@
+
+library(Rcpp)
+
+cppFunction('
+ NumericVector convolveCpp(NumericVector a, NumericVector b) {
+
+ int na = a.size(), nb = b.size();
+ int nab = na + nb - 1;
+ NumericVector xab(nab);
+
+ for (int i = 0; i < na; i++)
+ for (int j = 0; j < nb; j++)
+ xab[i + j] += a[i] * b[j];
+
+ return xab;
+ }
+')
+
+convolveCpp(c(1,2,3), matrix(3,3))
+
+
+cppFunction(depends='RcppArmadillo', code='
+ List fastLm(NumericVector yr, NumericMatrix Xr) {
+
+ int n = Xr.nrow(), k = Xr.ncol();
+
+ arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids copy
+ arma::colvec y(yr.begin(), yr.size(), false);
+
+ arma::colvec coef = arma::solve(X, y); // fit model y ~ X
+ arma::colvec resid = y - X*coef; // residuals
+
+ double sig2 = arma::as_scalar( arma::trans(resid)*resid/(n-k) );
+ // std.error of estimate
+ arma::colvec stderrest = arma::sqrt(
+ sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
+
+ return List::create(Named("coefficients") = coef,
+ Named("stderr") = stderrest
+ );
+}
+')
+
Added: pkg/Rcpp/inst/examples/Attributes/sourceCpp.R
===================================================================
--- pkg/Rcpp/inst/examples/Attributes/sourceCpp.R (rev 0)
+++ pkg/Rcpp/inst/examples/Attributes/sourceCpp.R 2012-11-10 19:33:54 UTC (rev 3929)
@@ -0,0 +1,10 @@
+
+library(Rcpp)
+
+sourceCpp("Export.cpp")
+fibonacci(5)
+
+
+sourceCpp("Depends.cpp")
+fastLm(c(1,2,3), matrix(3,3))
+
More information about the Rcpp-commits
mailing list