gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:32:33 28 4
gpt4 key购买 nike

以下 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);

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

最佳答案

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

代码

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

输出

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>

关于c++ - Rcpp: 'operator=' 矩阵和列表的模糊重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771818/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com