Skip to content

Expression class

Bases: Element

Represents a mathematical expression used to encapsulate expressions in an optimization model.

The Expression class is a subclass of the Element class and serves as a representation of a mathematical expression within an optimization model. It is specifically designed to encapsulate expressions and offers methods for performing various mathematical operations on those expressions.

Note: The supported mathematical operations on an Expression instance depend on the operations supported in the encapsulated expression. These operations can include arithmetic operations, functions, and any other operations defined by the underlying mathematical expression.

Source code in pyorlib/algebra/expressions/expression.py
class Expression(Element):
    """
    Represents a mathematical expression used to encapsulate expressions in an optimization model.

    The `Expression` class is a subclass of the `Element` class and serves as a representation of a mathematical
    expression within an optimization model. It is specifically designed to encapsulate expressions and offers
    methods for performing various mathematical operations on those expressions.

    **Note**: The supported mathematical operations on an `Expression` instance depend on the operations supported
    in the encapsulated expression. These operations can include arithmetic operations, functions, and
    any other operations defined by the underlying mathematical expression.
    """

    @property
    def raw(self) -> Any:
        return self.__expression

    def __init__(self, expression: Any):
        """
        Initialize an Expression instance.
        :param expression: The expression value to be encapsulated.
        :raises ValueError: If the expression is None.
        """
        # Applies validations
        if expression is None:
            raise ValueError("Expression cannot be None")

        # Instance attributes
        self.__expression: Any = expression
        """ The mathematical expression. """

    def _build_expression(self, expression: Any) -> Element:
        return Expression(expression=expression)

    def __str__(self) -> str:
        return str(self.raw)

    def __iadd__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression += other.raw
        else:
            self.__expression += other
        return self

    def __isub__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression -= other.raw
        else:
            self.__expression -= other
        return self

    def __imul__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression *= other.raw
        else:
            self.__expression *= other
        return self

    def __itruediv__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression /= other.raw
        else:
            self.__expression /= other
        return self

    def __ifloordiv__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression //= other.raw
        else:
            self.__expression //= other
        return self

    def __imod__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression %= other.raw
        else:
            self.__expression %= other
        return self

    def __ipow__(self, other: Any) -> Element:
        if isinstance(other, Element):
            self.__expression **= other.raw
        else:
            self.__expression **= other
        return self

Attributes

raw property

raw: Any

Functions

__add__

__add__(other: Any) -> Element

Addition operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be added.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the addition.

Source code in pyorlib/algebra/element.py
def __add__(self, other: Any) -> "Element":
    """
    Addition operation.
    :param other: The `number` or `Element` instance to be added.
    :return: A new `Element` instance representing the addition.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw + other.raw)
    else:
        return self._build_expression(expression=self.raw + other)

__radd__

__radd__(other: Any) -> Element

Right addition operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be added.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the addition.

Source code in pyorlib/algebra/element.py
def __radd__(self, other: Any) -> "Element":
    """
    Right addition operation.
    :param other: The `number` or `Element` instance to be added.
    :return: A new `Element` instance representing the addition.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw + self.raw)
    else:
        return self._build_expression(expression=other + self.raw)

__sub__

__sub__(other: Any) -> Element

Subtraction operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be subtracted.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the subtraction.

Source code in pyorlib/algebra/element.py
def __sub__(self, other: Any) -> "Element":
    """
    Subtraction operation.
    :param other: The `number` or `Element` instance to be subtracted.
    :return: A new `Element` instance representing the subtraction.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw - other.raw)
    else:
        return self._build_expression(expression=self.raw - other)

__rsub__

__rsub__(other: Any) -> Element

Right subtraction operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be subtracted.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the subtraction.

Source code in pyorlib/algebra/element.py
def __rsub__(self, other: Any) -> "Element":
    """
    Right subtraction operation.
    :param other: The `number` or `Element` instance to be subtracted.
    :return: A new `Element` instance representing the subtraction.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw - self.raw)
    else:
        return self._build_expression(expression=other - self.raw)

__mul__

__mul__(other: Any) -> Element

Multiplication operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be multiplied.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the multiplication.

