import sympy as S q=S.symbols('q',nonzero=True);l=1/q;delta=l-q p,u,v,d,x_a,x_b=S.symbols('p u v d x_a x_b') def two_Q(a,b): if a==b:return {(a,b):l} return {(b,a):S.Integer(1),**({(a,b):delta} if a>b else {})} def two_H(a,b): if a==b:return {(a,b):(x_a if a==1 else x_b)} return {(1,2):p,(2,1):u} if ab else {})} def three_H(a,b): if a==b:return {(a,b):x} lo,hi=sorted([a,b]);uv=uvars[lo,hi];tv=tvars[lo,hi] return {(b,a):uv} if a