Skip to content

Commit

Permalink
added option, solved a bugger
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanderloo committed Jan 15, 2016
1 parent 655ed5d commit 14bc996
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/R/feasibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# find straigtforward contradictions of the form 0 <= b or 0 == b
has_contradiction <- function(A,b,tol){
if (nrow(A)==0) return(FALSE)
if (ncol(A)==0 & abs(b) > tol) return(TRUE)
if (ncol(A)==0 & all(abs(b) > tol)) return(TRUE)
any(rowSums(abs(A)>tol) == 0 & (abs(b)>tol))
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/R/substval.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param b \code{[numeric]} vector
#' @param variables \code{[numeric|logical|character]} vector of column indices in \code{A}
#' @param values \code{[numeric]} vecor of values to substitute.
#' @param remove_columns Remove spurious columns when substituting?
#'
#' @return A \code{list} with the following components:
#'
Expand All @@ -19,13 +20,17 @@
#'
#'
#' @export
subst_value <- function(A, b, variables, values){
subst_value <- function(A, b, variables, values, remove_columns=FALSE){
check_sys(A=A, b=b)
if ( is.character(variables) ){
variables <- match(variables,colnames(A))
}

b <- as.vector(b - A[,variables,drop=FALSE] %*% values)
A <- A[,-variables,drop=FALSE]
if (remove_columns){
A <- A[,-variables,drop=FALSE]
} else {
A[,variables] <- 0
}
list(A=A, b=b)
}
}
8 changes: 4 additions & 4 deletions pkg/tests/testthat/test_substitute.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ subst_value(A,b,1,1)
test_that("value substitution",{

expect_equivalent(
subst_value(A,b,1,0)
subst_value(A,b,1,0,remove_columns=TRUE)
, list(A=A[,2:3],b=b)
)
expect_equivalent(
subst_value(A,b,c(1,3),c(0,1))
subst_value(A,b,c(1,3),c(0,1),remove_columns=TRUE)
,list(A=A[,2,drop=FALSE],b=b-9:12)
)
colnames(A) <- paste0("x",seq_len(ncol(A)))
expect_equivalent(
subst_value(A,b,"x1",0)
subst_value(A,b,"x1",0,remove_columns=TRUE)
, list(A=A[,2:3],b=b)
)

})
})

0 comments on commit 14bc996

Please sign in to comment.