# Expressions¶

CVXPY represents mathematical objects as expression trees. An expression tree is a collection of mathematical expressions linked together by one or more atoms. Expression trees are encoded as instances of the Expression class, and each Leaf in a tree is a Variable, Parameter, or Constant.

## Expression¶

class cvxpy.expressions.expression.Expression[source]

Bases: cvxpy.utilities.canonical.Canonical

A mathematical expression in a convex optimization problem.

Overloads many operators to allow for convenient creation of compound expressions (e.g., the sum of two expressions) and constraints.

property T

The transpose of the expression.

Type

Expression

__add__(other)[source]

Expression : Sum two expressions.

__div__(other)[source]

Expression : One expression divided by another.

__eq__(other)[source]

Equality : Creates a constraint self == other.

__ge__(other)[source]

NonPos : Creates an inequality constraint.

__le__(other)[source]

Inequality : Creates an inequality constraint self <= other.

__lshift__(other)[source]

PSD : Creates a negative semidefinite inequality.

__matmul__(other)[source]

Expression : Matrix multiplication of two expressions.

__mul__(other)[source]

Expression : The product of two expressions.

__pow__(power)[source]

Raise expression to a power.

Parameters

power (float) – The power to which to raise the expression.

Returns

The expression raised to power.

Return type

Expression

__radd__(other)[source]

Expression : Sum two expressions.

__rdiv__(other)[source]

Expression : Called for Number / Expression.

__rlshift__(other)[source]

PSD : Creates a negative semidefinite inequality.

__rmatmul__(other)[source]

Expression : Called for matrix @ Expression.

__rmul__(other)[source]

Expression : Called for Number * Expression.

__rrshift__(other)[source]

PSD : Creates a positive semidefinite inequality.

__rshift__(other)[source]

PSD : Creates a positive semidefinite inequality.

__rsub__(other)[source]

Expression : The difference of two expressions.

__rtruediv__(other)[source]

Expression : Called for Number / Expression.

__sub__(other)[source]

Expression : The difference of two expressions.

__truediv__(other)[source]

Expression : One expression divided by another.

property curvature

The curvature of the expression.

Type

str

abstract property domain

The constraints describing the closure of the region where the expression is finite.

Type

list

abstract property grad

Gives the (sub/super)gradient of the expression w.r.t. each variable.

Matrix expressions are vectorized, so the gradient is a matrix.

Returns

A map of variable to SciPy CSC sparse matrix; None if a variable value is missing.

Return type

dict

is_affine()[source]

Is the expression affine?

abstract is_concave()[source]

Is the expression concave?

is_constant()[source]

Is the expression constant?

abstract is_convex()[source]

Is the expression convex?

is_dcp(dpp=False)[source]

Checks whether the Expression is DCP.

Parameters

dpp (bool, optional) – If True, enforce the disciplined parametrized programming (DPP) ruleset; only relevant when the problem involves Parameters.

Returns

True if the Expression is DCP, False otherwise.

Return type

bool

is_dgp(dpp=False)[source]

Checks whether the Expression is log-log DCP.

Returns

True if the Expression is log-log DCP, False otherwise.

Return type

bool

abstract is_dpp(context='dcp')[source]

The expression is a disciplined parameterized expression.

is_dqcp()[source]

Checks whether the Expression is DQCP.

Returns

True if the Expression is DQCP, False otherwise.

Return type

bool

is_log_log_affine()[source]

Is the expression affine?

abstract is_log_log_concave()[source]

Is the expression log-log concave?

abstract is_log_log_convex()[source]

Is the expression log-log convex?

abstract is_nonneg()[source]

Is the expression positive?

abstract is_nonpos()[source]

Is the expression negative?

is_zero()[source]

Is the expression all zero?

abstract name()[source]

str : The string representation of the expression.

property ndim

The number of dimensions in the expression’s shape.

Type

int

abstract property shape

The expression dimensions.

Type

tuple

property sign

The sign of the expression.

Type

str

property size

The number of entries in the expression.

Type

int

abstract property value

The numeric value of the expression.

Type

NumPy.ndarray or None

## Leaf¶

class cvxpy.expressions.leaf.Leaf(shape, value=None, nonneg=False, nonpos=False, complex=False, imag=False, symmetric=False, diag=False, PSD=False, NSD=False, hermitian=False, boolean=False, integer=False, sparsity=None, pos=False, neg=False)[source]

A leaf node of an expression tree; i.e., a Variable, Constant, or Parameter.

