[Rcpp-devel] Error still there

Dirk Eddelbuettel edd at debian.org
Fri Sep 23 13:32:54 CEST 2016


On 23 September 2016 at 18:17, Amina Shahzadi wrote:
| Hello Dirk, Adler and Rcpp Colleagues
| 
| Would you please guide me why the following code is giving the error of "
| 
| cannot convert 'arma::subview_elem1<double, arma::mtOp<unsigned int, arma::mtGlue<unsigned int, arma::mtOp<unsigned int, arma::Col<unsigned int>, arma::op_rel_noteq>, arma::mtOp<unsigned int, arma::Col<unsigned int>, arma::op_rel_noteq>, arma::glue_rel_and>, arma::op_find_simple> >' to 'double' in initialization"
| 
| 
| The problem is in double alpha1 and double beta1.

The _core_ of the error message is reasonably clear: you cannot assign a arma
type (of some sort ... here stemming of the subsetting) to a double.

Solution: use as_scalar() -- see the Armadillo documentation for more -- ie
use these three lines at the core of your loops:

      double alpha1 = as_scalar(alpha.elem(find(index !=i && index !=j)));
      double beta1 = as_scalar(beta.elem(find(index !=j && index !=j)));
      a(i) = exp(alpha1+beta1);
                  
Dirk

| 
| 
| 
| The code is:
| 
| #include <RcppArmadillo.h>
| using namespace Rcpp;
| using namespace RcppArmadillo;
| //[[Rcpp::depends(RcppArmadillo)]]
| //[[Rcpp::export]]
| 
| // A example of indexing some elements of a vector
| 
| arma::vec Vec_Index(arma::vec alpha, arma::vec beta)
| {
|   int m = alpha.size();
|   arma::uvec index(m);
|   arma::vec a(m);
|   for(int i=0; i<m; i++)
|   {
|     index(i) = i;
|   }
|   for(int i=0; i<m; i++){
|     for(int j=0; j<m; j++){
|       double alpha1 = alpha.elem(find(index !=i && index !=j));
|       double beta1 = beta.elem(find(index !=j && index !=j));
|     a(i) = exp(alpha1+beta1);
|   }
|   }
|   return a;
| }
| 
| 
| 
| 
| 
| 
| 
| 
| 
| On Thu, Sep 15, 2016 at 2:52 AM, Avraham Adler <avraham.adler at gmail.com> wrote:
| 
|     Amina and I continued the conversation off list, but for closure purposes,
|     the "multiple" version was tweaked a bit as there was comparison between
|     signed and unsigned ints which did not throw an error for me, but did for
|     Amina. Final multiple version posted below.
| 
|     Thanks,
| 
|     Avi
| 
| 
|     #include <RcppArmadillo.h>
|     using namespace Rcpp;
|     using namespace RcppArmadillo;
|     using namespace arma;
|     //[[Rcpp::depends(RcppArmadillo)]]
| 
|     //[[Rcpp::export]]
|     vec Sample_Exp_Multi(size_t n, vec alpha, vec beta) {
|       size_t m = alpha.size();
|       vec answers(n);
|       uvec2 S;
|       for (size_t i = 0; i < n; ++i){
|         do {
|           S = randi<uvec>(2, distr_param(0, m - 1));
|         } while (S(0) == S(1));
|         answers(i) = exp(alpha(S(0)) + beta(S(1)));
|       }
|       return(answers);
|     }
| 
|     _______________________________________________
|     Rcpp-devel mailing list
|     Rcpp-devel at lists.r-forge.r-project.org
|     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| 
| 
| 
| 
| --
| Amina Shahzadi

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list