Here is what I am trying to do, with the hack for the symbols sym = 'r'+sym cse( ..., symbols=symbols(sym+'1:1000') )
so is there a way to replace that with something like this cse( ..., symbols=symbols(previous_last_symbol+1) ) previous_last_symbol = current_last_symbol >>>> def opt_eqs(eqs, max_iter=None, opt_all=None): if max_iter == None: max_iter = 10 if opt_all == None: opt_all = True eqs_opt = eqs sub_opt = [] sym = '' for i in range(max_iter): sym = 'r'+sym a1,b1 = cse( eqs_opt, symbols=symbols(sym+'1:1000') ) sub_new = sub_opt + a1 if opt_all: eqs_new = [ Eq( eq[0], eq[1] ) for eq in a1 ] + b1 cnt_new = count_ops(eqs_new,visual=False) cnt_opt = count_ops(eqs_opt,visual=False) else: eqs_new = b1 cnt_new = count_ops(eqs_new,visual=False) + count_ops(sub_new,visual=False) cnt_opt = count_ops(eqs_opt,visual=False) + count_ops(sub_opt,visual=False) print cnt_new, cnt_opt if cnt_new >= cnt_opt: break else: sub_opt = sub_new eqs_opt = eqs_new if opt_all: return eqs_opt else: return [ Eq( eq[0], eq[1] ) for eq in sub_opt ] + eqs_opt On Apr 18, 6:42 pm, Chris Smith <smi...@gmail.com> wrote: > On Wed, Apr 18, 2012 at 2:28 PM, vweber <valerywe...@hotmail.com> wrote: > > Dear All > > > When calling recursively cse, is it possible to tell cse what was the > > previous last symbols+1, eg > > It shouldn't need to be called recursively. But can you give an > example (using my cse branch)? Otherwise, if you are just adding new > expressions and you want the cse of them using replacements already > identified then you can apply the substitutions before running cse on > them. But don't forget to use a new set of symbols or else there will > be clashes since the default symbols (x0, x1,...) will be used again. > > >>> cse(cos(x+1)+sin(x+1)) > > ([(x0, x + 1)], [sin(x0) + cos(x0)])>>> r,e=_ > >>> newe=[tan(x+1), x + 1 + y, sin(y + x+1)/(y+x+1)] > >>> revr = [(v, k) for k, v in reversed(r)] > >>> syms = numbered_symbols('y') > >>> cse([ei.subs(revr) for ei in newe], syms) > > ([(y0, x0 + y)], [tan(x0), y0, sin(y0)/y0]) -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.