Source code in pyorlib/algebra/element.py
def __mul__(self, other: Any) -> "Element":
    """
    Multiplication operation.
    :param other: The `number` or `Element` instance to be multiplied.
    :return: A new `Element` instance representing the multiplication.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw * other.raw)
    else:
        return self._build_expression(expression=self.raw * other)

__rmul__

__rmul__(other: Any) -> Element

Right multiplication operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be multiplied.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the multiplication.

Source code in pyorlib/algebra/element.py
def __rmul__(self, other: Any) -> "Element":
    """
    Right multiplication operation.
    :param other: The `number` or `Element` instance to be multiplied.
    :return: A new `Element` instance representing the multiplication.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw * self.raw)
    else:
        return self._build_expression(expression=other * self.raw)

__truediv__

__truediv__(other: Any) -> Element

Division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the division.

Source code in pyorlib/algebra/element.py
def __truediv__(self, other: Any) -> "Element":
    """
    Division operation.
    :param other: The `number` or `Element` instance to be divided.
    :return: A new `Element` instance representing the division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw / other.raw)
    else:
        return self._build_expression(expression=self.raw / other)

__rtruediv__

__rtruediv__(other: Any) -> Element

Right division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the division.

Source code in pyorlib/algebra/element.py
def __rtruediv__(self, other: Any) -> "Element":
    """
    Right division operation.
    :param other: The `number` or `Element` instance to be divided.
    :return: A new `Element` instance representing the division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw / self.raw)
    else:
        return self._build_expression(expression=other / self.raw)

__floordiv__

__floordiv__(other: Any) -> Element

Floor division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be floor divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the floor division.

Source code in pyorlib/algebra/element.py
def __floordiv__(self, other: Any) -> "Element":
    """
    Floor division operation.
    :param other: The `number` or `Element` instance to be floor divided.
    :return: A new `Element` instance representing the floor division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw // other.raw)
    else:
        return self._build_expression(expression=self.raw // other)

__rfloordiv__

__rfloordiv__(other: Any) -> Element

Right floor division operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be floor divided.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the floor division.

Source code in pyorlib/algebra/element.py
def __rfloordiv__(self, other: Any) -> "Element":
    """
    Right floor division operation.
    :param other: The `number` or `Element` instance to be floor divided.
    :return: A new `Element` instance representing the floor division.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw // self.raw)
    else:
        return self._build_expression(expression=other // self.raw)

__mod__

__mod__(other: Any) -> Element

Modulo operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used for modulo.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the modulo operation.

Source code in pyorlib/algebra/element.py
def __mod__(self, other: Any) -> "Element":
    """
    Modulo operation.
    :param other: The `number` or `Element` instance to be used for modulo.
    :return: A new `Element` instance representing the modulo operation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw % other.raw)
    else:
        return self._build_expression(expression=self.raw % other)

__rmod__

__rmod__(other: Any) -> Element

Right modulo operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used for modulo.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the modulo operation.

Source code in pyorlib/algebra/element.py
def __rmod__(self, other: Any) -> "Element":
    """
    Right modulo operation.
    :param other: The `number` or `Element` instance to be used for modulo.
    :return: A new `Element` instance representing the modulo operation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw % self.raw)
    else:
        return self._build_expression(expression=other % self.raw)

__pow__

__pow__(other: Any) -> Element

Exponentiation operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used as the exponent.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the exponentiation.

Source code in pyorlib/algebra/element.py
def __pow__(self, other: Any) -> "Element":
    """
    Exponentiation operation.
    :param other: The `number` or `Element` instance to be used as the exponent.
    :return: A new `Element` instance representing the exponentiation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw**other.raw)
    else:
        return self._build_expression(expression=self.raw**other)

__rpow__

__rpow__(other: Any) -> Element

Right exponentiation operation.

PARAMETER DESCRIPTION
other

The number or Element instance to be used as the base.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the exponentiation.

Source code in pyorlib/algebra/element.py
def __rpow__(self, other: Any) -> "Element":
    """
    Right exponentiation operation.
    :param other: The `number` or `Element` instance to be used as the base.
    :return: A new `Element` instance representing the exponentiation.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=other.raw**self.raw)
    else:
        return self._build_expression(expression=other**self.raw)

__neg__

__neg__() -> Element

Negation operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the negation.

