gpt4 book ai didi

c - 使用 C 减少使用 R 中的结构的参数数量

转载 作者:行者123 更新时间:2023-12-04 05:42:15 24 4
gpt4 key购买 nike

我想创建某种结构来减少从 R 文件调用的参数数量。

在我的 R 文件(example.R)中,我有类似的内容:

  ret <- .Call("example", 

## data
as.double (t(x)),
as.integer (nr),
as.integer(ncol(x)),
as.double (y),
as.integer (nclass),
as.integer (cross),
.....
)

然后我的接口(interface) C 文件看起来像:
SEXP example(SEXP x, SEXP rows, SEXP cols, 
SEXP y, SEXP nclass, SEXP cross, SEXP sp_rows)
{
PROTECT( x = AS_NUMERIC( x ) );
PROTECT( y = AS_NUMERIC( y ) );

PROTECT( cross = AS_INTEGER( cross ) );
PROTECT( rows = AS_INTEGER( rows ) );
PROTECT( cols = AS_INTEGER( cols ) );
PROTECT( nclass = AS_INTEGER( nclass ) );
PROTECT( sp_rows = AS_INTEGER( sp_rows ) );

x_matrix = NUMERIC_POINTER(x);
y_vector = NUMERIC_POINTER(y);

int num_rows = INTEGER_VALUE(rows);
.....

这个想法是在 .R 文件中创建某种结构,以便我可以读取 .C 文件中的参数。因为需要的参数数量可能会增加,所以代码的人类可读性会急剧下降。

最佳答案

这是 RcppExamples 中的一个示例包裹:

RcppExport SEXP newRcppParamsExample(SEXP params) {

try { // or use BEGIN_RCPP macro

Rcpp::List rparam(params); // Get parameters in params.
std::string method = Rcpp::as<std::string>(rparam["method"]);
double tolerance = Rcpp::as<double>(rparam["tolerance"]);
int maxIter = Rcpp::as<int>(rparam["maxIter"]);
Rcpp::Date startDate = Rcpp::Date(Rcpp::as<int>(rparam["startDate"]));

Rprintf("\nIn C++, seeing the following value\n");
Rprintf("Method argument : %s\n", method.c_str());
Rprintf("Tolerance argument : %f\n", tolerance);
Rprintf("MaxIter argument : %d\n", maxIter);
Rprintf("Start date argument: %04d-%02d-%02d\n",
startDate.getYear(), startDate.getMonth(), startDate.getDay());

return Rcpp::List::create(Rcpp::Named("method", method),
Rcpp::Named("tolerance", tolerance),
Rcpp::Named("maxIter", maxIter),
Rcpp::Named("startDate", startDate),
Rcpp::Named("params", params));

} catch( std::exception &ex ) { // or use END_RCPP macro
forward_exception_to_r( ex );
} catch(...) {
::Rf_error( "c++ exception (unknown reason)" );
}
return R_NilValue; // -Wall
}

这是来自 R 的匹配调用:
RcppParamsExample <- function(params,
api=c("classic", "new")) {

api <- match.arg(api) # match to classic or new
fun <- paste(api, "RcppParamsExample", sep="")

## Check that params is properly set.
if (missing(params)) {
cat("\nIn R, setting default argument for params\n")
params <- list(method='BFGS',
tolerance=1.0e-8,
maxIter=1000,
startDate=as.Date('2006-7-15'))
}

## Make the call...
val <- .Call(fun,
params,
PACKAGE="RcppExamples")

val
}

关于c - 使用 C 减少使用 R 中的结构的参数数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11139907/

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