rcpp

Passing large matrices to RcppArmadillo function without creating copy (advanced constructors)

帅比萌擦擦* 提交于 2019-11-28 20:54:22
I want to pass a large matrix to a RcppArmadillo function (about 30,000*30,000) and have the feeling that this passing alone eats up all the performance gains. The question was also raised here with the suggested to solution to use advanced constructors with the copy_aux_mem = false argument. This seems to be a good solution also because I only need to read rows from the matrix without changing anything. I am having problems implementing the solution correctly though. This is probably just a simply syntax question. Here is my current set-up of the function call (simplified, of course):

Where can I learn how to write C code to speed up slow R functions? [closed]

跟風遠走 提交于 2019-11-28 14:57:39
What's the best resource for learning how to write C code for use with R? I know about the system and foreign language interfaces section of R extensions, but I find it pretty hard going. What are good resources (both online and offline) for writing C code for use with R? To clarify, I don't want to learn how to write C code, I want to learn how to better integrate R and C. For example, how do I convert from a C integer vector to a R integer vector (or vice versa) or from a C scalar to an R vector? Well there is the good old Use the source, Luke! --- R itself has plenty of (very efficient) C

In Rcpp, how to get a user-defined structure from C into R

╄→尐↘猪︶ㄣ 提交于 2019-11-28 14:13:51
Am using Rcpp packages and can get my C function to compile and run in R, but now I want to return a large, user-defined data structure to R. The fields in the structure are either numbers or strings - no new or odd types within the structure. The example below is simplified and doesn't compile, but it conveys the idea of my problem. typedef struct { char* firstname[128]; char* lastname[128]; int nbrOfSamples; } HEADER_INFO; // [[Rcpp::export]] HEADER_INFO* read_header(Rcpp::StringVector strings) { FILE *fp; MEF_HEADER_INFO *header; char * filename = (char*)(strings(0)); char * password =

Rcpp error: /usr/bin/ld cannot find -lgfortran

两盒软妹~` 提交于 2019-11-28 14:10:05
I am working through the book "Seamless R and C++ Integration with Rcpp". I am using R version 3.1.0 on Ubuntu 12.04. I cannot figure out how to properly link the necessary libraries. I have the following code in R: R> library(Rcpp) R> library(RcppArmadillo) R> suppressMessages(require(inline)) R> code <- ' + arma::mat coeff = Rcpp::as<arma::mat>(a); + arma::mat errors = Rcpp::as<arma::mat>(u); + int m = errors.n_rows; + int n = errors.n_cols; + arma::mat simdata(m,n); + simdata.row(0) = arma::zeros<arma::mat>(1, n); + for (int row=1; row < m; row++) { + simdata.row(row) = simdata.row(row-1)

Stack imbalance with RcppParallel

泪湿孤枕 提交于 2019-11-28 12:56:05
I wrote the following code to train myself to use RcppParallel. It is just a toy example. // [[Rcpp::depends(RcppParallel)]] #include <Rcpp.h> #include <RcppParallel.h> #include <iostream> using namespace Rcpp; using namespace RcppParallel; struct Lapin : public Worker { // input pars const NumericVector input; const size_t dim; // outputs a matrix NumericMatrix output; // two constructors Lapin(const NumericVector input, const int dim) : input(input), dim(dim), output(NumericMatrix(dim,dim)) {} Lapin(const Lapin & jeannot, Split) : input(jeannot.input), dim(jeannot.dim), output(NumericMatrix

Using dnorm with RcppArmadillo

核能气质少年 提交于 2019-11-28 12:42:19
From R , I'm trying to run sourceCpp on this file: #include <RcppArmadillo.h> // [[Rcpp::depends(RcppArmadillo)]] using namespace arma; using namespace Rcpp; // [[Rcpp::export]] vec dnormLog(vec x, vec means, vec sds) { int n = x.size(); vec res(n); for(int i = 0; i < n; i++) { res[i] = log(dnorm(x[i], means[i], sds[i])); } return res; } See this answer to see where I got the function from. This throws the error: no matching function for call to 'dnorm4' Which is the exact error I was hoping to prevent by using the loop, since the referenced answer mentions that dnorm is only vectorized with

Closest point to a path

旧城冷巷雨未停 提交于 2019-11-28 12:36:27
I have two sets of points, called path and centers . For each point in path , I would like an efficient method for finding the ID of the closest point in centers . I would like to do this in R. Below is a simple reproducible example. set.seed(1) n <- 10000 x <- 100*cumprod(1 + rnorm(n, 0.0001, 0.002)) y <- 50*cumprod(1 + rnorm(n, 0.0001, 0.002)) path <- data.frame(cbind(x=x, y=y)) centers <- expand.grid(x=seq(0, 500,by=0.5) + rnorm(1001), y=seq(0, 500, by=0.2) + rnorm(2501)) centers$id <- seq(nrow(centers)) x and y are coordinates. I would like to add a column to the path data.frame that has

Rcpp function crashes

不想你离开。 提交于 2019-11-28 12:13:33
My problem: I am using R.3.0.1 together with RStudio 0.97.551 on a 64bit Windows7 PC and I have begun to outsource a function to C/C++ using Rcpp. The function compiles, but evaluating it within an R function produces a runtime error. I am not able to find out why and how to fix this. Details Below is my cpp-file... let's say it's called "vector.cpp" #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector l5(double z, double k, double s, double K, double theta, double x, double h, NumericVector m){ int n = m.size(); NumericVector a(n); NumericVector bu(n); NumericVector b(n);

How to speed up or vectorize a for loop?

本小妞迷上赌 提交于 2019-11-28 10:16:29
I would like to increase the speed of my for loop via vectorization or using Data.table or something else. I have to run the code on 1,000,000 rows and my code is really slow. The code is fairly self-explanatory. I have included an explanation below just in case. I have included the input and the output of the function. Hopefully you will help me make the function faster. My goal is to bin the vector "Volume", where each bin is equal to 100 shares. The vector "Volume" contains the number of shares traded. Here is what it looks like: head(Volume, n = 60) [1] 5 3 1 5 3 1 1 1 1 1 1 1 18 1 1 18 2

Rcpp warning: “directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'”

左心房为你撑大大i 提交于 2019-11-28 10:09:36
This question relates to some others out there, like RccpArmadillo or element-wise-multiplication . However, my settings are such that I do not know what I have to edit/simlink to make Rccp run without giving me warnings. I am on an Mac 10.9 (mavericks) using the latest R version. At the very beginning, trying the following code I got from here RccpvsR , I got an error: ld: library not found for -lgfortran clang: error: linker command failed with exit code 1 (use -v to see invocation)` Then, based on RccpArmadillo I did the following: # Update FLIBS in ~/.R/Makevars FLIBS=-L/usr/local/Cellar