直接调用`Rcpp::List`中的元素以供进一步使用

发布于 2025-01-16 21:37:13 字数 835 浏览 1 评论 0原文

在应用程序中,我使用 List 来包含一些变量(doublearma::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-01-23 21:37:13

您需要从添加到(R 或 Rcpp,相同)List 时创建的 SEXP 类型进行强制转换。更正该问题并添加缺少的导出标签为我们提供了下面的内容,我还添加了示例调用(另一个不错的功能)。

代码

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
arma::mat f(){
    arma::mat x = arma::randn(3, 4) ;
    List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;

    return x - Rcpp::as<arma::mat>(y["a"]);
}

/*** R
set.seed(42) # reproducible RNG draws
f()
*/

输出

> Rcpp::sourceCpp("~/git/stackoverflow/71612260/answer.cpp")

> 
set.seed(42) # reproducible RNG draws

> 
f()
          [,1]      [,2]      [,3]      [,4]
[1,] -0.471335 -2.337283  1.205825  0.537811
[2,]  0.119150  1.251941  0.486474 -0.513627
[3,]  3.309860 -0.654003 -0.146678 -0.835439
> 

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

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
arma::mat f(){
    arma::mat x = arma::randn(3, 4) ;
    List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;

    return x - Rcpp::as<arma::mat>(y["a"]);
}

/*** R
set.seed(42) # reproducible RNG draws
f()
*/

Output

> Rcpp::sourceCpp("~/git/stackoverflow/71612260/answer.cpp")

> 
set.seed(42) # reproducible RNG draws

> 
f()
          [,1]      [,2]      [,3]      [,4]
[1,] -0.471335 -2.337283  1.205825  0.537811
[2,]  0.119150  1.251941  0.486474 -0.513627
[3,]  3.309860 -0.654003 -0.146678 -0.835439
> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文