#' Add Enclosing Parentheses
#'
#' @param exp a string vector consisting of symbolic expressions..
#' @param check = 1 to avoid adding redundant parens.
#' @param trim = 1 to remove leading and trailing blanks from exp.
#'
#' @return an object of the same size as exp
#'
#' @examples
#' exp=c("a","(a+b)","(a+b)-(c-d)")
#' add_paren( exp )
#' add_paren( exp, check=1 )
#'
#' @export
#'

add_paren <- function( exp, check=0, trim=0 ){
 # add enclosing parentheses
 # Shin-ichi Mayekawa
 # 20150817,18
 # 20170921
 #

 exp[]=unlist( lapply( exp,function(x){
  if( trim == 1 ) x=trim(x,leading=1)
  if( check == 1 ){
   if( substr(x,1,1) == "("  &&  locbalpar(trim( x,leading=1),1) == nchar(x) )
    return(x)
   else return( paste("(",x,")",sep="") )
  }
  else return( paste("(",x,")",sep="") )
 }  ) )

 return( exp )

} # add_paren


