Kronecker delta Handling

Module containing functions to handle Kronecker deltas in expressions.

When differentiating thermodynamic expressions, Kronecker deltas may appear. This module provides functions to handle these deltas, either by simplifying them or replacing them with piecewise expressions.

handle_sum_kronecker(expr: Expr, idx: Idx) → Expr[source]

Simplifies Kronecker deltas in summations of an expression.

Differentiating thermodynamic expressions with respect to mole numbers produces Kronecker deltas inside summations, for example Sum(f(k)*KroneckerDelta(i, k), (k, 1, nc)). This function reduces those summations exactly as Sum(f(k)*KroneckerDelta(i, k), (k, 1, nc)) = f(i), keeping every delta-free summation as a symbolic sympy.Sum (so the code generator can turn it into a loop and N stays variable).

The reduction is applied recursively to every Sum of expr:

  • a term of a summation containing KroneckerDelta(x, u) (either argument order) with u the summation index is substituted u -> x and taken out of the summation, dropping the u limit;

  • a term containing KroneckerDelta(u, v) with both indices bound by the summation is substituted u -> v and the u limit is dropped;

  • terms without such deltas stay inside the (possibly reduced) summation.

This is equivalent to evaluating the delta summations with Sum.doit() under the assumptions 1 <= x <= nc, but it never expands the delta-free summations (which made full mixture models impractical to differentiate).

Parameters:
  • expr (sp.Expr) – Expression to simplify.

  • idx (sp.Idx) – Index variable for the Kronecker delta. Normally this is either i or j.

Returns:

Simplified expression.

Return type:

sp.Expr

handle_free_kronecker(expr: Expr, kdx: Idx, idx: Idx) → Piecewise[source]

Replace expression cotanining Kronecker delta(i, k) with Pieacewise.

Parameters:
  • expr (sp.Expr) – Expression to replace.

  • kdx (sp.Idx) – Index variable of the Kronecker delta, usually k.

  • idx (sp.Idx) – Index variable to replace the Kronecker delta, usually i or j.

Returns:

Piecewise expression that replaces the Kronecker delta.

Return type:

sp.Piecewise