Expression#
- class gamspy.Expression(left: OperableType | ImplicitEquation | str | None, operator: str, right: OperableType | str | None)[source]#
Bases:
OperableRepresents an expression involving two operands and an operator.
This class constructs a binary expression tree that can be evaluated or translated into GAMS syntax.
- Parameters:
- leftOperableType | ImplicitEquation | None
Left operand.
- operatorstr
The operator symbol (e.g., ‘+’, ‘-’, ‘*’, ‘=’, ‘eq’).
- rightOperableType | str | None
Right operand.
- Attributes:
recordsEvaluates the expression and returns the resulting records.
- representation
Methods
gamsRepr()Representation of this Expression in GAMS language.
Declaration of the Expression in GAMS
Returns the LaTeX representation of this Expression.
toGraph()Return a
graphviz.Digraphof this expression's tree.toList()Convenience method to return the records of the expression as a list.
toValue()Convenience method to return expression records as a Python float.
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, name="a") >>> b = gp.Parameter(m, name="b") >>> expression = a * b >>> expression.gamsRepr() 'a * b'
- gamsRepr() str[source]#
Representation of this Expression in GAMS language.
- Returns:
- str
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, name="a") >>> b = gp.Parameter(m, name="b") >>> expression = a * b >>> expression.gamsRepr() 'a * b'
- getDeclaration() str[source]#
Declaration of the Expression in GAMS
- Returns:
- str
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, name="a") >>> b = gp.Parameter(m, name="b") >>> expression = a * b >>> expression.getDeclaration() 'a * b'
- latexRepr() str[source]#
Returns the LaTeX representation of this Expression.
- Returns:
- str
The LaTeX string.
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, name="a") >>> b = gp.Parameter(m, name="b") >>> (a + b).latexRepr() 'a + b'
- toGraph()[source]#
Return a
graphviz.Digraphof this expression’s tree.Each operator/operation is drawn as a box and each symbol or number as a leaf, so the structure of the expression can be inspected visually or rendered to a file. Requires the optional
graphvizdependency (pip install gamspy[graph]); aValidationErroris raised if it is not installed.- Returns:
- graphviz.Digraph
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, name="a") >>> b = gp.Parameter(m, name="b") >>> c = gp.Parameter(m, name="c") >>> d = gp.Parameter(m, name="d") >>> graph = (a * b + c / d).toGraph() >>> graph.render("expr", format="svg")
- toList() list[source]#
Convenience method to return the records of the expression as a list.
- Returns:
- list
Examples
>>> import numpy as np >>> import gamspy as gp >>> m = gp.Container() >>> i = gp.Set(m, records=range(3)) >>> a = gp.Parameter(m, domain=i, records=np.array([1,2,3])) >>> b = gp.Parameter(m, domain=i, records=np.array([4,5,6])) >>> (a + b).toList() [['0', 5.0], ['1', 7.0], ['2', 9.0]]
- toValue() float[source]#
Convenience method to return expression records as a Python float. Only possible if there is a single record as a result of the expression evaluation.
- Returns:
- float | None
- Raises:
- TypeError
In case the dimension of the expression is not zero.
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, records=5) >>> b = gp.Parameter(m, records=6) >>> (a + b).toValue() np.float64(11.0)
- property records: pd.DataFrame | None#
Evaluates the expression and returns the resulting records.
- Returns:
- pd.DataFrame | None
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> a = gp.Parameter(m, records=5) >>> b = gp.Parameter(m, records=6) >>> (a + b).records value 0 11.0
- property representation: str#
!! processed by numpydoc !!