From 21c3ea8e67043a3d036b83240a1d1a32da28cf36 Mon Sep 17 00:00:00 2001 From: gongyh Date: Fri, 2 Aug 2024 08:46:17 +0800 Subject: [PATCH] fix bugs in constraintUnimod --- R/constraints.R | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/R/constraints.R b/R/constraints.R index c02846a..e9c2dbf 100644 --- a/R/constraints.R +++ b/R/constraints.R @@ -132,13 +132,17 @@ constraintUnimod <- function(x, d, tol = 0) { # process each component separately for (a in seq_len(ncol(x))) { - # flatten peaks to the left of maximum - left_part <- (peak.ind[a] - 1):1 - x[, a] <- f(x[, a], max = x[peak.ind[a], a], indseq = left_part, step = +1) + if (peak.ind[a] != 1) { + # flatten peaks to the left of maximum + left_part <- (peak.ind[a] - 1):1 + x[, a] <- f(x[, a], max = x[peak.ind[a], a], indseq = left_part, step = +1) + } - # flatten peaks to the right of maximum - right_part <- (peak.ind[a] + 1):nvar - x[, a] <- f(x[, a], max = x[peak.ind[a], a], indseq = right_part, step = -1) + if (peak.ind[a] != nvar) { + # flatten peaks to the right of maximum + right_part <- (peak.ind[a] + 1):nvar + x[, a] <- f(x[, a], max = x[peak.ind[a], a], indseq = right_part, step = -1) + } } return(x)