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

问题描述 投票:3回答:1

以下Rcpp代码是生成相同编译错误的更大代码的最小可重现示例。似乎我无法将数字矩阵设置为列表,然后将列表再次设置为另一个矩阵。

#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);
}

上面的代码给出了我缩短的以下编译错误:

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

我的真正功能要复杂得多。我需要在几个循环中操作矩阵,并且在每个步骤中,矩阵由列表中的每个函数返回。然后,我需要提取这些列表以进一步操作矩阵。如何才能做到这一点?

c++ r compiler-errors rcpp
1个回答
2
投票

除了@Ralf已经提到的错误之外,你只是在尝试太多。有时我们需要一个中间步骤,因为模板魔术是......挑剔。以下作品。

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>
© www.soinside.com 2019 - 2024. All rights reserved.