在R中,如果我们有一个数据矩阵,比如一个100乘10矩阵X,一个带有可能值(0,1,2,3)的100个元素矢量t,我们可以很容易地找到一个简单的X矩阵y句法:
y = X[t == 1, ]
Run Code Online (Sandbox Code Playgroud)
但是,问题是,我怎么能用Rcpp的NumericMatrix做到这一点?
(或者,更一般地说,我怎么能在C++的任何容器中做到这一点?)
感谢Dirk的暗示,似乎是这样
NumericMatrix X(dataX);
IntegerVector T(dataT);
mat Xmat(X.begin(), X.nrow(), X.ncol(), false);
vec tIdx(T.begin(), T.size(), false);
mat y = X.rows(find(tIdx == 1));
Run Code Online (Sandbox Code Playgroud)
可以做我想做的事,但这似乎太冗长了.
我想同时使用RcppGSL和RcppArmadillo,是否可以在cxxfunction(内联CRAN软件包的)中使用多个插件?我找到了一种方法:
plug.ArmaGSL <- Rcpp:::Rcpp.plugin.maker(include.before='#include <RcppArmadillo.h>
#include <RcppGSL.h>
#include <gsl/gsl_rng.h>',
Depends=c("RcppGSL", "RcppArmadillo", "Rcpp"),
LinkingTo=c("RcppGSL", "RcppArmadillo", "Rcpp"),
libs="-lgsl -larmadillo")
registerPlugin("RcppArmaGSL", plug.ArmaGSL)
foo <- cxxfunction(signature(sM="numeric"), body=bodytxt, inc=inctxt, plugin="RcppArmaGSL")
Run Code Online (Sandbox Code Playgroud)
但似乎不那么直观。
我尝试学习Grand Central Dispatch(GCD)并使用以下代码进行测试:
使用GCD:
#include <dispatch/dispatch.h>
#include <vector>
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
const int N = atoi(argv[1]);
__block std::vector<int> a(N, 0);
dispatch_apply(N,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(size_t i)
{
a[i] = i;
#ifdef DEBUG
if ( i % atoi(argv[2]) == 0)
std::cout << a[i] << std::endl;
#endif
});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有GCD:
#include <vector>
#include <cstdlib>
#include <iostream>
int main(int argc, char *argv[])
{
const int N = atoi(argv[1]);
std::vector<int> a(N, 0);
for (int i = …Run Code Online (Sandbox Code Playgroud) 我想在Haskell中使用正则表达式将latex命令替换为字符
我尝试在ghci中进行以下正则表达式替换:
> putStrLn $
subRegex (mkRegex "(\\mbfA)([^[:alnum:]])") "\\mbfA \\mbfAlpha" "o\\2"
Run Code Online (Sandbox Code Playgroud)
得到这个结果:
\o \mbfAlpha
Run Code Online (Sandbox Code Playgroud)
这不是我想要的,我想要的是o \mbfAlpha.
在Python中,我可以得到我想要的东西:
In [7]: print(re.sub(r"(\\mbfA)(\W)", "o\\2", "\\mbfA \\mbfAlpha"))
o \mbfAlpha
Run Code Online (Sandbox Code Playgroud)
在Haskell中有什么方法可以用包含反斜杠的正则表达式替换字符串吗?
在下面的C++代码中,我想使用模板函数来确定两个向量是否完全相同,但是,我总是从模板函数中得到错误.你能给我一些关于如何从模板函数返回布尔值的建议吗?(我的C++编译器是g ++ 4.6)
编辑:在pop_back p1 p2 p3 p4之后,结果现在与我的预期相匹配.
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
template<class T> bool areTheyMatched(shared_ptr<vector<T>> p1, shared_ptr<vector<T>> p2) {
if ((*p1).size() == (*p2).size()) {
cout << (*p1).size() << endl;
for (unsigned int i = 0; i < (*p1).size(); i++) {
if ((*p1)[i] != (*p2)[i]) {
cout << (*p1)[i] << " " << (*p2)[i] << endl;
return false;
}
}
} else {
return false;
}
cout << "All elements are exactly the same" << …Run Code Online (Sandbox Code Playgroud)