# cvxpy.atoms.elementwise package¶

All of the atoms listed here operate elementwise on expressions. For example, exp exponentiates each entry of expressions that are supplied to it.

## abs¶

class cvxpy.abs(x)[source]

Bases: Elementwise

Elementwise absolute value

## entr¶

class cvxpy.entr(x)[source]

Bases: Elementwise

Elementwise $$-x\log x$$.

## exp¶

class cvxpy.exp(x)[source]

Bases: Elementwise

Elementwise $$e^{x}$$.

## huber¶

class cvxpy.huber(x, M: int = 1)[source]

Bases: Elementwise

The Huber function

$\begin{split}\operatorname{Huber}(x, M) = \begin{cases} 2M|x|-M^2 & \text{for } |x| \geq |M| \\ |x|^2 & \text{for } |x| \leq |M|. \end{cases}\end{split}$

$$M$$ defaults to 1.

Parameters:
• x (Expression) – The expression to which the huber function will be applied.

• M (Constant) – A scalar constant.

## inv_pos¶

cvxpy.inv_pos(x)[source]

$$x^{-1}$$ for $$x > 0$$.

## kl_div¶

class cvxpy.kl_div(x, y)[source]

Bases: Elementwise

$$x\log(x/y) - x + y$$

For disambiguation between kl_div and rel_entr, see https://github.com/cvxpy/cvxpy/issues/733

## log¶

class cvxpy.log(x)[source]

Bases: Elementwise

Elementwise $$\log x$$.

## log_normcdf¶

class cvxpy.log_normcdf(x)[source]

Bases:

Elementwise log of the cumulative distribution function of a standard normal random variable.

The implementation is a quadratic approximation with modest accuracy over [-4, 4]. For details on the nature of the approximation, refer to CVXPY GitHub PR #1224.

Note

SciPy’s analog of log_normcdf is called log_ndtr. We opted not to use that name because its meaning would not be obvious to the casual user.

## log1p¶

class cvxpy.log1p(x)[source]

Bases: log

Elementwise $$\log (1 + x)$$.

## loggamma¶

class cvxpy.loggamma(x)[source]

Bases:

Elementwise log of the gamma function.

Implementation has modest accuracy over the full range, approaching perfect accuracy as x goes to infinity. For details on the nature of the approximation, refer to CVXPY GitHub Issue #228.

## logistic¶

class cvxpy.logistic(x)[source]

Bases: Elementwise

$$\log(1 + e^{x})$$

This is a special case of log(sum(exp)) that is evaluates to a vector rather than to a scalar which is useful for logistic regression.

## maximum¶

class cvxpy.maximum(arg1, arg2, *args)[source]

Bases: Elementwise

Elementwise maximum of a sequence of expressions.

## minimum¶

cvxpy.minimum(arg1, arg2, *args) None[source]

Elementwise minimum of a sequence of expressions.

## neg¶

cvxpy.neg(x)[source]

Alias for -minimum{x, 0}.

## pos¶

cvxpy.pos(x)[source]

Alias for maximum{x,0}.

## power¶

class cvxpy.power(x, p, max_denom: int = 1024)[source]

Bases: Elementwise

Elementwise power function $$f(x) = x^p$$.

If expr is a CVXPY expression, then expr**p is equivalent to power(expr, p).

For DCP problems, the exponent p must be a numeric constant. For DGP problems, p can also be a scalar Parameter.

Specifically, the atom is given by the cases

$\begin{split}\begin{array}{ccl} p = 0 & f(x) = 1 & \text{constant, positive} \\ p = 1 & f(x) = x & \text{affine, increasing, same sign as x} \\ p = 2,4,8,\ldots &f(x) = |x|^p & \text{convex, signed monotonicity, positive} \\ p < 0 & f(x) = \begin{cases} x^p & x > 0 \\ +\infty & x \leq 0 \end{cases} & \text{convex, decreasing, positive} \\ 0 < p < 1 & f(x) = \begin{cases} x^p & x \geq 0 \\ -\infty & x < 0 \end{cases} & \text{concave, increasing, positive} \\ p > 1,\ p \neq 2,4,8,\ldots & f(x) = \begin{cases} x^p & x \geq 0 \\ +\infty & x < 0 \end{cases} & \text{convex, increasing, positive}. \end{array}\end{split}$

Note

For DCP problems, power assumes p has a rational representation with a small denominator. Approximations are employed when this is not the case. Specifically, power computes a rational approximation to p with a denominator up to max_denom. Increasing max_denom can give better approximations. When p is an int or Fraction object, the approximation is usually exact. No such approximation is used for DGP problems.

CVXPY supports exponential cone and power cone constraints. Such constraints could be used to handle the power atom in DCP problems without relying on approximations. Such an approach would also result in fewer variables than the current method, even when the current method is an exact reformulation. If you’re interested in helping enhance CVXPY with this ability, please get in touch with us and check out GitHub Issue 1222!

Note

The final domain, sign, monotonicity, and curvature of the power atom are determined by the rational approximation to p, not the input parameter p.

For example,

>>> from cvxpy import Variable, power
>>> x = Variable()
>>> g = power(x, 1.001)
>>> g.p
Fraction(1001, 1000)
>>> g
Expression(CONVEX, POSITIVE, (1, 1))
results in a convex atom with implicit constraint :math:x \geq 0, while
>>> g = power(x, 1.0001)
>>> g.p
1
>>> g
Expression(AFFINE, UNKNOWN, (1, 1))


results in an affine atom with no constraint on x.

• When $$p > 1$$ and p is not a power of two, the monotonically increasing version of the function with full domain,

$\begin{split}f(x) = \begin{cases} x^p & x \geq 0 \\ 0 & x < 0 \end{cases}\end{split}$

can be formed with the composition power(pos(x), p).

• The symmetric version with full domain,

$f(x) = |x|^p$

can be formed with the composition power(abs(x), p).

Parameters:
• x (cvxpy.Variable) –

• p (int, float, Fraction, or Parameter.) – Scalar power. p may be a Parameter in DGP programs, but not in DCP programs.

• max_denom (int) – The maximum denominator considered in forming a rational approximation of p; only relevant when solving as a DCP program.

## rel_entr¶

class cvxpy.rel_entr(x, y)[source]

Bases: Elementwise

$$x\log(x/y)$$

For disambiguation between rel_entr and kl_div, see https://github.com/cvxpy/cvxpy/issues/733

## scalene¶

cvxpy.scalene(x, alpha, beta)[source]

Alias for alpha*pos(x) + beta*neg(x).

## sqrt¶

cvxpy.sqrt(x)[source]

The square root of an expression.

## square¶

cvxpy.square(x)[source]

The square of an expression.

## xexp¶

class cvxpy.xexp(x)[source]

Bases: Elementwise

Elementwise $${x}*e^{x}$$.