xpress.Sum
a = xpress.Sum(container)
a = xpress.Sum(term1, term2, ...)
|
container
|
A container whose elements are to be summed. Recognised types:
list,
tuple,
dict (values only; keys are ignored), generator or iterator,
numpy.ndarray of any shape,
pandas.Series, and any object satisfying Python's sequence protocol.
|
|
term1, term2, ...
|
One or more terms. Each may be an
xpress.var, an Xpress linear, quadratic, general or nonlinear expression, or a Python
int or
float.
|
|
|
An
xpress.expression or
xpress.nonlin equal to the sum. The result is always an Xpress algebraic object, even when only a single term or only Python numbers are supplied, so it is directly usable in constraints and objectives without an additional type check.
|
import numpy as np
import pandas as pd
import xpress as xp
N = 20
S = range(N)
p = xp.problem()
x = [p.addVariable() for i in S]
y = [p.addVariable(vartype=xp.binary) for i in S]
# Multiple-argument form.
p.addConstraint(xp.Sum(x[0], x[1], x[2]) <= 3)
# Single-container form: list, slice, generator, dict, pandas Series.
p.setObjective(x[0] + xp.Sum(x[i]**2 for i in S))
p.addConstraint(xp.Sum(x[:5]) + xp.Sum(y[:5]) <= 10)
p.addConstraint(xp.Sum({i: x[i] for i in S}) == N)
p.addConstraint(xp.Sum(pd.Series(x)) <= 100)
# Multi-dimensional NumPy array is flattened to a scalar.
A = np.array([[x[0], x[1]], [x[2], x[3]]])
p.addConstraint(xp.Sum(A) <= 4)
# Mixed variables, linear terms, quadratic terms, and constants.
e = xp.Sum(x[0], 2 * x[1], x[0] * x[1], 5)
© 2001-2026 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.
