R/simplify.R

Defines functions expand_func expand_log expand_trig expand simplify

Documented in expand expand_func expand_log expand_trig simplify

#' Simplify expression
#'
#' @param x A `caracas_symbol`
#'
#' @concept simplify
#'
#' @export
simplify <- function(x) {
  if (!inherits(x, "caracas_symbol")) {
    stop(paste0("'x' ", TXT_NOT_CARACAS_SYMBOL))
  }
  
  ensure_sympy()
  
  z <- get_sympy()$simplify(x$pyobj)
  v <- construct_symbol_from_pyobj(z)
  
  return(v)
}



#' Expand expression
#'
#' @param x A `caracas_symbol`
#'
#' @concept simplify
#'
#' @export
expand <- function(x) {
  if (!inherits(x, "caracas_symbol")) {
    stop(paste0("'x' ", TXT_NOT_CARACAS_SYMBOL))
  }
  
  ensure_sympy()
  
  z <- get_sympy()$expand(x$pyobj)
  v <- construct_symbol_from_pyobj(z)
  
  return(v)
}


#' Expand a trigonometric expression
#'
#' @param x A `caracas_symbol`
#'
#' @concept simplify
#'
#' @export
expand_trig <- function(x) {
  if (!inherits(x, "caracas_symbol")) {
    stop(paste0("'x' ", TXT_NOT_CARACAS_SYMBOL))
  }
  
  ensure_sympy()
  
  z <- get_sympy()$expand_trig(x$pyobj)
  v <- construct_symbol_from_pyobj(z)
  
  return(v)
}



#' Expand a logarithmic expression
#' 
#' Note that `force` as described at 
#' <https://docs.sympy.org/latest/tutorial/simplification.html#expand-log> is used 
#' meaning that some assumptions are taken.
#' 
#' @param x A `caracas_symbol`
#' 
#' @examples 
#' if (has_sympy()) {
#'   x <- symbol('x')
#'   y <- symbol('y')
#'   z <- log(x*y)
#'   z
#'   expand_log(z)
#' }
#' 
#' @concept simplify
#' 
#' @export
expand_log <- function(x) {
  if (!inherits(x, "caracas_symbol")) {
    stop(paste0("'x' ", TXT_NOT_CARACAS_SYMBOL))
  }
  
  ensure_sympy()
  
  z <- get_sympy()$expand_log(x$pyobj, force = TRUE)
  v <- construct_symbol_from_pyobj(z)
  
  return(v)
}



#' Expand a function expression
#'
#' @param x A `caracas_symbol`
#'
#' @concept simplify
#'
#' @export
expand_func <- function(x) {
  if (!inherits(x, "caracas_symbol")) {
    stop(paste0("'x' ", TXT_NOT_CARACAS_SYMBOL))
  }
  
  ensure_sympy()
  
  z <- get_sympy()$expand_func(x$pyobj)
  v <- construct_symbol_from_pyobj(z)
  
  return(v)
}

Try the caracas package in your browser

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

caracas documentation built on Feb. 11, 2022, 9:07 a.m.