Initializing help system before first use

xpress.Sum

Purpose
Sums Xpress expressions and Python numbers into a single Xpress expression. Two calling conventions are supported: in the single-container form, xpress.Sum(container), the argument is treated as a container whose elements are summed; in the multiple-argument form, xpress.Sum(t1, t2, ...), each positional argument is treated as a single scalar term. Passing a container as one of several positional arguments raises xpress.ModelError.
Synopsis
a = xpress.Sum(container)
a = xpress.Sum(term1, term2, ...)
Arguments
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.
Return value
 
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.
Example
Valid uses of Sum:
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)
Further information
1. The multiple-argument form accepts only scalar terms: an xpress.var, an Xpress linear, quadratic, general or nonlinear expression, a Python int or float, or None. Passing a list, tuple, dict, numpy.ndarray, pandas.Series, generator, or any other container as one of several positional arguments raises xpress.ModelError; wrap the terms in a single container and use the single-container form instead.
2. Nested containers raise xpress.ModelError; only a top-level numpy.ndarray is flattened across its dimensions. For example, xpress.Sum([[a, b], [c]]) raises, while xpress.Sum(np.array([[a, b], [c, a]])) is summed as a + b + c + a.
3. None is skipped when it appears inside a container or as one of several positional arguments ( xpress.Sum(a, None, b) is equivalent to xpress.Sum(a, b)). A single None as the sole argument, xpress.Sum(None), raises xpress.ModelError.
4. Supported NumPy dtypes are object, float64, int32 and bool. Other dtypes — including int64, the default integer dtype on 64-bit platforms, and float32 — raise xpress.ModelError. Cast the array first, or build it with dtype=object.
5. Python sets are not supported and raise xpress.ModelError; convert the set to a list or tuple first.
6. Generators and iterators are consumed exactly once; a subsequent call with the same, exhausted generator yields an expression valued 0.
Related topics

© 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.