n2.add_step(replace(V,V2(r1,r2)),'replace potential with N=2') #n2.do_print() r_cm = Vector('r_cm',dim=2) r_12 = Vector('r_12',dim=2) r_12_def = definition(r_12, r2-r1) r_cm_def = definition(r_cm, (r1+r2)/2) V12 = Function('V') # replace r1,r2 with r_12 and r_cm # tranformation of limit region is hard, but the square box should be equivalent in periodic # boundary conditions # Jacobian is 1 n2.add_step(specialize_integral(r1,(r_12,r_cm)),'Switch variables') n2.add_step(replace(V2(r1,r2),V12(r_12)),'Specialize to a potential that depends only on interparticle distance') n2.add_step(replace(V12(r_12),V12(Abs(r_12))),'Depend only on the magnitude of the distance') #n2.add_step(replace_func(V12(r_12),Eq(V12,V12(Abs(r_12)))),'Depend only on the magnitude of the distance') n2.do_print() Vol = Symbol('Omega') n2.add_step(do_integral(Vol, [r_12]),'Integrate out r_cm (this step is still a hack)') L = Symbol('L') n2.add_step(identity(decompose),'Decompose into vector components') # Lower limit is adjusted by small amount to avoid a division by zero error at the origin. n2.add_step(identity(add_limits(-L/2+0.000000001,L/2)),'Add integration limits')
def peel_terms(s): lim_min = None lim_max = None for p in preorder_traversal(s): if isinstance(p, Sum): lmin = p.limits[0][1] lmax = p.limits[0][2] if lim_min: lim_min = max(lmin, lim_min) else: lim_min = lmin if lim_max: lim_max = min(lmax, lim_max) else: lim_max = lmax p = peel_sum_terms(lim_min, lim_max) new_s = rewrite(s, [(Sum, p)]) return new_s trap.add_step(identity(split_sum), 'Split sum') trap.add_step(identity(adjust_limits), 'Adjust limits') trap.add_step(identity(peel_terms), 'Peel terms') #trap.to_xhtml() trap.to_mathjax() if __name__ == '__main__': trap.do_print()
lim_min = None lim_max = None for p in preorder_traversal(s): if isinstance(p,Sum): lmin = p.limits[0][1] lmax = p.limits[0][2] if lim_min: lim_min = max(lmin,lim_min) else: lim_min = lmin if lim_max: lim_max = min(lmax,lim_max) else: lim_max = lmax p = peel_sum_terms(lim_min, lim_max) new_s = rewrite(s,[(Sum,p)]) return new_s trap.add_step(identity(split_sum),'Split sum') trap.add_step(identity(adjust_limits),'Adjust limits') trap.add_step(identity(peel_terms),'Peel terms') #trap.to_xhtml() trap.to_mathjax() if __name__ == '__main__': trap.do_print()