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 asSum(f(k)*KroneckerDelta(i, k), (k, 1, nc)) = f(i), keeping every delta-free summation as a symbolicsympy.Sum(so the code generator can turn it into a loop andNstays variable).The reduction is applied recursively to every
Sumofexpr:a term of a summation containing
KroneckerDelta(x, u)(either argument order) withuthe summation index is substitutedu -> xand taken out of the summation, dropping theulimit;a term containing
KroneckerDelta(u, v)with both indices bound by the summation is substitutedu -> vand theulimit 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 assumptions1 <= 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