From f34ef73f0c2f60c43074b5ef7976446c9f9d1398 Mon Sep 17 00:00:00 2001 From: adamwang15 Date: Sat, 10 Aug 2024 11:37:13 +1000 Subject: [PATCH] separate Df #43 --- inst/include/bsvarSIGNs_RcppExports.h | 42 +++++++++++++++ src/RcppExports.cpp | 78 +++++++++++++++++++++++++++ src/restrictions_zero.cpp | 67 ++++++++++++++++++++++- 3 files changed, 185 insertions(+), 2 deletions(-) diff --git a/inst/include/bsvarSIGNs_RcppExports.h b/inst/include/bsvarSIGNs_RcppExports.h index e99db65..2a45b8e 100644 --- a/inst/include/bsvarSIGNs_RcppExports.h +++ b/inst/include/bsvarSIGNs_RcppExports.h @@ -340,6 +340,48 @@ namespace bsvarSIGNs { return Rcpp::as(rcpp_result_gen); } + inline arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { + typedef SEXP(*Ptr_D_z)(SEXP,SEXP,SEXP); + static Ptr_D_z p_D_z = NULL; + if (p_D_z == NULL) { + validateSignature("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); + p_D_z = (Ptr_D_z)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z"); + } + RObject rcpp_result_gen; + { + RNGScope RCPP_rngScope_gen; + rcpp_result_gen = p_D_z(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); + } + if (rcpp_result_gen.inherits("interrupted-error")) + throw Rcpp::internal::InterruptedException(); + if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) + throw Rcpp::LongjumpException(rcpp_result_gen); + if (rcpp_result_gen.inherits("try-error")) + throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); + return Rcpp::as(rcpp_result_gen); + } + + inline arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h = 1e-10) { + typedef SEXP(*Ptr_D_gf)(SEXP,SEXP,SEXP); + static Ptr_D_gf p_D_gf = NULL; + if (p_D_gf == NULL) { + validateSignature("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); + p_D_gf = (Ptr_D_gf)R_GetCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf"); + } + RObject rcpp_result_gen; + { + RNGScope RCPP_rngScope_gen; + rcpp_result_gen = p_D_gf(Shield(Rcpp::wrap(Z)), Shield(Rcpp::wrap(x)), Shield(Rcpp::wrap(h))); + } + if (rcpp_result_gen.inherits("interrupted-error")) + throw Rcpp::internal::InterruptedException(); + if (Rcpp::internal::isLongjumpSentinel(rcpp_result_gen)) + throw Rcpp::LongjumpException(rcpp_result_gen); + if (rcpp_result_gen.inherits("try-error")) + throw Rcpp::exception(Rcpp::as(rcpp_result_gen).c_str()); + return Rcpp::as(rcpp_result_gen); + } + inline double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus) { typedef SEXP(*Ptr_log_volume_element)(SEXP,SEXP,SEXP); static Ptr_log_volume_element p_log_volume_element = NULL; diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 12d3c87..42b6dcf 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -568,6 +568,78 @@ RcppExport SEXP _bsvarSIGNs_g_fh_vec(SEXP ZSEXP, SEXP vec_structuralSEXP) { UNPROTECT(1); return rcpp_result_gen; } +// D_z +arma::mat D_z(const arma::field& Z, const arma::vec& x, const double h); +static SEXP _bsvarSIGNs_D_z_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); + Rcpp::traits::input_parameter< const double >::type h(hSEXP); + rcpp_result_gen = Rcpp::wrap(D_z(Z, x, h)); + return rcpp_result_gen; +END_RCPP_RETURN_ERROR +} +RcppExport SEXP _bsvarSIGNs_D_z(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { + SEXP rcpp_result_gen; + { + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = PROTECT(_bsvarSIGNs_D_z_try(ZSEXP, xSEXP, hSEXP)); + } + Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); + if (rcpp_isInterrupt_gen) { + UNPROTECT(1); + Rf_onintr(); + } + bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); + if (rcpp_isLongjump_gen) { + Rcpp::internal::resumeJump(rcpp_result_gen); + } + Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); + if (rcpp_isError_gen) { + SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); + UNPROTECT(1); + Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); + } + UNPROTECT(1); + return rcpp_result_gen; +} +// D_gf +arma::mat D_gf(const arma::field& Z, const arma::vec& x, const double h); +static SEXP _bsvarSIGNs_D_gf_try(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::traits::input_parameter< const arma::field& >::type Z(ZSEXP); + Rcpp::traits::input_parameter< const arma::vec& >::type x(xSEXP); + Rcpp::traits::input_parameter< const double >::type h(hSEXP); + rcpp_result_gen = Rcpp::wrap(D_gf(Z, x, h)); + return rcpp_result_gen; +END_RCPP_RETURN_ERROR +} +RcppExport SEXP _bsvarSIGNs_D_gf(SEXP ZSEXP, SEXP xSEXP, SEXP hSEXP) { + SEXP rcpp_result_gen; + { + Rcpp::RNGScope rcpp_rngScope_gen; + rcpp_result_gen = PROTECT(_bsvarSIGNs_D_gf_try(ZSEXP, xSEXP, hSEXP)); + } + Rboolean rcpp_isInterrupt_gen = Rf_inherits(rcpp_result_gen, "interrupted-error"); + if (rcpp_isInterrupt_gen) { + UNPROTECT(1); + Rf_onintr(); + } + bool rcpp_isLongjump_gen = Rcpp::internal::isLongjumpSentinel(rcpp_result_gen); + if (rcpp_isLongjump_gen) { + Rcpp::internal::resumeJump(rcpp_result_gen); + } + Rboolean rcpp_isError_gen = Rf_inherits(rcpp_result_gen, "try-error"); + if (rcpp_isError_gen) { + SEXP rcpp_msgSEXP_gen = Rf_asChar(rcpp_result_gen); + UNPROTECT(1); + Rf_error("%s", CHAR(rcpp_msgSEXP_gen)); + } + UNPROTECT(1); + return rcpp_result_gen; +} // log_volume_element double log_volume_element(const arma::field& Z, const arma::mat& A0, const arma::mat& Aplus); static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) { @@ -1061,6 +1133,8 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) { signatures.insert("arma::colvec(*zero_restrictions)(const arma::field&,const arma::colvec)"); signatures.insert("arma::colvec(*g_fh)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::colvec(*g_fh_vec)(const arma::field&,const arma::colvec)"); + signatures.insert("arma::mat(*D_z)(const arma::field&,const arma::vec&,const double)"); + signatures.insert("arma::mat(*D_gf)(const arma::field&,const arma::vec&,const double)"); signatures.insert("double(*log_volume_element)(const arma::field&,const arma::mat&,const arma::mat&)"); signatures.insert("double(*weight_zero)(const arma::field&,const arma::mat&,const arma::mat&,const arma::mat&)"); signatures.insert("arma::mat(*rzeroQ)(const arma::field&,const arma::mat&)"); @@ -1090,6 +1164,8 @@ RcppExport SEXP _bsvarSIGNs_RcppExport_registerCCallable() { R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_zero_restrictions", (DL_FUNC)_bsvarSIGNs_zero_restrictions_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh", (DL_FUNC)_bsvarSIGNs_g_fh_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_g_fh_vec", (DL_FUNC)_bsvarSIGNs_g_fh_vec_try); + R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_z", (DL_FUNC)_bsvarSIGNs_D_z_try); + R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_D_gf", (DL_FUNC)_bsvarSIGNs_D_gf_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_log_volume_element", (DL_FUNC)_bsvarSIGNs_log_volume_element_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_weight_zero", (DL_FUNC)_bsvarSIGNs_weight_zero_try); R_RegisterCCallable("bsvarSIGNs", "_bsvarSIGNs_rzeroQ", (DL_FUNC)_bsvarSIGNs_rzeroQ_try); @@ -1118,6 +1194,8 @@ static const R_CallMethodDef CallEntries[] = { {"_bsvarSIGNs_zero_restrictions", (DL_FUNC) &_bsvarSIGNs_zero_restrictions, 2}, {"_bsvarSIGNs_g_fh", (DL_FUNC) &_bsvarSIGNs_g_fh, 3}, {"_bsvarSIGNs_g_fh_vec", (DL_FUNC) &_bsvarSIGNs_g_fh_vec, 2}, + {"_bsvarSIGNs_D_z", (DL_FUNC) &_bsvarSIGNs_D_z, 3}, + {"_bsvarSIGNs_D_gf", (DL_FUNC) &_bsvarSIGNs_D_gf, 3}, {"_bsvarSIGNs_log_volume_element", (DL_FUNC) &_bsvarSIGNs_log_volume_element, 3}, {"_bsvarSIGNs_weight_zero", (DL_FUNC) &_bsvarSIGNs_weight_zero, 4}, {"_bsvarSIGNs_rzeroQ", (DL_FUNC) &_bsvarSIGNs_rzeroQ, 2}, diff --git a/src/restrictions_zero.cpp b/src/restrictions_zero.cpp index f091cae..4ef714e 100644 --- a/src/restrictions_zero.cpp +++ b/src/restrictions_zero.cpp @@ -101,6 +101,69 @@ arma::colvec g_fh_vec( return g_fh(Z, A0, Aplus); } +// [[Rcpp::interfaces(cpp)]] +// [[Rcpp::export]] +arma::mat D_z( + const arma::field& Z, + const arma::vec& x, + const double h = 1e-10 +) +{ + vec f_x = zero_restrictions(Z, x); + + int n = x.n_elem; + int m = f_x.n_elem; + + mat result(m, n); + + for (int i = 0; i < n; i++) + { + vec x_plus_h = x; + x_plus_h(i) += h; + + vec f_plus_h = zero_restrictions(Z, x_plus_h); + + for (int j = 0; j < m; j++) + { + result(j, i) = (f_plus_h(j) - f_x(j)) / h; + } + } + + return result; +} + + +// [[Rcpp::interfaces(cpp)]] +// [[Rcpp::export]] +arma::mat D_gf( + const arma::field& Z, + const arma::vec& x, + const double h = 1e-10 +) +{ + vec f_x = g_fh_vec(Z, x); + + int n = x.n_elem; + int m = f_x.n_elem; + + mat result(m, n); + + for (int i = 0; i < n; i++) + { + vec x_plus_h = x; + x_plus_h(i) += h; + + vec f_plus_h = g_fh_vec(Z, x_plus_h); + + for (int j = 0; j < m; j++) + { + result(j, i) = (f_plus_h(j) - f_x(j)) / h; + } + } + + return result; +} + // log volume element // [[Rcpp::interfaces(cpp)]] @@ -112,8 +175,8 @@ double log_volume_element( ) { colvec vec_structural = join_vert(vectorise(A0), vectorise(Aplus)); - mat Dz = Df([Z](const colvec& x) { return zero_restrictions(Z, x); }, vec_structural); - mat Dgf = Df([Z](const colvec& x) { return g_fh_vec(Z, x); }, vec_structural); + mat Dz = D_z(Z, vec_structural); + mat Dgf = D_gf(Z, vec_structural); mat DN = Dgf * null(Dz);