From ef0d907c50abe7c4dc0d3c0e94645b5bf6ef3626 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 16 Dec 2019 15:50:03 +0100
Subject: [PATCH] Avoid computing an extra W in optimParams

---
 pkg/R/optimParams.R | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R
index 13aec83..f06581e 100644
--- a/pkg/R/optimParams.R
+++ b/pkg/R/optimParams.R
@@ -257,14 +257,16 @@ setRefClass(
       else if (!is.numeric(θ0$b) || length(θ0$b) != K || any(is.na(θ0$b)))
         stop("θ0$b: length K, no NA")
       # TODO: stopping condition? N iterations? Delta <= epsilon ?
-      for (loop in 1:2)
+      loopMax <- 2
+      for (loop in 1:loopMax)
       {
         op_res = constrOptim( linArgs(θ0), .self$f, .self$grad_f,
           ui=cbind(
             rbind( rep(-1,K-1), diag(K-1) ),
             matrix(0, nrow=K, ncol=(d+1)*K) ),
           ci=c(-1,rep(0,K-1)) )
-        W <<- computeW(expArgs(op_res$par))
+        if (loop < loopMax) #avoid computing an extra W
+          W <<- computeW(expArgs(op_res$par))
         print(op_res$value) #debug
         print(expArgs(op_res$par)) #debug
       }
-- 
2.44.0