从 R 对象中提取 long[] [英] Extract long[] from R object
问题描述
我正在尝试为一些基于 C 的稀疏矩阵处理代码制作一个包装器(参见 上一个问题).为了调用主力 C 函数,我需要创建一个如下所示的结构:
I'm trying to make a wrapper for some C-based sparse-matrix-handling code (see previous question). In order to call the workhorse C function, I need to create a structure that looks like this:
struct smat {
long rows;
long cols;
long vals; /* Total non-zero entries. */
long *pointr; /* For each col (plus 1), index of first non-zero entry. */
long *rowind; /* For each nz entry, the row index. */
double *value; /* For each nz entry, the value. */
};
这些很好地对应于 dgCMatrix
稀疏矩阵.所以理想情况下,我只指向 dgCMatrix
中的内部数组(在验证 C 函数不会处理数据 [我还没有完成] 之后).
These correspond nicely to the slots in a dgCMatrix
sparse matrix. So ideally I'd just point to the internal arrays in the dgCMatrix
(after verifying that the C function won't twiddle with the data [which I haven't done yet]).
对于*value
,看起来我可以使用REALSXP
或其他东西来根据需要获得double[]
.但是对于 *pointr
和 *rowind
,我不确定获得适当数组的最佳方法.我是否需要遍历条目并将它们复制到新数组中,一边进行一边转换?或者 Rcpp
可以在这里提供一些糖吗?这是我第一次真正使用 Rcpp,但我还不是很熟悉.
For *value
, it looks like I'll be able to use REALSXP
or something to get a double[]
as desired. But for *pointr
and *rowind
, I'm not sure the best way to get at an appropriate array. Will I need to loop through the entries and copy them to new arrays, casting as I go? Or can Rcpp
provide some sugar here? This is the first time I've really used Rcpp much and I'm not well-versed in it yet.
谢谢.
我也遇到了一些我不明白的链接问题:
I'm also having some linking trouble that I don't understand:
Error in dyn.load(libLFile) :
unable to load shared object '/var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so':
dlopen(/var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so, 6): Symbol not found: __Z8svdLAS2AP4smatl
Referenced from: /var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so
Expected in: flat namespace
in /var/folders/TL/TL+wXnanH5uhWm4RtUrrjE+++TM/-Tmp-//RtmpAA9upc/file2d4606aa.so
我是否需要使用一些特殊的编译标志来创建我的库?
Do I need to be creating my library with some special compilation flags?
编辑 2:看起来我的 libargs
参数没有效果,所以 libsvd
符号永远不会进入库.我找不到使用 cxxfunction()
包含库的方法 - 这是我尝试过的,但额外的参数(一厢情愿地从 cfunction()
借来)是默默吞了下去:
Edit 2: it looks like my libargs
parameter has no effect, so libsvd
symbols never make it into the library. I can find no way to include libraries using cxxfunction()
- here's what I'd tried, but the extra parameters (wishful-thinkingly-borrowed from cfunction()
) are silently gobbled up:
fn <- cxxfunction(sig=c(nrow="integer", mi="long", mp="long", mx="numeric"),
body=code,
includes="#include <svdlib.h>\n",
cppargs="-I/Users/u0048513/Downloads/SVDLIBC",
libargs="-L/Users/u0048513/Downloads/SVDLIBC -lsvd",
plugin="Rcpp",
verbose=TRUE)
我觉得我的整个过程都是错误的,因为没有任何效果.有人朝正确的方向踢我吗?
I feel like I'm going about this whole process wrong, since nothing's working. Anyone kick me in the right direction?
推荐答案
我决定也在 Rcpp-devel 邮件列表上发布一个查询,并得到了一些很好的建议 &来自德克和道格的帮助:
I decided to also post a query on the Rcpp-devel mailing list, and got some good advice & help from Dirk and Doug:
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-February/001851.html
我对这些东西仍然不是很容易,但是已经到了.=)
I'm still not super-facile with this stuff, but getting there. =)
这篇关于从 R 对象中提取 long[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!