rcpp

Elementwise matrix multiplication: R versus Rcpp (How to speed this code up?)

强颜欢笑 提交于 2019-12-02 21:19:51
I am new to C++ programming (using Rcpp for seamless integration into R ), and I would appreciate some advice on how to speed up some calculations. Consider the following example: testmat <- matrix(1:9, nrow=3) testvec <- 1:3 testmat*testvec # [,1] [,2] [,3] #[1,] 1 4 7 #[2,] 4 10 16 #[3,] 9 18 27 Here, R recycled testvec so that, loosely speaking, testvec "became" a matrix of the same dimensions as testmat for the purpose of this multiplication. Then the Hadamard product is returned. I wish to implement this behavior using Rcpp , that is I want that each element of the i -th row in the matrix

Subset of a Rcpp Matrix that matches a logical statement

£可爱£侵袭症+ 提交于 2019-12-02 20:39:20
In R, if we have a data matrix, say a 100 by 10 matrix X, and a 100-elements vector t with possible values (0, 1, 2, 3), we can easily find a submatrix y of X using a simple syntax: y = X[t == 1, ] But, the problem is, how can I do that with Rcpp's NumericMatrix ? (Or, more generally, how can I do that in C++'s any containers ?) Thanks to Dirk's hint, it seems that NumericMatrix X(dataX); IntegerVector T(dataT); mat Xmat(X.begin(), X.nrow(), X.ncol(), false); vec tIdx(T.begin(), T.size(), false); mat y = X.rows(find(tIdx == 1)); Can do what I want, but that seems too lengthy. Dirk Eddelbuettel

How to use Rcpp to speed up a for loop?

旧时模样 提交于 2019-12-02 19:20:29
I have created a for loop and I would like to speed it up using the Rcpp library. I am not too familiar with C++. Can you please help me make my function faster? Thank you for your help! I have included my algorithm, the code, along with the input and the output, with the sessionInfo. Here is my algorithm: if the current price is above the previous price, mark (+1) in the column named TR if the current price is below the previous price, mark (-1) in the column named TR if the current price is the same as the previous price, mark the same thing as in the previous price in the column named TR

Within C++ functions, how are Rcpp objects passed to other functions (by reference or by copy)?

放肆的年华 提交于 2019-12-02 17:43:11
I've just finished writing a new version of the ABCoptim package using Rcpp. With around 30x speed ups, I'm very happy with the new version's performance (vs old version), but I'm still having some concerns on if I have space to improve performance without modifying too much the code. Within the main function of ABCoptim (written in C++) I'm passing around a Rcpp::List object containing "bees positions" (NumericMatrix) and some NumericVectors with important information for the algorithm itself. My question is, when I'm passing a Rcpp::List object around other functions, e.g. #include <Rcpp.h>

Rcpp: change a list item in a list of a list

安稳与你 提交于 2019-12-02 17:40:31
问题 Change a list item in a list of a list. Unfortunately, I could not find an answer to my question in the forum. Using rcpp, I want to directly change a list item in a list. I have the following approach: // [[Rcpp::export]] void test(){ Environment env = Environment::global_env(); List outerList = env["ListR"]; List innerList = polymeraseFlagList[0]; outerList((1)) ) "test"; // correctly changing inner list CharacterVector innerStr = innerList[1]; // correctly access to inner list element }

ld: warning: text-based stub file are out of sync. Falling back to library file for linking

本秂侑毒 提交于 2019-12-02 17:25:53
When I am trying to sourceCpp , it gives a warning: ld: warning: text-based stub file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation.tbd and library file /System/Library/Frameworks//CoreFoundation.framework/CoreFoundation are out of sync. Falling back to library file for linking. But the function actually works. Just wondering how to solve this warning. jiyongdong I had this problem while compiling Go code on my Mac too. The tld files were out of sync in /System/Library/Frameworks/ . The solution : Just links the recent frameworks from MacOSX.sdk to /Library/Frameworks/

Difference between RInside and Rcpp

懵懂的女人 提交于 2019-12-02 16:38:11
问题 I understand RInside allows C++ program to embed R code, while Rcpp enables R code to call C++ functions or library. Are there other differences and commonalities between RInside and Rcpp? Why RInside has a namespace called Rcpp? Do developers always need both RInside and Rcpp to call R code as a class in Cpp? 回答1: Rcpp: The Rcpp package provides R functions and a C++ library facilitating the integration of R and C++. RInside: The RInside package provides a few classes for seamless embedding

Why Rcpp::warning() is slower than R warning?

雨燕双飞 提交于 2019-12-02 14:24:39
问题 Consider the following functions foo and bar (differs only in printing warning) and their R equivalents fooR and barR : cppFunction(' void foo () { int i = 0; while (i < 1e3) { i++; } return; }') cppFunction(' void bar () { int i = 0; while (i < 1e3) { i++; Rcpp::warning("hello world!"); } return; }') fooR <- function() { i = 0; while (i < 1e3) { i = i+1; } } barR <- function() { i = 0; while (i < 1e3) { i = i+1; warning("hello world!") } } Obviously, printing warnings makes function slower,

MappedSparseMatrix in RcppEigen

不羁的心 提交于 2019-12-02 14:19:49
问题 I want to use conjugate gradient algorithm implemented in RcppEigen package for solving large sparse matrices. Since I am new to Rcpp and C++, I just started with the dense matrices. // [[Rcpp::depends(RcppEigen)]] #include <Rcpp.h> #include <RcppEigen.h> #include <Eigen/IterativeLinearSolvers> using Eigen::SparseMatrix; using Eigen::MappedSparseMatrix; using Eigen::Map; using Eigen::MatrixXd; using Eigen::VectorXd; using Rcpp::as; using Eigen::ConjugateGradient; typedef Eigen:

Speed up the loop operation in R

独自空忆成欢 提交于 2019-12-02 13:44:07
I have a big performance problem in R. I wrote a function that iterates over a data.frame object. It simply adds a new column to a data.frame and accumulates something. (simple operation). The data.frame has roughly 850K rows. My PC is still working (about 10h now) and I have no idea about the runtime. dayloop2 <- function(temp){ for (i in 1:nrow(temp)){ temp[i,10] <- i if (i > 1) { if ((temp[i,6] == temp[i-1,6]) & (temp[i,3] == temp[i-1,3])) { temp[i,10] <- temp[i,9] + temp[i-1,10] } else { temp[i,10] <- temp[i,9] } } else { temp[i,10] <- temp[i,9] } } names(temp)[names(temp) == "V10"] <-