1 core.expressions.Expression

core.expressions.Expression()

Abstract base class for all symbolic expressions.

Expressions form a tree structure that can be evaluated given variable values. All arithmetic operators are overloaded to build expression trees automatically.

1.1 Attributes

Name Type Description
_hash Cached hash value for the expression.
_degree Cached polynomial degree (None if not computed, -1 if non-polynomial).

1.2 Methods

Name Description
between Create range constraints: lb <= self <= ub.
constraint_eq Create an == constraint: self == other.
eq Create an == constraint: self == other.
evaluate Evaluate the expression given variable values.
get_variables Return all variables this expression depends on.
is_linear Check if this expression is linear (degree <= 1).
jacobian_row Return gradient with respect to each variable in O(1) if possible.

1.2.1 between

core.expressions.Expression.between(lb, ub)

Create range constraints: lb <= self <= ub.

1.2.1.1 Returns

Name Type Description
list[Constraint] List of two constraints: [self >= lb, self <= ub].

1.2.1.2 Example

x = Variable(“x”) constraints = x.between(0, 10)

1.2.2 constraint_eq

core.expressions.Expression.constraint_eq(other)

Create an == constraint: self == other.

Note: We use constraint_eq() instead of eq because eq is used for object identity comparison which is needed for sets/dicts.

.. deprecated:: Use :meth:eq instead. This method is kept for backwards compatibility.

1.2.3 eq

core.expressions.Expression.eq(other)

Create an == constraint: self == other.

Note: We use eq() instead of eq because eq is used for object identity comparison which is needed for sets/dicts.

1.2.4 evaluate

core.expressions.Expression.evaluate(values)

Evaluate the expression given variable values.

1.2.4.1 Parameters

Name Type Description Default
values Mapping[str, ArrayLike | float] Dictionary mapping variable names to their values. required

1.2.4.2 Returns

Name Type Description
NDArray[np.floating] | float The numerical result of evaluating the expression.

1.2.5 get_variables

core.expressions.Expression.get_variables()

Return all variables this expression depends on.

1.2.6 is_linear

core.expressions.Expression.is_linear()

Check if this expression is linear (degree <= 1).

1.2.6.1 Returns

Name Type Description
bool True if the expression is constant or linear in variables.

Uses cached degree computation for performance.

1.2.7 jacobian_row

core.expressions.Expression.jacobian_row(variables)

Return gradient with respect to each variable in O(1) if possible.

1.2.7.1 Parameters

Name Type Description Default
variables list[Variable] List of variables to compute gradients for. required

1.2.7.2 Returns

Name Type Description
list[Expression] | None List of gradient expressions if O(1) computation is possible,
list[Expression] | None None otherwise (fall back to individual gradient calls).