Skip to content

Commit

Permalink
update cpp export to r
Browse files Browse the repository at this point in the history
  • Loading branch information
SpatLyu committed Dec 11, 2024
1 parent ec74ae3 commit 7bb9cbd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ RcppGenGridEmbeddings <- function(mat, E) {
.Call(`_spEDM_RcppGenGridEmbeddings`, mat, E)
}

RcppGCCM4Grid <- function(xMatrix, yMatrix, lib_sizes, pred, E) {
.Call(`_spEDM_RcppGCCM4Grid`, xMatrix, yMatrix, lib_sizes, pred, E)
}

RcppConfidence <- function(r, n, level = 0.05) {
.Call(`_spEDM_RcppConfidence`, r, n, level)
}
Expand Down
1 change: 0 additions & 1 deletion src/GCCM4Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ std::vector<std::vector<double>> GCCM4Grid(
const std::vector<std::vector<double>>& xMatrix,
const std::vector<std::vector<double>>& yMatrix,
const std::vector<int>& lib_sizes,
const std::vector<std::pair<int, int>>& lib,
const std::vector<std::pair<int, int>>& pred,
int E,
int tau = 1,
Expand Down
1 change: 0 additions & 1 deletion src/GCCM4Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ std::vector<std::vector<double>> GCCM4Grid(
const std::vector<std::vector<double>>& xMatrix,
const std::vector<std::vector<double>>& yMatrix,
const std::vector<int>& lib_sizes,
const std::vector<std::pair<int, int>>& lib,
const std::vector<std::pair<int, int>>& pred,
int E,
int tau = 1,
Expand Down
58 changes: 58 additions & 0 deletions src/GridExp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <vector>
#include "CppGridUtils.h"
#include "GCCM4Grid.h"
#include <Rcpp.h>

// [[Rcpp::export]]
Expand Down Expand Up @@ -67,3 +68,60 @@ Rcpp::List RcppGenGridEmbeddings(Rcpp::NumericMatrix mat, int E) {

return result;
}

// [[Rcpp::export]]
Rcpp::NumericMatrix RcppGCCM4Grid(
const Rcpp::NumericMatrix& xMatrix,
const Rcpp::NumericMatrix& yMatrix,
const Rcpp::IntegerVector& lib_sizes,
const Rcpp::IntegerMatrix& pred,
int E) {

// Convert Rcpp NumericMatrix to std::vector<std::vector<double>>
std::vector<std::vector<double>> xMatrix_cpp(xMatrix.nrow(), std::vector<double>(xMatrix.ncol()));
for (int i = 0; i < xMatrix.nrow(); ++i) {
for (int j = 0; j < xMatrix.ncol(); ++j) {
xMatrix_cpp[i][j] = xMatrix(i, j);
}
}

// Convert Rcpp NumericMatrix to std::vector<std::vector<double>>
std::vector<std::vector<double>> yMatrix_cpp(yMatrix.nrow(), std::vector<double>(yMatrix.ncol()));
for (int i = 0; i < yMatrix.nrow(); ++i) {
for (int j = 0; j < yMatrix.ncol(); ++j) {
yMatrix_cpp[i][j] = yMatrix(i, j);
}
}

// Convert Rcpp IntegerVector to std::vector<int>
std::vector<int> lib_sizes_cpp(lib_sizes.size());
for (int i = 0; i < lib_sizes.size(); ++i) {
lib_sizes_cpp[i] = lib_sizes[i];
}

// Convert Rcpp IntegerMatrix to std::vector<std::pair<int, int>>
std::vector<std::pair<int, int>> pred_cpp(pred.nrow());
for (int i = 0; i < pred.nrow(); ++i) {
pred_cpp[i] = std::make_pair(pred(i, 0), pred(i, 1));
}

// Call the C++ function GCCM4Grid
std::vector<std::vector<double>> result = GCCM4Grid(
xMatrix_cpp,
yMatrix_cpp,
lib_sizes_cpp,
pred_cpp,
E
);

Rcpp::NumericMatrix resultMatrix(result.size(), 5);
for (size_t i = 0; i < result.size(); ++i) {
resultMatrix(i, 0) = result[i][0];
resultMatrix(i, 1) = result[i][1];
resultMatrix(i, 2) = result[i][2];
resultMatrix(i, 3) = result[i][3];
resultMatrix(i, 4) = result[i][4];
}

return resultMatrix;
}
16 changes: 16 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// RcppGCCM4Grid
Rcpp::NumericMatrix RcppGCCM4Grid(const Rcpp::NumericMatrix& xMatrix, const Rcpp::NumericMatrix& yMatrix, const Rcpp::IntegerVector& lib_sizes, const Rcpp::IntegerMatrix& pred, int E);
RcppExport SEXP _spEDM_RcppGCCM4Grid(SEXP xMatrixSEXP, SEXP yMatrixSEXP, SEXP lib_sizesSEXP, SEXP predSEXP, SEXP ESEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type xMatrix(xMatrixSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type yMatrix(yMatrixSEXP);
Rcpp::traits::input_parameter< const Rcpp::IntegerVector& >::type lib_sizes(lib_sizesSEXP);
Rcpp::traits::input_parameter< const Rcpp::IntegerMatrix& >::type pred(predSEXP);
Rcpp::traits::input_parameter< int >::type E(ESEXP);
rcpp_result_gen = Rcpp::wrap(RcppGCCM4Grid(xMatrix, yMatrix, lib_sizes, pred, E));
return rcpp_result_gen;
END_RCPP
}
// RcppConfidence
Rcpp::NumericVector RcppConfidence(double r, int n, double level);
RcppExport SEXP _spEDM_RcppConfidence(SEXP rSEXP, SEXP nSEXP, SEXP levelSEXP) {
Expand Down Expand Up @@ -106,6 +121,7 @@ END_RCPP
static const R_CallMethodDef CallEntries[] = {
{"_spEDM_RcppLaggedVar4Grid", (DL_FUNC) &_spEDM_RcppLaggedVar4Grid, 2},
{"_spEDM_RcppGenGridEmbeddings", (DL_FUNC) &_spEDM_RcppGenGridEmbeddings, 2},
{"_spEDM_RcppGCCM4Grid", (DL_FUNC) &_spEDM_RcppGCCM4Grid, 5},
{"_spEDM_RcppConfidence", (DL_FUNC) &_spEDM_RcppConfidence, 3},
{"_spEDM_RcppLinearTrendRM", (DL_FUNC) &_spEDM_RcppLinearTrendRM, 4},
{"_spEDM_RcppLaggedVar4Lattice", (DL_FUNC) &_spEDM_RcppLaggedVar4Lattice, 2},
Expand Down

0 comments on commit 7bb9cbd

Please sign in to comment.