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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
Thank you for your reply. For the moment I have implemented the
following -
@staticmethod
def update_dict(expr,D,f_update):
for term in expr.args:
base = term.args_cnc()[1]
l_base = len(base)
if l_base > 0:
if l_base == 2:
base = base[0]*base[1]
else:
base = base[0]
if base not in D:
D[base] = f_update(base)
return
and update the dictionary each time before using subs where expr is the
sympy expression containing the e_{i}*e_{j} or e_{i}**2 factors.
I will try your method after working on some other problems for a while.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.