如何从RcppGSL :: Matrix获取gsl_matrix指针

时间:2016-09-26 23:20:23

标签: c++ r rcpp

我正在尝试编写一个R包,它是使用RcppGSL的C ++库的包装器。我已经成功安装了gsl,并且包检查在Rcpp函数编译停止:

#include <Rcpp.h>
#include <RcppGSL.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <stdlib.h>
#include "graphm-0.52/graph.h"

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_permutation.h>


using namespace std;
using namespace Rcpp;

// declare a dependency on the RcppGSL package; also activates plugin
// (but not needed when ’LinkingTo: RcppGSL’ is used with a package)
//
// [[Rcpp::depends(RcppGSL)]]

//
//
// [[Rcpp::export]]
Rcpp::List run_graph_match(const RcppGSL::Matrix & A, const RcppGSL::Matrix & B, const Rcpp::List &algorithm_params ){
  graph graphm_obj_A;
  graphm_obj_A.set_adjmatrix (((*A)));
  graph graphm_obj_B;
  graphm_obj_B.set_adjmatrix (((*B)));


}

函数graph :: set_adjmatrix(const gsl_matrix * A)接受const gsl_matrix指针。 我的问题是从const RcppGSL :: Matrix引用到const gsl_matrix指针需要什么样的正确引用级别。我想只有一个,但我得到以下错误

graphmatch_rcpp.cpp:31:34: error: no matching function for call to 'graph::set_adjmatrix(RcppGSL::matrix<double>::gsltype&)'
   graphm_obj_A.set_adjmatrix((*A)));
                                  ^
graphmatch_rcpp.cpp:31:34: note: candidate is:
In file included from graphmatch_rcpp.cpp:9:0:
graphm-0.52/graph.h:52:9: note: int graph::set_adjmatrix(const gsl_matrix*)
     int set_adjmatrix(const gsl_matrix* _gm_A);
         ^
graphm-0.52/graph.h:52:9: note:   no known conversion for argument 1 from 'RcppGSL::matrix<double>::gsltype {aka gsl_matrix}' to 'const gsl_matrix*'
graphmatch_rcpp.cpp:33:34: error: no matching function for call to 'graph::set_adjmatrix(RcppGSL::matrix<double>::gsltype&)'
   graphm_obj_B.set_adjmatrix((*B)));

这表明我应该再次引用它并转换为const指针。从RcppGSL :: matrix到gsl_matrix有一些隐式转换,所以我不确定我是否理解细节。任何人都可以更熟悉RcppGSL,Rcpp可以回答我如何通过RcppGSL :: matrix&amp;到'const gsl_matrix *'?

1 个答案:

答案 0 :(得分:0)

RcppGSL :: Matrix是GSL矩阵的代理;它与GSL矩阵不同。如果您使用的“graphm”需要一个我将首先实例化一个。

fastLm.cpp我们这样做,它将'down'上的RcppGSL :: Matrix传递给GSL:

// [[Rcpp::export]]
Rcpp::List fastLm(const RcppGSL::Matrix &X, const RcppGSL::Vector &y) {

    int n = X.nrow(), k = X.ncol();
    double chisq;

    RcppGSL::Vector coef(k);                // to hold the coefficient vector 
    RcppGSL::Matrix cov(k,k);               // and the covariance matrix

    // the actual fit requires working memory we allocate and free
    gsl_multifit_linear_workspace *work = gsl_multifit_linear_alloc (n, k);
    gsl_multifit_linear (X, y, coef, cov, &chisq, work);
    gsl_multifit_linear_free (work);

    // assign diagonal to a vector, then take square roots to get std.error
    Rcpp::NumericVector std_err;
    std_err = gsl_matrix_diagonal(cov);     // need two step decl. and assignment
    std_err = Rcpp::sqrt(std_err);          // sqrt() is an Rcpp sugar function

    return Rcpp::List::create(Rcpp::Named("coefficients") = coef, 
                              Rcpp::Named("stderr")       = std_err,
                              Rcpp::Named("df.residual")  = n - k);

}