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:
ElementwiseElementwise absolute value.
Computes the elementwise absolute value of the input.
- Mathematical definition:
- \[f(x) = |x|\]
- Parameters:¶
- x : Expression¶
Input expression. Can be real or complex.
entr¶
exp¶
- class cvxpy.exp(x)[source]¶
Bases:
ElementwiseElementwise exponential function.
Computes the elementwise exponential of the input.
- Mathematical definition:
- \[f(x) = e^{x}\]
- Parameters:¶
- x : Expression¶
Input expression.
huber¶
-
class cvxpy.huber(x, M: int =
1)[source]¶ Bases:
ElementwiseThe 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.
inv_pos¶
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:
ElementwiseElementwise natural logarithm.
Computes the elementwise natural logarithm of the input.
- Mathematical definition:
- \[f(x) = \log(x)\]
- Domain:
\(x > 0\)
- Parameters:¶
- x : Expression¶
Input expression. Must be elementwise positive.
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_normcdfis called log_ndtr. We opted not to use that name because its meaning would not be obvious to the casual user.
log1p¶
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:
ElementwiseElementwise maximum of a sequence of expressions.
Computes the elementwise maximum over two or more input expressions.
- Mathematical definition:
- \[f(x_1, x_2, \dots, x_n) = \max\{x_1, x_2, \dots, x_n\}\]
- Parameters:¶
- arg1 : Expression¶
First input expression.
- arg2 : Expression¶
Second input expression.
- *args : Expression¶
Additional input expressions.
minimum¶
- cvxpy.minimum(arg1, arg2, *args) None[source]¶
Elementwise minimum of a sequence of expressions.
Computes the elementwise minimum over two or more input expressions.
- Mathematical definition:
- \[f(x_1, x_2, \dots, x_n) = \min\{x_1, x_2, \dots, x_n\}\]
- Parameters:¶
- arg1 : Expression¶
First input expression.
- arg2 : Expression¶
Second input expression.
- *args : Expression¶
Additional input expressions.
neg¶
pos¶
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, OR a positive constant if p is a variable.
- p : int, float, Fraction, Parameter, or Expression¶
The exponent. If p is a non-constant Expression and x is a positive constant, uses the identity b**x = exp(x * log(b)).
- 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, PowerApprox, or exp expression
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¶
sqrt¶
square¶
xexp¶
Not (~x)¶
- class cvxpy.logic.Not(arg)[source]¶
Bases:
LogicExpressionLogical 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:
_NaryLogicExpressionLogical 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:
_NaryLogicExpressionLogical 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:
_NaryLogicExpressionLogical 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)