from sympy.core.rules import Transform help(Transform)
On Tue, Apr 30, 2013 at 9:46 PM, Alan Bromborsky <abro...@verizon.net>wrote: > On 04/30/2013 11:35 AM, Chris Smith wrote: > >> Well, I think it would be an abuse of subs to make it work with your >> autoupdating dictionary. If I understand mul_dict, it doesn't contain any >> entries at the start -- it becomes populated as you request ei*ej values. >> What about using Transform: >> >> # a rule that all Muls get multiplied by 2 >> >> >>> m = {} # the dictionary that keeps track of the rule >> >>> (x+x*y+z*a).xreplace(**Transform(lambda k: m.setdefault(k, 2*k), >> lambda w: w.is_Mul)) >> 2*a*z + 2*x*y + x >> >>> m >> {x*y: 2*x*y, a*z: 2*a*z} >> >> But that is not the right way to benefit from the dictionary since the >> argument to setdefault is called every time. Instead (and I verify by using >> the `ok` function that delays the number of seconds equal to its argument) >> this is how it should be done: >> >> >>> def ok(i): >> ... t=time() >> ... while time()-t<i:pass >> ... return i >> ... >> >>> (3*x+3*x*y+3*z*a).xreplace(**Transform(lambda k: m[k] if k in m else >> m.setdefault(k, ok(k)), lambda w: w.is_Integer)) >> 3*a*z + 3*x*y + 3*x >> >> That only takes 3 seconds to run, not 9. >> >> /c >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to >> sympy+unsubscribe@**googlegroups.com<sympy%2bunsubscr...@googlegroups.com> >> . >> To post to this group, send email to sympy@googlegroups.com. >> Visit this group at >> http://groups.google.com/**group/sympy?hl=en-US<http://groups.google.com/group/sympy?hl=en-US> >> . >> For more options, visit >> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >> . >> >> >> Where is Transform() documented, what does it do? > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to > sympy+unsubscribe@**googlegroups.com<sympy%2bunsubscr...@googlegroups.com> > . > To post to this group, send email to sympy@googlegroups.com. > Visit this group at > http://groups.google.com/**group/sympy?hl=en-US<http://groups.google.com/group/sympy?hl=en-US> > . > For more options, visit > https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> > . > > > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.