简体   繁体   English

C ++中的矩阵/向量与R math独立库?

[英]Matrices/Vectors in C++ with the R math standalone library?

All, 所有,

I have been playing with the R math standalone library in C++. 我一直在使用C ++中的R math独立库。 I quite like being able to generate random numbers and use distribution functions that I am comfortable with from R. My question is: is it possible to use the matrix operations (multiplication, transpose, inverse, Chol, etc) that are available in R in a standalone library? 我非常希望能够生成随机数并使用R中我熟悉的分布函数。我的问题是:是否可以使用R中可用的矩阵运算(乘法,转置,逆,Chol等)一个独立的图书馆? I don't see them in Rmath.h. 我在Rmath.h中没有看到它们。

If matrix operations are available to use in standalone C++ code, it seems that the R API becomes the perfect open source computational engine. 如果可以在独立的C ++代码中使用矩阵运算,那么R API似乎成为完美的开源计算引擎。 Are people using it in this fashion? 人们是否以这种方式使用它?

Thanks so much for your guidance and suggestions! 非常感谢您的指导和建议!

Most, if not all, of the things you mention are provided by LAPACK or BLAS Fortran code that R links to, not something R provides new code for itself. 大多数(如果不是全部)你提到的东西都是由R链接的LAPACK或BLAS Fortran代码提供的,而不是R为自己提供新代码的东西。

If you are interested in using C++ with R, look at the Rcpp package by Dirk Eddelbuettel and Romain Francois. 如果您对使用C ++和R感兴趣,请查看Dirk Eddelbuettel和Romain Francois的Rcpp软件包 Dirk has also written the RcppArmadillo package as an interface to the Armadillo C++ linear algebra library that can do the matrix operations you mention. Dirk还编写了RcppArmadillo软件包作为Armadillo C ++线性代数库的接口,可以执行您提到的矩阵运算。

Whether this is of use will depend on whether you are wanting to write separate C++ code that is accessed outside R, or interfacing C++ code that you access within R. Rcpp facilitates (greatly) the latter. 这是否有用将取决于您是否想要编写在R外部访问的单独C ++代码,或者在R中访问您访问的C ++代码.Rcpp(大大)促进后者。 Take a look at Armadillo directly if it is the former situation. 如果是以前的情况,请直接看看Armadillo。

I think the original questions starts at the wrong end. 我认为原始问题从错误的一端开始。 There is no C++ in R, and no C++ API in R itself. R中没有C ++,R本身也没有C ++ API。

So if you want to access R matrix functions, you have to go through the R API -- which is a C layer and very much not C++. 因此,如果要访问R矩阵函数,则必须通过R API - 这是一个C层而非 C ++。

Gavin in his answer (and comments) and Josh are spot-on: You probably want something like Armadillo for high-level matrix algebra. Gavin在他的回答(和评论)和Josh中都是正确的:你可能想要像Armadillo这样的高级矩阵代数。 Armadillo (just like related libraries) eventually goes to BLAS and LAPACK --- as does R. [ Doug Bates usually reminds us that there is one important difference related to pivoted decompositions; Armadillo(就像相关的库一样)最终进入BLAS和LAPACK ---就像R一样。[Doug Bates通常提醒我们,与旋转分解有一个重要的区别; this is touched upon in the fastLm() implementation and example in RcppArmadillo. 这在fastLm()实现和示例中有所涉及。 ] ]

Lastly, Rcpp can help with both cases: 最后,Rcpp可以帮助解决这两种情况:

  • whether you want to extend R by calling C++ code you wrote, where Rcpp makes it easy to pass objects back and forth, or 是否要通过调用您编写的C ++代码来扩展R,其中Rcpp可以轻松地来回传递对象,或者

  • whether you want to embed R inside your C++ application using RInside as Rcpp once again provides the glue 是否要使用RInside将R嵌入到C ++应用程序中,因为Rcpp再次提供了粘合剂

The rcpp-devel list is a good place for more in-depth discussions and examples. rcpp-devel列表是进行更深入讨论和示例的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM