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 or Parameter¶

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, approx: bool = True)[source]¶

Bases:

Factory function for elementwise power.

Parameters:¶
x : Expression¶

The base expression.

p : int, float, Fraction, or Parameter¶

The exponent.

max_denom : int¶

Maximum denominator for rational approximation.

approx : bool¶

When True (default), uses SOC approximation. When False, uses power cone (exact).

Return type:¶

Power or PowerApprox

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}\).

Not (~x)¶

class cvxpy.logic.Not(arg)[source]¶

Bases: LogicExpression

Logical NOT of a boolean expression.

Returns 1 - x, i.e., flips 0 to 1 and 1 to 0.

Can also be written with the ~ operator: ~x.

Parameters:¶
arg : Expression¶

A boolean variable or LogicExpression.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
not_x = ~x                # operator syntax
not_x = cp.logic.Not(x)   # equivalent functional syntax

And (x & y)¶

class cvxpy.logic.And(arg1, arg2, *args)[source]¶

Bases: _NaryLogicExpression

Logical AND of boolean expressions.

Returns 1 if and only if all arguments equal 1, and 0 otherwise.

For two operands, can also be written with the & operator: x & y.

Parameters:¶
*args : Expression¶

Two or more boolean variables or LogicExpressions.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
y = cp.Variable(boolean=True)
both = x & y                  # operator syntax
both = cp.logic.And(x, y)     # equivalent functional syntax
all3 = cp.logic.And(x, y, z)  # n-ary (3+ args) requires functional syntax

Or (x | y)¶

class cvxpy.logic.Or(arg1, arg2, *args)[source]¶

Bases: _NaryLogicExpression

Logical OR of boolean expressions.

Returns 1 if and only if at least one argument equals 1, and 0 otherwise.

For two operands, can also be written with the | operator: x | y.

Parameters:¶
*args : Expression¶

Two or more boolean variables or LogicExpressions.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
y = cp.Variable(boolean=True)
either = x | y                # operator syntax
either = cp.logic.Or(x, y)    # equivalent functional syntax
any3 = cp.logic.Or(x, y, z)   # n-ary (3+ args) requires functional syntax

Xor (x ^ y)¶

class cvxpy.logic.Xor(arg1, arg2, *args)[source]¶

Bases: _NaryLogicExpression

Logical XOR of boolean expressions.

For two arguments: result is 1 iff exactly one is 1. For n arguments: result is 1 iff an odd number are 1 (parity).

For two operands, can also be written with the ^ operator: x ^ y.

Parameters:¶
*args : Expression¶

Two or more boolean variables or LogicExpressions.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
y = cp.Variable(boolean=True)
exclusive = x ^ y                 # operator syntax
exclusive = cp.logic.Xor(x, y)    # equivalent functional syntax
parity3 = cp.logic.Xor(x, y, z)   # n-ary (3+ args) requires functional syntax

implies (x => y)¶

cvxpy.logic.implies(x, y)[source]¶

Logical implication: x => y.

Returns 1 unless x = 1 and y = 0. Equivalent to Or(Not(x), y).

Parameters:¶
x : Expression¶

A boolean variable or LogicExpression.

y : Expression¶

A boolean variable or LogicExpression.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
y = cp.Variable(boolean=True)
expr = cp.logic.implies(x, y)

iff (x <=> y)¶

cvxpy.logic.iff(x, y)[source]¶

Logical biconditional: x <=> y.

Returns 1 if and only if x and y have the same value. Equivalent to Not(Xor(x, y)).

Parameters:¶
x : Expression¶

A boolean variable or LogicExpression.

y : Expression¶

A boolean variable or LogicExpression.

Examples

import cvxpy as cp

x = cp.Variable(boolean=True)
y = cp.Variable(boolean=True)
expr = cp.logic.iff(x, y)