Skip to content

Commit

Permalink
separate Df #43
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwang15 committed Aug 10, 2024
1 parent 868d658 commit f34ef73
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 2 deletions.
42 changes: 42 additions & 0 deletions inst/include/bsvarSIGNs_RcppExports.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,48 @@ namespace bsvarSIGNs {
return Rcpp::as<arma::colvec >(rcpp_result_gen);
}

inline arma::mat D_z(const arma::field<arma::mat>& 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<arma::mat>&,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<SEXP>(Rcpp::wrap(Z)), Shield<SEXP>(Rcpp::wrap(x)), Shield<SEXP>(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<std::string>(rcpp_result_gen).c_str());
return Rcpp::as<arma::mat >(rcpp_result_gen);
}

inline arma::mat D_gf(const arma::field<arma::mat>& 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<arma::mat>&,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<SEXP>(Rcpp::wrap(Z)), Shield<SEXP>(Rcpp::wrap(x)), Shield<SEXP>(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<std::string>(rcpp_result_gen).c_str());
return Rcpp::as<arma::mat >(rcpp_result_gen);
}

inline double log_volume_element(const arma::field<arma::mat>& 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;
Expand Down
78 changes: 78 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<arma::mat>& 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<arma::mat>& >::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<arma::mat>& 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<arma::mat>& >::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<arma::mat>& Z, const arma::mat& A0, const arma::mat& Aplus);
static SEXP _bsvarSIGNs_log_volume_element_try(SEXP ZSEXP, SEXP A0SEXP, SEXP AplusSEXP) {
Expand Down Expand Up @@ -1061,6 +1133,8 @@ static int _bsvarSIGNs_RcppExport_validate(const char* sig) {
signatures.insert("arma::colvec(*zero_restrictions)(const arma::field<arma::mat>&,const arma::colvec)");
signatures.insert("arma::colvec(*g_fh)(const arma::field<arma::mat>&,const arma::mat&,const arma::mat&)");
signatures.insert("arma::colvec(*g_fh_vec)(const arma::field<arma::mat>&,const arma::colvec)");
signatures.insert("arma::mat(*D_z)(const arma::field<arma::mat>&,const arma::vec&,const double)");
signatures.insert("arma::mat(*D_gf)(const arma::field<arma::mat>&,const arma::vec&,const double)");
signatures.insert("double(*log_volume_element)(const arma::field<arma::mat>&,const arma::mat&,const arma::mat&)");
signatures.insert("double(*weight_zero)(const arma::field<arma::mat>&,const arma::mat&,const arma::mat&,const arma::mat&)");
signatures.insert("arma::mat(*rzeroQ)(const arma::field<arma::mat>&,const arma::mat&)");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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},
Expand Down
67 changes: 65 additions & 2 deletions src/restrictions_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<arma::mat>& 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<arma::mat>& 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)]]
Expand All @@ -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);

Expand Down

0 comments on commit f34ef73

Please sign in to comment.