From 75c50caeadec8499f6dc07c9abde8a3882023543 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Tue, 5 Dec 2023 22:47:49 -0600 Subject: [PATCH 01/10] Convert to (unsigned int) --- src/rxData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rxData.cpp b/src/rxData.cpp index a03907bce..99dd65f53 100644 --- a/src/rxData.cpp +++ b/src/rxData.cpp @@ -4886,7 +4886,7 @@ SEXP rxSolve_(const RObject &obj, const List &rxControl, warning(_("since throwing warning with NA time, change to single threaded")); op->cores=1; } - seedEng(op->cores); + seedEng((unsigned int)(op->cores)); if (_globals.pendingDoses != NULL) { int i=0; while (_globals.pendingDoses[i] != NULL){ From aa16ba84aea72ef2ebdd6faab5eb71df9dd89137 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Tue, 5 Dec 2023 23:25:30 -0600 Subject: [PATCH 02/10] Fix more csan issues --- src/rxData.cpp | 4 ++-- src/rxode2parse.cpp | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/rxData.cpp b/src/rxData.cpp index 99dd65f53..a653e4bbf 100644 --- a/src/rxData.cpp +++ b/src/rxData.cpp @@ -42,7 +42,7 @@ void resetSolveLinB(); using namespace Rcpp; using namespace arma; -typedef void (*seedEng_t)(uint32_t ncores); +typedef void (*seedEng_t)(int ncores); extern seedEng_t seedEng; #include "cbindThetaOmega.h" @@ -4886,7 +4886,7 @@ SEXP rxSolve_(const RObject &obj, const List &rxControl, warning(_("since throwing warning with NA time, change to single threaded")); op->cores=1; } - seedEng((unsigned int)(op->cores)); + seedEng((int)(op->cores)); if (_globals.pendingDoses != NULL) { int i=0; while (_globals.pendingDoses[i] != NULL){ diff --git a/src/rxode2parse.cpp b/src/rxode2parse.cpp index cb64e85b7..6e6c06ac3 100644 --- a/src/rxode2parse.cpp +++ b/src/rxode2parse.cpp @@ -105,6 +105,9 @@ END_RCPP extern "C" SEXP _rxode2parse_udfEnvSet(SEXP udf) { BEGIN_RCPP + if (Rf_isNull(udf)) { + return R_NilValue; + } if (Rf_length(udf) == 0 || Rf_length(udf) == 1) { return R_NilValue; } From 216be7097e37dcda390bbce63e0b895f03b517ba Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Tue, 5 Dec 2023 23:43:05 -0600 Subject: [PATCH 03/10] Only initialize sigma when there is something useful --- src/rxData.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/rxData.cpp b/src/rxData.cpp index a653e4bbf..84095842f 100644 --- a/src/rxData.cpp +++ b/src/rxData.cpp @@ -2144,15 +2144,23 @@ List rxSimThetaOmega(const Nullable ¶ms = R_NilValue, } if (_globals.gsigma != NULL) free(_globals.gsigma); rx->neps = sigma0.n_rows; - _globals.gsigma = (double*)malloc((rx->neps * rx->neps + 2 * rx->neps)* sizeof(double)); - std::copy(&sigma0[0], &sigma0[0] + rx->neps * rx->neps, _globals.gsigma + 2 * rx->neps); + if (rx->neps > 0) { + _globals.gsigma = (double*)malloc((rx->neps * rx->neps + 2 * rx->neps)* sizeof(double)); + std::copy(&sigma0[0], &sigma0[0] + rx->neps * rx->neps, + _globals.gsigma + 2 * rx->neps); + } else { + _globals.gsigma = NULL; + } _globals.nSigma = 0; + } arma::vec in = as(sigmaLower); arma::vec lowerSigmaV = fillVec(in, sigma0.n_rows); arma::vec upperSigmaV = fillVec(in, sigma0.n_rows); - std::copy(&lowerSigmaV[0], &lowerSigmaV[0] + rx->neps, _globals.gsigma); - std::copy(&upperSigmaV[0], &upperSigmaV[0] + rx->neps, _globals.gsigma + rx->neps); + if (rx->neps > 0) { + std::copy(&lowerSigmaV[0], &lowerSigmaV[0] + rx->neps, _globals.gsigma); + std::copy(&upperSigmaV[0], &upperSigmaV[0] + rx->neps, _globals.gsigma + rx->neps); + } // structure of _globals.gsigma is // lower // upper From 42160c1e3053c70b8bfe57086bb27a437ee66fd5 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 06:36:39 -0600 Subject: [PATCH 04/10] Fix approx index in nocb --- src/approx.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/approx.c b/src/approx.c index 0d9c3c993..1e8b33ced 100644 --- a/src/approx.c +++ b/src/approx.c @@ -81,7 +81,7 @@ static inline double getValue(int idx, double *y, rx_solving_options_ind *ind, r if (op->f2 == 1.0 && op->f1 == 0.0) { // use nocb // Go forward - while (ISNA(ret) && i != ind->n_all_times){ + while (ISNA(ret) && i != ind->n_all_times-1){ i++; ret = y[ind->ix[i]]; } if (ISNA(ret)){ @@ -309,4 +309,3 @@ void _update_par_ptr(double t, unsigned int id, rx_solve *rx, int idxIn) { /* void doSort(rx_solving_options_ind *ind); */ void sortInd(rx_solving_options_ind *ind); - From 6f862a4e2874f455b1c723a87a654224df3b8305 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:21:03 -0600 Subject: [PATCH 05/10] CF fix --- src/rxData.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rxData.cpp b/src/rxData.cpp index 84095842f..265dca3d7 100644 --- a/src/rxData.cpp +++ b/src/rxData.cpp @@ -2152,7 +2152,6 @@ List rxSimThetaOmega(const Nullable ¶ms = R_NilValue, _globals.gsigma = NULL; } _globals.nSigma = 0; - } arma::vec in = as(sigmaLower); arma::vec lowerSigmaV = fillVec(in, sigma0.n_rows); From 02462d2b1930eed557f76e7ac6c53ac58edc1108 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:42:53 -0600 Subject: [PATCH 06/10] Take care of unsanitized behavior --- man/reexports.Rd | 1 + src/rxode2_df.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/man/reexports.Rd b/man/reexports.Rd index f1310e9e9..365c14ab7 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -82,3 +82,4 @@ below to see their documentation. \item{rxode2random}{\code{\link[rxode2random:dot-cbindOme]{.cbindOme}}, \code{\link[rxode2random:dot-expandPars]{.expandPars}}, \code{\link[rxode2random:dot-vecDf]{.vecDf}}, \code{\link[rxode2random]{cvPost}}, \code{\link[rxode2random]{invWR1d}}, \code{\link[rxode2random]{phi}}, \code{\link[rxode2random]{rinvchisq}}, \code{\link[rxode2random]{rLKJ1}}, \code{\link[rxode2random]{rxGetSeed}}, \code{\link[rxode2random]{rxGetSeed}}, \code{\link[rxode2random]{rxRmvn}}, \code{\link[rxode2random]{rxSeedEng}}, \code{\link[rxode2random]{rxSetSeed}}, \code{\link[rxode2random]{rxSetSeed}}, \code{\link[rxode2random]{rxSetSeed}}, \code{\link[rxode2random:rxWithSeed]{rxWithPreserveSeed}}, \code{\link[rxode2random]{rxWithSeed}}, \code{\link[rxode2random]{rxWithSeed}}} }} +\value{ Inherited from parent routine } diff --git a/src/rxode2_df.cpp b/src/rxode2_df.cpp index 47043bc31..29f0bd583 100644 --- a/src/rxode2_df.cpp +++ b/src/rxode2_df.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "checkmate.h" #include // for uint64_t rather than unsigned long long #include "../inst/include/rxode2.h" @@ -756,7 +757,13 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) { dfi = INTEGER(tmp); /* if (j == 0) RSprintf("j: %d, %d; %f\n", j, i, get_fkeep(j, curi + i)); */ // is this ntimes = nAllTimes or nObs time for this subject...? - dfi[ii] = (int) (get_fkeep(j, curi + ind->ix[i], ind)); + double curD = get_fkeep(j, curi + ind->ix[i], ind); + if (ISNA(curD) || isnan(curD)) { + dfi[ii] = NA_REAL; + } else { + dfi[ii] = (int) (curD); + } + } jj++; } From fe67693ad0cd4b538a490c08ea226bd87e54f404 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:44:34 -0600 Subject: [PATCH 07/10] Add needed std:: prefix --- src/rxode2_df.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rxode2_df.cpp b/src/rxode2_df.cpp index 29f0bd583..4976158de 100644 --- a/src/rxode2_df.cpp +++ b/src/rxode2_df.cpp @@ -758,7 +758,7 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) { /* if (j == 0) RSprintf("j: %d, %d; %f\n", j, i, get_fkeep(j, curi + i)); */ // is this ntimes = nAllTimes or nObs time for this subject...? double curD = get_fkeep(j, curi + ind->ix[i], ind); - if (ISNA(curD) || isnan(curD)) { + if (ISNA(curD) || std::isnan(curD)) { dfi[ii] = NA_REAL; } else { dfi[ii] = (int) (curD); From 5174563c7e81ae0767884f01f0a592036e4f4b6f Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:47:52 -0600 Subject: [PATCH 08/10] change to NA_INTEGER --- src/rxode2_df.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rxode2_df.cpp b/src/rxode2_df.cpp index 4976158de..7a3ed22c3 100644 --- a/src/rxode2_df.cpp +++ b/src/rxode2_df.cpp @@ -759,7 +759,7 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) { // is this ntimes = nAllTimes or nObs time for this subject...? double curD = get_fkeep(j, curi + ind->ix[i], ind); if (ISNA(curD) || std::isnan(curD)) { - dfi[ii] = NA_REAL; + dfi[ii] = NA_INTEGER; } else { dfi[ii] = (int) (curD); } From 5a30a1158c88cb6771efa5a3b1178d2a7f389af4 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:53:26 -0600 Subject: [PATCH 09/10] Sanitary fixes for logical keeps --- src/rxode2_df.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rxode2_df.cpp b/src/rxode2_df.cpp index 7a3ed22c3..5794e4f03 100644 --- a/src/rxode2_df.cpp +++ b/src/rxode2_df.cpp @@ -752,7 +752,12 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) { } else if (TYPEOF(tmp) == LGLSXP) { // Everything here is double dfi = LOGICAL(tmp); - dfi[ii] = (int) (get_fkeep(j, curi + ind->ix[i], ind)); + double curD = get_fkeep(j, curi + ind->ix[i], ind); + if (ISNA(curD) || std::isnan(curD)) { + dfi[ii] = NA_LOGICAL; + } else { + dfi[ii] = (int) (curD); + } } else { dfi = INTEGER(tmp); /* if (j == 0) RSprintf("j: %d, %d; %f\n", j, i, get_fkeep(j, curi + i)); */ From d60cfeb0e99d8009c4a78324d8e221041737e2b0 Mon Sep 17 00:00:00 2001 From: "Matthew L. Fidler" Date: Wed, 6 Dec 2023 09:54:10 -0600 Subject: [PATCH 10/10] Drop blank line for CF --- src/rxode2_df.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rxode2_df.cpp b/src/rxode2_df.cpp index 5794e4f03..4a1754c0a 100644 --- a/src/rxode2_df.cpp +++ b/src/rxode2_df.cpp @@ -768,7 +768,6 @@ extern "C" SEXP rxode2_df(int doDose0, int doTBS) { } else { dfi[ii] = (int) (curD); } - } jj++; }