Source code in pyorlib/algebra/element.py
def __neg__(self) -> "Element":
    """
    Negation operation.
    :return: A new `Element` instance representing the negation.
    """
    return self._build_expression(expression=-self.raw)

__pos__

__pos__() -> Element

Positive operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the positive value.

Source code in pyorlib/algebra/element.py
def __pos__(self) -> "Element":
    """
    Positive operation.
    :return: A new `Element` instance representing the positive value.
    """
    return self._build_expression(expression=+self.raw)

__abs__

__abs__() -> Element

Absolute value operation.

RETURNS DESCRIPTION
Element

A new Element instance representing the absolute value.

Source code in pyorlib/algebra/element.py
def __abs__(self) -> "Element":
    """
    Absolute value operation.
    :return: A new `Element` instance representing the absolute value.
    """
    return self._build_expression(expression=abs(self.raw))

__eq__

__eq__(other: Any) -> Element

Equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __eq__(self, other: Any) -> "Element":  # type: ignore[override]
    """
    Equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw == other.raw)
    else:
        return self._build_expression(expression=self.raw == other)

__ne__

__ne__(other: Any) -> Element

Not equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __ne__(self, other: Any) -> "Element":  # type: ignore[override]
    """
    Not equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw != other.raw)
    else:
        return self._build_expression(expression=self.raw != other)

__lt__

__lt__(other: Any) -> Element

Less than comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __lt__(self, other: Any) -> "Element":
    """
    Less than comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw < other.raw)
    else:
        return self._build_expression(expression=self.raw < other)

__le__

__le__(other: Any) -> Element

Less than or equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __le__(self, other: Any) -> "Element":
    """
    Less than or equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw <= other.raw)
    else:
        return self._build_expression(expression=self.raw <= other)

__gt__

__gt__(other: Any) -> Element

Greater than comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __gt__(self, other: Any) -> "Element":
    """
    Greater than comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw > other.raw)
    else:
        return self._build_expression(expression=self.raw > other)

__ge__

__ge__(other: Any) -> Element

Greater than or equal to comparison.

PARAMETER DESCRIPTION
other

The number or Element instance to be compared.

TYPE: Any

RETURNS DESCRIPTION
Element

A new Element instance representing the comparison result.

Source code in pyorlib/algebra/element.py
def __ge__(self, other: Any) -> "Element":
    """
    Greater than or equal to comparison.
    :param other: The `number` or `Element` instance to be compared.
    :return: A new `Element` instance representing the comparison result.
    """
    if isinstance(other, Element):
        return self._build_expression(expression=self.raw >= other.raw)
    else:
        return self._build_expression(expression=self.raw >= other)

__init__

__init__(expression: Any)

Initialize an Expression instance.

PARAMETER DESCRIPTION
expression

The expression value to be encapsulated.

TYPE: Any

RAISES DESCRIPTION
ValueError

If the expression is None.

Source code in pyorlib/algebra/expressions/expression.py
def __init__(self, expression: Any):
    """
    Initialize an Expression instance.
    :param expression: The expression value to be encapsulated.
    :raises ValueError: If the expression is None.
    """
    # Applies validations
    if expression is None:
        raise ValueError("Expression cannot be None")

    # Instance attributes
    self.__expression: Any = expression
    """ The mathematical expression. """

__iadd__

__iadd__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __iadd__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression += other.raw
    else:
        self.__expression += other
    return self

__isub__

__isub__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __isub__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression -= other.raw
    else:
        self.__expression -= other
    return self

__imul__

__imul__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __imul__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression *= other.raw
    else:
        self.__expression *= other
    return self

__itruediv__

__itruediv__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __itruediv__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression /= other.raw
    else:
        self.__expression /= other
    return self

__ifloordiv__

__ifloordiv__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __ifloordiv__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression //= other.raw
    else:
        self.__expression //= other
    return self

__imod__

__imod__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __imod__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression %= other.raw
    else:
        self.__expression %= other
    return self

__ipow__

__ipow__(other: Any) -> Element
Source code in pyorlib/algebra/expressions/expression.py
def __ipow__(self, other: Any) -> Element:
    if isinstance(other, Element):
        self.__expression **= other.raw
    else:
        self.__expression **= other
    return self