Nothing
#####
## DO NOT EDIT THIS FILE!! EDIT THE SOURCE INSTEAD: rsrc_tree/atoms/log_sum_exp.R
#####
## CVXPY SOURCE: atoms/log_sum_exp.py
## LogSumExp -- log(sum(exp(x))), axis-aware
LogSumExp <- new_class("LogSumExp", parent = AxisAtom, package = "CVXR",
constructor = function(x, axis = NULL, keepdims = FALSE, id = NULL) {
if (is.null(id)) id <- next_expr_id()
x <- as_expr(x)
if (!is.null(axis)) .validate_axis(axis, length(x@shape))
shape <- .axis_shape(x@shape, axis, keepdims)
obj <- new_object(S7_object(),
id = as.integer(id),
.cache = new.env(parent = emptyenv()),
args = list(x),
shape = shape,
axis = axis,
keepdims = keepdims
)
validate_arguments(obj)
obj
}
)
# -- sign: nonneg when arg is nonneg ------------------------------
method(sign_from_args, LogSumExp) <- function(x) {
list(is_nonneg = is_nonneg(x@args[[1L]]),
is_nonpos = FALSE)
}
# -- curvature: convex --------------------------------------------
method(is_atom_convex, LogSumExp) <- function(x) TRUE
method(is_atom_concave, LogSumExp) <- function(x) FALSE
# -- monotonicity: always increasing ------------------------------
method(is_incr, LogSumExp) <- function(x, idx, ...) TRUE
method(is_decr, LogSumExp) <- function(x, idx, ...) FALSE
# -- numeric: log(sum(exp(x))) ------------------------------------
method(numeric_value, LogSumExp) <- function(x, values, ...) {
v <- values[[1L]]
axis <- x@axis
keepdims <- x@keepdims
if (is.null(axis)) {
## Reduce all: scalar
## Numerically stable: max + log(sum(exp(v - max)))
mx <- max(v)
result <- mx + log(sum(exp(v - mx)))
matrix(result, 1L, 1L)
} else if (axis == 2L) {
## axis=2: Reduce along rows (column-wise) -> one value per column
mx <- apply(v, 2L, max)
result <- mx + log(colSums(exp(sweep(v, 2L, mx))))
if (keepdims) matrix(result, nrow = 1L) else matrix(result, nrow = 1L)
} else {
## axis == 1: Reduce along columns -> one value per row
mx <- apply(v, 1L, max)
result <- mx + log(rowSums(exp(sweep(v, 1L, mx))))
if (keepdims) matrix(result, ncol = 1L) else matrix(result, ncol = 1L)
}
}
# -- graph_implementation: stub -----------------------------------
method(graph_implementation, LogSumExp) <- function(x, arg_objs, shape, data = NULL, ...) {
cli_abort("graph_implementation for {.cls LogSumExp} not yet implemented.")
}
#' Log-sum-exp: log(sum(exp(x)))
#'
#' @param x An Expression
#' @param axis NULL (all), 1 (row-wise), or 2 (column-wise)
#' @param keepdims Logical: keep reduced dimensions?
#' @returns A LogSumExp atom
#' @export
log_sum_exp <- function(x, axis = NULL, keepdims = FALSE) {
LogSumExp(x, axis, keepdims)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.