expression¶
Contains functionality for dealing with expressions.
- Exports:
>>> from pylp.expression import Expression
-
class
pylp.expression.
Expression
(operator: str, operands: list)[source]¶ Represents an expression in a linear programming problem.
Notes
A list of operands is given in order to reduce the height of the expression tree.
- Normally, an operator operates on two operands. Thus the expression:
- 5 + 5 + 5
- can be represented as a tree:
- / 5 +
- / 5 5
But as the expression gets longer, a naive approach would result in a very tall tree. In order to evaluate the tree, we would have to traverse all the way to its leafs, which at its deepest part would probably result in a stack overflow (maximum recursion error).
- But treating the operands as a list, results in a nicer tree:
/ | 5 5 5
And this tree stays at the same height the more operands it holds.
This prevents a stack overflow from occurring. This also results in better performance as well.