alb*_*rto 4 c++ r armadillo rcpp
我想在访问RcppArmadillo中的矩阵元素时禁用绑定检查.
犰狳的文件说
可以通过编辑文件include/armadillo_bits/config.hpp来配置Armadillo.通过取消注释或注释掉下面列出的特定#define,可以启用或禁用特定功能.
但是在R包的上下文中,我该如何激活该指令?
我试过用它来创建一个config.h 文件
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif
Run Code Online (Sandbox Code Playgroud)
然后将它包含在我的文件夹的每个 .cpp文件中/src,但我不确定它是否正常工作,或者除了#include "config.h"在每个.cpp文件中添加一个以外还有其他方法.
目前我有一个.cpp(包含主算法的那个),它以:
#include "configs.h"
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
SEXP sample_gibbs_cpp(const arma::vec& v_n, const arma::mat& W,
arma::vec h_n, double alpha = 1, double beta = 1, int iter=100,
double burnin = 0.5){
... code ...
}
Run Code Online (Sandbox Code Playgroud)
然后是其他人
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
... code ...
Run Code Online (Sandbox Code Playgroud)
我的描述文件:
Package: mypackage
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends:
R (>= 3.2.3)
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 5.0.1
Imports:
ggplot2,
dplyr,
tidyr,
rstan
LinkingTo: Rcpp, RcppArmadillo, RcppEigen
SystemRequirements: C++11
Run Code Online (Sandbox Code Playgroud)
我编译我的包:
devtools::load_all()
Run Code Online (Sandbox Code Playgroud)
订单在这里很重要 该#define声明必须包含之前列入#include<RcppArmadillo.h>
一个例子:
custom_config.h
#ifndef CONFIG_LOADED
#define CONFIG_LOADED
#define ARMA_NO_DEBUG
#endif
Run Code Online (Sandbox Code Playgroud)
example_compiled_file.cpp
#include "custom_config.h"
#include <RcppArmadillo.h>
// [[Rcpp::export]]
void test_pkg(const arma::vec& x) {
// Should not trigger error bound checking with debug flag on.
double my_val_protected = x(0);
// Never triggers error bound checking
double my_val = x.at(0);
}
Run Code Online (Sandbox Code Playgroud)
注意:由于这是一个包,因此// [[Rcpp::depends(RcppArmadillo)]]不需要使用.相反,您必须在文件的字段中指定RcppArmadillo并包含在字段中.您必须从函数中最低限度地导入(最好是:) .RcppLinkingTo:DESCRIPTIONRcppImports:RcppevalCpp
例如,描述必须:
Imports: Rcpp (>= 0.12.15)
LinkingTo: Rcpp, RcppArmadillo
Run Code Online (Sandbox Code Playgroud)