R/074_atoms_elementwise_loggamma.R

Defines functions loggamma

Documented in loggamma

#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/atoms/elementwise/loggamma.R
#####

## CVXPY SOURCE: atoms/elementwise/loggamma.py
## Loggamma -- piecewise linear approximation of log(gamma(x))

#' Elementwise log of the gamma function
#'
#' @description
#' Piecewise linear approximation of `log(gamma(x))`.
#' Has modest accuracy over the full range, approaching perfect accuracy
#' as x goes to infinity.
#'
#' @param x An Expression or numeric value (must be positive for DCP).
#' @returns A convex Expression representing log(gamma(x)).
#' @export
loggamma <- function(x) {
  x <- as_expr(x)
  Maximum(
    2.18382 - 3.62887 * x,
    1.79241 - 2.4902 * x,
    1.21628 - 1.37035 * x,
    0.261474 - 0.28904 * x,
    0.577216 - 0.577216 * x,
    -0.175517 + 0.03649 * x,
    -1.27572 + 0.621514 * x,
    -0.845568 + 0.422784 * x,
    -0.577216 * x - log(x),
    0.918939 - x - entr(x) - 0.5 * log(x)
  )
}

Try the CVXR package in your browser

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

CVXR documentation built on March 6, 2026, 9:10 a.m.