【问题标题】:How to get gsl_matrix pointer from from RcppGSL::Matrix如何从 RcppGSL::Matrix 获取 gsl_matrix 指针
【发布时间】:2016-09-26 23:20:23
【问题描述】:

我正在尝试编写一个 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 & 传递给 'const gsl_matrix*' 吗?

【问题讨论】:

    标签: c++ r rcpp


    【解决方案1】:

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

    fastLm.cpp 中,我们这样做了,它会将“向下”的 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);
    
    }
    

    【讨论】:

    • 我的理解是它是 gsl_matrix 的包装器。当上面的 fastlm 函数被调用时,X 和 y 已经有了 gsl_ 数据,并且当 gsl_multifit_linear 被调用时,这些数据被传递给了 gsl 函数。这是错的吗?
    • “代理”是我更喜欢的一个术语,因为没有复制。 Xy 具有相同意义的数据 AB 具有您的功能 run_graph_match
    • mvabund 也使用了大量的 GSL 矩阵,也许看看那里?
    • 我明白你所说的代理是什么意思。考虑这个函数 Rcpp:List RtoAnovaCpp(const List & rparam, RcppGSL::Matrix & Y, RcppGSL::Matrix & X, RcppGSL::Matrix & isXvarIn, Rcpp::Nullable<:matrix> & bID)
    • 它在构造函数 AnovaTest anova(&mm, Y, X, isXvarIn) 中使用 X 和 Y 作为参数。所以 Y 是 gsl_matrix 指针的代理。
    猜你喜欢
    • 2021-04-11
    • 2021-01-07
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2013-12-30
    • 2018-06-14
    相关资源
    最近更新 更多