Just select a subset by using indexing (in the following example: classic python-based slicing; but more complex indexing / numpy-style is possible):
Example:
from cvxpy import *
x = Variable(5)
constraints = []
constraints.append(x >= 0) # all vars
constraints.append(x <= 10) # all vars
constraints.append(sum_entries(x[:3]) <= 3) # only part of vector; sum(first-three) <=3
objective = Maximize(sum_entries(x))
problem = Problem(objective, constraints)
problem.solve()
print(problem.status)
print(x.value.T)
Output:
optimal
[[ 1. 1. 1. 10. 10.]]
Note: as of cvxpy 1.0, sum_entries has been changed to sum
I also suspect you are misunderstanding the problem here, but that formula-image is of course incomplete to be implemented.
Answer from sascha on Stack OverflowCvxpy
cvxpy.org › tutorial › functions › index.html
Atomic Functions -
The functions max and min give ... and minimum to find the max or min of a list of scalar expressions. The CVXPY function sum sums all the entries in a single expression....
Cvxpy
cvxpy.org › api_reference › cvxpy.atoms.affine.html
Affine Atoms -
class cvxpy.cumsum(expr: Expression, axis: None | int = 0)[source]¶ ... Cumulative sum of the elements of an expression.
Cvxpy
cvxpy.org › examples › basic › least_squares.html
Least-squares -
In a least-squares, or linear regression, problem, we have measurements \(A \in \mathcal{R}^{m \times n}\) and \(b \in \mathcal{R}^m\) and seek a vector \(x \in \mathcal{R}^{n}\) such that \(Ax\) is close to \(b\). Closeness is defined as the sum of the squared differences: \[\sum_{i=1}^m (a_i^Tx ...
Snyk
snyk.io › advisor › cvxpy › functions › cvxpy.sum
How to use the cvxpy.sum function in cvxpy | Snyk
cvxgrp / cvxportfolio / cvxportfolio / policies.py View on Github · t : pd.timestamp Timestamp for the optimization. """ if t is None: t = dt.datetime.today() value = sum(portfolio) w = portfolio / value z = cvx.Variable(w.size) # TODO pass index wplus = w.values + z if isinstance(self.return_forecast, BaseReturnsModel): alpha_term = self.return_forecast.weight_expr(t, wplus) else: alpha_term = cvx.sum(cvx.multiply( values_in_time(self.return_forecast, t).values, wplus)) assert(alpha_term.is_concave()) costs, constraints = [], [] for cost in self.costs: cost_expr, const_expr = cost.weight_expr(t, wplus, z, value) costs.append(cost_expr) constraints += const_expr constraints += [item for item in (con.weight_expr(t, wplus, z, value) for con in self.constraints)] for el in costs:
Readthedocs
ajfriendcvxpy.readthedocs.io › en › latest › tutorial › intro
What is CVXPY? — CVXPY 0.2.25 documentation
Use the CVXPY function sum_entries to sum the entries of a single CVXPY matrix or vector expression.
Readthedocs
ajfriendcvxpy.readthedocs.io › en › latest › tutorial › functions
Functions — CVXPY 0.2.25 documentation - Read the Docs
The input to bmat is a list of lists of CVXPY expressions. It constructs a block matrix. The elements of each inner list are stacked horizontally and then the resulting block matrices are stacked vertically. The output \(y\) of conv(c, x) has size \(n+m-1\) and is defined as \(y[k]=\sum_{j=0}^k c[j]x[k-j]\).
Cvxpy
cvxpy.org › _modules › cvxpy › atoms › affine › sum.html
cvxpy.atoms.affine.sum -
""" import builtins from functools ... AxisAtom from cvxpy.constraints.constraint import Constraint class Sum(AxisAtom, AffAtom): """Sum the entries of an expression over a given axis....
Cvxpy
cvxpy.org › _modules › cvxpy › atoms › sum_squares.html
cvxpy.atoms.sum_squares -
See the License for the specific ... sum_squares( expr, axis: Optional[Union[int, Tuple[int, ...]]] = None, keepdims: bool = False ): """The sum of the squares of the entries....
Cvxpy
cvxpy.org › api_reference › cvxpy.expressions.html
Expressions -
Expression : Sum two expressions.
Cvxpy
cvxpy.org › api_reference › cvxpy.transforms.html
Transforms -
Combines objectives as a weighted sum.
Cvxpy
cvxpy.org › tutorial › advanced › index.html
Advanced Features -
# create a 3-dimensional variable (locations, days, hours) x = cp.Variable((12, 10, 24)) constraints = [ cp.sum(x, axis=(0, 2)) <= 2000, # constrain the daily usage across all locations x[:, :, :12] <= 100, # constrain the first 12 hours of each day at every location x[:, 3, :] == 0,] # constrain the usage on the fourth day to be zero obj = cp.Minimize(cp.sum_squares(x)) prob = cp.Problem(obj, constraints) prob.solve() Please refer to NumPy’s excellent reference on N-dimensional arrays and the array API standard for more details on how to manipulate N-dimensional arrays. Our goal is to match the NumPy API as closely as possible. ... N-dimensional support is still experimental and may not work with all CVXPY features.
Cvxpy
cvxpy.org › _modules › cvxpy › atoms › affine › cumsum.html
cvxpy.atoms.affine.cumsum -
""" def __init__(self, expr: Expression, axis: int = 0) -> None: super(cumsum, self).__init__(expr, axis) @AffAtom.numpy_numeric def numeric(self, values): """ Returns the cumulative sum of elements of an expression over an axis.
Cvxpy
cvxpy.org › version › 1.2 › api_reference › cvxpy.expressions.html
Expressions — CVXPY 1.2 documentation
Expression : Sum two expressions.
GitHub
github.com › cvxpy › cvxpy › issues › 1784
cp.sum is inconsistent with np.ndarray · Issue #1784 · cvxpy/cvxpy
Published May 23, 2022
Author michael-123123
Cvxpy
cvxpy.org › api_reference › cvxpy.atoms.other_atoms.html
Other Atoms -
Sum of the largest k eigenvalues.
Cvxpy
cvxpy.org › _modules › cvxpy › atoms › sum_largest.html
cvxpy.atoms.sum_largest -
""" from typing import Tuple import numpy as np import scipy.sparse as sp import cvxpy.interface as intf from cvxpy.atoms.atom import Atom · [docs] class sum_largest(Atom): """ Sum of the largest k values in the expression X """ def __init__(self, x, k: int) -> None: self.k = k super(sum_largest, self).__init__(x) def validate_arguments(self) -> None: """Verify that k is a positive integer.
Cvxpy
cvxpy.org › updates › index.html
Changes to CVXPY — CVXPY 1.5 documentation
Adds methods to CVXPY expressions that are found on NumPy ndarrays such as .sum(), .max(), and .mean()
CVXPY
cvxpy.org
Cvxpy
import cvxpy as cp import numpy as np # Problem data. m = 30 n = 20 np.random.seed(1) A = np.random.randn(m, n) b = np.random.randn(m) # Construct the problem. x = cp.Variable(n) objective = cp.Minimize(cp.sum_squares(A @ x - b)) constraints = [0 <= x, x <= 1] prob = cp.Problem(objective, constraints) # The optimal objective value is returned by `prob.solve()`. result = prob.solve() # The optimal value for x is stored in `x.value`. print(x.value) # The optimal Lagrange multiplier for a constraint is stored in # `constraint.dual_value`. print(constraints[0].dual_value)