在 Rcpp 中使用 OptimLib 时出错
Errors when using OptimLib in Rcpp
我在 Rcpp.First 中使用 OptimLib 库时遇到错误 我在线复制了 sphere_fn
函数,以便稍后在 optim 函数中使用它。但是,这个 shere_fn
没有用。
Error: can not initialize a member subobject of type `'arma::Col<double>*'with an lvalue of type 'SEXP'(aka'SEXPREC *').`
看来问题出在grad_out,但是optim函数需要这个输入表单。
例如调用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 Attributes 为你翻译它。 void *
应该映射到什么? arma::vec*.
同上
只需传递一个arma::vec,它就会在内部使用一个指针。研究使用 RcppArmadillo 的包中的一些现有示例,也许还可以浏览一些小插曲。
我在 Rcpp.First 中使用 OptimLib 库时遇到错误 我在线复制了 sphere_fn
函数,以便稍后在 optim 函数中使用它。但是,这个 shere_fn
没有用。
Error: can not initialize a member subobject of type `'arma::Col<double>*'with an lvalue of type 'SEXP'(aka'SEXPREC *').`
看来问题出在grad_out,但是optim函数需要这个输入表单。
例如调用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 Attributes 为你翻译它。 void *
应该映射到什么? arma::vec*.
只需传递一个arma::vec,它就会在内部使用一个指针。研究使用 RcppArmadillo 的包中的一些现有示例,也许还可以浏览一些小插曲。