Sum#

class gamspy.Sum(domain: OperationIndexType, expression: OperationRhsType)[source]#

Bases: Operation

Represents a sum operation over a domain.

Parameters:
domainSet | Alias | ImplicitSet | Sequence[Set | Alias], Domain, Condition
rhsOperation
Expression
MathOp
ImplicitVariable
ImplicitParameter
int
bool
Variable
Parameter
Attributes:
records

Evaluates the operation and returns the resulting records.

Methods

gamsRepr()

Representation of the Sum operation in GAMS language.

latexRepr()

Representation of this operation in Latex.

toGraph()

Return a graphviz.Digraph of this operation's tree.

toList()

Convenience method to return the records of the operation as a list.

toValue()

Convenience method to return the records of the operation as a Python float.

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=['i1','i2', 'i3'])
>>> v = gp.Variable(m, "v")
>>> e = gp.Equation(m, "e", type="eq")
>>> d = gp.Parameter(m, "d", domain=[i], records=[("i1", 1), ("i2", 2), ("i3", 4)])
>>> e[...] = gp.Sum(i, d[i]) <= v
gamsRepr()[source]#

Representation of the Sum operation in GAMS language.

Returns:
str

Examples

>>> from gamspy import Container, Set, Parameter, Variable, Sum
>>> m = Container()
>>> i = Set(m, "i", records=['i1','i2', 'i3'])
>>> c = Parameter(m, "c", domain=i)
>>> v = Variable(m, "v", domain=i)
>>> Sum(i, c[i]*v[i]).gamsRepr()
'sum(i,c(i) * v(i))'
latexRepr() str#

Representation of this operation in Latex.

Returns:
str

Examples

>>> import numpy as np
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=range(3))
>>> a = gp.Parameter(m, "a", domain=i, records=np.array([3,5,7]))
>>> print(gp.Sum(i, a[i]).latexRepr())
toGraph()#

Return a graphviz.Digraph of this operation’s tree.

The operation (e.g. sum) is drawn as a box with an edge to each index of its domain (labeled index) and an edge to its body. Requires the optional graphviz dependency (pip install gamspy[graph]).

Returns:
graphviz.Digraph

Examples

>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=range(3))
>>> a = gp.Parameter(m, "a", domain=i)
>>> graph = gp.Sum(i, a[i]).toGraph()
toList() list#

Convenience method to return the records of the operation as a list.

Returns:
list | None

Examples

>>> import numpy as np
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=["i1", "i2"])
>>> j = gp.Set(m, "j", records=["j1", "j2"])
>>> a = gp.Parameter(m, "a", domain=[i, j], records=np.array([(1,2), (3,4)]))
>>> gp.Sum(i, a[i, j]).toList()
[['j1', 4.0], ['j2', 6.0]]
toValue() float#

Convenience method to return the records of the operation as a Python float. Only possible if there is a single record as a result of the operation.

Returns:
float | None

Examples

>>> import numpy as np
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=["i1", "i2"])
>>> a = gp.Parameter(m, "a", domain=i, records=np.array([1,2]))
>>> gp.Sum(i, a[i]).toValue()
np.float64(3.0)
property records: pd.DataFrame | None#

Evaluates the operation and returns the resulting records.

Returns:
pd.DataFrame | None

Examples

>>> import numpy as np
>>> import gamspy as gp
>>> m = gp.Container()
>>> i = gp.Set(m, "i", records=["i1", "i2"])
>>> a = gp.Parameter(m, "a", domain=i, records=np.array([1,2]))
>>> gp.Sum(i, a[i]).records
   value
0    3.0