Re: [sympy] auto_update_dict in subs

2013-05-01 Thread Alan Bromborsky
On 04/30/2013 10:57 PM, Chris Smith wrote: There are different ways to organize this, but here is a compact way -- not necessarily the best. I don't know what your 'func's look like so I am just showing the transformation of nc by multiplying or dividing by 2: def mul_xform(func): def

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
How about letting cacheit be your dictionary? (And I don't see how subs was going to be used from what you said.) class aud(object): ... def __init__(self, func): ... self.func = func ... @cacheit ... def __getitem__(self, arg): ... return self.func(arg) ... __call__ =

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Alan Bromborsky
On 04/30/2013 09:26 AM, Chris Smith wrote: How about letting cacheit be your dictionary? (And I don't see how subs was going to be used from what you said.) class aud(object): ... def __init__(self, func): ... self.func = func ... @cacheit ... def __getitem__(self, arg): ...

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
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 = {}

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Alan Bromborsky
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

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Alan Bromborsky
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

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
@staticmethod def update_dict(expr,D,f_update): for term in expr.args: base = term.args_cnc()[1] l_base = len(base) Just a suggestion -- since you don't care about the number of bases, how about if base: base = Mul._from_args(base) # much faster

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
from sympy.core.rules import Transform help(Transform) On Tue, Apr 30, 2013 at 9:46 PM, Alan Bromborsky abro...@verizon.netwrote: 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

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Alan Bromborsky
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

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
def product_bases(w): nc = w.args_cnc()[1] return len(nc) == 2 or len(nc) == 1 and nc[0].is_Pow and nc[0].exp == 2 but could you have b1**2*b1**2 - b1**4 in which case the test above would be nc[0].exp.is_Integer and sqrt(nc[0].exp).is_Integer might be better? Could you have

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Alan Bromborsky
On 04/30/2013 12:46 PM, Chris Smith wrote: def product_bases(w): nc = w.args_cnc()[1] return len(nc) == 2 or len(nc) == 1 and nc[0].is_Pow and nc[0].exp == 2 but could you have b1**2*b1**2 - b1**4 in which case the test above would be nc[0].exp.is_Integer and

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
# untested but something like... m = {} def func(nc): return 2*nc # whatever your rule is def product_bases(w): nc = w.args_cnc()[1] if len(nc) == 2 or len(nc) == 1 and nc[0].is_Pow and nc[0].exp == 2: return Mul._from_args(nc) def updater(p, func): p2 =

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
My updater was wrong, I think. It should be more like this: def updater(p, func): p2 = product_bases(p) if p2: if p2 not in m: m[p2] = func(p2) return m[p2] return p -- You received this message because you are subscribed to the Google Groups sympy group. To

Re: [sympy] auto_update_dict in subs

2013-04-30 Thread Chris Smith
There are different ways to organize this, but here is a compact way -- not necessarily the best. I don't know what your 'func's look like so I am just showing the transformation of nc by multiplying or dividing by 2: def mul_xform(func): def product_bases(nc): if len(nc) == 2 or

[sympy] auto_update_dict in subs

2013-04-29 Thread Alan Bromborsky
I wrote a class (see below) for a automatically updating dictionary. For example let - def xsq(x): return(x*x) D = auto_update_dict(xsq) D[2] calls xsq and returns 4 call D[2] and xsq is not called since the previous call has called xsq and placed the result in the dictionary. Again 4

Re: [sympy] auto_update_dict in subs

2013-04-29 Thread Aaron Meurer
I suppose subs should be changed to just iterate over the entries. Another option would be to create an object that implements __iter__ that returns (old, new) pairs. Does subs do this just for simplicity, or does it need to have all the pairs at once before it can do the substitution? Aaron

Re: [sympy] auto_update_dict in subs

2013-04-29 Thread Chris Smith
When you give a dictionary the keys are unordered and to be canonical, the keys are sorted. What sort of expression are you wanting to do what type of substitution on? Maybe replace, xreplace or Transform are better matches for what you want to do. -- You received this message because you are