直接调用`Rcpp::List`中的元素以供进一步使用
在应用程序中,我使用 List
来包含一些变量(double
、arma::mat
和其他类型),然后获取 >arma::mat
组件在此列表中,供进一步直接使用,例如矩阵加法。但是,会引发一些错误。
下面是一个玩具示例,抛出与我遇到的相同的错误:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
arma::mat f(){
arma::mat x = arma::randn(3, 4) ;
List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;
return x - y["a"] ;
}
错误信息是
ambiguous overload for 'operator-' (operand types are 'arma::mat'
{aka 'arma::Mat<double>'} and 'Rcpp::Vector<19>::NameProxy' {aka
'Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>'})
Is there any way to use the y["a"]
direct in numericcalculated?
In an application, I use List
to contain some variables (double
, arma::mat
and other types), and then take the arma::mat
component in this list for futher use directly, such as matrix addition. However, some error is thrown.
Below is a toy example to throw the same error as I met:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
arma::mat f(){
arma::mat x = arma::randn(3, 4) ;
List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;
return x - y["a"] ;
}
The error information is
ambiguous overload for 'operator-' (operand types are 'arma::mat'
{aka 'arma::Mat<double>'} and 'Rcpp::Vector<19>::NameProxy' {aka
'Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>'})
Is there any way to use the y["a"]
directly in numerical computation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从添加到(R 或 Rcpp,相同)
List
时创建的SEXP
类型进行强制转换。更正该问题并添加缺少的导出标签为我们提供了下面的内容,我还添加了示例调用(另一个不错的功能)。代码
输出
You need cast back from the
SEXP
type you create when adding to an (R or Rcpp, it's the same)List
. Correcting that and adding a missing exports tag gives us what is below where I also added the sample invocation (another nice feature).Code
Output