From 25f42c7783f745dbf7047444fb376272f07523d3 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 14 Aug 2024 16:03:37 -0500 Subject: [PATCH 1/3] Use lotri nearPD --- R/nearpd.R | 5 ----- src/cvPost.cpp | 6 ++++-- src/nearPD.cpp | 14 ++++++++------ 3 files changed, 12 insertions(+), 13 deletions(-) delete mode 100644 R/nearpd.R diff --git a/R/nearpd.R b/R/nearpd.R deleted file mode 100644 index 1b2eb5c4e..000000000 --- a/R/nearpd.R +++ /dev/null @@ -1,5 +0,0 @@ -.nearPD <- function(mat) { - .ret <- try(Matrix::nearPD(mat), silent=TRUE) - if (inherits(.ret, "try-error")) return(NULL) - as.matrix(.ret$mat) -} diff --git a/src/cvPost.cpp b/src/cvPost.cpp index 745f7a2b5..b48f6cac2 100644 --- a/src/cvPost.cpp +++ b/src/cvPost.cpp @@ -12,10 +12,12 @@ #include "../inst/include/rxode2_as.h" #include +extern "C" { + iniLotri; +} + extern "C" SEXP chin(SEXP a, SEXP b); -// place links here -iniLotri; List etTrans(List inData, const RObject &mv, bool addCmt, bool dropUnits, bool allTimeVar, diff --git a/src/nearPD.cpp b/src/nearPD.cpp index d1245e6d2..515006197 100644 --- a/src/nearPD.cpp +++ b/src/nearPD.cpp @@ -5,6 +5,8 @@ #define STRICT_R_HEADER #include #include "nearPD.h" +#define lotriArma +#include using namespace arma; using namespace Rcpp; @@ -12,14 +14,14 @@ using namespace Rcpp; Function getRxFn(std::string name); bool rxNearPD(arma::mat &ret, const arma::mat in) { - Function mnearpd = getRxFn(".nearPD"); - RObject retRO = mnearpd(in); - if (Rf_isMatrix(retRO)) { - ret = as(retRO); + ret = mat(in.n_rows, in.n_cols); + if (lotriNearPDarma(ret, in)) { return true; + } else { + ret = in; + return false; } - ret = in; - return false; + return false; // nocov } unsigned int rxNearPdChol(Rcpp::NumericMatrix &ret, Rcpp::NumericMatrix x, bool isChol) { From f92a906085066d4cf5e997d78cb77c2c9840fc13 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Thu, 15 Aug 2024 14:38:14 -0500 Subject: [PATCH 2/3] Fix for using lotri's nearPD --- src/nearPD.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/nearPD.cpp b/src/nearPD.cpp index 515006197..67b881613 100644 --- a/src/nearPD.cpp +++ b/src/nearPD.cpp @@ -5,16 +5,14 @@ #define STRICT_R_HEADER #include #include "nearPD.h" -#define lotriArma #include +lotriNearPDarmaSetup + using namespace arma; using namespace Rcpp; -Function getRxFn(std::string name); - bool rxNearPD(arma::mat &ret, const arma::mat in) { - ret = mat(in.n_rows, in.n_cols); if (lotriNearPDarma(ret, in)) { return true; } else { @@ -27,7 +25,6 @@ bool rxNearPD(arma::mat &ret, const arma::mat in) { unsigned int rxNearPdChol(Rcpp::NumericMatrix &ret, Rcpp::NumericMatrix x, bool isChol) { arma::mat tmpM = as(x); arma::mat reta; - if (!x.hasAttribute("dimnames")) { return rxNearPdChol_not_named; } else if (isChol) { From 82db8ad76ffa88ff54c5251207443f004089858d Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Thu, 15 Aug 2024 15:21:55 -0500 Subject: [PATCH 3/3] Remove reference to .nearPD --- tests/testthat/test-nearpd.R | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/testthat/test-nearpd.R b/tests/testthat/test-nearpd.R index a5e34fbda..4daea824c 100644 --- a/tests/testthat/test-nearpd.R +++ b/tests/testthat/test-nearpd.R @@ -60,7 +60,3 @@ rxTest({ NA) }) }) - -test_that(".nearPD expected null for matrix that cannot become positive definite", { - expect_null(.nearPD(matrix(Inf))) -})