[英]Errors when using OptimLib in Rcpp
I encountered an error when using OptimLib library in Rcpp.First I copied sphere_fn
function online in order to use it later in optim function.在 Rcpp 中使用 OptimLib 库时遇到错误。首先我在线复制
sphere_fn
function 以便稍后在 optim function 中使用它。 However, this shere_fn
didn't work.但是,这个
shere_fn
没有用。
Error: can not initialize a member subobject of type `'arma::Col<double>*'with an lvalue of type 'SEXP'(aka'SEXPREC *').`
It seems the problem was at grad_out,but this input form is required for optim function.似乎问题出在 grad_out,但优化 function 需要此输入表单。
For example, the optim algorithm is called using:例如,使用以下命令调用 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);
Could anyone help me with this?谁能帮我解决这个问题?
My code is:我的代码是:
#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;
}
Well sometime you may need to put in the time to learn to walk before you can compete in races.好吧,有时您可能需要花时间学习走路,然后才能参加比赛。
In other words, you cannot just put semi-arbitrary signatures in there and expect Rcpp Attributes to translate it it all for you.换句话说,您不能只是将半任意签名放在那里并期望 Rcpp 属性为您翻译它。 What is
void *
supposed to map to? map 的
void *
应该是什么? Ditto for arma::vec*.同样适用于 arma::vec*。
Just pass an arma::vec, it will use a pointer internally.只需传递一个 arma::vec,它将在内部使用一个指针。 Study some existing examples from working RcppArmadillo-using package, and maybe glance at some of the vignettes.
从使用 package 的工作 RcppArmadillo 中研究一些现有的例子,也许看看一些小插曲。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.