Affine Atoms¶
All of the atoms listed here are affine in their arguments.
AddExpression¶
- class cvxpy.atoms.affine.add_expr.AddExpression(arg_groups: Iterable[Expression])[source]¶
Bases:
AffAtomThe sum of any number of expressions.
MulExpression¶
- class cvxpy.atoms.affine.binary_operators.MulExpression(lh_exp, rh_exp)[source]¶
Bases:
BinaryOperatorMatrix 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:
BinaryOperatorDivision by scalar.
Can be created by using the / operator of expression.
bmat¶
- cvxpy.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.
conj¶
convolve¶
- class cvxpy.convolve(*args)[source]¶
Bases:
AffAtom1D 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.\]Matches numpy.convolve
- Parameters:¶
- lh_expr : Constant
A constant scalar or 1D vector.
- rh_expr : Expression
A scalar or 1D vector.
cumsum¶
-
class cvxpy.cumsum(expr: Expression, axis: int | None =
0)[source]¶ Bases:
AffAtom,AxisAtomCumulative sum of the elements of an expression.
diag¶
-
cvxpy.diag(expr, k: int =
0) diag_mat | diag_vec[source]¶ Extracts the diagonal from a matrix or makes a vector a diagonal matrix.
diff¶
-
cvxpy.diff(x, k: int =
1, axis: int =0)[source]¶ Computes kth order differences along the specified axis.
Takes in an array and returns an array with the kth order differences along the given axis. The output shape is the same as the input except the size along the specified axis is reduced by k.
- diff(x) returns the differences between adjacent elements along axis 0:
[x[1] - x[0], x[2] - x[1], …]
diff(x, 2) is the second-order differences, equivalently diff(diff(x))
diff(x, 0) returns the array x unchanged
- Parameters:¶
- x : Expression or array-like¶
Input array.
- k : int, optional¶
The number of times values are differenced. Default is 1.
- axis : int, optional¶
The axis along which the difference is taken. Default is 0. Note: NumPy’s np.diff uses axis=-1 as default.
- Returns:¶
The kth order differences along the specified axis.
- Return type:¶
einsum¶
-
cvxpy.einsum(subscripts, *exprs, optimize=
'greedy')[source]¶ Evaluates the Einstein summation convention on the given expressions.
This atom is the CVXPY analog of NumPy’s einsum function numpy.einsum [1], and it maintains the same syntax and semantics.
The einsum operation is evaluated by contracting pairs of expressions by elementwise multiplication and summation. The order in which the contractions are performed affects both the memory usage and the FLOP count required.
The optimize parameter determines whether to contract expressions using the optimal or greedy ordering. The cost to compute the optimal path is exponential in the number of distinct subscripts, while the cost to compute the greedy path is cubic in the number of distinct subscripts. We typically expect the greedy search to produce the optimal path for most problems.
Examples
>>> import cvxpy as cp >>> A = cp.Variable((3, 4)) >>> B = cp.Variable((4, 5)) >>> # Matrix multiplication >>> result = cp.einsum('ij,jk->ik', A, B) >>> >>> # Trace of a matrix >>> C = cp.Variable((3, 3)) >>> trace = cp.einsum('ii->', C)References
[1] https://numpy.org/doc/stable/reference/generated/numpy.einsum.html
- Parameters:¶
- subscripts : str¶
The subscripts for the einsum operation.
- exprs : Expression¶
The expressions to contract.
- optimize : {bool, 'greedy', 'optimal'}, optional¶
Whether to contract the expressions using the optimal or greedy ordering. Defaults to “greedy”.
- Returns:¶
The contracted expression.
- Return type:¶
hstack¶
- cvxpy.hstack(arg_list) Hstack[source]¶
Horizontal concatenation of an arbitrary number of Expressions.
- Parameters:¶
- arg_list : list of Expression¶
The Expressions to concatenate.
imag¶
index¶
-
class cvxpy.atoms.affine.index.index(expr, key, orig_key=
None)[source]¶ Bases:
AffAtomIndexing/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]])
Examples
>>> import cvxpy as cp >>> import numpy as np >>> x = cp.Variable((2, 3, 4)) >>> x[..., 2].shape (2, 3) >>> x[..., np.newaxis].shape (2, 3, 4, 1) >>> x[1, 2].shape (4,)
kron¶
matmul¶
- cvxpy.matmul(lh_exp, rh_exp) MulExpression[source]¶
Matrix multiplication.
mean¶
multiply¶
- class cvxpy.multiply(lh_expr, rh_expr)[source]¶
Bases:
MulExpressionMultiplies two expressions elementwise.
outer¶
- cvxpy.outer(x, y)[source]¶
Return the outer product of (x,y).
- Parameters:¶
- x : Expression, int, float, NumPy ndarray, or nested list thereof.¶
Input is flattened if not already a vector. The linear argument to the outer product.
- y : Expression, int, float, NumPy ndarray, or nested list thereof.¶
Input is flattened if not already a vector. The transposed-linear argument to the outer product.
- Returns:¶
expr – The outer product of (x,y), linear in x and transposed-linear in y.
- Return type:¶
partial_trace¶
-
cvxpy.partial_trace(expr, dims: tuple[int], axis: int | None =
0)[source]¶ Assumes \(\texttt{expr} = X_1 \otimes \cdots \otimes X_n\) is a 2D Kronecker product composed of \(n = \texttt{len(dims)}\) implicit subsystems. Letting \(k = \texttt{axis}\), the returned expression represents the partial trace of \(\texttt{expr}\) along its \(k^{\text{th}}\) implicit subsystem:
\[\text{tr}(X_k) (X_1 \otimes \cdots \otimes X_{k-1} \otimes X_{k+1} \otimes \cdots \otimes X_n).\]
partial_transpose¶
-
cvxpy.partial_transpose(expr, dims: tuple[int, ...], axis: int | None =
0)[source]¶ Assumes \(\texttt{expr} = X_1 \otimes ... \otimes X_n\) is a 2D Kronecker product composed of \(n = \texttt{len(dims)}\) implicit subsystems. Letting \(k = \texttt{axis}\), the returned expression is a partial transpose of \(\texttt{expr}\), with the transpose applied to its \(k^{\text{th}}\) implicit subsystem:
\[X_1 \otimes ... \otimes X_k^T \otimes ... \otimes X_n.\]
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
expris not a scalar.
psd_wrap¶
real¶
reshape¶
-
class cvxpy.reshape(expr, shape: int | tuple[int, ...], order: 'F' | 'C' | None =
None)[source]¶ Bases:
AffAtomReshapes 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 reshape
- shape : tuple or int¶
The shape to reshape to
- order : F(ortran) or C¶
vdot¶
- cvxpy.vdot(x, y)[source]¶
Return the standard inner product (or “scalar product”) of (x,y).
- Parameters:¶
- x : Expression, int, float, NumPy ndarray, or nested list thereof.¶
The conjugate-linear argument to the inner product.
- y : Expression, int, float, NumPy ndarray, or nested list thereof.¶
The linear argument to the inner product.
- Returns:¶
expr – The standard inner product of (x,y), conjugate-linear in x. We always have
expr.shape == ().- Return type:¶
Notes
The arguments
xandycan be nested lists; these lists will be flattened independently of one another.For example, if
x = [[a],[b]]andy = [c, d](witha,b,c,dreal scalars), then this function returns an Expression representinga * c + b * d.
sum¶
-
cvxpy.sum(expr, axis=
None, keepdims=False) None[source]¶ Sum the entries of an expression over a given axis.
- Parameters:¶
- expr : Expression¶
The expression to sum the entries of.
- axis : None or int or tuple of ints, optional¶
The axis or axes along which to sum. The default (axis=None) will sum all of the elements of the expression.
Added in version 1.6.0.
If axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple.
- keepdims : bool, optional¶
If set to true, the axes which are summed over remain in the output as dimensions with size one.
Examples
>>> import cvxpy as cp >>> x = cp.Variable((2, 3, 4)) >>> expr = cp.sum(x) >>> expr.shape () >>> expr = cp.sum(x, axis=0) >>> expr.shape (3, 4) >>> expr = cp.sum(x, axis=(1, 2)) >>> expr.shape (2,)
trace¶
- class cvxpy.trace(expr)[source]¶
Bases:
TLDR: Use alternate formulation for trace(A@B) for more efficient computation. trace(A@B) normally is O(n^3) because of the A@B operation. However, trace(A@B) only requires diagonal entries of A@B, which can be computed by taking the sum of element-wise product of A.T * B in O(n^2) time. In fact, vdot does this operation more robustly, using conj(A) instead of transpose.
transpose¶
-
class cvxpy.transpose(expr, axes=
None)[source]¶ Bases:
AffAtomTranspose an expression.
For an n-D expression, if axes are given, the order indicates the permutation of axes.
broadcast_to¶
- class cvxpy.broadcast_to(expr, shape)[source]¶
Bases:
AffAtomBroadcast the expression given a shape input
swapaxes¶
moveaxis¶
- class cvxpy.moveaxis(expr, source: list[int], destination: list[int])[source]¶
Bases:
Move axes of the expression to new positions.
permute_dims¶
- class cvxpy.permute_dims(expr, axes: list[int])[source]¶
Bases:
Permute the dimensions of the expression.
Alias for transpose with specified axes.
NegExpression¶
- class cvxpy.atoms.affine.unary_operators.NegExpression(expr)[source]¶
Bases:
UnaryOperatorNegation of an expression.
upper_tri¶
- class cvxpy.upper_tri(expr)[source]¶
Bases:
AffAtomThe vectorized strictly upper-triangular 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.vec(X, order: 'F' | 'C' | None =
None)[source]¶ Flattens the matrix X into a 1-d array.
vec_to_upper_tri¶
-
cvxpy.atoms.affine.upper_tri.vec_to_upper_tri(expr, strict: bool =
False)[source]¶ Reshapes a vector into an upper triangular matrix in row-major order. The strict argument specifies whether an upper or a strict upper triangular matrix should be returned. Inverts cp.upper_tri.
vstack¶
stack¶
-
cvxpy.stack(arrays: Sequence[object] | Iterable[object], axis: int =
0) Expression[source]¶ Join a sequence of expressions along a new axis.