R/083_atoms_elementwise_trig.R

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

## CVXPY SOURCE: atoms/elementwise/trig.py
## Sin / Cos / Tan -- elementwise trigonometric atoms (CVXPY 1.9.0, DNLP).
## Smooth but neither convex nor concave; used in the disciplined-nonlinear-
## programming (DNLP) path via is_atom_smooth() = TRUE.

# ==================================================================
# sin(x)
# ==================================================================
Sin <- new_class("Sin", 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(Sin, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = .shape(x)
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: always unknown (trig.py:36-40) -------------------------
method(sign_from_args, Sin) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave (trig.py:42-50) --------
method(is_atom_convex, Sin) <- function(x) FALSE
method(is_atom_concave, Sin) <- function(x) FALSE

# -- smooth: TRUE (trig.py:52-54) ---------------------------------
method(is_atom_smooth, Sin) <- function(x) TRUE

# -- monotonicity: neither (trig.py:56-64) ------------------------
method(is_incr, Sin) <- function(x, idx, ...) FALSE
method(is_decr, Sin) <- function(x, idx, ...) FALSE

# -- numeric (trig.py:30-34) --------------------------------------
method(numeric_value, Sin) <- function(x, values, ...) {
  sin(values[[1L]])
}

# -- graph_implementation: stub (DNLP atom, no cone canon) --------
method(graph_implementation, Sin) <- function(x, arg_objs, shape, data = NULL, ...) {
  cli_abort("graph_implementation for {.cls Sin} not yet implemented.")
}

# -- .grad: d/dx sin(x) = cos(x) (trig.py:71-77) ------------------
method(.grad, Sin) <- function(x, values, ...) {
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(cos(values[[1L]]), rows, cols))
}

# ==================================================================
# cos(x)
# ==================================================================
Cos <- new_class("Cos", 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(Cos, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = .shape(x)
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: always unknown (trig.py:93-97) -------------------------
method(sign_from_args, Cos) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave (trig.py:99-107) -------
method(is_atom_convex, Cos) <- function(x) FALSE
method(is_atom_concave, Cos) <- function(x) FALSE

# -- smooth: TRUE (trig.py:109-111) -------------------------------
method(is_atom_smooth, Cos) <- function(x) TRUE

# -- monotonicity: neither (trig.py:113-121) ----------------------
method(is_incr, Cos) <- function(x, idx, ...) FALSE
method(is_decr, Cos) <- function(x, idx, ...) FALSE

# -- numeric (trig.py:87-91) --------------------------------------
method(numeric_value, Cos) <- function(x, values, ...) {
  cos(values[[1L]])
}

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

# -- .grad: d/dx cos(x) = -sin(x) (trig.py:128-134) ---------------
method(.grad, Cos) <- function(x, values, ...) {
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(-sin(values[[1L]]), rows, cols))
}

# ==================================================================
# tan(x)
# ==================================================================
Tan <- new_class("Tan", 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(Tan, S7_object(),
      id    = as.integer(id),
      .cache = new.env(parent = emptyenv()),
      args  = list(x),
      shape = .shape(x)
    )
    validate_arguments(obj)
    obj
  }
)

# -- sign: always unknown (trig.py:150-154) -----------------------
method(sign_from_args, Tan) <- function(x) {
  list(is_nonneg = FALSE, is_nonpos = FALSE)
}

# -- curvature: neither convex nor concave (trig.py:156-164) ------
method(is_atom_convex, Tan) <- function(x) FALSE
method(is_atom_concave, Tan) <- function(x) FALSE

# -- smooth: TRUE (trig.py:166-168) -------------------------------
method(is_atom_smooth, Tan) <- function(x) TRUE

# -- monotonicity: neither (trig.py:170-178) ----------------------
method(is_incr, Tan) <- function(x, idx, ...) FALSE
method(is_decr, Tan) <- function(x, idx, ...) FALSE

# -- numeric (trig.py:144-148) ------------------------------------
method(numeric_value, Tan) <- function(x, values, ...) {
  tan(values[[1L]])
}

# -- domain: -pi/2 <= x <= pi/2 (trig.py:180-183) -----------------
method(atom_domain, Tan) <- function(x) {
  arg <- .args(x)[[1L]]
  list(-pi / 2 <= arg, arg <= pi / 2)
}

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

# -- .grad: d/dx tan(x) = 1/cos(x)^2 (trig.py:185-191) ------------
method(.grad, Tan) <- function(x, values, ...) {
  rows <- as.integer(prod(.arg_shape(x)))
  cols <- as.integer(prod(.shape(x)))
  list(.elemwise_grad_to_diag(1 / cos(values[[1L]])^2, rows, cols))
}

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.