Coverage for /usr/lib/python3/dist-packages/sympy/strategies/tools.py: 64%
11 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
1from . import rl
2from .core import do_one, exhaust, switch
3from .traverse import top_down
6def subs(d, **kwargs):
7 """ Full simultaneous exact substitution.
9 Examples
10 ========
12 >>> from sympy.strategies.tools import subs
13 >>> from sympy import Basic, S
14 >>> mapping = {S(1): S(4), S(4): S(1), Basic(S(5)): Basic(S(6), S(7))}
15 >>> expr = Basic(S(1), Basic(S(2), S(3)), Basic(S(4), Basic(S(5))))
16 >>> subs(mapping)(expr)
17 Basic(4, Basic(2, 3), Basic(1, Basic(6, 7)))
18 """
19 if d:
20 return top_down(do_one(*map(rl.subs, *zip(*d.items()))), **kwargs)
21 else:
22 return lambda x: x
25def canon(*rules, **kwargs):
26 """ Strategy for canonicalization.
28 Explanation
29 ===========
31 Apply each rule in a bottom_up fashion through the tree.
32 Do each one in turn.
33 Keep doing this until there is no change.
34 """
35 return exhaust(top_down(exhaust(do_one(*rules)), **kwargs))
38def typed(ruletypes):
39 """ Apply rules based on the expression type
41 inputs:
42 ruletypes -- a dict mapping {Type: rule}
44 Examples
45 ========
47 >>> from sympy.strategies import rm_id, typed
48 >>> from sympy import Add, Mul
49 >>> rm_zeros = rm_id(lambda x: x==0)
50 >>> rm_ones = rm_id(lambda x: x==1)
51 >>> remove_idents = typed({Add: rm_zeros, Mul: rm_ones})
52 """
53 return switch(type, ruletypes)