R/substval.R

Defines functions subst_value

Documented in subst_value

#' Substitute a value in a system of linear (in)equations
#'
#' @section Details:
#' A system of the form \code{Ax <= b} can be simplified if one or more of the
#' \code{x[i]} values is fixed.
#'
#'
#' @param A \code{[numeric]} matrix
#' @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 \code{[logical]} Remove spurious columns when substituting?
#' @param eps \code{[numeric]} scalar. Any value with absolute value below \code{eps} will be 
#'        interpreted as zero.
#'
#' @return A \code{list} with the following components:
#' 
#' \itemize{
#'   \item{\code{A}: the \code{A} corresponding to the simplified sytem.}
#'   \item{\code{b}: the constant vector corresponding to the new system}
#' }
#'
#'
#' @export
subst_value <- function(A, b, variables, values, remove_columns=FALSE, eps=1e-8){
  check_sys(A=A, b=b)
  if ( is.character(variables) ){
    variables <- match(variables,colnames(A))
  }
  values[abs(values)<eps] <- 0
  b <- as.vector(b - A[,variables,drop=FALSE] %*% values)
  if (remove_columns){
    A <- A[,-variables,drop=FALSE]
  } else {
    A[,variables] <- 0
  }
  list(A=A, b=b)
}

Try the lintools package in your browser

Any scripts or data that you put into this service are public.

lintools documentation built on Jan. 17, 2023, 1:06 a.m.