![](/img/trans.png)
[英]Rcpp: Recommended code structure when using data frames with Rcpp (inline)
[英]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.