# An Example-Driven Hands-On Introduction to Rcpp

Save this PDF as:

Size: px
Start display at page:

## Transcription

1 Why What When Where How Examples Arma Doc An Example-Driven Hands-On Introduction to Rcpp Dr. @eddelbuettel user! 2014 Pre-conference Tutorial UCLA, 30 June 2014

2 Why What When Where How Examples Arma Doc R C++ Vision Features Outline 1 Why R C++ Vision Features

3 Why What When Where How Examples Arma Doc R C++ Vision Features Why R? Programming with Data Chambers, Computational Methods for Data Analysis. Wiley, Becker, Chambers, Chambers and and Wilks. The Hastie. Statistical New S Language. Models in S. Chapman & Hall, Chapman & Hall, Chambers. Programming with Data. Springer, Thanks to John Chambers for sending me high-resolution scans of the covers of his books. Chambers. Software for Data Analysis: Programming with R. Springer, 2008

4 Why What When Where How Examples Arma Doc R C++ Vision Features Why R? Succinct and expressive density.default(x = xx) xx <- faithful[,"eruptions"] fit <- density(xx) plot(fit) Density N = 272 Bandwidth =

5 Why What When Where How Examples Arma Doc R C++ Vision Features Why R? Succinct and expressive xx <- faithful[,"eruptions"] density.default(x = xx) fit1 <- density(xx) fit2 <- replicate(10000, { x <- sample(xx,replace=true); density(x, from=min(fit1\$x), to=max(fit1\$x))\$y ) fit3 <- apply(fit2, 1, quantile,c(0.025,0.975)) plot(fit1, ylim=range(fit3)) polygon(c(fit1\$x,rev(fit1\$x)), c(fit3[1,], rev(fit3[2,])), col='grey', border=f) lines(fit1) Density The example was posted by Greg Snow on r-help a few years ago. N = 272 Bandwidth =

6 Why What When Where How Examples Arma Doc R C++ Vision Features Why R? Interactive R enables us to work interactively explore and visualize data access, retrieve and/or generate data summarize and report into pdf, html,... making it a preferred environment for many data analysts.

7 Why What When Where How Examples Arma Doc R C++ Vision Features Why R? Extensible R has always been extensible via C via a bare-bones interface described in Writing R Extensions Fortran which is also used internally by R Java via rjava by S Urbanek C++ but essentially at the bare-bones level of C So in theory this worked yet tedious in practice.

8 Why What When Where How Examples Arma Doc R C++ Vision Features Why C++? Asking Google [currently] leads to 64,200,000 hits. Wikipedia: C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose, powerful programming language. C++ is industrial-strength, vendor-independent, widely-used, and still evolving. In science & research, one of the most frequently-used languages: If there is something you want to use / connect to, it probably has a C/C++ API. As a widely used language it also has good tool support (debuggers, profilers, code analysis).

9 Why What When Where How Examples Arma Doc R C++ Vision Features Why C++? Scott Meyers: View C++ as a federation of languages C provides a rich inheritance and interoperability as Unix, Windows,... are all build on C. Object-Oriented C++ just to provide endless discussions about exactly what OO is or should be. Templated C++ which is mighty powerful; template meta programming unequalled in other languages. The STL which is a specific template library which is powerful but has its own conventions. C++11 adds enough to be called a fifth language.

10 Why What When Where How Examples Arma Doc R C++ Vision Features Why C++? Mature yet current Strong performance focus: You don t pay for what you don t use Leave no room for a language between the machine level and C++ Yet also powerfully abtract and high-level C++11 and beyond are a big deal giving us new language features While there are complexities, Rcpp users are mostly shielded

11 Why What When Where How Examples Arma Doc R C++ Vision Features Interface Vision Source: John Chambers, personal communication.

12 Why What When Where How Examples Arma Doc R C++ Vision Features Interface Vision Use trusted numerical libraries (mostly/exclusively written in Fortran) Provide environment which statistician could use more easily Enable interactive and iterative data exploration Make it extensibilty for research into statistical methods C.f. John Chambers (2008) regarding Mission and Directive

13 Why What When Where How Examples Arma Doc R C++ Vision Features Interface Vision R offers us the best of both worlds: Compiled code with Access to proven libraries and algorithms in C/C++/Fortran Extremely high performance (in both serial and parallel modes) Interpeted code with An accessible high-level language made for Programming with Data An interactive workflow for data analysis Support for rapid prototyping, research, and experimentation

14 Why What When Where How Examples Arma Doc R C++ Vision Features Why Rcpp? Easy to learn it really does not have to be that complicated we will look at a few examples Easy to use as it avoids build and OS system complexities thanks to the R infrastrucure Expressive it allows for vectorised C++ using Rcpp Sugar Seamless access to all R objects: vector, matrix, list, S3/S4/RefClass, Environment, Function,... Speed gains for a variety of tasks Rcpp excels precisely where R struggles: loops, function calls,... Extensions greatly facilitates access to external libraries using eg Rcpp modules

15 Why What When Where How Examples Arma Doc R API C++ Outline 2 What R API C++

16 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Everything evolves around.call At the C++ level: SEXP foo(sexp a, SEXP b, SEXP C,...) and at the R level: res <-.Call("foo", a, b, c,..., PACKAGE="mypkg")

17 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Seamless interchange of R objects: C API of R #include <R.h> #include <Rdefines.h> SEXP convolve2(sexp a, SEXP b) { int i, j, na, nb, nab; double *xa, *xb, *xab; SEXP ab; PROTECT(a = AS_NUMERIC(a)); PROTECT(b = AS_NUMERIC(b)); na = LENGTH(a); nb = LENGTH(b); nab = na + nb - 1; PROTECT(ab = NEW_NUMERIC(nab)); xa = NUMERIC_POINTER(a); xb = NUMERIC_POINTER(b); xab = NUMERIC_POINTER(ab); for(i = 0; i < nab; i++) xab[i] = 0.0; for(i = 0; i < na; i++) for(j = 0; j < nb; j++) xab[i + j] += xa[i] * xb[j]; UNPROTECT(3); return(ab);

18 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Seamless interchange of R objects: Rcpp version #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] 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;

19 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Seamless interchange of R objects Any R object can be passed down to C++ code: vectors, matrices, list,... But also functions, environments and more. This includes S3 and S4 objects as well as Reference Classes. Object attributes can be accessed directly. Objects can be created at the C++ level, and the R garbage collector does the right thing as if were an R-created object.

20 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Seamless use of RNGs set.seed(42); runif(5) ## [1] cppfunction(' NumericVector r1(int n) { NumericVector x(n); for (int i=0; i<n; i++) x[i] = R::runif(0,1); return(x); ') set.seed(42); r1(5) ## [1] cppfunction('numericvector r2(int n) { return runif(n,0,1); ') set.seed(42); r2(5) ## [1]

21 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Sugar: R version pir <- function(n) { x <- runif(n) y <- runif(n) d <- sqrt(x^2 + y^2) return(4 * sum(d <= 1.0) / N)

22 Why What When Where How Examples Arma Doc R API C++ What can Rcpp do? Sugar: C++ version #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double pisugar(const int N) { NumericVector x = runif(n); NumericVector y = runif(n); NumericVector d = sqrt(x*x + y*y); return 4.0 * sum(d <= 1.0) / N;

23 Why What When Where How Examples Arma Doc A First Example A Second Example Outline 3 When A First Example A Second Example

24 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: An Introductory Example Consider a function defined as f (n) such that { n when n < 2 f (n 1) + f (n 2) when n 2

25 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: Simple R Implementation fibr <- function(n) { if (n < 2) return(n) return(fibr(n-1) + fibr(n-2)) ## Using it on first 11 arguments sapply(0:10, fibr) ## [1]

26 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: Timing R Implementation benchmark(fibr(10),fibr(15),fibr(20))[,1:4] ## test replications elapsed relative ## 1 fibr(10) ## 2 fibr(15) ## 3 fibr(20)

27 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: C++ Implementation cppfunction(" int fibcpp(int n) { if (n < 2) return(n); return(fibcpp(n-1) + fibcpp(n-2)); ") ## Using it on first 11 arguments sapply(0:10, fibcpp) ## [1]

28 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: Putting it all together fibr <- function(n) { if (n<2) return(n) return(fibr(n-1) + fibr(n-2)) cppfunction('int fibcpp(int n) { if (n<2) return n; return fibcpp(n-2) + fibcpp(n-1); ') benchmark(fibr(25), fibcpp(25), order="relative")[,1:4] ## test replications elapsed relative ## 2 fibcpp(25) ## 1 fibr(25)

29 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup:: VAR(1) Simulation Let s consider a simple possible VAR(1) system of k variables. For k = 2: X t = X t 1 B + E t where X t is a row vector of length 2, B is a 2 by 2 matrix and E t is a row of the error matrix of 2 columns.

30 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup:: VAR(1) Simulation In R code, given both the coefficient and error matrices (revealing k and n): rsim <- function(b,e) { X <- matrix(0,nrow(e), ncol(e)) for (r in 2:nrow(E)) { X[r,] = X[r-1, ] %*% B + E[r, ] return(x)

31 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? Easy speedup: VAR(1) Simulation cppfunction('arma::mat cppsim(arma::mat B, arma::mat E) int m = E.n_rows; int n = E.n_cols; arma::mat X(m,n); X.row(0) = arma::zeros<arma::mat>(1,n); for (int r=1; r<m; r++) { X.row(r) = X.row(r-1) * B + E.row(r); return X; ', depends="rcpparmadillo") a <- matrix(c(0.5,0.1,0.1,0.5),nrow=2) e <- matrix(rnorm(10000),ncol=2) benchmark(cppsim(a,e), rsim(a,e), order="relative")[,1:4] ## test replications elapsed relative ## 1 cppsim(a, e) ## 2 rsim(a, e)

32 Why What When Where How Examples Arma Doc A First Example A Second Example When do we use Rcpp? New things: Easy access to C/C++ libraries Sometimes speed is not the only reason C and C++ provide a enormous amount of libraries and APIs we may want to use Easy to provide access to as Rcpp eases data transfer to/from R Rcpp modules can make it even easier

33 Why What When Where How Examples Arma Doc Outline 4 Where

34 Why What When Where How Examples Arma Doc Where is Rcpp being used? Numbers as of June 2014 Rcpp is used by 229 packages on CRAN used by another 27 package on BioConductor cited 105 times (Google Scholar count for 2011 JSS paper)

35 Why What When Where How Examples Arma Doc Where is Rcpp being used? Several well-known packages Amelia Gary King et al: Multiple Imputation; uses Rcpp and RcppArmadillo forecast Rob Hyndman et al: (Automated) Time-series forecasting; uses Rcpp and RcppArmadillo RStan Andrew Gelman et al: Bayesian models / MCMC rugarch Alexios Ghalanos: Sophisticated financial models; using Rcpp and RcppArmadillo lme4 Doug Bates et al: Hierarchical/Mixed Linear Models; uses Rcpp and RcppEigen. dplyr, bigviz,... Hadley Wickham: Data munging; high-dim. visualization for million obs.

36 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton Outline 5 How Setup evalcpp cppfunction sourcecpp skeleton

37 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Uses only standard R tools to build packages Depending on the platform, one needs Windows the Rtools kit for Windows, properly installed see CRAN, the Installation manual and many tutorials; the installr package may help OS X the Xcode command-line tools (plus possibly the Fortran compiler) see Simon s pages Linux generally just work out of the box Several environments can be used to work with Rcpp RStudio is very popular. No additional requirements for Rcpp beyond being able to compile R packages.

38 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Easy to test ## evaluate a C++ expression, retrieve result evalcpp("2 + 2") ## [1] 4 ## a little fancier evalcpp("std::numeric_limits<double>::max()") ## [1] 1.798e+308 ## create ad-hoc R function 'square' cppfunction('int square(int x) { return x*x;') square(7l) ## [1] 49

39 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Basic Usage: evalcpp evalcpp() evaluates a single C++ expression. Includes and dependencies can be declared. This allows us to quickly check C++ constructs. evalcpp("2 * M_PI") ## [1] 6.283

40 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Basic Usage: cppfunction() cppfunction() creates, compiles and links a C++ file, and creates an R function to access it. cppfunction(" int usecpp11() { auto x = 10; return x; ", plugins=c("cpp11")) usecpp11() # same identifier as C++ function ## [1] 10

41 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Basic Usage: sourcecpp() sourcecpp() is the actual workhorse behind evalcpp() and cppfunction(). It is described in more detail in the package vignette Rcpp-attributes. A key feature are the plugins and dependency options: other packages can provide a plugin to supply require compile-time parameters (cf RcppArmadillo, RcppEigen, RcppGSL). We have also provided plugins for other compiler features. These allow to enable support for C++11 (and beyond), as well as for OpenMP.

42 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? Basic Usage: Rcpp.package.skeleton() To create a complete and working package, the Rcpp.package.skeleton() function can be used. It extends the base R function package.skeleton() and supports the same set of options. For Rcpp use is also supports (via additional options) Rcpp Modules and Rcpp Attributes both of which can be included with working examples The vignette Rcpp-package has complete details.

43 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? RStudio makes it very easy: Single File

44 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? RStudio example cont ed The following file gets created: #include <Rcpp.h> using namespace Rcpp; // Below is a simple example of exporting a C++ function to R. // You can source this function into an R session using the // Rcpp::sourceCpp function (or via the Source button on the // editor toolbar) // For more on using Rcpp click the Help button on the editor // toolbar // [[Rcpp::export]] int timestwo(int x) { return x * 2;

45 Why What When Where How Examples Arma Doc Setup evalcpp cppfunction sourcecpp skeleton How do we use Rcpp? RStudio makes it very easy: Package

46 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Outline 6 Examples CumSum R Fun Boost Subset CtoC++ xts XPtr

47 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Cumulative Sum A basic looped version: #include <Rcpp.h> #include <numeric> using namespace Rcpp; // for std::partial_sum // [[Rcpp::export]] NumericVector cumsum1(numericvector x){ // initialize an accumulator variable double acc = 0; // initialize the result vector NumericVector res(x.size()); for(int i = 0; i < x.size(); i++){ acc += x[i]; res[i] = acc; return res;

48 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Cumulative Sum An STL variant: // [[Rcpp::export]] NumericVector cumsum2(numericvector x){ // initialize the result vector NumericVector res(x.size()); std::partial_sum(x.begin(), x.end(), res.begin()); return res;

49 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Cumulative Sum Or just Rcpp sugar: // [[Rcpp::export]] NumericVector cumsum3(numericvector x){ return cumsum(x); // compute + return result vector Of course, all results are the same. cppfunction('numericvector cumsum3(numericvector x) { return cumsum(x); ') x <- 1:10 all.equal(cumsum(x), cumsum3(x)) ## [1] TRUE

50 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Calling an R function from C++ #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector callfunction(numericvector x, Function f) { NumericVector res = f(x); return res; /*** R callfunction(x, fivenum) */

51 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Using Boost via BH: Greatest Common Denominator // [[Rcpp::depends(BH)]] #include <Rcpp.h> #include <boost/math/common_factor.hpp> // [[Rcpp::export]] int computegcd(int a, int b) { return boost::math::gcd(a, b); // [[Rcpp::export]] int computelcm(int a, int b) { return boost::math::lcm(a, b);

52 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Using Boost via BH: Lexical Cast // [[Rcpp::depends(BH)]] #include <Rcpp.h> #include <boost/lexical_cast.hpp> using boost::lexical_cast; using boost::bad_lexical_cast; // [[Rcpp::export]] std::vector<double> lexicalcast(std::vector<std::string> v) { std::vector<double> res(v.size()); for (int i=0; i<v.size(); i++) { try { res[i] = lexical_cast<double>(v[i]); catch(bad_lexical_cast &) { res[i] = NA_REAL; return res; // R> lexicalcast(c("1.23", ".4", "1000", "foo", "42", "pi/4")( // [1] NA NA

53 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Using Boost via BH: Date Calculations // [[Rcpp::depends(BH)]] #include <Rcpp.h> // One include file from Boost #include <boost/date_time/gregorian/gregorian_types.hpp> using namespace boost::gregorian; // [[Rcpp::export]] Rcpp::Date getimmdate(int mon, int year) { // compute third Wednesday of given month / year date d = nth_day_of_the_week_in_month( nth_day_of_the_week_in_month::third, Wednesday, mon).get_date(year); date::ymd_type ymd = d.year_month_day(); return Rcpp::Date(ymd.year, ymd.month, ymd.day);

54 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Using Boost via BH: FOREACH #include <Rcpp.h> #include <boost/foreach.hpp> using namespace Rcpp; // [[Rcpp::depends(BH)]] // the C-style upper-case macro name is a bit ugly #define foreach BOOST_FOREACH // [[Rcpp::export]] NumericVector square( NumericVector x ) { // elem is a reference to each element in x // we can re-assign to these elements as well foreach( double& elem, x ) { elem = elem*elem; return x; C++11 now has something similar in a smarter for loop.

55 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Using Boost via BH: Regular Expressions NB: Needs Sys.setenv("PKG_LIBS"="-lboost_regex") to link. // boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_exam #include <Rcpp.h> #include <string> #include <boost/regex.hpp> bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4[- ]){3\\d{4"); return boost::regex_match(s, e); // [[Rcpp::export]] std::vector<bool> regexdemo(std::vector<std::string> s) { int n = s.size(); std::vector<bool> v(n); for (int i=0; i<n; i++) v[i] = validate_card_format(s[i]); return valid;

56 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Vector Subsetting New / improved in Rcpp : #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector positives(numericvector x) { return x[x > 0]; // [[Rcpp::export]] List first_three(list x) { IntegerVector idx = IntegerVector::create(0, 1, 2); return x[idx]; // [[Rcpp::export]] List with_names(list x, CharacterVector y) { return x[y];

57 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Converting C to C++: A plyr example The job of split_indices() is simple: given a vector x of integers, it returns a list where the i-th element of the list is an integer vector containing the positions of x equal to i. I will spare you the C API version.

58 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Converting C to C++: A plyr example #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] std::vector<std::vector<int> > split_indices(integervector x, int n = 0) { if (n < 0) stop("n must be a pos. int."); std::vector<std::vector<int> > ids(n); int nx = x.size(); for (int i = 0; i < nx; ++i) { if (x[i] > n) { ids.resize(x[i]); ids[x[i] - 1].push_back(i + 1); return ids;

59 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Creating xts objects in C++ #include <Rcpp.h> using namespace Rcpp; NumericVector createxts(int sv, int ev) { IntegerVector ind = seq(sv, ev); // values NumericVector dv(ind); // date(time)s == reals dv = dv * 86400; // scaled to days dv.attr("tzone") = "UTC"; // index has attributes dv.attr("tclass") = "Date"; NumericVector xv(ind); // data has same index xv.attr("dim") = IntegerVector::create(ev-sv+1,1); xv.attr("index") = dv; CharacterVector cls = CharacterVector::create("xts","zoo"); xv.attr("class") = cls; xv.attr(".indexclass") = "Date"; //... some more attributes... return xv;

60 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Function Pointers Consider two simple functions modifying a given Armadillo vector: // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace arma; using namespace Rcpp; vec fun1_cpp(const vec& x) { // a first function vec y = x + x; return (y); vec fun2_cpp(const vec& x) { // and a second function vec y = 10*x; return (y);

61 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Function Pointers Using a typedef to declare an interface to a function taking and returning a vector and a function returning a function pointer given a string argument typedef vec (*funcptr)(const vec& x); // [[Rcpp::export]] XPtr<funcPtr> putfunptrinxptr(std::string fstr) { if (fstr == "fun1") return(xptr<funcptr>(new funcptr(&fun1_cpp))); else if (fstr == "fun2") return(xptr<funcptr>(new funcptr(&fun2_cpp))); else // runtime err.: NULL no XPtr return XPtr<funcPtr>(R_NilValue);

62 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Function Pointers We then create a function calling the supplied function on a given vector by unpacking the function pointer: // [[Rcpp::export]] vec callviaxptr(const vec x, SEXP xpsexp) { XPtr<funcPtr> xpfun(xpsexp); funcptr fun = *xpfun; vec y = fun(x); return (y);

63 Why What When Where How Examples Arma Doc CumSum R Fun Boost Subset CtoC++ xts XPtr Function Pointers ## get us a function fun <- putfunptrinxptr("fun1") ## and pass it down to C++ to ## have it applied on given vector callviaxptr(1:4, fun) ## [,1] ## [1,] 2 ## [2,] 4 ## [3,] 6 ## [4,] 8 Could use same mechanism for user-supplied functions, gradients, or samplers,...

64 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Outline 7 Armadillo Overview Users Examples Case Study: FastLM Case Study: Kalman Filter Case Study: Sparse Matrices

65 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Armadillo

66 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse What is Armadillo? From arma.sf.net and slightly edited Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use. The syntax is deliberately similar to Matlab. Integer, floating point and complex numbers are supported. A delayed evaluation approach is employed (at compile-time) to combine several operations into one and reduce (or eliminate) the need for temporaries. Useful for conversion of research code into production environments, or if C++ has been decided as the language of choice, due to speed and/or integration capabilities.

67 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse What is Armadillo? From arma.sf.net and slightly edited Armadillo is a C++ linear algebra library (matrix maths) aiming towards a good balance between speed and ease of use. The syntax is deliberately similar to Matlab. Integer, floating point and complex numbers are supported. A delayed evaluation approach is employed (at compile-time) to combine several operations into one and reduce (or eliminate) the need for temporaries. Useful for conversion of research code into production environments, or if C++ has been decided as the language of choice, due to speed and/or integration capabilities.

68 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Armadillo highlights Provides integer, floating point and complex vectors, matrices and fields (3d) with all the common operations. Very good documentation and examples at website a technical report (Sanderson, 2010) Modern code, building upon and extending from earlier matrix libraries. Responsive and active maintainer, frequent updates. Used by MLPACK; cf Curtin et al (JMLR, 2013)

69 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse RcppArmadillo highlights Template-only builds no linking, and available whereever R and a compiler work (but Rcpp is needed)! Easy with R packages: just add LinkingTo: RcppArmadillo, Rcpp to DESCRIPTION (i.e., no added cost beyond Rcpp) Data exchange really seamless from R via Rcpp Frequently updated; documentation includes Eddelbuettel and Sanderson (CSDA, 2014).

70 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Well-know packages using RcppArmadillo Amelia by Gary King et al: Multiple Imputation from cross-section, time-series or both; forecast by Rob Hyndman et al: Time-series forecasting including state space and automated ARIMA modeling; rugarch by Alexios Ghalanos: Sophisticated financial time series models; grbase by Søren Højsgaard: Graphical modeling

71 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Armadillo Eigenvalues #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] arma::vec geteigenvalues(arma::mat M) { return arma::eig_sym(m);

72 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Armadillo Eigenvalues set.seed(42); X <- matrix(rnorm(4*4), 4, 4) Z <- X %*% t(x); geteigenvalues(z) ## [,1] ## [1,] ## [2,] ## [3,] ## [4,] # R gets the same results (in reverse) # and also returns the eigenvectors.

73 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Multivariate Normal RNG Draw #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] arma::mat mvrnormarma(int n, arma::vec mu, arma::mat sigma) { arma::mat Y = arma::randn(n, sigma.n_cols); return arma::repmat(mu, 1, n).t() + Y * arma::chol(sigma);

74 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Background Implementations of fastlm() have been a staple all along the development of Rcpp The very first version was in response to a question by Ivo Welch on r-help. The request was for a fast function to estimate parameters and their standard errors from a linear model, It used GSL functions to estimate ˆβ as well as its standard errors ˆσ as lm.fit() in R only returns the former. It had since been reimplemented for RcppArmadillo and RcppEigen.

75 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Initial RcppArmadillo src/fastlm.cpp #include <RcppArmadillo.h> extern "C" SEXP fastlm(sexp Xs, SEXP ys) { try { Rcpp::NumericVector yr(ys); Rcpp::NumericMatrix Xr(Xs); int n = Xr.nrow(), k = Xr.ncol(); arma::mat X(Xr.begin(), n, k, false); copy arma::colvec y(yr.begin(), yr.size(), false); // creates Rcpp vector from SEXP // creates Rcpp matrix from SEXP // reuses memory, avoids extra arma::colvec coef = arma::solve(x, y); // fit model y X arma::colvec res = y - X*coef; // residuals double s2 = std::inner_product(res.begin(), res.end(), res.begin(), 0.0)/(n - k); arma::colvec std_err = // std.errors of coefficients arma::sqrt(s2*arma::diagvec(arma::pinv(arma::trans(x)*x))); return Rcpp::List::create(Rcpp::Named("coefficients") = coef, Rcpp::Named("stderr") = std_err, Rcpp::Named("df.residual") = n - k ); catch( std::exception &ex ) { forward_exception_to_r( ex ); catch(...) { ::Rf_error( "c++ exception (unknown reason)" ); return R_NilValue; // -Wall

76 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Edited version of earlier RcppArmadillo s src/fastlm.cpp // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace Rcpp; using namespace arma; // [[Rcpp::export]] List fastlm(numericvector yr, NumericMatrix Xr) { int n = Xr.nrow(), k = Xr.ncol(); mat X(Xr.begin(), n, k, false); colvec y(yr.begin(), yr.size(), false); colvec coef = solve(x, y); colvec resid = y - X*coef; double sig2 = as_scalar(trans(resid)*resid/(n-k)); colvec stderrest = sqrt(sig2 * diagvec( inv(trans(x)*x)) ); return List::create(Named("coefficients") = coef, Named("stderr") = stderrest, Named("df.residual") = n - k );

77 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Current version of RcppArmadillo s src/fastlm.cpp // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace Rcpp; using namespace arma; // [[Rcpp::export]] List fastlm(const arma::mat& X, const arma::colvec& y) { int n = X.n_rows, k = X.n_cols; colvec coef = solve(x, y); colvec resid = y - X*coef; double sig2 = as_scalar(trans(resid)*resid/(n-k)); colvec stderrest = sqrt(sig2 * diagvec( inv(trans(x)*x)) ); return List::create(Named("coefficients") = coef, Named("stderr") = stderrest, Named("df.residual") = n - k );

78 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Note on as<>() casting with Armadillo arma::colvec y = Rcpp::as<arma::colvec>(ys); arma::mat X = Rcpp::as<arma::mat>(Xs); Convenient, yet incurs an additional copy. Next variant uses two steps, but only a pointer to objects is copied: Rcpp::NumericVector yr(ys); Rcpp::NumericMatrix Xr(Xs); int n = Xr.nrow(), k = Xr.ncol(); arma::mat X(Xr.begin(), n, k, false); arma::colvec y(yr.begin(), yr.size(), false); Preferable if performance is a concern. Since last fall RcppArmadillo has efficient const references too.

79 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Faster Linear Model with FastLm Performance comparison Running the script included in the RcppArmadillo package: r fastlm.r Loading required package: Rcpp test replications relative elapsed 2 flmtwocasts(x, y) flmconstref(x, y) flmonecast(x, y) fastlmpuredotcall(x, y) fastlmpure(x, y) lm.fit(x, y) fastlm(frm, data = trees) lm(frm, data = trees)

80 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter Setup at Mathworks site The position of an object is estimated based on past values of 6 1 state vectors X and Y for position, V X and V Y for speed, and A X and A Y for acceleration. Position updates as a function of the speed X = X 0 + V X dt and Y = Y 0 + V Y dt, which is updated as a function of the (unobserved) acceleration: V x = V X,0 + A X dt and V y = V Y,0 + A Y dt.

81 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter Basic Matlab Function % Copyright 2010 The MathWorks, Inc. function y = kalmanfilter(z) % #codegen dt=1; % Initialize state transition matrix A=[1 0 dt 0 0 0;... % [x ] dt 0 0;... % [y ] dt 0;... % [Vx] dt;... % [Vy] ;... % [Ax] ]; % [Ay] H = [ ; ]; Q = eye(6); R = 1000 * eye(2); persistent x_est p_est if isempty(x_est) x_est = zeros(6, 1); p_est = zeros(6, 6); end % Predicted state and covariance x_prd = A * x_est; p_prd = A * p_est * A + Q; % Estimation S = H * p_prd * H + R; B = H * p_prd ; klm_gain = (S \ B) ; % Estimated state and covariance x_est = x_prd+klm_gain*(z-h*x_prd); p_est = p_prd-klm_gain*h*p_prd; % Compute the estimated measurements y = H * x_est; end % of the function Plus a simple wrapper function calling this function.

82 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter: In R Easy enough first naive solution FirstKalmanR <- function(pos) { kf <- function(z) { dt <- 1 A <- matrix(c(1, 0, dt, 0, 0, 0, # x 0, 1, 0, dt, 0, 0, # y 0, 0, 1, 0, dt, 0, # Vx 0, 0, 0, 1, 0, dt, # Vy 0, 0, 0, 0, 1, 0, # Ax 0, 0, 0, 0, 0, 1), # Ay 6, 6, byrow=true) H <- matrix( c(1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), 2, 6, byrow=true) Q <- diag(6) R < * diag(2) ## estimation S <- H %*% t(pprd) %*% t(h) + R B <- H %*% t(pprd) ## kalmangain <- (S \ B) kg <- t(solve(s, B)) ## est. state and cov, assign to vars in parent env xest <<- xprd + kg %*% (z-h%*%xprd) pest <<- pprd - kg %*% H %*% pprd ## compute the estimated measurements y <- H %*% xest xest <- matrix(0, 6, 1) pest <- matrix(0, 6, 6) N <- nrow(pos) y <- matrix(na, N, 2) ## predicted state and covriance xprd <- A %*% xest pprd <- A %*% pest %*% t(a) + Q for (i in 1:N) { y[i,] <- kf(t(pos[i drop=false])) invisible(y)

83 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter: In R Easy enough with some minor refactoring KalmanR <- function(pos) { kf <- function(z) { ## predicted state and covriance xprd <- A %*% xest pprd <- A %*% pest %*% t(a) + Q ## estimation S <- H %*% t(pprd) %*% t(h) + R B <- H %*% t(pprd) ## kg <- (S \ B) kg <- t(solve(s, B)) ## estimated state and covariance ## assigned to vars in parent env xest <<- xprd + kg %*% (z-h%*%xprd) pest <<- pprd - kg %*% H %*% pprd A <- matrix(c(1, 0, dt, 0, 0, 0, # x 0, 1, 0, dt, 0, 0, # y 0, 0, 1, 0, dt, 0, # Vx 0, 0, 0, 1, 0, dt, # Vy 0, 0, 0, 0, 1, 0, # Ax 0, 0, 0, 0, 0, 1),# Ay 6, 6, byrow=true) H <- matrix(c(1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), 2, 6, byrow=true) Q <- diag(6) R < * diag(2) N <- nrow(pos) y <- matrix(na, N, 2) xest <- matrix(0, 6, 1) pest <- matrix(0, 6, 6) ## compute the estimated measurements y <- H %*% xest dt <- 1 for (i in 1:N) { y[i,] <- kf(t(pos[i drop=false])) invisible(y)

84 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter: In C++ Using a simple class // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> using namespace arma; class Kalman { private: mat A, H, Q, R, xest, pest; double dt; public: // constructor, sets up data structures Kalman() : dt(1.0) { A.eye(6,6); A(0,2) = A(1,3) = dt; A(2,4) = A(3,5) = dt; H.zeros(2,6); H(0,0) = H(1,1) = 1.0; Q.eye(6,6); R = 1000 * eye(2,2); xest.zeros(6,1); pest.zeros(6,6); // sole member func.: estimate model mat estimate(const mat & Z) { unsigned int n = Z.n_rows, k = Z.n_cols; mat Y = zeros(n, k); mat xprd, pprd, S, B, kg; colvec z, y; for (unsigned int i = 0; i<n; i++) { z = Z.row(i).t(); // predicted state and covariance xprd = A * xest; pprd = A * pest * A.t() + Q; // estimation S = H * pprd.t() * H.t() + R; B = H * pprd.t(); kg = (solve(s, B)).t(); // estimated state and covariance xest = xprd + kg * (z - H * xprd); pest = pprd - kg * H * pprd; // compute estimated measurements y = H * xest; Y.row(i) = y.t(); return Y; Example-driven ; Intro to Rcpp

85 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter in C++ Trivial to use from R Given the code from the previous slide, we just add // [[Rcpp::export]] mat KalmanCpp(mat Z) { Kalman K; mat Y = K.estimate(Z); return Y;

86 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter: Performance Quite satisfactory relative to R Even byte-compiled better R version is 66 times slower: R> FirstKalmanRC <- cmpfun(firstkalmanr) R> KalmanRC <- cmpfun(kalmanr) R> R> stopifnot(identical(kalmanr(pos), KalmanRC(pos)), + all.equal(kalmanr(pos), KalmanCpp(pos)), + identical(firstkalmanr(pos), FirstKalmanRC(pos)), + all.equal(kalmanr(pos), FirstKalmanR(pos))) R> R> res <- benchmark(kalmanr(pos), KalmanRC(pos), + FirstKalmanR(pos), FirstKalmanRC(pos), + KalmanCpp(pos), + columns = c("test", "replications", + "elapsed", "relative"), + order="relative", + replications=100) R> R> print(res) test replications elapsed relative 5 KalmanCpp(pos) KalmanRC(pos) KalmanR(pos) FirstKalmanRC(pos) FirstKalmanR(pos)

87 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Kalman Filter: Figure Last but not least we can redo the plot as well Object Trajectory and Kalman Filter Estimate y Trajectory Estimate x

88 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Sparse Matrices Growing (but incomplete) support in Armadillo A nice example for work on R objects. i <- c(1,3:8) j <- c(2,9,6:10) x <- 7 * (1:7) A <- sparsematrix(i, j, x = x) A ## 8 x 10 sparse Matrix of class "dgcmatrix" ## ## [1,] ## [2,] ## [3,] ## [4,] ## [5,] ## [6,] ## [7,] ## [8,]

89 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Sparse Matrices Representation in R str(a) ## Formal class 'dgcmatrix' [package "Matrix"] with 6 slots i : int [1:7] p : int [1:11] Dim : int [1:2] 8 10 Dimnames:List of 2 ##....\$ : NULL ##....\$ : NULL x : num [1:7] factors : list() Note how the construction was in terms of < i, j, x >, yet the representation in in terms of < i, p, x > CSC format.

90 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Sparse Matrices C++ access #include <RcppArmadillo.h> using namespace Rcpp; using namespace arma; // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::export]] sp_mat armaex(s4 mat, bool show) { IntegerVector dims = mat.slot("dim"); arma::urowvec i = Rcpp::as<arma::urowvec>(mat.slot("i")); arma::urowvec p = Rcpp::as<arma::urowvec>(mat.slot("p")); arma::vec x = Rcpp::as<arma::vec>(mat.slot("x")); int nrow = dims[0], ncol = dims[1]; arma::sp_mat res(i, p, x, nrow, ncol); if (show) Rcpp::Rcout << res << std::endl; return res;

91 Why What When Where How Examples Arma Doc Overview Users Examples FastLM Kalman Sparse Sparse Matrices C++ access sourcecpp('code/sparseex.cpp') i <- c(1,3:8) j <- c(2,9,6:10) x <- 7 * (1:7) A <- sparsematrix(i, j, x = x) B <- armaex(a, TRUE) ## [matrix size: 8x10; n_nonzero: 7; density: 8.75%] ## ## (0, 1) ## (3, 5) ## (4, 6) ## (5, 7) ## (2, 8) ## (6, 8) ## (7, 9)

92 Why What When Where How Examples Arma Doc Basics Gallery Book Outline 8 Doc Basics Gallery Book

93 Why What When Where How Examples Arma Doc Basics Gallery Book What Else? Basic Documentation The package comes with eight pdf vignettes, and numerous help pages. The introductory vignettes are now published (Rcpp and RcppEigen in J Stat Software, RcppArmadillo in Comp. Stat.& Data Anal.). The rcpp-devel list is the recommended resource, generally very helpful, and fairly low volume. StackOverflow has over 500 posts too. Several blog posts introduce/discuss features.

94 Why What When Where How Examples Arma Doc Basics Gallery Book What Else? Rcpp Gallery: 80+ working and detailed examples

95 Why What When Where How Examples Arma Doc Basics Gallery Book What Else? The Rcpp book In print since June 2013

### Introduction to Rcpp - A Tutorial by Aditi Garg

Introduction to Rcpp - A Tutorial by Aditi Garg 1 Introduction The Rcpp package provides C++ classes that greatly facilitate interfacing C or C++ code in R packages using the.call() interface provided

### Packing C++ with R Advanced Statistical Programming Camp

Introduction This note documents the transition from creating R functions from C++ code via Rcpp helper functions to creating R functions by building and installing an R package. In both cases, many of

### Speeding up R with Rcpp

Speeding up R with Rcpp Stephen Cristiano Department of Biostatistics Johns Hopkins University March 1, 2014 What is Rcpp? Rcpp: Seamless integration between R and C++. Extremely simple to connect C++

### Exposing C++ functions and classes with Rcpp modules

Exposing C++ functions and classes with Rcpp modules Dirk Eddelbuettel Romain François Rcpp version 0.11.5 as of March 4, 2015 Abstract This note discusses Rcpp modules. Rcpp modules allow programmers

### Package RcppEigen. August 29, 2016

Type Package Package RcppEigen August 29, 2016 Title 'Rcpp' Integration for the 'Eigen' Templated Linear Algebra Library Version 0.3.2.9.0 Date 2016-08-20 Author Douglas Bates, Dirk Eddelbuettel, Romain

### Package RcppEigen. February 15, 2013

Package RcppEigen February 15, 2013 Type Package Title Rcpp integration for the Eigen templated linear algebra library. Version 0.3.1.2.1 Date 2013-01-14 Author Douglas Bates, Romain Francois and Dirk

### An Incomplete C++ Primer. University of Wyoming MA 5310

An Incomplete C++ Primer University of Wyoming MA 5310 Professor Craig C. Douglas http://www.mgnet.org/~douglas/classes/na-sc/notes/c++primer.pdf C++ is a legacy programming language, as is other languages

### An introduction to the Simplified Wrapper and Interface Generator (SWIG)

An introduction to the Simplified Wrapper and Interface Generator (SWIG) Department of Aerospace Engineering IIT Bombay 18, July 2007 Outline 1 Introduction 2 Usage 3 Interface files Introduction High

### CMYK 0/100/100/20 66/54/42/17 34/21/10/0 Why is R slow? How to run R programs faster? Tomas Kalibera

CMYK 0/100/100/20 66/54/42/17 34/21/10/0 Why is R slow? How to run R programs faster? Tomas Kalibera My Background Virtual machines, runtimes for programming languages Real-time Java Automatic memory management

### Object Oriented Software Design II

Object Oriented Software Design II Introduction to C++ Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 20, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

### Big Data Analytics in R. Matthew J. Denny. University of Massachusetts Amherst. mdenny@polsci.umass.edu. March 31, 2015. www.mjdenny.

Big Data Analytics in R Matthew J. Denny University of Massachusetts Amherst mdenny@polsci.umass.edu March 31, 2015 www.mjdenny.com Overview 1. Overview of High Performance Computing/Big Data Analytics

### Syllabus for CS 134 Java Programming

- Java Programming Syllabus Page 1 Syllabus for CS 134 Java Programming Computer Science Course Catalog 2000-2001: This course is an introduction to objectoriented programming using the Java language.

### Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University Next 4 lessons: Outline Scientific programming: best practices Classical learning (Hoepfield network) Probabilistic learning

### Lecture 03 Bits, Bytes and Data Types

Lecture 03 Bits, Bytes and Data Types In this lecture Computer Languages Assembly Language The compiler Operating system Data and program instructions Bits, Bytes and Data Types ASCII table Data Types

### The Not Quite R (NQR) Project: Explorations Using the Parrot Virtual Machine

The Not Quite R (NQR) Project: Explorations Using the Parrot Virtual Machine Michael J. Kane 1 and John W. Emerson 2 1 Yale Center for Analytical Sciences, Yale University 2 Department of Statistics, Yale

### BHARATHIAR UNIVERSITY: COIMBATORE CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++ LANGUAGE

Certificate in Advanced Programming - C++ Language Page 1 of 7 BHARATHIAR UNIVERSITY: COIMBATORE 641046 CENTRE FOR COLLABORATION OF INDUSTRY AND INSTITUTIONS(CCII) CERTIFICATE IN ADVANCED PROGRAMMING C++

### Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn 2009. Claus Führer Simulation Tools Automn 2009 1 / 65

Simulation Tools Python for MATLAB Users I Claus Führer Automn 2009 Claus Führer Simulation Tools Automn 2009 1 / 65 1 Preface 2 Python vs Other Languages 3 Examples and Demo 4 Python Basics Basic Operations

### C++ tutorial. C++ tutorial

Introduction I I will assume that you know some basics of C++: # include < iostream > int main ( void ) { std :: cout

### JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers

JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers Technology White Paper JStatCom Engineering, www.jstatcom.com by Markus Krätzig, June 4, 2007 Abstract JStatCom is a software framework

### Adding large data support to R

Adding large data support to R Luke Tierney Department of Statistics & Actuarial Science University of Iowa January 4, 2013 Luke Tierney (U. of Iowa) Large data in R January 4, 2013 1 / 15 Introduction

### SL-110: Fundamentals of Java Revision 15 October Sun Educational Services Instructor-Led Course Description

Sun Educational Services Instructor-Led Course Description Fundamentals of Java SL-110 The Fundamentals of the Java course provides students, with little or no programming experience, with the basics of

### Virtual Teaching and Painting Platform for the Colour Blind

IOSR Journal of Computer Engineering (IOSR-JCE) e-issn: 2278-0661, p- ISSN: 2278-8727Volume 16, Issue 3, Ver. VI (May-Jun. 2014), PP 01-09 Virtual Teaching and Painting Platform for the Colour Blind 1

### Your big data options M AY 2 1, 2 0 1 4

M A N I P U L AT I N G B I G DATA I N R R O B E RT J. CA R R O L L M AY 2 1, 2 0 1 4 This document introduces the data.table package for fast manipulation of big data objects. This is but one option among

### CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler 1) Operating systems a) Windows b) Unix and Linux c) Macintosh 2) Data manipulation tools a) Text Editors b) Spreadsheets

### CPLEX Tutorial Handout

CPLEX Tutorial Handout What Is ILOG CPLEX? ILOG CPLEX is a tool for solving linear optimization problems, commonly referred to as Linear Programming (LP) problems, of the form: Maximize (or Minimize) c

### CORBA Programming with TAOX11. The C++11 CORBA Implementation

CORBA Programming with TAOX11 The C++11 CORBA Implementation TAOX11: the CORBA Implementation by Remedy IT TAOX11 simplifies development of CORBA based applications IDL to C++11 language mapping is easy

### Crash Course in Java

Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed Computing Netprog 2002 Java Intro 1 What is

### D is for Science. John Colvin

D is for Science John Colvin What is scientific programming? You want to do science, using a computer but the existing software isn t up to the task Scientific Programming Simulations Data Analysis Visualisations

### Sources: On the Web: Slides will be available on:

C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,

### Introduction to Python

Introduction to Python Sophia Bethany Coban Problem Solving By Computer March 26, 2014 Introduction to Python Python is a general-purpose, high-level programming language. It offers readable codes, and

### Programming Languages & Tools

4 Programming Languages & Tools Almost any programming language one is familiar with can be used for computational work (despite the fact that some people believe strongly that their own favorite programming

### Integrating TAU With Eclipse: A Performance Analysis System in an Integrated Development Environment

Integrating TAU With Eclipse: A Performance Analysis System in an Integrated Development Environment Wyatt Spear, Allen Malony, Alan Morris, Sameer Shende {wspear, malony, amorris, sameer}@cs.uoregon.edu

### PGR Computing Programming Skills

PGR Computing Programming Skills Dr. I. Hawke 2008 1 Introduction The purpose of computing is to do something faster, more efficiently and more reliably than you could as a human do it. One obvious point

### The C Programming Language course syllabus associate level

TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming

### Programming with Data: Using and extending R

Intro S/R Extending Rcpp Examples Summary Programming with Data: Using and extending R, Ph.D. edd@debian.org, Dirk.Eddelbuettel@R-Project.org Invited Guest Lecture ACM Student Chapter University of Chicago

### Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Compiling Object Oriented Languages What is an Object-Oriented Programming Language? Last time Dynamic compilation Today Introduction to compiling object oriented languages What are the issues? Objects

### Data Analysis with MATLAB. 2013 The MathWorks, Inc. 1

Data Analysis with MATLAB 2013 The MathWorks, Inc. 1 Agenda Introduction Data analysis with MATLAB and Excel Break Developing applications with MATLAB Solving larger problems Summary 2 Modeling the Solar

### 1 (1x17 =17 points) 2 (21 points) 3 (5 points) 4 (3 points) 5 (4 points) Total ( 50points) Page 1

CS 1621 MIDTERM EXAM 1 Name: Problem 1 (1x17 =17 points) 2 (21 points) 3 (5 points) 4 (3 points) 5 (4 points) Total ( 50points) Score Page 1 1. (1 x 17 = 17 points) Determine if each of the following statements

### On the (un)suitability of Java to be the first programming language

On the (un)suitability of Java to be the first programming language Mirjana Ivanovic Faculty of Science, Department of Mathematics and Informatics Trg Dositeja Obradovica 4, Novi Sad mira @im.ns.ac.yu

### Xcode Project Management Guide. (Legacy)

Xcode Project Management Guide (Legacy) Contents Introduction 10 Organization of This Document 10 See Also 11 Part I: Project Organization 12 Overview of an Xcode Project 13 Components of an Xcode Project

### DSC 2003 Working Papers (Draft Versions) http://www.ci.tuwien.ac.at/conferences/dsc-2003/ Rserve

DSC 2003 Working Papers (Draft Versions) http://www.ci.tuwien.ac.at/conferences/dsc-2003/ Rserve A fast way to provide R functionality to applications Simon Urbanek Department of Computer Oriented Statistics

### TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL

TECH TUTORIAL: EMBEDDING ANALYTICS INTO A DATABASE USING SOURCEPRO AND JMSL This white paper describes how to implement embedded analytics within a database using SourcePro and the JMSL Numerical Library,

### PARALLEL JAVASCRIPT. Norm Rubin (NVIDIA) Jin Wang (Georgia School of Technology)

PARALLEL JAVASCRIPT Norm Rubin (NVIDIA) Jin Wang (Georgia School of Technology) JAVASCRIPT Not connected with Java Scheme and self (dressed in c clothing) Lots of design errors (like automatic semicolon

### The ff package: Handling Large Data Sets in R with Memory Mapped Pages of Binary Flat Files

UseR! 2007, Iowa State University, Ames, August 8-108 2007 The ff package: Handling Large Data Sets in R with Memory Mapped Pages of Binary Flat Files D. Adler, O. Nenadić, W. Zucchini, C. Gläser Institute

### Course 10550A: Programming in Visual Basic with Microsoft Visual Studio 2010 OVERVIEW

Course 10550A: Programming in Visual Basic with Microsoft Visual Studio 2010 OVERVIEW About this Course This course teaches you Visual Basic language syntax, program structure, and implementation by using

### 7.0. What s New in. SOFTWARE QUALITY ASSURANCE TOOLS & TECHNOLOGY PROFESSIONAL SERVICES ACADEMY P a g e 1

SOFTWARE QUALITY ASSURANCE TOOLS & TECHNOLOGY PROFESSIONAL SERVICES ACADEMY P a g e 1 What s New in 7.0 Cantata 7.0, available from February 2016, is a major new release of Cantata. This document outlines

### Course: Introduction to Java Using Eclipse Training

Course: Introduction to Java Using Eclipse Training Course Length: Duration: 5 days Course Code: WA1278 DESCRIPTION: This course introduces the Java programming language and how to develop Java applications

### 1 Topic. 2 Scilab. 2.1 What is Scilab?

1 Topic Data Mining with Scilab. I know the name "Scilab" for a long time (http://www.scilab.org/en). For me, it is a tool for numerical analysis. It seemed not interesting in the context of the statistical

### Advanced C++ Template Techniques: An Introduction to Meta-Programming for Scientific Computing

Advanced C++ Template Techniques: An Introduction to Meta-Programming for Scientific Computing Dr Conrad Sanderson Senior Research Scientist http://conradsanderson.id.au Version 1.2 (2015-03-09) (C) 2009-2015

### Computational Mathematics with Python

Boolean Arrays Classes Computational Mathematics with Python Basics Olivier Verdier and Claus Führer 2009-03-24 Olivier Verdier and Claus Führer Computational Mathematics with Python 2009-03-24 1 / 40

### An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

1 2 3 4 An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Charles J. Ammon / Penn State August, 2011

### Solving linear systems. Solving linear systems p. 1

Solving linear systems Solving linear systems p. 1 Overview Chapter 12 from Michael J. Quinn, Parallel Programming in C with MPI and OpenMP We want to find vector x = (x 0,x 1,...,x n 1 ) as solution of

### R, Other Languages and Object-Oriented Programming

R, Other Languages and Object-Oriented Programming John M. Chambers November 23, 2010 Outline 1. The benefits of multilingualism -- For serious research or data analysis, consider using more than one.

### Data Structure. Lecture 3

Data Structure Lecture 3 Data Structure Formally define Data structure as: DS describes not only set of objects but the ways they are related, the set of operations which may be applied to the elements

### Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Overview Elements of Programming Languages Lecture 12: Object-oriented functional programming James Cheney University of Edinburgh November 6, 2015 We ve now covered: basics of functional and imperative

### Computational Mathematics with Python

Computational Mathematics with Python Basics Claus Führer, Jan Erik Solem, Olivier Verdier Spring 2010 Claus Führer, Jan Erik Solem, Olivier Verdier Computational Mathematics with Python Spring 2010 1

### Java Application Developer Certificate Program Competencies

Java Application Developer Certificate Program Competencies After completing the following units, you will be able to: Basic Programming Logic Explain the steps involved in the program development cycle

### Dynamics and Vibrations Mupad tutorial

Dynamics and Vibrations Mupad tutorial School of Engineering Brown University This tutorial introduces the MATLAB mupad environment for symbolic calculations. You should work through the MATLAB tutorial

### Intermediate Python Tutorial

Intermediate Python Tutorial Python is free software and is an easy to learn powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented

### Kerby Shedden October, 2007. Overview of R

Kerby Shedden October, 2007 Overview of R R R is a programming language for statistical computing, data analysis, and graphics. It is a re-implementation of the S language, which was developed in the 1980

### "Charting the Course to Your Success!" MOC Programming in C# with Microsoft Visual Studio Course Summary

Description Course Summary The course focuses on C# program structure, language syntax, and implementation details with.net Framework 4.0. This course describes the new enhancements in the C# 4.0 language

### Lecture 1 Introduction to Android

These slides are by Dr. Jaerock Kwon at. The original URL is http://kettering.jrkwon.com/sites/default/files/2011-2/ce-491/lecture/alecture-01.pdf so please use that instead of pointing to this local copy

### C programming: exercise sheet L2-STUE (2011-2012)

C programming: exercise sheet L2-STUE (2011-2012) Algorithms and Flowcharts Exercise 1: comparison Write the flowchart and associated algorithm that compare two numbers a and b. Exercise 2: 2 nd order

### IEEEXTREME PROGRAMMING COMPETITION PROBLEM & INSTRUCTION BOOKLET #3

IEEEXTREME PROGRAMMING COMPETITION 2008 PROBLEM & INSTRUCTION BOOKLET #3 Instructions Read all the problems carefully. Each of the problems has a problem number (shown on top), a title, an approximate

### Big Data, R, and HPC

Big Data, R, and HPC A Survey of Advanced Computing with R Drew Schmidt April 16, 2015 Drew Schmidt Big Data, R, and HPC About Me @wrathematics http://librestats.com https://github.com/wrathematics http://wrathematics.info

### AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

AMATH 352 Lecture 3 MATLAB Tutorial MATLAB (short for MATrix LABoratory) is a very useful piece of software for numerical analysis. It provides an environment for computation and the visualization. Learning

### Visual Basic Programming. An Introduction

Visual Basic Programming An Introduction Why Visual Basic? Programming for the Windows User Interface is extremely complicated. Other Graphical User Interfaces (GUI) are no better. Visual Basic provides

### Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Outline 1 Computer Architecture hardware components programming environments 2 Getting Started with Python installing Python executing Python code 3 Number Systems decimal and binary notations running

### Parallel Computing for Data Science

Parallel Computing for Data Science With Examples in R, C++ and CUDA Norman Matloff University of California, Davis USA (g) CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint

### Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating

### CRASH COURSE PYTHON. Het begint met een idee

CRASH COURSE PYTHON nr. Het begint met een idee This talk Not a programming course For data analysts, who want to learn Python For optimizers, who are fed up with Matlab 2 Python Scripting language expensive

### Combining C++ and python using SWIG

What is SWIG? Combining C++ and python using SWIG Asa Ben-Hur A tool for interfacing various programming languages to C/C++ (supported languages: C#, chicken, lisp, guile, java, lua, scheme, Ocaml, perl,

### Creating and Using Databases for Android Applications

Creating and Using Databases for Android Applications Sunguk Lee * 1 Research Institute of Industrial Science and Technology Pohang, Korea sunguk@rist.re.kr *Correspondent Author: Sunguk Lee* (sunguk@rist.re.kr)

### Retour d expérience : portage d une application haute-performance vers un langage de haut niveau

Retour d expérience : portage d une application haute-performance vers un langage de haut niveau ComPAS/RenPar 2013 Mathias Bourgoin - Emmanuel Chailloux - Jean-Luc Lamotte 16 Janvier 2013 Our Goals Globally

### Introduction to Matlab

Information Technology Rice University Document UNIX 17 July 11, 2002 Introduction to Matlab Basic Matlab Commands and Syntax This document teaches the user how to create Matlab matrices, learn about Matlab

### Java (12 Weeks) Introduction to Java Programming Language

Java (12 Weeks) Topic Lecture No. Introduction to Java Programming Language 1 An Introduction to Java o Java as a Programming Platform, The Java "White Paper" Buzzwords, Java and the Internet, A Short

### Pattern Insight Clone Detection

Pattern Insight Clone Detection TM The fastest, most effective way to discover all similar code segments What is Clone Detection? Pattern Insight Clone Detection is a powerful pattern discovery technology

### C++ Programming Language

C++ Programming Language Lecturer: Yuri Nefedov 7th and 8th semesters Lectures: 34 hours (7th semester); 32 hours (8th semester). Seminars: 34 hours (7th semester); 32 hours (8th semester). Course abstract

### C Primer. Fall Introduction C vs. Java... 1

CS 33 Intro Computer Systems Doeppner C Primer Fall 2016 Contents 1 Introduction 1 1.1 C vs. Java.......................................... 1 2 Functions 1 2.1 The main() Function....................................

### Brian Caffo. Using.Call in R

Brian Caffo Using.Call in R R s.call Interface to C.Call is a suped up version of.c Pass R objects to C Create R objects in C Manipulate R objects in C Return R objects from C Call R functions from C The

### Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

CS 110B - Rule Storage Classes Page 18-1 Attributes are distinctive features of a variable. Data type, int or double for example, is an attribute. Storage class is another attribute. There are four storage

### Tutorial Program. 1. Basics

1. Basics Working environment Dealing with matrices Useful functions Logical operators Saving and loading Data management Exercises 2. Programming Basics graphics settings - ex Functions & scripts Vectorization

### LAB 1 REPRESENTATION OF NUMBERS AND INTRODUCTION TO C

LAB 1 REPRESENTATION OF NUMBERS AND INTRODUCTION TO C 1. LAB OBJECTIVE The objective of this lab is to review binary numbers and to review/introduce you to the C programming language and the MATLAB environment.

### Tutorial-3a: First steps with C++ programming

HTTP://WWW.HEP.LU.SE/COURSES/MNXB01 Introduction to Programming and Computing for Scientists (2015 HT) Tutorial-3a: First steps with C++ programming Programming for Scientists Tutorial 3a 1 / 17 Quick

### Oracle Developer Studio 12.5

Oracle Developer Studio 12.5 Oracle Developer Studio is the #1 development environment for building C, C++, Fortran and Java applications for Oracle Solaris and Linux operating systems. Oracle Developer

### Introduction to parallel computing in R

Introduction to parallel computing in R Clint Leach April 10, 2014 1 Motivation When working with R, you will often encounter situations in which you need to repeat a computation, or a series of computations,

### Final Exam Review. CS 1428 Fall Jill Seaman. Final Exam

Final Exam Review CS 1428 Fall 2011 Jill Seaman 1 Final Exam Friday, December 9, 11:00am to 1:30pm Derr 241 (here) Closed book, closed notes, clean desk Comprehensive (covers entire course) 25% of your

### C++ for Game Programmers

C++ for Game Programmers Course Description C++ has become one of the favourite programming language for game programmers. Reasons for wide spread acceptability of C++ are plenty, but primary reasons are,

### Getting Started with Matlab (in Computer Science at UBC)

Getting Started with Matlab (in Computer Science at UBC) Ian Mitchell Department of Computer Science The University of British Columbia Why Matlab? Outline Why not C / C++ / Java / Fortran? Why not Perl

### Oracle Data Miner (Extension of SQL Developer 4.0)

An Oracle White Paper September 2013 Oracle Data Miner (Extension of SQL Developer 4.0) Integrate Oracle R Enterprise Mining Algorithms into a workflow using the SQL Query node Denny Wong Oracle Data Mining

### GEDAE TM - A Graphical Programming and Autocode Generation Tool for Signal Processor Applications

GEDAE TM - A Graphical Programming and Autocode Generation Tool for Signal Processor Applications Harris Z. Zebrowitz Lockheed Martin Advanced Technology Laboratories 1 Federal Street Camden, NJ 08102

### 1/20/2016 INTRODUCTION

INTRODUCTION 1 Programming languages have common concepts that are seen in all languages This course will discuss and illustrate these common concepts: Syntax Names Types Semantics Memory Management We

### Introduction. Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications

Introduction Why (GIS) Programming? Streamline routine/repetitive procedures Implement new algorithms Customize user applications 1 Computer Software Architecture Application macros and scripting - AML,

### Ivor Horton's Beginning Visual C

Brochure More information from http://www.researchandmarkets.com/reports/2708412/ Ivor Horton's Beginning Visual C++ 2013 Description: Learn C++ Programming and Visual C++ 2013 the faster, easier way This

### Elemental functions: Writing data-parallel code in C/C++ using Intel Cilk Plus

Elemental functions: Writing data-parallel code in C/C++ using Intel Cilk Plus A simple C/C++ language extension construct for data parallel operations Robert Geva robert.geva@intel.com Introduction Intel

### Bachelor of Games and Virtual Worlds (Programming) Subject and Course Summaries

First Semester Development 1A On completion of this subject students will be able to apply basic programming and problem solving skills in a 3 rd generation object-oriented programming language (such as

### INDEX. C programming Page 1 of 10. 5) Function. 1) Introduction to C Programming

INDEX 1) Introduction to C Programming a. What is C? b. Getting started with C 2) Data Types, Variables, Constants a. Constants, Variables and Keywords b. Types of Variables c. C Keyword d. Types of C