简体   繁体   English

Rcpp:'operator ='矩阵和列表的模糊重载

[英]Rcpp: ambiguous overload for 'operator=' Matrix and List

The following Rcpp code is the minimal reproducible example for a much larger code that generates the identical compilation error. 以下Rcpp代码是生成相同编译错误的更大代码的最小可重现示例。 It seems that I cannot asign a numeric matrix to a list and the list then again to another matrix. 似乎我无法将数字矩阵设置为列表,然后将列表再次设置为另一个矩阵。

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
List return_a(NumericMatrix a, NumericMatrix b){
    //the function only returns the input matrix a
    List result(1);
    result(0) = a;
    return(result);
}


//[[Rcpp::export]]
List wrapper_cpp(NumericMatrix a, NumericMatrix b){
    //the function is a dummy wrapper for much more code
    List Step1(1);
    List results(1);    
    Step1 = return_a(a,b);
    a = Step1(0);   
    results(0) = a;
    return(results);
}

The code above gives the following compilation error that I shortened: 上面的代码给出了我缩短的以下编译错误:

error: ambiguous overload for 'operator=' (operand types are 'Rcpp::NumericMatrix {aka Rcpp::Matrix<14>}' and 'Rcpp::Vector<19>::Proxy ...
a = Step1(0);

My real function is much more complex. 我的真正功能要复杂得多。 I need to manipulate matrices in several loops and in each step the matrices are returned by each function within a list. 我需要在几个循环中操作矩阵,并且在每个步骤中,矩阵由列表中的每个函数返回。 I then need to extract these lists to manipulate the matrices further. 然后,我需要提取这些列表以进一步操作矩阵。 How can this be done? 如何才能做到这一点?

Besides the error that @Ralf already mentioned, you were simply trying too much. 除了@Ralf已经提到的错误之外,你只是在尝试太多。 Sometimes we need an intermediate step as the template magic is ... finicky. 有时我们需要一个中间步骤,因为模板魔术是......挑剔。 The following works. 以下作品。

Code

#include <Rcpp.h>
using namespace Rcpp;

//[[Rcpp::export]]
List return_a(NumericMatrix a, NumericMatrix b){
  //the function only returns the input matrix a
  List result(1);
  result(0) = a;
  return(result);
}


//[[Rcpp::export]]
List wrapper_cpp(NumericMatrix a, NumericMatrix b){
  //the function is a dummy wrapper for much more code
  List results(1);
  List Step1 = return_a(a,b);
  NumericMatrix tmp = Step1(0);
  results(0) = tmp;
  return(results);
}

Output 产量

R> Rcpp::sourceCpp("~/git/stackoverflow/54771818/answer.cpp")
R> wrapper_cpp(matrix(1:4,2,2), matrix(4:1,2,2))
[[1]]
     [,1] [,2]
[1,]    1    3
[2,]    2    4

R>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM