首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在cpp11代码中使用带整数的循环

在cpp11代码中使用带整数的循环
EN

Stack Overflow用户
提问于 2022-04-03 06:02:35
回答 1查看 158关注 0票数 -2

我正在尝试为一个赋值编写一个cpp11 (R包)代码,并且我遇到了一个与Rcpp (另一个R包)无关的问题。

Rcpp和cpp11都允许在R中使用C++用户定义的函数。例如,使用它们,我可以编写一个函数来获得两个变量之间的关联,除了一个非标准的标头(如[[cpp11::register]] )之外,我还需要定义诸如"x是一个双重向量“这样的变量。

我是从https://github.com/lrberge/fixest/blob/master/src/misc_funs.cpp#L62-L81改编的,除了独立的Stan代码,但这是一个MWE。但是,除了https://cpp11.r-lib.org/articles/converting.html之外,我找不到很多调整现有Rcpp代码的例子。

Rcpp

我创建了一个文件"rcpp_case.cpp“

代码语言:javascript
运行
复制
#include <Rcpp.h>
#include <math.h>

using namespace Rcpp;
using namespace std;

// [[Rcpp::export]]
NumericVector RcppFun(int Q, IntegerVector nbCluster) {
  int q;
  int sum_cases=0;
  IntegerVector start(Q), end(Q);

  for(q=0 ; q<Q ; q++){
    // the total number of clusters (eg if man/woman and 10 countries: total of 12 cases)
    sum_cases += nbCluster(q);
    if(q == 0){
      start(q) = 0;
      end(q) = nbCluster(q);
    } else {
      start(q) = start(q-1) + nbCluster(q-1);
      end(q) = end(q-1) + nbCluster(q);
    }
  }

  return(q);
}

R

在R中,我可以使用cpp文件中包含的函数:

代码语言:javascript
运行
复制
library(Rcpp)

sourceCpp("RcppFun.cpp")

RcppFun(Q = c(2L), nbCluster = c(1L,2L,3L))

> RcppFun(Q = c(2L), nbCluster = c(1L,2L,3L))
[1] 0 0

Rcpp -> cpp11

代码语言:javascript
运行
复制
#include <cpp11.hpp>
#include <math.h>

using namespace cpp11;
using namespace std;

[[cpp11::register]]
doubles cpp11_fun(int Q, integers nbCluster) {
  int q;
  int sum_cases=0;
  writable::integers start(Q), end(Q);

  for(q=0 ; q<Q ; q++){
    // the total number of clusters (eg if man/woman and 10 countries: total of 12 cases)
    sum_cases += nbCluster(q);
    if(q == 0){
      start(q) = 0;
      end(q) = nbCluster(q);
    } else {
      start(q) = start(q-1) + nbCluster(q-1);
      end(q) = end(q-1) + nbCluster(q);
    }
  }

  return(q);
}
代码语言:javascript
运行
复制
library(cpp11)

cpp_source("cpp11_fun.cpp")

> cpp_source("cpp11_fun.cpp")
/imputation-model/cpp11_fun.cpp: In function ‘cpp11::doubles cpp11_fun(int, cpp11::integers)’:
/imputation-model/cpp11_fun.cpp:17:29: error: no match for call to ‘(cpp11::integers {aka cpp11::r_vector<int>}) (int&)’
   17 |     sum_cases += nbCluster(q);
      |                             ^
/imputation-model/cpp11_fun.cpp:19:14: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int&)’
   19 |       start(q) = 0;
      |              ^
/imputation-model/cpp11_fun.cpp:20:12: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int&)’
   20 |       end(q) = nbCluster(q);
      |            ^
/imputation-model/cpp11_fun.cpp:20:27: error: no match for call to ‘(cpp11::integers {aka cpp11::r_vector<int>}) (int&)’
   20 |       end(q) = nbCluster(q);
      |                           ^
/imputation-model/cpp11_fun.cpp:22:14: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int&)’
   22 |       start(q) = start(q-1) + nbCluster(q-1);
      |              ^
/imputation-model/cpp11_fun.cpp:22:27: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int)’
   22 |       start(q) = start(q-1) + nbCluster(q-1);
      |                           ^
/imputation-model/cpp11_fun.cpp:22:44: error: no match for call to ‘(cpp11::integers {aka cpp11::r_vector<int>}) (int)’
   22 |       start(q) = start(q-1) + nbCluster(q-1);
      |                                            ^
/imputation-model/cpp11_fun.cpp:23:12: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int&)’
   23 |       end(q) = end(q-1) + nbCluster(q);
      |            ^
/imputation-model/cpp11_fun.cpp:23:23: error: no match for call to ‘(cpp11::writable::integers {aka cpp11::writable::r_vector<int>}) (int)’
   23 |       end(q) = end(q-1) + nbCluster(q);
      |                       ^
/imputation-model/cpp11_fun.cpp:23:38: error: no match for call to ‘(cpp11::integers {aka cpp11::r_vector<int>}) (int&)’
   23 |       end(q) = end(q-1) + nbCluster(q);
      |                                      ^
/imputation-model/cpp11_fun.cpp:27:11: error: invalid conversion from ‘int’ to ‘SEXP’ {aka ‘SEXPREC*’} [-fpermissive]
   27 |   return(q);
      |           ^
      |           |
      |           int
In file included from /usr/lib/R/site-library/cpp11/include/cpp11/list.hpp:10,
                 from /usr/lib/R/site-library/cpp11/include/cpp11/data_frame.hpp:12,
                 from /usr/lib/R/site-library/cpp11/include/cpp11.hpp:7,
                 from /imputation-model/cpp11_fun.cpp:1:
/usr/lib/R/site-library/cpp11/include/cpp11/r_vector.hpp:368:41: note:   initializing argument 1 of ‘cpp11::r_vector<T>::r_vector(SEXP) [with T = double; SEXP = SEXPREC*]’
  368 | inline r_vector<T>::r_vector(const SEXP data)
      |                              ~~~~~~~~~~~^~~~
make: *** [/usr/lib/R/etc/Makeconf:177: /imputation-model/cpp11_fun.o] Error 1
Error: Compilation failed.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-07 21:04:41

您有几个小错误,这些错误在积累过程中产生了几条错误消息:

您的签名返回一个int

  • your

  • doublereturn语句有一个未使用的变量(只是警告)

  • --您用圆括号对向量进行索引,cpp11似乎更喜欢方括号

以下工作如下:

代码语言:javascript
运行
复制
#include <cpp11.hpp>
#include <math.h>

using namespace cpp11;
using namespace std;

[[cpp11::register]]
int cpp11_fun(int Q, integers nbCluster) {
    int q;
    int sum_cases=0;
    writable::integers start(Q), end(Q);

    for(q=0 ; q<Q ; q++){
        // total nb of clusters (eg if man/woman and 10 countries)
        sum_cases += nbCluster[q];
        if (q == 0){
            start[q] = 0;
            end[q] = nbCluster[q];
        } else {
            start[q] = start[q-1] + nbCluster[q-1];
            end[q] = end[q-1] + nbCluster[q];
        }
    }
    return(q);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71723394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档