pystencils.sympyextensions.math.subs_additive

Contents

pystencils.sympyextensions.math.subs_additive#

pystencils.sympyextensions.math.subs_additive(expr, replacement, subexpression, required_match_replacement=0.5, required_match_original=None)#

Transformation for replacing a given subexpression inside a sum.

Examples

The next example demonstrates the advantage of replace_additive compared to sympy.subs: >>> x, y, z, k = sp.symbols(“x y z k”) >>> subs_additive(3*x + 3*y, replacement=k, subexpression=x + y) 3*k

Terms that don’t match completely can be substituted at the cost of additional terms. This trade-off is managed using the required_match parameters. >>> subs_additive(3*x + 3*y + z, replacement=k, subexpression=x+y+z, required_match_original=1.0) 3*x + 3*y + z >>> subs_additive(3*x + 3*y + z, replacement=k, subexpression=x+y+z, required_match_original=0.5) 3*k - 2*z >>> subs_additive(3*x + 3*y + z, replacement=k, subexpression=x+y+z, required_match_original=2) 3*k - 2*z

Parameters:
  • expr (Expr) – input expression

  • replacement (Expr) – expression that is inserted for subexpression (if found)

  • subexpression (Expr) – expression to replace

  • required_match_replacement (Union[int, float, None]) –

    • if float: the percentage of terms of the subexpression that has to be matched in order to replace

    • if integer: the total number of terms that has to be matched in order to replace

    • None: is equal to integer 1

    • if both match parameters are given, both restrictions have to be fulfilled (i.e. logical AND)

  • required_match_original (Union[int, float, None]) –

    • if float: the percentage of terms of the original addition expression that has to be matched

    • if integer: the total number of terms that has to be matched in order to replace

    • None: is equal to integer 1

Return type:

Expr

Returns:

new expression with replacement