gpt4 book ai didi

c - 在R中调用C时是否有可访问的 vector 接口(interface)函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:52 24 4
gpt4 key购买 nike

什么时候在R中使用.Call.C与 vector 参数相关,我目前的做法是处理一些属性,比如长度、最大值等,在 R 中,然后将这些属性作为参数传递给 C 函数。

来自 R extension ,至少有一个函数名 length 可用。那么C中是否有类似R vector 函数的接口(interface),如maxminrep

最佳答案

Rcpp 具有minmaxrep 等基本函数。考虑以下示例(假设它称为 example.cpp):

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector exampleMinMax(NumericVector x) {
NumericVector out(2);
out[0] = min(x);
out[1] = max(x);
return out;
}

// [[Rcpp::export]]
NumericVector exampleRep(NumericVector x, int n) {
NumericVector out = rep_each(x, n);
return out;
}

然后在 R 中你可以做:

library(Rcpp)
sourceCpp("example.cpp")
exampleMinMax(1:10)

[1] 1 10

exampleRep(1:10, 2)

[1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10

关于c - 在R中调用C时是否有可访问的 vector 接口(interface)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43420179/

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