sum#

VarDictND.sum(*pattern)[source]#

Sum all variables, or a subset based on wildcard pattern, in an expression.

Parameters:
*patternAny, optional

For subsets, the pattern requires one value for each dimension of the N-dim tuple key. The single-character string '*' (asterisk) can be used as a wildcard to represent all possible values for a dimension.

Returns:
xpress.expression
Raises:
TypeError

If the pattern includes non-scalar(s).

ValueError

If the pattern is not the same as the length of N-dim tuple keys.

ValueError

If the pattern has no wildcard or all wildcards.

Examples

Create xpress problem:

>>> import xpress as xp
>>> import warnings
>>> warnings.filterwarnings('ignore', category=xp.LicenseWarning)
>>> prob = xp.problem()

Create index-set:

>>> arcs = IndexSetND([('A', 'B'), ('B', 'C'), ('C', 'B')], names=['ori', 'des'])

Add variables:

>>> from opti_extensions.xpress import addVariables
>>> arc_flow = addVariables(prob, arcs, name='arc-flow', ub=10, vartype=xp.continuous)

Sum all variables:

>>> arc_flow.sum()
arc-flow(('A', 'B')) + arc-flow(('B', 'C')) + arc-flow(('C', 'B'))

Sum subset of variables having 'B' at the second dimension index:

>>> arc_flow.sum('*', 'B')
arc-flow(('A', 'B')) + arc-flow(('C', 'B'))

Sum subset of variables having 'Z' at the first dimension index:

>>> arc_flow.sum('Z', '*')
0