rcpp - 使用 RcppArmadillo 时调用 one 或 eye 函数失败
问题描述
我想使用one
或eye
函数Armadillo
来构造矩阵或向量。但是,它不允许我这样做。这是一个示例代码:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
SEXP Generate(arma::mat Mat){
arma::mat Mat_2 = ones<mat>(5,6);
}
错误信息让我想起use of undeclared idenfier of 'mat'
. 当我删除<mat>
时,另一个按摩说use of undeclared idenfier of 'ones'
。
我查找了包含该ones
功能的犰狳教程。我想知道为什么我的代码无法调用它。我错过了什么?
解决方案
你的代码有几个问题:
- 为什么要回来
SEXP
?使用类型对您有利 Mat
如果你不使用它为什么要传入?- 无
return
声明 - 名称空间的使用有点松散。
清理后的版本如下:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat Generate(int n=5, int k=6){
arma::mat m = arma::ones<arma::mat>(n,k);
return m;
}
/*** R
Generate()
*/
它编译并运行良好:
> Rcpp::sourceCpp("~/git/stackoverflow/67006975/answer.cpp")
> Generate()
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 1 1 1 1 1 1
[3,] 1 1 1 1 1 1
[4,] 1 1 1 1 1 1
[5,] 1 1 1 1 1 1
>
推荐阅读
- c# - 如何从 _Loginpartial.cshtml 存储用户名并在 .cs 文件中使用它
- ddev - 带有 DDEV 的 LDAP 的 Apache 配置
- javascript - 将 API 发布到我的数据库,JSON 不会加载
- ruby-on-rails - 如何在 Refinery CMS 中启动并运行功能规范登录助手?
- django - 如何在 Django 模型上创建一个重复的通用字段,该字段不会在管理员中呈现来自其他对象的条目?
- linux - 如何在 bash 中动态创建变量?
- ios - 如何在iOS中平均拉伸图像的左右边缘?
- python - 通过 pysftp 附加到 SFTP 服务器上的现有文件
- python - python - 为什么我的def没有调用就自动运行
- swift - 当child为列表时,按child查询过滤