作者Wush978 (拒看低质媒体)
看板R_Language
标题Re: [问题] Rcpp 初学
时间Wed Jan 28 21:37:57 2015
※ 引述《gsuper (Logit(odds))》之铭言:
: cppFunction(
: '
: int fx(NumericVector x)
: { int ss = x.size();
: return ss;
: }
: '
: )
: ---------------
: 在上述的 function 中
: 1.NumericVector
: 是否是 Rcpp 提供的特殊变数宣告方式?
: 还是这是正常的 C语言 之宣告?
: [SEXP object]
: Integer(Vector|Matrix) ; integer vectors and matrices
: Numeric(Vector|Matrix) ; numeric
: Logical(Vector|Matrix) ; logical
: Character(Vector|Matrix) ; character
: Raw(Vector|Matrix) ; raw
: Complex(Vector|Matrix) ; complex
: GenericVector ; list
: --------------
short answer: 是
long answer:
这些类别是Rcpp利用C++的物件导向特性,
利用RAII:
http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
的技术,将建立R 物件相关的C API所封装而成的类别
所以的确是Rcpp提供的变数宣告方式。
: 2. x.size()
: 这是类似 size(x) 的感觉
: 请问 size() 是 C 的 function 还是 R 的?
short answer: 是Rcpp的
long answer:
size这个「方法」是Rcpp模仿c++ STL:
http://en.wikipedia.org/wiki/Standard_Template_Library
风格建立的方法,
所以只要include<Rcpp>就有了
Rcpp是刻意要遵循STL 的风格,这样才可以直接套用许多STL 的演算法,如:
NumericVector x = ...;
std::sort(x.begin(), x.end());
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.81.76.108
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1422452280.A.1CF.html
※ 编辑: Wush978 (111.81.76.108), 01/28/2015 21:39:06
1F:推 ee012345: 推一个精辟解释 01/31 02:27
2F:→ gsuper: 将像之前的一串贡丸与一盒贡丸解释 vector 与 matrix 啦 01/31 03:44
3F:→ gsuper: 就 01/31 03:44