A leaf may carry attributes that constrain the set values permissible for it. Leafs can have no more than one attribute, with the exception that a leaf may be both nonpos and nonneg or both boolean in some indices and integer in others.

An error is raised if a leaf is assigned a value that contradicts one or more of its attributes. See the project method for a convenient way to project a value onto a leaf’s domain.

Parameters
• shape (tuple or int) – The leaf dimensions. Either an integer n for a 1D shape, or a tuple where the semantics are the same as NumPy ndarray shapes. Shapes cannot be more than 2D.

• value (numeric type) – A value to assign to the leaf.

• nonneg (bool) – Is the variable constrained to be nonnegative?

• nonpos (bool) – Is the variable constrained to be nonpositive?

• complex (bool) – Is the variable complex valued?

• symmetric (bool) – Is the variable symmetric?

• diag (bool) – Is the variable diagonal?

• PSD (bool) – Is the variable constrained to be positive semidefinite?

• NSD (bool) – Is the variable constrained to be negative semidefinite?

• Hermitian (bool) – Is the variable Hermitian?

• boolean (bool or list of tuple) – Is the variable boolean? True, which constrains the entire Variable to be boolean, False, or a list of indices which should be constrained as boolean, where each index is a tuple of length exactly equal to the length of shape.

• integer (bool or list of tuple) – Is the variable integer? The semantics are the same as the boolean argument.

• sparsity (list of tuplewith) – Fixed sparsity pattern for the variable.

• pos (bool) – Is the variable positive?

• neg (bool) – Is the variable negative?

property T

The transpose of the expression.

Type

Expression

property ndim

The number of dimensions in the expression’s shape.

Type

int

project(val)[source]

Project value onto the attribute set of the leaf.

A sensible idiom is leaf.value = leaf.project(val).

Parameters

val (numeric type) – The value assigned.

Returns

The value rounded to the attribute type.

Return type

numeric type

project_and_assign(val)[source]

Project and assign a value to the variable.

property shape

The dimensions of the expression.

Type

tuple

property size

The number of entries in the expression.

Type

int

property value

The numeric value of the parameter.

Type

NumPy.ndarray or None

## Variable¶

class cvxpy.expressions.variable.Variable(shape=(), name=None, var_id=None, **kwargs)[source]

The optimization variables in a problem.

property T

The transpose of the expression.

Type

Expression

name()[source]

str : The name of the variable.

property ndim

The number of dimensions in the expression’s shape.

Type

int

project(val)

Project value onto the attribute set of the leaf.

A sensible idiom is leaf.value = leaf.project(val).

Parameters

val (numeric type) – The value assigned.

Returns

The value rounded to the attribute type.

Return type

numeric type

project_and_assign(val)

Project and assign a value to the variable.

property shape

The dimensions of the expression.

Type

tuple

property size

The number of entries in the expression.

Type

int

property value

The numeric value of the parameter.

Type

NumPy.ndarray or None

## Parameter¶

class cvxpy.expressions.constants.parameter.Parameter(shape=(), name=None, value=None, id=None, **kwargs)[source]

Parameters in optimization problems.

Parameters are constant expressions whose value may be specified after problem creation. The only way to modify a problem after its creation is through parameters. For example, you might choose to declare the hyper-parameters of a machine learning model to be Parameter objects; more generally, Parameters are useful for computing trade-off curves.

property T

The transpose of the expression.

Type

Expression

property ndim

The number of dimensions in the expression’s shape.

Type

int

project(val)

Project value onto the attribute set of the leaf.

A sensible idiom is leaf.value = leaf.project(val).

Parameters

val (numeric type) – The value assigned.

Returns

The value rounded to the attribute type.

Return type

numeric type

project_and_assign(val)

Project and assign a value to the variable.

property shape

The dimensions of the expression.

Type

tuple

property size

The number of entries in the expression.

Type

int

property value

The numeric value of the parameter.

Type

NumPy.ndarray or None

## Constant¶

class cvxpy.expressions.constants.Constant(value)[source]

A constant value.

Raw numerical constants (Python primite types, NumPy ndarrays, and NumPy matrices) are implicitly cast to constants via Expression operator overloading. For example, if x is an expression and c is a raw constant, then x + c creates an expression by casting c to a Constant.

property T

The transpose of the expression.

Type

Expression

property ndim

The number of dimensions in the expression’s shape.

Type

int

property shape

Returns the (row, col) dimensions of the expression.

property size

The number of entries in the expression.

Type

int

property value

The numeric value of the constant.

Type

NumPy.ndarray or None