R/082_atoms_elementwise_normcdf.R

Defines functions normcdf

Documented in normcdf

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

## CVXPY SOURCE: atoms/elementwise/normcdf.py
## Normcdf -- elementwise standard normal CDF Phi(x) (CVXPY 1.9.0, DNLP).
## Smooth, nonnegative, increasing; neither convex nor concave.  Distinct from
## the existing log_normcdf atom (a concave 1.8.2 atom in log_normcdf.R).

Normcdf <- new_class("Normcdf", parent = Elementwise, package = "CVXR",
  constructor = function(x, id = NULL) {
    if (FALSE) new_object(S7_object())  ## S7 static-check guard
    if (is.null(id)) id <- next_expr_id()
    x <- as_expr(x)
    obj <- .fast_new(Normcdf, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = .shape(x)
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: always nonneg (normcdf.py:37-41) -----------------------
method(sign_from_args, Normcdf) <- function(x) {
  list(is_nonneg = TRUE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave (normcdf.py:43-51) -----
method(is_atom_convex, Normcdf) <- function(x) FALSE
method(is_atom_concave, Normcdf) <- function(x) FALSE

# -- smooth: TRUE (normcdf.py:53-55) ------------------------------
method(is_atom_smooth, Normcdf) <- function(x) TRUE

# -- monotonicity: increasing (normcdf.py:57-65) ------------------
method(is_incr, Normcdf) <- function(x, idx, ...) TRUE
method(is_decr, Normcdf) <- function(x, idx, ...) FALSE

# -- numeric: Phi(x) (normcdf.py:31-35) ---------------------------
## sp.stats.norm.cdf == R's pnorm (standard normal CDF).
method(numeric_value, Normcdf) <- function(x, values, ...) {
  pnorm(values[[1L]])
}

# -- graph_implementation: stub ----------------------------------
method(graph_implementation, Normcdf) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls Normcdf} not yet implemented.")
}

# -- .grad: d/dx Phi(x) = phi(x) (normcdf.py:72-78) ---------------
## exp(-0.5 x^2)/sqrt(2 pi) == R's dnorm (standard normal PDF).
method(.grad, Normcdf) <- function(x, values, ...) {
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(dnorm(values[[1L]]), rows, cols))
}

#' Standard Normal Cumulative Distribution Function
#'
#' Elementwise standard normal CDF \eqn{\Phi(x)}.  A smooth (DNLP) atom: it is
#' neither convex nor concave, so it is usable only on the disciplined-nonlinear-
#' programming path.
#'
#' @param x An Expression.
#' @returns A Normcdf atom.
#' @export
normcdf <- function(x) {
  Normcdf(x)
}

Try the CVXR package in your browser

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

CVXR documentation built on Aug. 24, 2026, 9:10 a.m.