Affine Atoms

All of the atoms listed here are affine in their arguments.

AddExpression

class cvxpy.atoms.affine.add_expr.AddExpression(arg_groups)[source]

Bases: AffAtom

The sum of any number of expressions.

MulExpression

class cvxpy.atoms.affine.binary_operators.MulExpression(lh_exp, rh_exp)[source]

Bases: BinaryOperator

Matrix multiplication.

The semantics of multiplication are exactly as those of NumPy’s matmul function, except here multiplication by a scalar is permitted. MulExpression objects can be created by using the ‘*’ operator of the Expression class.

Parameters:
  • lh_exp (Expression) – The left-hand side of the multiplication.

  • rh_exp (Expression) – The right-hand side of the multiplication.

DivExpression

class cvxpy.atoms.affine.binary_operators.DivExpression(lh_expr, rh_expr)[source]

Bases: BinaryOperator

Division by scalar.

Can be created by using the / operator of expression.

Bmat

cvxpy.atoms.affine.bmat.bmat(block_lists)[source]

Constructs a block matrix.

Takes a list of lists. Each internal list is stacked horizontally. The internal lists are stacked vertically.

Parameters:

block_lists (list of lists) – The blocks of the block matrix.

Returns:

The CVXPY expression representing the block matrix.

Return type:

CVXPY expression

conv

class cvxpy.atoms.affine.conv.conv(lh_expr, rh_expr)[source]

Bases: AffAtom

1D discrete convolution of two vectors.

The discrete convolution \(c\) of vectors \(a\) and \(b\) of lengths \(n\) and \(m\), respectively, is a length-\((n+m-1)\) vector where

\[c_k = \sum_{i+j=k} a_ib_j, \quad k=0, \ldots, n+m-2.\]
Parameters:
  • lh_expr (Constant) – A constant 1D vector or a 2D column vector.

  • rh_expr (Expression) – A 1D vector or a 2D column vector.

cumsum

class cvxpy.atoms.affine.cumsum.cumsum(expr: Expression, axis: int = 0)[source]

Bases: AffAtom, AxisAtom

Cumulative sum.

expr

The expression being summed.

Type:

CVXPY expression

axis

The axis to sum across if 2D.

Type:

int

diag

cvxpy.atoms.affine.diag.diag(expr) diag_mat | diag_vec[source]

Extracts the diagonal from a matrix or makes a vector a diagonal matrix.

Parameters:

expr (Expression or numeric constant) – A vector or square matrix.

Returns:

An Expression representing the diagonal vector/matrix.

Return type:

Expression

diff

cvxpy.atoms.affine.diff.diff(x, k: int = 1, axis: int = 0)[source]

Vector of kth order differences.

Takes in a vector of length n and returns a vector of length n-k of the kth order differences.

diff(x) returns the vector of differences between adjacent elements in the vector, that is

[x[2] - x[1], x[3] - x[2], …]

diff(x, 2) is the second-order differences vector, equivalently diff(diff(x))

diff(x, 0) returns the vector x unchanged

hstack

cvxpy.atoms.affine.hstack.hstack(arg_list) Hstack[source]

Horizontal concatenation of an arbitrary number of Expressions.

Parameters:

arg_list (list of Expression) – The Expressions to concatenate.

index

class cvxpy.atoms.affine.index.index(expr, key, orig_key=None)[source]

Bases: AffAtom

Indexing/slicing into an Expression.

CVXPY supports NumPy-like indexing semantics via the Expression class’ overloading of the [] operator. This is a low-level class constructed by that operator, and it should not be instantiated directly.

Parameters:
  • expr (Expression) – The expression indexed/sliced into.

  • key – The index/slicing key (i.e. expr[key[0],key[1]]).

kron

class cvxpy.atoms.affine.kron.kron(lh_expr, rh_expr)[source]

Bases: AffAtom

Kronecker product.

matmul

cvxpy.atoms.affine.binary_operators.matmul(lh_exp, rh_exp) MulExpression[source]

Matrix multiplication.

multiply

class cvxpy.atoms.affine.binary_operators.multiply(lh_expr, rh_expr)[source]

Bases: MulExpression

Multiplies two expressions elementwise.

promote

cvxpy.atoms.affine.promote.promote(expr: Expression, shape: Tuple[int, ...])[source]

Promote a scalar expression to a vector/matrix.

Parameters:
  • expr (Expression) – The expression to promote.

  • shape (tuple) – The shape to promote to.

Raises:

ValueError – If expr is not a scalar.

reshape

class cvxpy.atoms.affine.reshape.reshape(expr, shape: Tuple[int, int], order: str = 'F')[source]

Bases: AffAtom

Reshapes the expression.

Vectorizes the expression then unvectorizes it into the new shape. The entries are reshaped and stored in column-major order, also known as Fortran order.

Parameters:
  • expr (Expression) – The expression to promote.

  • shape (tuple or int) – The shape to promote to.

  • order (F(ortran) or C) –

sum

cvxpy.atoms.affine.sum.sum(expr, axis: int | None = None, keepdims: bool = False) None[source]

Sum the entries of an expression.

Parameters:
  • expr (Expression) – The expression to sum the entries of.

  • axis (int) – The axis along which to sum.

  • keepdims (bool) – Whether to drop dimensions after summing.

trace

class cvxpy.atoms.affine.trace.trace(expr)[source]

Bases: AffAtom

The sum of the diagonal entries of a matrix.

Parameters:

expr (Expression) – The expression to sum the diagonal of.

transpose

class cvxpy.atoms.affine.transpose.transpose(expr, axes=None)[source]

Bases: AffAtom

Transpose an expression.

NegExpression

class cvxpy.atoms.affine.unary_operators.NegExpression(expr)[source]

Bases: UnaryOperator

Negation of an expression.

upper_tri

class cvxpy.atoms.affine.upper_tri.upper_tri(expr)[source]

Bases: AffAtom

The vectorized strictly upper-triagonal entries.

The vectorization is performed by concatenating (partial) rows. For example, if ``` A = np.array([[10, 11, 12, 13],

[14, 15, 16, 17], [18, 19, 20, 21], [22, 23, 24, 25]])

` then we have ` upper_tri(A).value == np.array([11, 12, 13, 16, 17, 21]) ```

vec

cvxpy.atoms.affine.vec.vec(X)[source]

Flattens the matrix X into a vector in column-major order.

Parameters:

X (Expression or numeric constant) – The matrix to flatten.

Returns:

An Expression representing the flattened matrix.

Return type:

Expression

vstack

cvxpy.atoms.affine.vstack.vstack(arg_list) Vstack[source]

Wrapper on vstack to ensure list argument.