armadillo

convert MATLAB cell type to c++

怎甘沉沦 提交于 2019-12-04 08:15:31
i'm converting a MATLAB program in c++ using Armadillo for matrix algebra. i'm stuck on cell type. someone has some hints? That's because 'cell' is not really a type - it is a placeholder for anything you want to place in it. The closest thing I can think of in languages such as C# and Python is a 'tuple', which intrinsically can contain anonymous types. Since C++ does not have a built-in tuple type, I suggest you take a look at Boost , which is a very comprehensive, mature and open-source library for practically anything you need in C++. Under Boost, take a look at the Fusion library, or if

Fast LAPACK/BLAS for matrix multiplication

﹥>﹥吖頭↗ 提交于 2019-12-04 04:37:03
I'm exploring the Armadillo C++ library for linear algebra at the moment. As far as I understood it uses LAPACK/BLAS library for basic matrix operations (e.g. matrix multiplication). As a Windows user I downloaded LAPACK/BLAS from here: http://icl.cs.utk.edu/lapack-for-windows/lapack/#running . The problem is that matrix multiplications are very slow comparing to Matlab or even R. For example, Matlab multiplies two 1000x1000 matrices in ~0.15 seconds on my computer, R needs ~1 second, while C++/Armadillo/LAPACK/BLAS needs more than 10 seconds for that. So, Matlab is based on highly optimized

Applying the optim function in R in C++ with Rcpp

本秂侑毒 提交于 2019-12-03 17:08:44
I am trying to call R function optim() in Rcpp . I saw an example in Calling R's optim function from within C++ using Rcpp , but I am unable to modify it correctly for my use case. Basically, the objective function depends on the x and y but I want to optimize it with respect to b . Here is the R code that does what I want: example_r = function(b, x, y) { phi = rnorm(length(x)) tar_val = (x ^ 2 + y ^ 2) * b * phi objftn_r = function(beta, x, y) { obj_val = (x ^ 2 + y ^ 2) * beta return(obj_val) } b1 = optim(b, function(beta) { sum((objftn_r(beta, x, y) - tar_val) ^ 2) }, method = "BFGS")$par

How do I convert an armadillo matrix to a vector of vectors?

天大地大妈咪最大 提交于 2019-12-03 16:23:08
问题 I created an armadillo c++ matrix as follows: arma::mat A; A.zeros(3,4); I want to convert it to a vector of vectors defined by std::vector< std::vector<double> > B(3, std::vector<double>(4) ); How do I set B to equal A? If there is not an easy way for a vector of vectors, what about an array of arrays, i.e., what if I defined B to be double B[3][4]; 回答1: In such cases you should use arma::conv_to which is a totally superb feature of arma. Note that this method will require from a source

Deciding between NumericVector and arma::vec in Rcpp

谁说我不能喝 提交于 2019-12-03 10:42:39
问题 With RcppArmadillo the conversion from R to Rcpp with arma::vec is just as easy as with Rcpp and NumericVector . My project utilizes RcppArmadillo. I'm unsure what to use, NumericVector or arma::vec ? What are the key differences between those two? When to use which? Is there a performance/memory advantage of using one over the other? Are the only difference the member functions? And, as a bonus question: should I even consider arma::colvec or arma::rowvec ? 回答1: What are the key differences

A C++ version of the %in% operator in R

家住魔仙堡 提交于 2019-12-03 09:01:06
问题 Is there any function in C++ equivalent to %in% operator in R? Consider the command below in R: which(y %in% x) I tried to find something equivalent in C++ (specifically in Armadillo) and I couldn't find anything. I then wrote my own function which is very slow compared to the R command above. Here is what I wrote: #include <RcppArmadillo.h> // [[Rcpp::depends("RcppArmadillo")]] // [[Rcpp::export]] arma::uvec myInOperator(arma::vec myBigVec, arma::vec mySmallVec ){ arma::uvec rslt = find

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

亡梦爱人 提交于 2019-12-03 07:52:33
问题 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

How do I convert an armadillo matrix to a vector of vectors?

偶尔善良 提交于 2019-12-03 05:44:00
I created an armadillo c++ matrix as follows: arma::mat A; A.zeros(3,4); I want to convert it to a vector of vectors defined by std::vector< std::vector<double> > B(3, std::vector<double>(4) ); How do I set B to equal A? If there is not an easy way for a vector of vectors, what about an array of arrays, i.e., what if I defined B to be double B[3][4]; In such cases you should use arma::conv_to which is a totally superb feature of arma. Note that this method will require from a source object to be able to be interpreted as a vector. That is why we need to do this iteratively for every row. Here

Is armadillo solve() thread safe?

懵懂的女人 提交于 2019-12-03 04:08:06
In my code I have loop in which I construct and over determined linear system and try to solve it: #pragma omp parallel for for (int i = 0; i < n[0]+1; i++) { for (int j = 0; j < n[1]+1; j++) { for (int k = 0; k < n[2]+1; k++) { arma::mat A(max_points, 2); arma::mat y(max_points, 1); // initialize A and y arma::vec solution = solve(A,y); } } } Sometimes, quite randomly the program hangs or the results in the solution vector are NaN. And if I put do this: arma::vec solution; #pragma omp critical { solution = solve(weights*A,weights*y); } then these problem don't seem to happen anymore. When it

Deciding between NumericVector and arma::vec in Rcpp

老子叫甜甜 提交于 2019-12-03 00:23:27
With RcppArmadillo the conversion from R to Rcpp with arma::vec is just as easy as with Rcpp and NumericVector . My project utilizes RcppArmadillo. I'm unsure what to use, NumericVector or arma::vec ? What are the key differences between those two? When to use which? Is there a performance/memory advantage of using one over the other? Are the only difference the member functions? And, as a bonus question: should I even consider arma::colvec or arma::rowvec ? What are the key differences between those two? The *Vector and *Matrix classes in Rcpp act as wrappers for R 's SEXP representation, e.g