繁体   English   中英

在 Rcpp 中使用 OptimLib 时出错

[英]Errors when using OptimLib in Rcpp

在 Rcpp 中使用 OptimLib 库时遇到错误。首先我在线复制sphere_fn function 以便稍后在 optim function 中使用它。 但是,这个shere_fn没有用。

Error: can not initialize a member subobject of type `'arma::Col<double>*'with an lvalue of type 'SEXP'(aka'SEXPREC *').` 

似乎问题出在 grad_out,但优化 function 需要此输入表单。

例如,使用以下命令调用 optim 算法:

bool cg(arma::vec& init_out_vals, std::function<double (const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)> opt_objfn, void* opt_data);  

谁能帮我解决这个问题?

我的代码是:

#include <iostream>
#include <math.h>       /* sqrt */

#define USE_RCPP_ARMADILLO
#include "optim.hpp"

// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace std;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
//   http://www.rcpp.org/
//   http://adv-r.had.co.nz/Rcpp.html
//   http://gallery.rcpp.org/
//


// [[Rcpp::export]]
double sphere_fn(const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)
{
  double obj_val = arma::dot(vals_inp,vals_inp);
  //
  if (grad_out) {
    *grad_out = 2.0*vals_inp;
  }
  //
  return obj_val;
}

好吧,有时您可能需要花时间学习走路,然后才能参加比赛。

换句话说,您不能只是将半任意签名放在那里并期望 Rcpp 属性为您翻译它。 map 的void *应该是什么? 同样适用于 arma::vec*。

只需传递一个 arma::vec,它将在内部使用一个指针。 从使用 package 的工作 RcppArmadillo 中研究一些现有的例子,也许看看一些小插曲。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM