Мне нужно преобразовать следующий код R в эквивалент r cpp, потому что R очень медленно работает с циклами; Я совершенно новичок в R cpp. Я знаю, что это длинный код, но мне просто нужно знать, синтаксис правильный или неправильный? Я застрял с медленным R для петель, и у меня заканчивается время!
LC<- function(graph,adjMat)
{
LC <- list()
LCV <- matrix()
LCVF <- matrix()
CN <- matrix()
WFM <- matrix()
tabu <- list()
for (i in adjMat$edgeID)
{
if(i %in% tabu){next}
LCV <- t(ends(graph,i,names = TRUE))
CN <- intersect(neighbors(Graph,V(Graph)[LCV[1]],mode = "all"),
neighbors(Graph,V(Graph)[LCV[2]],mode = "all"))
if(length(CN) <= 3 | is.null(CN)){next}
WFM <- WFM(graph,adjMat,LCV)
for(i in CN)
{
condition <- WFM(graph,adjMat,rbind(LCV,i)) >= WFM
if(condition == TRUE)
{
LCVF <- sort(rbind(LCV,i))
tabu <- append(tabu,list(insideOfCommEdgeIDs(graph,LCVF)))
}
}
LCVF <- list(t(LCVF))
LCVF <- list(unique(unlist(LCVF,use.names = FALSE)))
LC <- append(LC,LCVF)
}
return(unique(LC))
}
Вот моя попытка в R cpp:
#include <Rcpp.h>
#include <igraph>
// [[Rcpp::plugins(cpp11)]]
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector LCrcpp(DataFrame g, ComplexMatrix a)
{
// Environment
Environment igraph = Environment("package:igraph");
Environment base = Environment("package:base");
// Functions
Function endIgraph = igraph["ends"];
Function Vrcpp = igraph["V"];
Function neighborsIgraph = igraph["neighbors"];
Function intersectRcpp = base["intersect"];
Function rbindRcpp = base["rbind"];
Function appendRcpp = base["append"];
Function tt = base["t"];
Function listt = base["list"];
Function unlistt = base["unlist"];
Function uniquet = base["unique"];
Function isnull = base["is.null"];
//Variables
Rcpp::List LC;
Rcpp::NumericMatrix LCV;
Rcpp::NumericMatrix LCVF;
Rcpp::NumericMatrix CN;
Rcpp::NumericMatrix WFM;
Rcpp::List Tabu;
Rcpp:bool Flag = false;
for(int i = 0; i <= length(a); ++i)
{
for(int j = 0; j <= length(Tabu); j++)
{
if(a[i][4] = Tabu[j])
{
Flag = true;
break;
}
}
if(Flag)
{
Flag = false;
continue;
}
LCV = endIgraph(a[i][4]);
CN = intersectRcpp(
neighborsIgraph(g,Vrcpp(g)[LCV[0]],mode="all"),
neighborsIgraph(g,Vrcpp(g)[LCV[1]],mode="all")
);
if(length(CN) <= 1 || isnull(CN)){continue;};
for(int i = 0; i <= length(CN); ++i)
{
if(WFM(g,a,rbindRcpp(LCV,CN[i])) >= WFM(g,a,LCV))
{
LCVF = sort(rbindRcpp(LCV,CN[i]));
Tabu = appendRcpp(Tabu,list(insideOfCommEdgeIDs(g,LCVF)));
}
}
LCVF = listt(tt(LCVF));
LCVF = listt(uniquet(unlistt(LCVF,use.names = FALSE)));
LC = appendRcpp(LC,LCVF);
}
return(LC);
}
Я знаю, что это может быть скучно, но кто-то может проверить только синтаксис?